Document Viewers (#79)
* Recording document views * Add 'views' to document response * Basic displaying of document views, probably want it more sublte than this? But hey, lets get it in there * Bigly improves. RESTful > RPC * Display of who's viewed doc * Add Popover, add Scrollable, move views store * Working server tests 💁 * Document Stars (#81) * Added: Starred documents * UI is dumb but functionality works * Star now displayed inline in title * Optimistic rendering * Documents Endpoints (#85) * More seeds, documents.list endpoint * Upgrade deprecated middleware * document.viewers, specs * Add documents.starred Add request specs for star / unstar endpoints * Basic /starred page * Remove comment * Fixed double layout
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"verbose": true,
|
||||
"testPathDirs": [
|
||||
"rootDir": "..",
|
||||
"roots": [
|
||||
"<rootDir>/server"
|
||||
],
|
||||
"setupFiles": [
|
||||
@@ -1,9 +1,11 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`#auth.login should login with email 1`] = `
|
||||
Object {
|
||||
"avatarUrl": "http://example.com/avatar.png",
|
||||
"id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61",
|
||||
"name": "User 1",
|
||||
"username": "user1"
|
||||
"username": "user1",
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -12,7 +14,7 @@ Object {
|
||||
"avatarUrl": "http://example.com/avatar.png",
|
||||
"id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61",
|
||||
"name": "User 1",
|
||||
"username": "user1"
|
||||
"username": "user1",
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -21,7 +23,7 @@ Object {
|
||||
"error": "validation_error",
|
||||
"message": "username/email is required",
|
||||
"ok": false,
|
||||
"status": 400
|
||||
"status": 400,
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -30,7 +32,7 @@ Object {
|
||||
"error": "validation_error",
|
||||
"message": "username/email is required",
|
||||
"ok": false,
|
||||
"status": 400
|
||||
"status": 400,
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -39,7 +41,7 @@ Object {
|
||||
"error": "validation_error",
|
||||
"message": "username/email is required",
|
||||
"ok": false,
|
||||
"status": 400
|
||||
"status": 400,
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -48,7 +50,7 @@ Object {
|
||||
"error": "validation_error",
|
||||
"message": "name is required",
|
||||
"ok": false,
|
||||
"status": 400
|
||||
"status": 400,
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -57,7 +59,7 @@ Object {
|
||||
"error": "user_exists_with_email",
|
||||
"message": "User already exists with this email",
|
||||
"ok": false,
|
||||
"status": 400
|
||||
"status": 400,
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -66,7 +68,7 @@ Object {
|
||||
"error": "user_exists_with_username",
|
||||
"message": "User already exists with this username",
|
||||
"ok": false,
|
||||
"status": 400
|
||||
"status": 400,
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -75,6 +77,6 @@ Object {
|
||||
"error": "validation_error",
|
||||
"message": "email is invalid",
|
||||
"ok": false,
|
||||
"status": 400
|
||||
"status": 400,
|
||||
}
|
||||
`;
|
||||
|
||||
46
server/api/__snapshots__/documents.test.js.snap
Normal file
46
server/api/__snapshots__/documents.test.js.snap
Normal file
@@ -0,0 +1,46 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`#documents.list should require authentication 1`] = `
|
||||
Object {
|
||||
"error": "authentication_required",
|
||||
"message": "Authentication required",
|
||||
"ok": false,
|
||||
"status": 401,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`#documents.star should require authentication 1`] = `
|
||||
Object {
|
||||
"error": "authentication_required",
|
||||
"message": "Authentication required",
|
||||
"ok": false,
|
||||
"status": 401,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`#documents.starred should require authentication 1`] = `
|
||||
Object {
|
||||
"error": "authentication_required",
|
||||
"message": "Authentication required",
|
||||
"ok": false,
|
||||
"status": 401,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`#documents.unstar should require authentication 1`] = `
|
||||
Object {
|
||||
"error": "authentication_required",
|
||||
"message": "Authentication required",
|
||||
"ok": false,
|
||||
"status": 401,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`#documents.viewed should require authentication 1`] = `
|
||||
Object {
|
||||
"error": "authentication_required",
|
||||
"message": "Authentication required",
|
||||
"ok": false,
|
||||
"status": 401,
|
||||
}
|
||||
`;
|
||||
@@ -1,9 +1,11 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`#user.info should require authentication 1`] = `
|
||||
Object {
|
||||
"error": "authentication_required",
|
||||
"message": "Authentication required",
|
||||
"ok": false,
|
||||
"status": 401
|
||||
"status": 401,
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -13,9 +15,9 @@ Object {
|
||||
"avatarUrl": "http://example.com/avatar.png",
|
||||
"id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61",
|
||||
"name": "User 1",
|
||||
"username": "user1"
|
||||
"username": "user1",
|
||||
},
|
||||
"ok": true,
|
||||
"status": 200
|
||||
"status": 200,
|
||||
}
|
||||
`;
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import TestServer from 'fetch-test-server';
|
||||
import app from '..';
|
||||
import { flushdb, sequelize, seed } from '../test/support';
|
||||
import { flushdb, seed } from '../test/support';
|
||||
|
||||
const server = new TestServer(app.callback());
|
||||
|
||||
beforeEach(flushdb);
|
||||
afterAll(() => server.close());
|
||||
afterAll(() => sequelize.close());
|
||||
|
||||
describe('#auth.signup', async () => {
|
||||
it('should signup a new user', async () => {
|
||||
|
||||
@@ -1,46 +1,84 @@
|
||||
// @flow
|
||||
import Router from 'koa-router';
|
||||
import httpErrors from 'http-errors';
|
||||
import isUUID from 'validator/lib/isUUID';
|
||||
|
||||
const URL_REGEX = /^[a-zA-Z0-9-]*-([a-zA-Z0-9]{10,15})$/;
|
||||
|
||||
import auth from './middlewares/authentication';
|
||||
import pagination from './middlewares/pagination';
|
||||
import { presentDocument } from '../presenters';
|
||||
import { Document, Collection } from '../models';
|
||||
import { Document, Collection, Star, View } from '../models';
|
||||
|
||||
const router = new Router();
|
||||
|
||||
const getDocumentForId = async id => {
|
||||
try {
|
||||
let document;
|
||||
if (isUUID(id)) {
|
||||
document = await Document.findOne({
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
} else if (id.match(URL_REGEX)) {
|
||||
document = await Document.findOne({
|
||||
where: {
|
||||
urlId: id.match(URL_REGEX)[1],
|
||||
},
|
||||
});
|
||||
} else {
|
||||
throw httpErrors.NotFound();
|
||||
}
|
||||
return document;
|
||||
} catch (e) {
|
||||
// Invalid UUID
|
||||
throw httpErrors.NotFound();
|
||||
}
|
||||
};
|
||||
router.post('documents.list', auth(), pagination(), async ctx => {
|
||||
let { sort = 'updatedAt', direction } = ctx.body;
|
||||
if (direction !== 'ASC') direction = 'DESC';
|
||||
|
||||
const user = ctx.state.user;
|
||||
const documents = await Document.findAll({
|
||||
where: { teamId: user.teamId },
|
||||
order: [[sort, direction]],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
|
||||
let data = await Promise.all(documents.map(doc => presentDocument(ctx, doc)));
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data,
|
||||
};
|
||||
});
|
||||
|
||||
router.post('documents.viewed', auth(), pagination(), async ctx => {
|
||||
let { sort = 'updatedAt', direction } = ctx.body;
|
||||
if (direction !== 'ASC') direction = 'DESC';
|
||||
|
||||
const user = ctx.state.user;
|
||||
const views = await View.findAll({
|
||||
where: { userId: user.id },
|
||||
order: [[sort, direction]],
|
||||
include: [{ model: Document }],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
|
||||
let data = await Promise.all(
|
||||
views.map(view => presentDocument(ctx, view.document))
|
||||
);
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data,
|
||||
};
|
||||
});
|
||||
|
||||
router.post('documents.starred', auth(), pagination(), async ctx => {
|
||||
let { sort = 'updatedAt', direction } = ctx.body;
|
||||
if (direction !== 'ASC') direction = 'DESC';
|
||||
|
||||
const user = ctx.state.user;
|
||||
const views = await Star.findAll({
|
||||
where: { userId: user.id },
|
||||
order: [[sort, direction]],
|
||||
include: [{ model: Document }],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
|
||||
let data = await Promise.all(
|
||||
views.map(view => presentDocument(ctx, view.document))
|
||||
);
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data,
|
||||
};
|
||||
});
|
||||
|
||||
// FIXME: This really needs specs :/
|
||||
router.post('documents.info', auth(), async ctx => {
|
||||
const { id } = ctx.body;
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
const document = await getDocumentForId(id);
|
||||
const document = await Document.findById(id);
|
||||
|
||||
if (!document) throw httpErrors.NotFound();
|
||||
|
||||
@@ -52,20 +90,14 @@ router.post('documents.info', auth(), async ctx => {
|
||||
if (document.teamId !== user.teamId) {
|
||||
throw httpErrors.NotFound();
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
data: await presentDocument(ctx, document, {
|
||||
includeCollection: true,
|
||||
includeCollaborators: true,
|
||||
}),
|
||||
};
|
||||
} else {
|
||||
ctx.body = {
|
||||
data: await presentDocument(ctx, document, {
|
||||
includeCollaborators: true,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
data: await presentDocument(ctx, document, {
|
||||
includeCollection: document.private,
|
||||
includeCollaborators: true,
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
router.post('documents.search', auth(), async ctx => {
|
||||
@@ -94,6 +126,34 @@ router.post('documents.search', auth(), async ctx => {
|
||||
};
|
||||
});
|
||||
|
||||
router.post('documents.star', auth(), async ctx => {
|
||||
const { id } = ctx.body;
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
const user = await ctx.state.user;
|
||||
const document = await Document.findById(id);
|
||||
|
||||
if (!document || document.teamId !== user.teamId)
|
||||
throw httpErrors.BadRequest();
|
||||
|
||||
await Star.findOrCreate({
|
||||
where: { documentId: document.id, userId: user.id },
|
||||
});
|
||||
});
|
||||
|
||||
router.post('documents.unstar', auth(), async ctx => {
|
||||
const { id } = ctx.body;
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
const user = await ctx.state.user;
|
||||
const document = await Document.findById(id);
|
||||
|
||||
if (!document || document.teamId !== user.teamId)
|
||||
throw httpErrors.BadRequest();
|
||||
|
||||
await Star.destroy({
|
||||
where: { documentId: document.id, userId: user.id },
|
||||
});
|
||||
});
|
||||
|
||||
router.post('documents.create', auth(), async ctx => {
|
||||
const { collection, title, text, parentDocument, index } = ctx.body;
|
||||
ctx.assertPresent(collection, 'collection is required');
|
||||
@@ -154,7 +214,7 @@ router.post('documents.update', auth(), async ctx => {
|
||||
ctx.assertPresent(title || text, 'title or text is required');
|
||||
|
||||
const user = ctx.state.user;
|
||||
const document = await getDocumentForId(id);
|
||||
const document = await Document.findById(id);
|
||||
|
||||
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
|
||||
|
||||
@@ -186,13 +246,13 @@ router.post('documents.move', auth(), async ctx => {
|
||||
if (index) ctx.assertPositiveInteger(index, 'index must be an integer (>=0)');
|
||||
|
||||
const user = ctx.state.user;
|
||||
const document = await getDocumentForId(id);
|
||||
const document = await Document.findById(id);
|
||||
|
||||
if (!document || document.teamId !== user.teamId) throw httpErrors.NotFound();
|
||||
|
||||
// Set parent document
|
||||
if (parentDocument) {
|
||||
const parent = await getDocumentForId(parentDocument);
|
||||
const parent = await Document.findById(parentDocument);
|
||||
if (parent.atlasId !== document.atlasId)
|
||||
throw httpErrors.BadRequest(
|
||||
'Invalid parentDocument (must be same collection)'
|
||||
@@ -226,7 +286,7 @@ router.post('documents.delete', auth(), async ctx => {
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
|
||||
const user = ctx.state.user;
|
||||
const document = await getDocumentForId(id);
|
||||
const document = await Document.findById(id);
|
||||
const collection = await Collection.findById(document.atlasId);
|
||||
|
||||
if (!document || document.teamId !== user.teamId)
|
||||
@@ -240,7 +300,7 @@ router.post('documents.delete', auth(), async ctx => {
|
||||
);
|
||||
}
|
||||
|
||||
// Delete all chilren
|
||||
// Delete all children
|
||||
try {
|
||||
await collection.deleteDocument(document);
|
||||
} catch (e) {
|
||||
|
||||
158
server/api/documents.test.js
Normal file
158
server/api/documents.test.js
Normal file
@@ -0,0 +1,158 @@
|
||||
import TestServer from 'fetch-test-server';
|
||||
import app from '..';
|
||||
import { View, Star } from '../models';
|
||||
import { flushdb, seed } from '../test/support';
|
||||
|
||||
const server = new TestServer(app.callback());
|
||||
|
||||
beforeEach(flushdb);
|
||||
afterAll(() => server.close());
|
||||
|
||||
describe('#documents.list', async () => {
|
||||
it('should return documents', async () => {
|
||||
const { user, document } = await seed();
|
||||
const res = await server.post('/api/documents.list', {
|
||||
body: { token: user.getJwtToken() },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(2);
|
||||
expect(body.data[0].id).toEqual(document.id);
|
||||
});
|
||||
|
||||
it('should allow changing sort direction', async () => {
|
||||
const { user, document } = await seed();
|
||||
const res = await server.post('/api/documents.list', {
|
||||
body: { token: user.getJwtToken(), direction: 'ASC' },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data[1].id).toEqual(document.id);
|
||||
});
|
||||
|
||||
it('should require authentication', async () => {
|
||||
const res = await server.post('/api/documents.list');
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(401);
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#documents.viewed', async () => {
|
||||
it('should return empty result if no views', async () => {
|
||||
const { user } = await seed();
|
||||
const res = await server.post('/api/documents.viewed', {
|
||||
body: { token: user.getJwtToken() },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(0);
|
||||
});
|
||||
|
||||
it('should return recently viewed documents', async () => {
|
||||
const { user, document } = await seed();
|
||||
await View.increment({ documentId: document.id, userId: user.id });
|
||||
|
||||
const res = await server.post('/api/documents.viewed', {
|
||||
body: { token: user.getJwtToken() },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(1);
|
||||
expect(body.data[0].id).toEqual(document.id);
|
||||
});
|
||||
|
||||
it('should require authentication', async () => {
|
||||
const res = await server.post('/api/documents.viewed');
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(401);
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#documents.starred', async () => {
|
||||
it('should return empty result if no stars', async () => {
|
||||
const { user } = await seed();
|
||||
const res = await server.post('/api/documents.starred', {
|
||||
body: { token: user.getJwtToken() },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(0);
|
||||
});
|
||||
|
||||
it('should return starred documents', async () => {
|
||||
const { user, document } = await seed();
|
||||
await Star.create({ documentId: document.id, userId: user.id });
|
||||
|
||||
const res = await server.post('/api/documents.starred', {
|
||||
body: { token: user.getJwtToken() },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(1);
|
||||
expect(body.data[0].id).toEqual(document.id);
|
||||
});
|
||||
|
||||
it('should require authentication', async () => {
|
||||
const res = await server.post('/api/documents.starred');
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(401);
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#documents.star', async () => {
|
||||
it('should star the document', async () => {
|
||||
const { user, document } = await seed();
|
||||
|
||||
const res = await server.post('/api/documents.star', {
|
||||
body: { token: user.getJwtToken(), id: document.id },
|
||||
});
|
||||
|
||||
const stars = await Star.findAll();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(stars.length).toEqual(1);
|
||||
expect(stars[0].documentId).toEqual(document.id);
|
||||
});
|
||||
|
||||
it('should require authentication', async () => {
|
||||
const res = await server.post('/api/documents.star');
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(401);
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#documents.unstar', async () => {
|
||||
it('should unstar the document', async () => {
|
||||
const { user, document } = await seed();
|
||||
await Star.create({ documentId: document.id, userId: user.id });
|
||||
|
||||
const res = await server.post('/api/documents.unstar', {
|
||||
body: { token: user.getJwtToken(), id: document.id },
|
||||
});
|
||||
|
||||
const stars = await Star.findAll();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(stars.length).toEqual(0);
|
||||
});
|
||||
|
||||
it('should require authentication', async () => {
|
||||
const res = await server.post('/api/documents.star');
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(401);
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
@@ -8,6 +8,7 @@ import auth from './auth';
|
||||
import user from './user';
|
||||
import collections from './collections';
|
||||
import documents from './documents';
|
||||
import views from './views';
|
||||
import hooks from './hooks';
|
||||
import apiKeys from './apiKeys';
|
||||
|
||||
@@ -59,6 +60,7 @@ router.use('/', auth.routes());
|
||||
router.use('/', user.routes());
|
||||
router.use('/', collections.routes());
|
||||
router.use('/', documents.routes());
|
||||
router.use('/', views.routes());
|
||||
router.use('/', hooks.routes());
|
||||
router.use('/', apiKeys.routes());
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ const server = new TestServer(app.callback());
|
||||
|
||||
beforeEach(flushdb);
|
||||
afterAll(() => server.close());
|
||||
afterAll(() => sequelize.close());
|
||||
|
||||
describe('#user.info', async () => {
|
||||
it('should return known user', async () => {
|
||||
|
||||
52
server/api/views.js
Normal file
52
server/api/views.js
Normal file
@@ -0,0 +1,52 @@
|
||||
// @flow
|
||||
import Router from 'koa-router';
|
||||
import httpErrors from 'http-errors';
|
||||
import auth from './middlewares/authentication';
|
||||
import { presentView } from '../presenters';
|
||||
import { View, Document } from '../models';
|
||||
|
||||
const router = new Router();
|
||||
|
||||
router.post('views.list', auth(), async ctx => {
|
||||
const { id } = ctx.body;
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
|
||||
const views = await View.findAll({
|
||||
where: {
|
||||
documentId: id,
|
||||
},
|
||||
order: [['updatedAt', 'DESC']],
|
||||
});
|
||||
|
||||
// Collectiones
|
||||
let users = [];
|
||||
let count = 0;
|
||||
await Promise.all(
|
||||
views.map(async view => {
|
||||
count = view.count;
|
||||
return users.push(await presentView(ctx, view));
|
||||
})
|
||||
);
|
||||
|
||||
ctx.body = {
|
||||
data: {
|
||||
users,
|
||||
count,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
router.post('views.create', auth(), async ctx => {
|
||||
const { id } = ctx.body;
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
|
||||
const user = ctx.state.user;
|
||||
const document = await Document.findById(id);
|
||||
|
||||
if (!document || document.teamId !== user.teamId)
|
||||
throw httpErrors.BadRequest();
|
||||
|
||||
await View.increment({ documentId: document.id, userId: user.id });
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -4,7 +4,7 @@
|
||||
"dialect": "postgres"
|
||||
},
|
||||
"test": {
|
||||
"use_env_variable": "DATABASE_URL",
|
||||
"use_env_variable": "DATABASE_URL_TEST",
|
||||
"dialect": "postgres"
|
||||
},
|
||||
"production": {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import compress from 'koa-compress';
|
||||
import helmet from 'koa-helmet';
|
||||
import { contentSecurityPolicy } from 'koa-helmet';
|
||||
import logger from 'koa-logger';
|
||||
import mount from 'koa-mount';
|
||||
import Koa from 'koa';
|
||||
@@ -72,7 +72,7 @@ app.use(mount('/api', api));
|
||||
app.use(mount(routes));
|
||||
|
||||
app.use(
|
||||
helmet.csp({
|
||||
contentSecurityPolicy({
|
||||
directives: {
|
||||
defaultSrc: ["'self'"],
|
||||
styleSrc: ["'self'", "'unsafe-inline'"],
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
up: function(queryInterface, Sequelize) {
|
||||
queryInterface.createTable('apiKeys', {
|
||||
@@ -41,6 +39,6 @@ module.exports = {
|
||||
},
|
||||
|
||||
down: function(queryInterface, Sequelize) {
|
||||
queryInterface.createTable('apiKeys');
|
||||
queryInterface.dropTable('apiKeys');
|
||||
},
|
||||
};
|
||||
|
||||
40
server/migrations/20170601032359-add-views.js
Normal file
40
server/migrations/20170601032359-add-views.js
Normal file
@@ -0,0 +1,40 @@
|
||||
module.exports = {
|
||||
up: function(queryInterface, Sequelize) {
|
||||
queryInterface.createTable('views', {
|
||||
id: {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
},
|
||||
documentId: {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: false,
|
||||
},
|
||||
userId: {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: false,
|
||||
},
|
||||
count: {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: false,
|
||||
defaultValue: 1,
|
||||
},
|
||||
createdAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
updatedAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
});
|
||||
queryInterface.addIndex('views', ['documentId', 'userId'], {
|
||||
indicesType: 'UNIQUE',
|
||||
});
|
||||
},
|
||||
|
||||
down: function(queryInterface, Sequelize) {
|
||||
queryInterface.removeIndex('views', ['documentId', 'userId']);
|
||||
queryInterface.dropTable('views');
|
||||
},
|
||||
};
|
||||
35
server/migrations/20170604052347-add-stars.js
Normal file
35
server/migrations/20170604052347-add-stars.js
Normal file
@@ -0,0 +1,35 @@
|
||||
module.exports = {
|
||||
up: function(queryInterface, Sequelize) {
|
||||
queryInterface.createTable('stars', {
|
||||
id: {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: false,
|
||||
primaryKey: true,
|
||||
},
|
||||
documentId: {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: false,
|
||||
},
|
||||
userId: {
|
||||
type: Sequelize.UUID,
|
||||
allowNull: false,
|
||||
},
|
||||
createdAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
updatedAt: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
},
|
||||
});
|
||||
queryInterface.addIndex('stars', ['documentId', 'userId'], {
|
||||
indicesType: 'UNIQUE',
|
||||
});
|
||||
},
|
||||
|
||||
down: function(queryInterface, Sequelize) {
|
||||
queryInterface.removeIndex('stars', ['documentId', 'userId']);
|
||||
queryInterface.dropTable('stars');
|
||||
},
|
||||
};
|
||||
@@ -1,8 +1,8 @@
|
||||
import { DataTypes, sequelize } from '../sequelize';
|
||||
import randomstring from 'randomstring';
|
||||
|
||||
const Team = sequelize.define(
|
||||
'team',
|
||||
const ApiKey = sequelize.define(
|
||||
'apiKeys',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
@@ -30,4 +30,4 @@ const Team = sequelize.define(
|
||||
}
|
||||
);
|
||||
|
||||
export default Team;
|
||||
export default ApiKey;
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
import slug from 'slug';
|
||||
import randomstring from 'randomstring';
|
||||
import { DataTypes, sequelize } from '../sequelize';
|
||||
import _ from 'lodash';
|
||||
import Document from './Document';
|
||||
import _ from 'lodash';
|
||||
|
||||
slug.defaults.mode = 'rfc3986';
|
||||
|
||||
@@ -54,6 +54,14 @@ const Collection = sequelize.define(
|
||||
await collection.save();
|
||||
},
|
||||
},
|
||||
classMethods: {
|
||||
associate: models => {
|
||||
Collection.hasMany(models.Document, {
|
||||
as: 'documents',
|
||||
foreignKey: 'atlasId',
|
||||
});
|
||||
},
|
||||
},
|
||||
instanceMethods: {
|
||||
getUrl() {
|
||||
// const slugifiedName = slug(this.name);
|
||||
@@ -173,6 +181,4 @@ const Collection = sequelize.define(
|
||||
}
|
||||
);
|
||||
|
||||
Collection.hasMany(Document, { as: 'documents', foreignKey: 'atlasId' });
|
||||
|
||||
export default Collection;
|
||||
|
||||
@@ -2,21 +2,24 @@
|
||||
import slug from 'slug';
|
||||
import _ from 'lodash';
|
||||
import randomstring from 'randomstring';
|
||||
|
||||
import isUUID from 'validator/lib/isUUID';
|
||||
import { DataTypes, sequelize } from '../sequelize';
|
||||
import { convertToMarkdown } from '../../frontend/utils/markdown';
|
||||
import { truncateMarkdown } from '../utils/truncate';
|
||||
import User from './User';
|
||||
import Revision from './Revision';
|
||||
|
||||
const URL_REGEX = /^[a-zA-Z0-9-]*-([a-zA-Z0-9]{10,15})$/;
|
||||
|
||||
slug.defaults.mode = 'rfc3986';
|
||||
const slugify = text =>
|
||||
slug(text, {
|
||||
remove: /[.]/g,
|
||||
});
|
||||
|
||||
const createRevision = async doc => {
|
||||
const createRevision = doc => {
|
||||
// Create revision of the current (latest)
|
||||
await Revision.create({
|
||||
return Revision.create({
|
||||
title: doc.title,
|
||||
text: doc.text,
|
||||
html: doc.html,
|
||||
@@ -26,10 +29,13 @@ const createRevision = async doc => {
|
||||
});
|
||||
};
|
||||
|
||||
const documentBeforeSave = async doc => {
|
||||
const createUrlId = doc => {
|
||||
return (doc.urlId = doc.urlId || randomstring.generate(10));
|
||||
};
|
||||
|
||||
const beforeSave = async doc => {
|
||||
doc.html = convertToMarkdown(doc.text);
|
||||
doc.preview = truncateMarkdown(doc.text, 160);
|
||||
|
||||
doc.revisionCount += 1;
|
||||
|
||||
// Collaborators
|
||||
@@ -65,7 +71,6 @@ const Document = sequelize.define(
|
||||
html: DataTypes.TEXT,
|
||||
preview: DataTypes.TEXT,
|
||||
revisionCount: { type: DataTypes.INTEGER, defaultValue: 0 },
|
||||
|
||||
parentDocumentId: DataTypes.UUID,
|
||||
createdById: {
|
||||
type: DataTypes.UUID,
|
||||
@@ -86,13 +91,11 @@ const Document = sequelize.define(
|
||||
{
|
||||
paranoid: true,
|
||||
hooks: {
|
||||
beforeValidate: doc => {
|
||||
doc.urlId = doc.urlId || randomstring.generate(10);
|
||||
},
|
||||
beforeCreate: documentBeforeSave,
|
||||
beforeUpdate: documentBeforeSave,
|
||||
afterCreate: async doc => await createRevision(doc),
|
||||
afterUpdate: async doc => await createRevision(doc),
|
||||
beforeValidate: createUrlId,
|
||||
beforeCreate: beforeSave,
|
||||
beforeUpdate: beforeSave,
|
||||
afterCreate: createRevision,
|
||||
afterUpdate: createRevision,
|
||||
},
|
||||
instanceMethods: {
|
||||
getUrl() {
|
||||
@@ -110,34 +113,47 @@ const Document = sequelize.define(
|
||||
};
|
||||
},
|
||||
},
|
||||
classMethods: {
|
||||
associate: models => {
|
||||
Document.belongsTo(models.User);
|
||||
},
|
||||
findById: async id => {
|
||||
if (isUUID(id)) {
|
||||
return Document.findOne({
|
||||
where: { id },
|
||||
});
|
||||
} else if (id.match(URL_REGEX)) {
|
||||
return Document.findOne({
|
||||
where: {
|
||||
urlId: id.match(URL_REGEX)[1],
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
searchForUser: (user, query, options = {}) => {
|
||||
const limit = options.limit || 15;
|
||||
const offset = options.offset || 0;
|
||||
|
||||
const sql = `
|
||||
SELECT * FROM documents
|
||||
WHERE "searchVector" @@ plainto_tsquery('english', :query) AND
|
||||
"teamId" = '${user.teamId}'::uuid AND
|
||||
"deletedAt" IS NULL
|
||||
ORDER BY ts_rank(documents."searchVector", plainto_tsquery('english', :query)) DESC
|
||||
LIMIT :limit OFFSET :offset;
|
||||
`;
|
||||
|
||||
return sequelize.query(sql, {
|
||||
replacements: {
|
||||
query,
|
||||
limit,
|
||||
offset,
|
||||
},
|
||||
model: Document,
|
||||
});
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
Document.belongsTo(User);
|
||||
|
||||
Document.searchForUser = async (user, query, options = {}) => {
|
||||
const limit = options.limit || 15;
|
||||
const offset = options.offset || 0;
|
||||
|
||||
const sql = `
|
||||
SELECT * FROM documents
|
||||
WHERE "searchVector" @@ plainto_tsquery('english', :query) AND
|
||||
"teamId" = '${user.teamId}'::uuid AND
|
||||
"deletedAt" IS NULL
|
||||
ORDER BY ts_rank(documents."searchVector", plainto_tsquery('english', :query)) DESC
|
||||
LIMIT :limit OFFSET :offset;
|
||||
`;
|
||||
|
||||
const documents = await sequelize.query(sql, {
|
||||
replacements: {
|
||||
query,
|
||||
limit,
|
||||
offset,
|
||||
},
|
||||
model: Document,
|
||||
});
|
||||
|
||||
return documents;
|
||||
};
|
||||
|
||||
export default Document;
|
||||
|
||||
23
server/models/Star.js
Normal file
23
server/models/Star.js
Normal file
@@ -0,0 +1,23 @@
|
||||
// @flow
|
||||
import { DataTypes, sequelize } from '../sequelize';
|
||||
|
||||
const Star = sequelize.define(
|
||||
'star',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
classMethods: {
|
||||
associate: models => {
|
||||
Star.belongsTo(models.Document);
|
||||
Star.belongsTo(models.User);
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export default Star;
|
||||
@@ -1,7 +1,5 @@
|
||||
import { DataTypes, sequelize } from '../sequelize';
|
||||
import Collection from './Collection';
|
||||
import Document from './Document';
|
||||
import User from './User';
|
||||
|
||||
const Team = sequelize.define(
|
||||
'team',
|
||||
@@ -16,6 +14,13 @@ const Team = sequelize.define(
|
||||
slackData: DataTypes.JSONB,
|
||||
},
|
||||
{
|
||||
classMethods: {
|
||||
associate: models => {
|
||||
Team.hasMany(models.Collection, { as: 'atlases' });
|
||||
Team.hasMany(models.Document, { as: 'documents' });
|
||||
Team.hasMany(models.User, { as: 'users' });
|
||||
},
|
||||
},
|
||||
instanceMethods: {
|
||||
async createFirstCollection(userId) {
|
||||
const atlas = await Collection.create({
|
||||
@@ -37,8 +42,4 @@ const Team = sequelize.define(
|
||||
}
|
||||
);
|
||||
|
||||
Team.hasMany(Collection, { as: 'atlases' });
|
||||
Team.hasMany(Document, { as: 'documents' });
|
||||
Team.hasMany(User, { as: 'users' });
|
||||
|
||||
export default Team;
|
||||
|
||||
@@ -26,6 +26,14 @@ const User = sequelize.define(
|
||||
jwtSecret: encryptedFields.vault('jwtSecret'),
|
||||
},
|
||||
{
|
||||
classMethods: {
|
||||
associate: models => {
|
||||
User.hasMany(models.ApiKey, { as: 'apiKeys' });
|
||||
User.hasMany(models.Collection, { as: 'collections' });
|
||||
User.hasMany(models.Document, { as: 'documents' });
|
||||
User.hasMany(models.View, { as: 'views' });
|
||||
},
|
||||
},
|
||||
instanceMethods: {
|
||||
getJwtToken() {
|
||||
return JWT.sign({ id: this.id }, this.jwtSecret);
|
||||
|
||||
@@ -3,7 +3,6 @@ import { User } from '.';
|
||||
import { flushdb, sequelize } from '../test/support';
|
||||
|
||||
beforeEach(flushdb);
|
||||
afterAll(() => sequelize.close());
|
||||
|
||||
it('should set JWT secret and password digest', async () => {
|
||||
const user = User.build({
|
||||
|
||||
35
server/models/View.js
Normal file
35
server/models/View.js
Normal file
@@ -0,0 +1,35 @@
|
||||
// @flow
|
||||
import { DataTypes, sequelize } from '../sequelize';
|
||||
|
||||
const View = sequelize.define(
|
||||
'view',
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
primaryKey: true,
|
||||
},
|
||||
count: {
|
||||
type: DataTypes.INTEGER,
|
||||
defaultValue: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
classMethods: {
|
||||
associate: models => {
|
||||
View.belongsTo(models.Document);
|
||||
View.belongsTo(models.User);
|
||||
},
|
||||
increment: async where => {
|
||||
const [model, created] = await View.findOrCreate({ where });
|
||||
if (!created) {
|
||||
model.count += 1;
|
||||
model.save();
|
||||
}
|
||||
return model;
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
export default View;
|
||||
@@ -1,8 +1,29 @@
|
||||
// @flow
|
||||
import User from './User';
|
||||
import Team from './Team';
|
||||
import Collection from './Collection';
|
||||
import Document from './Document';
|
||||
import Revision from './Revision';
|
||||
import ApiKey from './ApiKey';
|
||||
import View from './View';
|
||||
import Star from './Star';
|
||||
|
||||
export { User, Team, Collection, Document, Revision, ApiKey };
|
||||
const models = {
|
||||
User,
|
||||
Team,
|
||||
Collection,
|
||||
Document,
|
||||
Revision,
|
||||
ApiKey,
|
||||
View,
|
||||
Star,
|
||||
};
|
||||
|
||||
// based on https://github.com/sequelize/express-example/blob/master/models/index.js
|
||||
Object.keys(models).forEach(modelName => {
|
||||
if ('associate' in models[modelName]) {
|
||||
models[modelName].associate(models);
|
||||
}
|
||||
});
|
||||
|
||||
export { User, Team, Collection, Document, Revision, ApiKey, View, Star };
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
exports[`test presents a user 1`] = `
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`presents a user 1`] = `
|
||||
Object {
|
||||
"avatarUrl": "http://example.com/avatar.png",
|
||||
"id": "123",
|
||||
"name": "Test User",
|
||||
"username": "testuser"
|
||||
"username": "testuser",
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`test presents a user without slack data 1`] = `
|
||||
exports[`presents a user without slack data 1`] = `
|
||||
Object {
|
||||
"avatarUrl": null,
|
||||
"id": "123",
|
||||
"name": "Test User",
|
||||
"username": "testuser"
|
||||
"username": "testuser",
|
||||
}
|
||||
`;
|
||||
|
||||
9
server/presenters/apiKey.js
Normal file
9
server/presenters/apiKey.js
Normal file
@@ -0,0 +1,9 @@
|
||||
function present(ctx, key) {
|
||||
return {
|
||||
id: key.id,
|
||||
name: key.name,
|
||||
secret: key.secret,
|
||||
};
|
||||
}
|
||||
|
||||
export default present;
|
||||
46
server/presenters/collection.js
Normal file
46
server/presenters/collection.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import _ from 'lodash';
|
||||
import { Document } from '../models';
|
||||
import presentDocument from './document';
|
||||
|
||||
async function present(ctx, collection, includeRecentDocuments = false) {
|
||||
ctx.cache.set(collection.id, collection);
|
||||
|
||||
const data = {
|
||||
id: collection.id,
|
||||
url: collection.getUrl(),
|
||||
name: collection.name,
|
||||
description: collection.description,
|
||||
type: collection.type,
|
||||
createdAt: collection.createdAt,
|
||||
updatedAt: collection.updatedAt,
|
||||
};
|
||||
|
||||
if (collection.type === 'atlas')
|
||||
data.navigationTree = collection.navigationTree;
|
||||
|
||||
if (includeRecentDocuments) {
|
||||
const documents = await Document.findAll({
|
||||
where: {
|
||||
atlasId: collection.id,
|
||||
},
|
||||
limit: 10,
|
||||
order: [['updatedAt', 'DESC']],
|
||||
});
|
||||
|
||||
const recentDocuments = [];
|
||||
await Promise.all(
|
||||
documents.map(async document => {
|
||||
recentDocuments.push(
|
||||
await presentDocument(ctx, document, {
|
||||
includeCollaborators: true,
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
data.recentDocuments = _.orderBy(recentDocuments, ['updatedAt'], ['desc']);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
export default present;
|
||||
@@ -1,27 +1,17 @@
|
||||
import _ from 'lodash';
|
||||
import { Document, Collection, User } from './models';
|
||||
import { Collection, Star, User, View } from '../models';
|
||||
import presentUser from './user';
|
||||
import presentCollection from './collection';
|
||||
|
||||
import presentUser from './presenters/user';
|
||||
|
||||
export { presentUser };
|
||||
|
||||
export async function presentTeam(ctx, team) {
|
||||
ctx.cache.set(team.id, team);
|
||||
|
||||
return {
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
};
|
||||
}
|
||||
|
||||
export async function presentDocument(ctx, document, options) {
|
||||
async function present(ctx, document, options) {
|
||||
options = {
|
||||
includeCollection: true,
|
||||
includeCollaborators: true,
|
||||
includeViews: true,
|
||||
...options,
|
||||
};
|
||||
ctx.cache.set(document.id, document);
|
||||
|
||||
const userId = ctx.state.user.id;
|
||||
const data = {
|
||||
id: document.id,
|
||||
url: document.getUrl(),
|
||||
@@ -38,6 +28,16 @@ export async function presentDocument(ctx, document, options) {
|
||||
collaborators: [],
|
||||
};
|
||||
|
||||
data.starred = !!await Star.findOne({
|
||||
where: { documentId: document.id, userId },
|
||||
});
|
||||
|
||||
if (options.includeViews) {
|
||||
data.views = await View.sum('count', {
|
||||
where: { documentId: document.id },
|
||||
});
|
||||
}
|
||||
|
||||
if (options.includeCollection) {
|
||||
data.collection = await ctx.cache.get(document.atlasId, async () => {
|
||||
const collection =
|
||||
@@ -77,56 +77,4 @@ export async function presentDocument(ctx, document, options) {
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function presentCollection(
|
||||
ctx,
|
||||
collection,
|
||||
includeRecentDocuments = false
|
||||
) {
|
||||
ctx.cache.set(collection.id, collection);
|
||||
|
||||
const data = {
|
||||
id: collection.id,
|
||||
url: collection.getUrl(),
|
||||
name: collection.name,
|
||||
description: collection.description,
|
||||
type: collection.type,
|
||||
createdAt: collection.createdAt,
|
||||
updatedAt: collection.updatedAt,
|
||||
};
|
||||
|
||||
if (collection.type === 'atlas') {
|
||||
data.documents = await collection.getDocumentsStructure();
|
||||
}
|
||||
|
||||
if (includeRecentDocuments) {
|
||||
const documents = await Document.findAll({
|
||||
where: {
|
||||
atlasId: collection.id,
|
||||
},
|
||||
limit: 10,
|
||||
order: [['updatedAt', 'DESC']],
|
||||
});
|
||||
|
||||
const recentDocuments = [];
|
||||
await Promise.all(
|
||||
documents.map(async document => {
|
||||
recentDocuments.push(
|
||||
await presentDocument(ctx, document, {
|
||||
includeCollaborators: true,
|
||||
})
|
||||
);
|
||||
})
|
||||
);
|
||||
data.recentDocuments = _.orderBy(recentDocuments, ['updatedAt'], ['desc']);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
export function presentApiKey(ctx, key) {
|
||||
return {
|
||||
id: key.id,
|
||||
name: key.name,
|
||||
secret: key.secret,
|
||||
};
|
||||
}
|
||||
export default present;
|
||||
16
server/presenters/index.js
Normal file
16
server/presenters/index.js
Normal file
@@ -0,0 +1,16 @@
|
||||
// @flow
|
||||
import presentUser from './user';
|
||||
import presentView from './view';
|
||||
import presentDocument from './document';
|
||||
import presentCollection from './collection';
|
||||
import presentApiKey from './apiKey';
|
||||
import presentTeam from './team';
|
||||
|
||||
export {
|
||||
presentUser,
|
||||
presentView,
|
||||
presentDocument,
|
||||
presentCollection,
|
||||
presentApiKey,
|
||||
presentTeam,
|
||||
};
|
||||
10
server/presenters/team.js
Normal file
10
server/presenters/team.js
Normal file
@@ -0,0 +1,10 @@
|
||||
function present(ctx, team) {
|
||||
ctx.cache.set(team.id, team);
|
||||
|
||||
return {
|
||||
id: team.id,
|
||||
name: team.name,
|
||||
};
|
||||
}
|
||||
|
||||
export default present;
|
||||
@@ -1,7 +1,7 @@
|
||||
// @flow
|
||||
import User from '../models/User';
|
||||
|
||||
async function presentUser(ctx: Object, user: User) {
|
||||
function present(ctx: Object, user: User) {
|
||||
ctx.cache.set(user.id, user);
|
||||
|
||||
return {
|
||||
@@ -12,4 +12,4 @@ async function presentUser(ctx: Object, user: User) {
|
||||
};
|
||||
}
|
||||
|
||||
export default presentUser;
|
||||
export default present;
|
||||
|
||||
18
server/presenters/view.js
Normal file
18
server/presenters/view.js
Normal file
@@ -0,0 +1,18 @@
|
||||
// @flow
|
||||
import { View, User } from '../models';
|
||||
import { presentUser } from '../presenters';
|
||||
|
||||
async function present(ctx: Object, view: View) {
|
||||
let data = {
|
||||
count: view.count,
|
||||
user: undefined,
|
||||
};
|
||||
const user = await ctx.cache.get(
|
||||
view.userId,
|
||||
async () => await User.findById(view.userId)
|
||||
);
|
||||
data.user = await presentUser(ctx, user);
|
||||
return data;
|
||||
}
|
||||
|
||||
export default present;
|
||||
@@ -1,8 +1,10 @@
|
||||
// @flow
|
||||
import Sequelize from 'sequelize';
|
||||
import EncryptedField from 'sequelize-encrypted';
|
||||
import debug from 'debug';
|
||||
|
||||
const secretKey = process.env.SEQUELIZE_SECRET;
|
||||
|
||||
export const encryptedFields = EncryptedField(Sequelize, secretKey);
|
||||
|
||||
export const DataTypes = Sequelize;
|
||||
|
||||
@@ -21,9 +21,7 @@ function runMigrations() {
|
||||
path: './server/migrations',
|
||||
},
|
||||
});
|
||||
return umzug.up().then(() => {
|
||||
return sequelize.close();
|
||||
});
|
||||
return umzug.up();
|
||||
}
|
||||
|
||||
runMigrations();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { User } from '../models';
|
||||
// @flow
|
||||
import { User, Document, Collection, Team } from '../models';
|
||||
import { sequelize } from '../sequelize';
|
||||
|
||||
export function flushdb() {
|
||||
@@ -6,23 +7,61 @@ export function flushdb() {
|
||||
const tables = Object.keys(sequelize.models).map(model =>
|
||||
sql.quoteTable(sequelize.models[model].getTableName())
|
||||
);
|
||||
const query = `TRUNCATE ${tables.join(', ')} CASCADE`;
|
||||
|
||||
const query = `TRUNCATE ${tables.join(', ')} CASCADE`;
|
||||
return sequelize.query(query);
|
||||
}
|
||||
|
||||
const seed = async () => {
|
||||
await User.create({
|
||||
const team = await Team.create({
|
||||
id: '86fde1d4-0050-428f-9f0b-0bf77f8bdf61',
|
||||
name: 'Team',
|
||||
slackId: 'T2399UF2P',
|
||||
slackData: {
|
||||
id: 'T2399UF2P',
|
||||
},
|
||||
});
|
||||
|
||||
const user = await User.create({
|
||||
id: '86fde1d4-0050-428f-9f0b-0bf77f8bdf61',
|
||||
email: 'user1@example.com',
|
||||
username: 'user1',
|
||||
name: 'User 1',
|
||||
password: 'test123!',
|
||||
slackId: '123',
|
||||
teamId: team.id,
|
||||
slackId: 'U2399UF2P',
|
||||
slackData: {
|
||||
id: 'U2399UF2P',
|
||||
image_192: 'http://example.com/avatar.png',
|
||||
},
|
||||
});
|
||||
|
||||
const collection = await Collection.create({
|
||||
id: '86fde1d4-0050-428f-9f0b-0bf77f8bdf61',
|
||||
name: 'Collection',
|
||||
urlId: 'collection',
|
||||
teamId: team.id,
|
||||
creatorId: user.id,
|
||||
type: 'atlas',
|
||||
});
|
||||
|
||||
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...',
|
||||
});
|
||||
|
||||
return {
|
||||
user,
|
||||
collection,
|
||||
document,
|
||||
team,
|
||||
};
|
||||
};
|
||||
|
||||
export { seed, sequelize };
|
||||
|
||||
Reference in New Issue
Block a user