Added Event model and logging to Collection

This commit is contained in:
Jori Lallo
2017-10-09 23:27:34 -07:00
parent d44ef41c34
commit 7c3cfbc9da
3 changed files with 139 additions and 0 deletions

View File

@ -0,0 +1,52 @@
module.exports = {
up: function(queryInterface, Sequelize) {
queryInterface.createTable('events', {
id: {
type: Sequelize.UUID,
allowNull: false,
primaryKey: true,
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
data: {
type: Sequelize.JSONB,
allowNull: false,
},
userId: {
type: Sequelize.UUID,
allowNull: true,
references: {
model: 'users',
},
},
collectionId: {
type: Sequelize.UUID,
allowNull: true,
references: {
model: 'collections',
},
},
teamId: {
type: Sequelize.UUID,
allowNull: true,
references: {
model: 'teams',
},
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
},
});
},
down: function(queryInterface, Sequelize) {
queryInterface.dropTable('events');
},
};

View File

@ -3,6 +3,7 @@ import slug from 'slug';
import randomstring from 'randomstring'; import randomstring from 'randomstring';
import { DataTypes, sequelize } from '../sequelize'; import { DataTypes, sequelize } from '../sequelize';
import Document from './Document'; import Document from './Document';
import Event from './Event';
import _ from 'lodash'; import _ from 'lodash';
// $FlowIssue invalid flow-typed // $FlowIssue invalid flow-typed
@ -125,6 +126,12 @@ Collection.prototype.addDocumentToStructure = async function(
options = {} options = {}
) { ) {
if (!this.documentStructure) return; if (!this.documentStructure) return;
const existingData = {
old: this.documentStructure,
documentId: document,
parentDocumentId: document.parentDocumentId,
index,
};
// If moving existing document with children, use existing structure to // If moving existing document with children, use existing structure to
// keep everything in shape and not loose documents // keep everything in shape and not loose documents
@ -163,6 +170,16 @@ Collection.prototype.addDocumentToStructure = async function(
this.documentStructure = this.documentStructure; this.documentStructure = this.documentStructure;
await this.save(); await this.save();
await Event.create({
name: 'Collection#addDocumentToStructure',
data: {
...existingData,
new: this.documentStructure,
},
collectionId: this.id,
teamId: this.teamId,
});
return this; return this;
}; };
@ -222,6 +239,12 @@ Collection.prototype.removeDocument = async function(
) { ) {
if (!this.documentStructure) return; if (!this.documentStructure) return;
let returnValue; let returnValue;
const existingData = {
old: this.documentStructure,
documentId: document,
parentDocumentId: document.parentDocumentId,
options,
};
// Helper to destroy all child documents for a document // Helper to destroy all child documents for a document
const deleteChildren = async documentId => { const deleteChildren = async documentId => {
@ -268,6 +291,17 @@ Collection.prototype.removeDocument = async function(
); );
if (options.deleteDocument) await this.save(); if (options.deleteDocument) await this.save();
await Event.create({
name: 'Collection#removeDocument',
data: {
...existingData,
new: this.documentStructure,
},
collectionId: this.id,
teamId: this.teamId,
});
return returnValue; return returnValue;
}; };

53
server/models/Event.js Normal file
View File

@ -0,0 +1,53 @@
// @flow
import { DataTypes, sequelize } from '../sequelize';
const Event = sequelize.define('event', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: DataTypes.STRING,
data: DataTypes.JSONB,
userId: {
type: 'UUID',
allowNull: true,
references: {
model: 'users',
},
},
collectionId: {
type: 'UUID',
allowNull: true,
references: {
model: 'collections',
},
},
teamId: {
type: 'UUID',
allowNull: true,
references: {
model: 'teams',
},
},
});
Event.associate = models => {
Event.belongsTo(models.User, {
as: 'user',
foreignKey: 'userId',
});
Event.belongsTo(models.Collection, {
as: 'collection',
foreignKey: 'collectionId',
});
Event.belongsTo(models.Team, {
as: 'team',
foreignKey: 'teamId',
});
};
export default Event;