diff --git a/server/api/documents.js b/server/api/documents.js index 9c705ef2..5c3f5513 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -19,7 +19,7 @@ router.post('documents.list', auth(), pagination(), async ctx => { const user = ctx.state.user; let where = { teamId: user.teamId }; - if (collection) where = { ...where, atlasId: collection }; + if (collection) where = { ...where, collectionId: collection }; const starredScope = { method: ['withStarred', user.id] }; const documents = await Document.scope('defaultScope', starredScope).findAll({ @@ -49,7 +49,7 @@ router.post('documents.pinned', auth(), pagination(), async ctx => { const documents = await Document.scope('defaultScope', starredScope).findAll({ where: { teamId: user.teamId, - atlasId: collection, + collectionId: collection, pinnedById: { // $FlowFixMe [Op.ne]: null, @@ -329,7 +329,7 @@ router.post('documents.create', auth(), async ctx => { parentDocumentObj = await Document.findOne({ where: { id: parentDocument, - atlasId: collection.id, + collectionId: collection.id, }, }); authorize(user, 'read', parentDocumentObj); @@ -337,7 +337,7 @@ router.post('documents.create', auth(), async ctx => { let document = await Document.create({ parentDocumentId: parentDocumentObj.id, - atlasId: collection.id, + collectionId: collection.id, teamId: user.teamId, userId: user.id, lastModifiedById: user.id, diff --git a/server/api/documents.test.js b/server/api/documents.test.js index 0c9a61d9..67e0efa1 100644 --- a/server/api/documents.test.js +++ b/server/api/documents.test.js @@ -155,7 +155,10 @@ describe('#documents.list', async () => { it('should allow filtering by collection', async () => { const { user, document } = await seed(); const res = await server.post('/api/documents.list', { - body: { token: user.getJwtToken(), collection: document.atlasId }, + body: { + token: user.getJwtToken(), + collection: document.collectionId, + }, }); const body = await res.json(); diff --git a/server/migrations/20180808061353-cleanup.js b/server/migrations/20180808061353-cleanup.js new file mode 100644 index 00000000..f78f49ca --- /dev/null +++ b/server/migrations/20180808061353-cleanup.js @@ -0,0 +1,19 @@ +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.renameColumn('documents', 'atlasId', 'collectionId'); + await queryInterface.removeColumn('documents', 'private'); + await queryInterface.addColumn('events', 'documentId', { + type: Sequelize.UUID, + allowNull: true, + }); + }, + down: async (queryInterface, Sequelize) => { + await queryInterface.renameColumn('documents', 'collectionId', 'atlasId'); + await queryInterface.removeColumn('events', 'documentId'); + await queryInterface.addColumn('documents', 'private', { + type: Sequelize.BOOLEAN, + allowNull: false, + defaultValue: true, + }); + } +} \ No newline at end of file diff --git a/server/models/Collection.js b/server/models/Collection.js index 10b32dd6..a3f9dc8f 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -53,7 +53,7 @@ const Collection = sequelize.define( // Create intro document if first collection for team const document = await Document.create({ parentDocumentId: null, - atlasId: collection.id, + collectionId: collection.id, teamId: collection.teamId, userId: collection.creatorId, lastModifiedById: collection.creatorId, @@ -78,7 +78,7 @@ const Collection = sequelize.define( Collection.associate = models => { Collection.hasMany(models.Document, { as: 'documents', - foreignKey: 'atlasId', + foreignKey: 'collectionId', onDelete: 'cascade', }); Collection.belongsTo(models.Team, { @@ -99,7 +99,7 @@ Collection.associate = models => { Collection.addHook('afterDestroy', async model => { await Document.destroy({ where: { - atlasId: model.id, + collectionId: model.id, }, }); }); diff --git a/server/models/Collection.test.js b/server/models/Collection.test.js index c70b6b16..930ce991 100644 --- a/server/models/Collection.test.js +++ b/server/models/Collection.test.js @@ -124,7 +124,7 @@ describe('#updateDocument', () => { // Add a child for testing const newDocument = await Document.create({ parentDocumentId: document.id, - atlasId: collection.id, + collectionId: collection.id, teamId: collection.teamId, userId: collection.creatorId, lastModifiedById: collection.creatorId, @@ -160,7 +160,7 @@ describe('#moveDocument', () => { // Add a child for testing const newDocument = await Document.create({ parentDocumentId: document.id, - atlasId: collection.id, + collectionId: collection.id, teamId: collection.teamId, userId: collection.creatorId, lastModifiedById: collection.creatorId, @@ -195,7 +195,7 @@ describe('#removeDocument', () => { // Verify that the document was removed const collectionDocuments = await Document.findAndCountAll({ where: { - atlasId: collection.id, + collectionId: collection.id, }, }); expect(collectionDocuments.count).toBe(1); @@ -207,7 +207,7 @@ describe('#removeDocument', () => { // Add a child for testing const newDocument = await Document.create({ parentDocumentId: document.id, - atlasId: collection.id, + collectionId: collection.id, teamId: collection.teamId, userId: collection.creatorId, lastModifiedById: collection.creatorId, @@ -223,7 +223,7 @@ describe('#removeDocument', () => { expect(collection.documentStructure.length).toBe(1); const collectionDocuments = await Document.findAndCountAll({ where: { - atlasId: collection.id, + collectionId: collection.id, }, }); expect(collectionDocuments.count).toBe(1); @@ -235,7 +235,7 @@ describe('#removeDocument', () => { // Add a child for testing const newDocument = await Document.create({ parentDocumentId: document.id, - atlasId: collection.id, + collectionId: collection.id, teamId: collection.teamId, userId: collection.creatorId, lastModifiedById: collection.creatorId, @@ -257,7 +257,7 @@ describe('#removeDocument', () => { const collectionDocuments = await Document.findAndCountAll({ where: { - atlasId: collection.id, + collectionId: collection.id, }, }); expect(collectionDocuments.count).toBe(2); diff --git a/server/models/Document.js b/server/models/Document.js index 4fc51a37..8a1cd294 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -119,7 +119,7 @@ const Document = sequelize.define( Document.associate = models => { Document.belongsTo(models.Collection, { as: 'collection', - foreignKey: 'atlasId', + foreignKey: 'collectionId', onDelete: 'cascade', }); Document.belongsTo(models.Team, { @@ -266,7 +266,7 @@ Document.searchForUser = async ( Document.addHook('beforeSave', async model => { if (!model.publishedAt) return; - const collection = await Collection.findById(model.atlasId); + const collection = await Collection.findById(model.collectionId); if (collection.type !== 'atlas') return; await collection.updateDocument(model); @@ -276,7 +276,7 @@ Document.addHook('beforeSave', async model => { Document.addHook('afterCreate', async model => { if (!model.publishedAt) return; - const collection = await Collection.findById(model.atlasId); + const collection = await Collection.findById(model.collectionId); if (collection.type !== 'atlas') return; await collection.addDocumentToStructure(model); @@ -295,7 +295,7 @@ Document.addHook('afterDestroy', model => Document.prototype.publish = async function() { if (this.publishedAt) return this.save(); - const collection = await Collection.findById(this.atlasId); + const collection = await Collection.findById(this.collectionId); if (collection.type !== 'atlas') return this.save(); await collection.addDocumentToStructure(this); diff --git a/server/models/Event.js b/server/models/Event.js index c962a419..a36cb01d 100644 --- a/server/models/Event.js +++ b/server/models/Event.js @@ -20,6 +20,10 @@ Event.associate = models => { as: 'collection', foreignKey: 'collectionId', }); + Event.belongsTo(models.Collection, { + as: 'document', + foreignKey: 'documentId', + }); Event.belongsTo(models.Team, { as: 'team', foreignKey: 'teamId', diff --git a/server/presenters/document.js b/server/presenters/document.js index 37534e63..e84d7704 100644 --- a/server/presenters/document.js +++ b/server/presenters/document.js @@ -50,7 +50,7 @@ async function present(ctx: Object, document: Document, options: ?Options) { if (!options.isPublic) { data.pinned = !!document.pinnedById; - data.collectionId = document.atlasId; + data.collectionId = document.collectionId; data.createdBy = presentUser(ctx, document.createdBy); data.updatedBy = presentUser(ctx, document.updatedBy); diff --git a/server/services/slack/index.js b/server/services/slack/index.js index cb2113be..6903b9c9 100644 --- a/server/services/slack/index.js +++ b/server/services/slack/index.js @@ -63,7 +63,7 @@ class Slack { const integration = await Integration.findOne({ where: { teamId: document.teamId, - collectionId: document.atlasId, + collectionId: document.collectionId, service: 'slack', type: 'post', }, diff --git a/server/test/factories.js b/server/test/factories.js index a93b4d69..55333547 100644 --- a/server/test/factories.js +++ b/server/test/factories.js @@ -81,9 +81,9 @@ export async function buildDocument(overrides: Object = {}) { overrides.userId = user.id; } - if (!overrides.atlasId) { + if (!overrides.collectionId) { const collection = await buildCollection(overrides); - overrides.atlasId = collection.id; + overrides.collectionId = collection.id; } return Document.create({ diff --git a/server/test/support.js b/server/test/support.js index 4f6b08fa..dba75df6 100644 --- a/server/test/support.js +++ b/server/test/support.js @@ -65,7 +65,7 @@ const seed = async () => { const document = await Document.create({ parentDocumentId: null, - atlasId: collection.id, + collectionId: collection.id, teamId: team.id, userId: collection.creatorId, lastModifiedById: collection.creatorId,