Legwork for initial documents for atlases

This commit is contained in:
Jori Lallo
2016-07-26 00:05:10 -07:00
parent c88cc1f83b
commit e706e1c77c
6 changed files with 64 additions and 38 deletions

View File

@@ -11,24 +11,27 @@ const Atlas = sequelize.define('atlas', {
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
name: DataTypes.STRING,
description: DataTypes.STRING,
type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes }},
type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes } },
creatorId: DataTypes.UUID,
/* type: atlas */
navigationTree: DataTypes.JSONB,
}, {
tableName: 'atlases',
hooks: {
// beforeValidate: (doc) => {
// doc.urlId = randomstring.generate(15);
// },
// beforeCreate: (doc) => {
// doc.html = convertToMarkdown(doc.text);
// doc.preview = truncateMarkdown(doc.text, 160);
// },
// beforeUpdate: (doc) => {
// doc.html = convertToMarkdown(doc.text);
// doc.preview = truncateMarkdown(doc.text, 160);
// },
afterCreate: async (atlas) => {
if (atlas.type !== 'atlas') return;
await Document.create({
parentDocumentId: null,
atlasId: atlas.id,
teamId: atlas.teamId,
userId: atlas.creatorId,
lastModifiedById: atlas.creatorId,
title: 'Introduction',
text: '# Introduction',
});
},
},
instanceMethods: {
async getStructure() {
@@ -40,9 +43,9 @@ const Atlas = sequelize.define('atlas', {
const children = await Document.findAll({ where: {
parentDocumentId: document.id,
atlasId: this.id,
}});
} });
let childNodes = []
const childNodes = [];
await Promise.all(children.map(async (child) => {
childNodes.push(await getNodeForDocument(child));
}));
@@ -53,23 +56,23 @@ const Atlas = sequelize.define('atlas', {
url: document.getUrl(),
children: childNodes,
};
}
};
const rootDocument = await Document.findOne({
where: {
parentDocumentId: null,
atlasId: this.id,
}
},
});
if (rootDocument) {
return await getNodeForDocument(rootDocument);
} else {
return; // TODO should create a root doc
return true; // TODO should create a root doc
}
},
async updateNavigationTree(tree = this.navigationTree) {
let nodeIds = [];
const nodeIds = [];
nodeIds.push(tree.id);
const rootDocument = await Document.findOne({
@@ -80,7 +83,7 @@ const Atlas = sequelize.define('atlas', {
});
if (!rootDocument) throw new Error;
let newTree = {
const newTree = {
id: tree.id,
title: rootDocument.title,
url: rootDocument.getUrl(),
@@ -102,7 +105,7 @@ const Atlas = sequelize.define('atlas', {
title: childDocument.title,
url: childDocument.getUrl(),
children: await getIdsForChildren(child.children),
})
});
nodeIds.push(child.id);
}
}
@@ -114,7 +117,7 @@ const Atlas = sequelize.define('atlas', {
attributes: ['id'],
where: {
atlasId: this.id,
}
},
});
const documentIds = documents.map(doc => doc.id);
@@ -133,7 +136,7 @@ const Atlas = sequelize.define('atlas', {
title: document.title,
url: document.getUrl(),
children: [],
}
};
const insertNode = (node) => {
if (document.parentDocumentId === node.id) {
@@ -141,7 +144,7 @@ const Atlas = sequelize.define('atlas', {
} else {
node.children = node.children.map(childNode => {
return insertNode(childNode);
})
});
}
return node;
@@ -172,8 +175,8 @@ const Atlas = sequelize.define('atlas', {
};
this.navigationTree = await deleteNodeAndDocument(this.navigationTree, document.id);
}
}
},
},
});
Atlas.hasMany(Document, { as: 'documents', foreignKey: 'atlasId' });

View File

@@ -13,7 +13,7 @@ import {
import User from './User';
import Revision from './Revision';
slug.defaults.mode ='rfc3986';
slug.defaults.mode = 'rfc3986';
const generateSlug = (title, urlId) => {
const slugifiedTitle = slug(title);
@@ -35,7 +35,7 @@ const Document = sequelize.define('document', {
text: DataTypes.TEXT,
html: DataTypes.TEXT,
preview: DataTypes.TEXT,
revisionCount: { type: DataTypes.INTEGER, defaultValue: 0, },
revisionCount: { type: DataTypes.INTEGER, defaultValue: 0 },
parentDocumentId: DataTypes.UUID,
lastModifiedById: {
@@ -43,7 +43,7 @@ const Document = sequelize.define('document', {
allowNull: false,
references: {
model: 'users',
}
},
},
}, {
hooks: {
@@ -59,7 +59,7 @@ const Document = sequelize.define('document', {
return `${slugifiedTitle}-${this.urlId}`;
},
getUrl() {
return `/documents/${ this.id }`;
return `/documents/${this.id}`;
},
async createRevision() {
// Create revision of the current (latest)
@@ -72,7 +72,7 @@ const Document = sequelize.define('document', {
documentId: this.id,
});
},
}
},
});
Document.belongsTo(User);

View File

@@ -13,12 +13,13 @@ const Team = sequelize.define('team', {
slackData: DataTypes.JSONB,
}, {
instanceMethods: {
async createFirstAtlas() {
async createFirstAtlas(userId) {
const atlas = await Atlas.create({
name: this.name,
description: 'Your first Atlas',
type: 'journal',
teamId: this.id,
creatorId: userId,
});
return atlas;
}