Manually create a new document for second collection

This commit is contained in:
Jori Lallo
2017-07-17 23:30:16 -07:00
parent 63288227db
commit bbbf17c223
7 changed files with 115 additions and 60 deletions

View File

@ -39,19 +39,29 @@ const Collection = sequelize.define(
collection.urlId = collection.urlId || randomstring.generate(10);
},
afterCreate: async collection => {
const team = await collection.getTeam();
const collections = await team.getCollections();
// Don't auto-create for journal types, yet
if (collection.type !== 'atlas') return;
const document = await Document.create({
parentDocumentId: null,
atlasId: collection.id,
teamId: collection.teamId,
userId: collection.creatorId,
lastModifiedById: collection.creatorId,
createdById: collection.creatorId,
title: 'Introduction',
text: '# Introduction\n\nLets get started...',
});
collection.documentStructure = [document.toJSON()];
if (collections.length === 0) {
// Create intro document if first collection for team
const document = await Document.create({
parentDocumentId: null,
atlasId: collection.id,
teamId: collection.teamId,
userId: collection.creatorId,
lastModifiedById: collection.creatorId,
createdById: collection.creatorId,
title: 'Introduction',
text: '# Introduction\n\nLets get started...',
});
collection.documentStructure = [document.toJSON()];
} else {
// Let user create first document
collection.documentStructure = [];
}
await collection.save();
},
},
@ -65,6 +75,9 @@ Collection.associate = models => {
as: 'documents',
foreignKey: 'atlasId',
});
Collection.belongsTo(models.Team, {
as: 'team',
});
Collection.addScope('withRecentDocuments', {
include: [
{