Legwork for initial documents for atlases
This commit is contained in:
@@ -27,8 +27,6 @@ router.post('auth.slack', async (ctx) => {
|
|||||||
throw httpErrors.BadRequest();
|
throw httpErrors.BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(data);
|
|
||||||
|
|
||||||
if (!data.ok) throw httpErrors.BadRequest(data.error);
|
if (!data.ok) throw httpErrors.BadRequest(data.error);
|
||||||
|
|
||||||
// Temp to block
|
// Temp to block
|
||||||
@@ -40,13 +38,13 @@ router.post('auth.slack', async (ctx) => {
|
|||||||
|
|
||||||
// Team
|
// Team
|
||||||
let team = await Team.findOne({ where: { slackId: data.team.id } });
|
let team = await Team.findOne({ where: { slackId: data.team.id } });
|
||||||
|
let teamExisted = !!team;
|
||||||
if (!team) {
|
if (!team) {
|
||||||
team = await Team.create({
|
team = await Team.create({
|
||||||
name: data.team.name,
|
name: data.team.name,
|
||||||
slackId: data.team.id,
|
slackId: data.team.id,
|
||||||
slackData: data.team,
|
slackData: data.team,
|
||||||
});
|
});
|
||||||
await team.createFirstAtlas();
|
|
||||||
} else {
|
} else {
|
||||||
team.name = data.team.name;
|
team.name = data.team.name;
|
||||||
team.slackData = data.team;
|
team.slackData = data.team;
|
||||||
@@ -56,18 +54,23 @@ router.post('auth.slack', async (ctx) => {
|
|||||||
if (user) {
|
if (user) {
|
||||||
user.slackAccessToken = data.access_token;
|
user.slackAccessToken = data.access_token;
|
||||||
user.slackData = data.user;
|
user.slackData = data.user;
|
||||||
user = await user.save();
|
await user.save();
|
||||||
} else {
|
} else {
|
||||||
user = await team.createUser({
|
user = await User.create({
|
||||||
slackId: data.user.id,
|
slackId: data.user.id,
|
||||||
username: data.user.name,
|
username: data.user.name,
|
||||||
name: data.user.name,
|
name: data.user.name,
|
||||||
email: data.user.email,
|
email: data.user.email,
|
||||||
|
teamId: team.id,
|
||||||
slackData: data.user,
|
slackData: data.user,
|
||||||
slackAccessToken: data.access_token,
|
slackAccessToken: data.access_token,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!teamExisted) {
|
||||||
|
await team.createFirstAtlas(user.id);
|
||||||
|
}
|
||||||
|
|
||||||
ctx.body = { data: {
|
ctx.body = { data: {
|
||||||
user: await presentUser(user),
|
user: await presentUser(user),
|
||||||
team: await presentTeam(team),
|
team: await presentTeam(team),
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ router.post('atlases.create', auth(), async (ctx) => {
|
|||||||
description,
|
description,
|
||||||
type: type || 'atlas',
|
type: type || 'atlas',
|
||||||
teamId: user.teamId,
|
teamId: user.teamId,
|
||||||
|
creatorId: user.id,
|
||||||
});
|
});
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
@@ -66,8 +67,8 @@ router.post('atlases.list', auth(), pagination(), async (ctx) => {
|
|||||||
|
|
||||||
// Atlases
|
// Atlases
|
||||||
let data = [];
|
let data = [];
|
||||||
await Promise.all(atlases.forEach(async (atlas) => {
|
await Promise.all(atlases.map(async (atlas) => {
|
||||||
data.push(await presentAtlas(atlas, true));
|
return data.push(await presentAtlas(atlas, true));
|
||||||
}));
|
}));
|
||||||
|
|
||||||
data = _orderBy(data, ['updatedAt'], ['desc']);
|
data = _orderBy(data, ['updatedAt'], ['desc']);
|
||||||
|
|||||||
18
server/migrations/20160726061511-atlas-creator.js
Normal file
18
server/migrations/20160726061511-atlas-creator.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
up: function (queryInterface, Sequelize) {
|
||||||
|
queryInterface.addColumn(
|
||||||
|
'atlases',
|
||||||
|
'creatorId',
|
||||||
|
{
|
||||||
|
type: Sequelize.UUID,
|
||||||
|
allowNull: true,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
down: function (queryInterface, Sequelize) {
|
||||||
|
queryInterface.removeColumn('documents', 'creatorId');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -11,24 +11,27 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
|
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
|
||||||
name: DataTypes.STRING,
|
name: DataTypes.STRING,
|
||||||
description: DataTypes.STRING,
|
description: DataTypes.STRING,
|
||||||
type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes }},
|
type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes } },
|
||||||
|
creatorId: DataTypes.UUID,
|
||||||
|
|
||||||
/* type: atlas */
|
/* type: atlas */
|
||||||
navigationTree: DataTypes.JSONB,
|
navigationTree: DataTypes.JSONB,
|
||||||
}, {
|
}, {
|
||||||
tableName: 'atlases',
|
tableName: 'atlases',
|
||||||
hooks: {
|
hooks: {
|
||||||
// beforeValidate: (doc) => {
|
afterCreate: async (atlas) => {
|
||||||
// doc.urlId = randomstring.generate(15);
|
if (atlas.type !== 'atlas') return;
|
||||||
// },
|
|
||||||
// beforeCreate: (doc) => {
|
await Document.create({
|
||||||
// doc.html = convertToMarkdown(doc.text);
|
parentDocumentId: null,
|
||||||
// doc.preview = truncateMarkdown(doc.text, 160);
|
atlasId: atlas.id,
|
||||||
// },
|
teamId: atlas.teamId,
|
||||||
// beforeUpdate: (doc) => {
|
userId: atlas.creatorId,
|
||||||
// doc.html = convertToMarkdown(doc.text);
|
lastModifiedById: atlas.creatorId,
|
||||||
// doc.preview = truncateMarkdown(doc.text, 160);
|
title: 'Introduction',
|
||||||
// },
|
text: '# Introduction',
|
||||||
|
});
|
||||||
|
},
|
||||||
},
|
},
|
||||||
instanceMethods: {
|
instanceMethods: {
|
||||||
async getStructure() {
|
async getStructure() {
|
||||||
@@ -40,9 +43,9 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
const children = await Document.findAll({ where: {
|
const children = await Document.findAll({ where: {
|
||||||
parentDocumentId: document.id,
|
parentDocumentId: document.id,
|
||||||
atlasId: this.id,
|
atlasId: this.id,
|
||||||
}});
|
} });
|
||||||
|
|
||||||
let childNodes = []
|
const childNodes = [];
|
||||||
await Promise.all(children.map(async (child) => {
|
await Promise.all(children.map(async (child) => {
|
||||||
childNodes.push(await getNodeForDocument(child));
|
childNodes.push(await getNodeForDocument(child));
|
||||||
}));
|
}));
|
||||||
@@ -53,23 +56,23 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
url: document.getUrl(),
|
url: document.getUrl(),
|
||||||
children: childNodes,
|
children: childNodes,
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
const rootDocument = await Document.findOne({
|
const rootDocument = await Document.findOne({
|
||||||
where: {
|
where: {
|
||||||
parentDocumentId: null,
|
parentDocumentId: null,
|
||||||
atlasId: this.id,
|
atlasId: this.id,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (rootDocument) {
|
if (rootDocument) {
|
||||||
return await getNodeForDocument(rootDocument);
|
return await getNodeForDocument(rootDocument);
|
||||||
} else {
|
} else {
|
||||||
return; // TODO should create a root doc
|
return true; // TODO should create a root doc
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async updateNavigationTree(tree = this.navigationTree) {
|
async updateNavigationTree(tree = this.navigationTree) {
|
||||||
let nodeIds = [];
|
const nodeIds = [];
|
||||||
nodeIds.push(tree.id);
|
nodeIds.push(tree.id);
|
||||||
|
|
||||||
const rootDocument = await Document.findOne({
|
const rootDocument = await Document.findOne({
|
||||||
@@ -80,7 +83,7 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
});
|
});
|
||||||
if (!rootDocument) throw new Error;
|
if (!rootDocument) throw new Error;
|
||||||
|
|
||||||
let newTree = {
|
const newTree = {
|
||||||
id: tree.id,
|
id: tree.id,
|
||||||
title: rootDocument.title,
|
title: rootDocument.title,
|
||||||
url: rootDocument.getUrl(),
|
url: rootDocument.getUrl(),
|
||||||
@@ -102,7 +105,7 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
title: childDocument.title,
|
title: childDocument.title,
|
||||||
url: childDocument.getUrl(),
|
url: childDocument.getUrl(),
|
||||||
children: await getIdsForChildren(child.children),
|
children: await getIdsForChildren(child.children),
|
||||||
})
|
});
|
||||||
nodeIds.push(child.id);
|
nodeIds.push(child.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,7 +117,7 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
attributes: ['id'],
|
attributes: ['id'],
|
||||||
where: {
|
where: {
|
||||||
atlasId: this.id,
|
atlasId: this.id,
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
const documentIds = documents.map(doc => doc.id);
|
const documentIds = documents.map(doc => doc.id);
|
||||||
|
|
||||||
@@ -133,7 +136,7 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
title: document.title,
|
title: document.title,
|
||||||
url: document.getUrl(),
|
url: document.getUrl(),
|
||||||
children: [],
|
children: [],
|
||||||
}
|
};
|
||||||
|
|
||||||
const insertNode = (node) => {
|
const insertNode = (node) => {
|
||||||
if (document.parentDocumentId === node.id) {
|
if (document.parentDocumentId === node.id) {
|
||||||
@@ -141,7 +144,7 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
} else {
|
} else {
|
||||||
node.children = node.children.map(childNode => {
|
node.children = node.children.map(childNode => {
|
||||||
return insertNode(childNode);
|
return insertNode(childNode);
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
@@ -172,8 +175,8 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.navigationTree = await deleteNodeAndDocument(this.navigationTree, document.id);
|
this.navigationTree = await deleteNodeAndDocument(this.navigationTree, document.id);
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
Atlas.hasMany(Document, { as: 'documents', foreignKey: 'atlasId' });
|
Atlas.hasMany(Document, { as: 'documents', foreignKey: 'atlasId' });
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import {
|
|||||||
import User from './User';
|
import User from './User';
|
||||||
import Revision from './Revision';
|
import Revision from './Revision';
|
||||||
|
|
||||||
slug.defaults.mode ='rfc3986';
|
slug.defaults.mode = 'rfc3986';
|
||||||
|
|
||||||
const generateSlug = (title, urlId) => {
|
const generateSlug = (title, urlId) => {
|
||||||
const slugifiedTitle = slug(title);
|
const slugifiedTitle = slug(title);
|
||||||
@@ -35,7 +35,7 @@ const Document = sequelize.define('document', {
|
|||||||
text: DataTypes.TEXT,
|
text: DataTypes.TEXT,
|
||||||
html: DataTypes.TEXT,
|
html: DataTypes.TEXT,
|
||||||
preview: DataTypes.TEXT,
|
preview: DataTypes.TEXT,
|
||||||
revisionCount: { type: DataTypes.INTEGER, defaultValue: 0, },
|
revisionCount: { type: DataTypes.INTEGER, defaultValue: 0 },
|
||||||
|
|
||||||
parentDocumentId: DataTypes.UUID,
|
parentDocumentId: DataTypes.UUID,
|
||||||
lastModifiedById: {
|
lastModifiedById: {
|
||||||
@@ -43,7 +43,7 @@ const Document = sequelize.define('document', {
|
|||||||
allowNull: false,
|
allowNull: false,
|
||||||
references: {
|
references: {
|
||||||
model: 'users',
|
model: 'users',
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
hooks: {
|
hooks: {
|
||||||
@@ -59,7 +59,7 @@ const Document = sequelize.define('document', {
|
|||||||
return `${slugifiedTitle}-${this.urlId}`;
|
return `${slugifiedTitle}-${this.urlId}`;
|
||||||
},
|
},
|
||||||
getUrl() {
|
getUrl() {
|
||||||
return `/documents/${ this.id }`;
|
return `/documents/${this.id}`;
|
||||||
},
|
},
|
||||||
async createRevision() {
|
async createRevision() {
|
||||||
// Create revision of the current (latest)
|
// Create revision of the current (latest)
|
||||||
@@ -72,7 +72,7 @@ const Document = sequelize.define('document', {
|
|||||||
documentId: this.id,
|
documentId: this.id,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
Document.belongsTo(User);
|
Document.belongsTo(User);
|
||||||
|
|||||||
@@ -13,12 +13,13 @@ const Team = sequelize.define('team', {
|
|||||||
slackData: DataTypes.JSONB,
|
slackData: DataTypes.JSONB,
|
||||||
}, {
|
}, {
|
||||||
instanceMethods: {
|
instanceMethods: {
|
||||||
async createFirstAtlas() {
|
async createFirstAtlas(userId) {
|
||||||
const atlas = await Atlas.create({
|
const atlas = await Atlas.create({
|
||||||
name: this.name,
|
name: this.name,
|
||||||
description: 'Your first Atlas',
|
description: 'Your first Atlas',
|
||||||
type: 'journal',
|
type: 'journal',
|
||||||
teamId: this.id,
|
teamId: this.id,
|
||||||
|
creatorId: userId,
|
||||||
});
|
});
|
||||||
return atlas;
|
return atlas;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user