Document Archive (#921)

* WIP: Archive

* WIP

* Finishing up archive endpoints

* WIP

* Update docs

* Flow

* Stash

* Add toast message confirmations

* Redirect handling, fixed publishhing info for archived docs

* Redirect to collection instead of home, remove unused pub info

* Account for deleted parent

* Trash -> Archive
Allow reading of archived docs

* Dont overload deletedAt

* Fixes

* 💚

* ParentDocumentId wipe for unarchived sub docs

* Fix: CMD+S exits editing
Fix: Duplicate user name on published but unedited docs

* Improve jank on paginated lists

* Prevent editing when archived

* 💚
Separate lint / flow steps
This commit is contained in:
Tom Moor
2019-04-06 16:20:27 -07:00
committed by GitHub
parent 76957865bb
commit 642c11ff7d
39 changed files with 811 additions and 311 deletions

View File

@@ -100,6 +100,38 @@ router.post('documents.pinned', auth(), pagination(), async ctx => {
};
});
router.post('documents.archived', auth(), pagination(), async ctx => {
const { sort = 'updatedAt' } = ctx.body;
let direction = ctx.body.direction;
if (direction !== 'ASC') direction = 'DESC';
const user = ctx.state.user;
const collectionIds = await user.collectionIds();
const documents = await Document.findAll({
where: {
teamId: user.teamId,
collectionId: collectionIds,
archivedAt: {
// $FlowFixMe
[Op.ne]: null,
},
},
order: [[sort, direction]],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(
documents.map(document => presentDocument(ctx, document))
);
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';
@@ -235,7 +267,7 @@ router.post('documents.info', auth({ required: false }), async ctx => {
},
],
});
if (!share) {
if (!share || share.document.archivedAt) {
throw new InvalidRequestError('Document could not be found for shareId');
}
document = share.document;
@@ -300,18 +332,29 @@ router.post('documents.revisions', auth(), pagination(), async ctx => {
router.post('documents.restore', auth(), async ctx => {
const { id, revisionId } = ctx.body;
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(revisionId, 'revisionId is required');
const user = ctx.state.user;
const document = await Document.findById(id);
authorize(user, 'update', document);
const revision = await Revision.findById(revisionId);
authorize(document, 'restore', revision);
if (document.archivedAt) {
authorize(user, 'unarchive', document);
document.text = revision.text;
document.title = revision.title;
await document.save();
// restore a previously archived document
await document.unarchive(user.id);
// restore a document to a specific revision
} else if (revisionId) {
authorize(user, 'update', document);
const revision = await Revision.findById(revisionId);
authorize(document, 'restore', revision);
document.text = revision.text;
document.title = revision.title;
await document.save();
} else {
ctx.assertPresent(revisionId, 'revisionId is required');
}
ctx.body = {
data: await presentDocument(ctx, document),
@@ -530,20 +573,30 @@ router.post('documents.move', auth(), async ctx => {
};
});
router.post('documents.archive', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const document = await Document.findById(id);
authorize(user, 'archive', document);
await document.archive(user.id);
ctx.body = {
data: await presentDocument(ctx, document),
};
});
router.post('documents.delete', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
const user = ctx.state.user;
const document = await Document.findById(id);
authorize(ctx.state.user, 'delete', document);
authorize(user, 'delete', document);
const collection = document.collection;
if (collection && collection.type === 'atlas') {
// Delete document and all of its children
await collection.removeDocument(document);
}
await document.destroy();
await document.delete();
ctx.body = {
success: true,

View File

@@ -27,6 +27,18 @@ describe('#documents.info', async () => {
expect(body.data.id).toEqual(document.id);
});
it('should return archived document', async () => {
const { user, document } = await seed();
await document.archive(user.id);
const res = await server.post('/api/documents.info', {
body: { token: user.getJwtToken(), id: document.id },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.id).toEqual(document.id);
});
it('should not return published document in collection not a member of', async () => {
const { user, document, collection } = await seed();
collection.private = true;
@@ -86,6 +98,20 @@ describe('#documents.info', async () => {
expect(res.status).toEqual(400);
});
it('should not return document from archived shareId', async () => {
const { document, user } = await seed();
const share = await buildShare({
documentId: document.id,
teamId: document.teamId,
});
await document.archive(user.id);
const res = await server.post('/api/documents.info', {
body: { shareId: share.id },
});
expect(res.status).toEqual(400);
});
it('should return document from shareId with token', async () => {
const { user, document, collection } = await seed();
const share = await buildShare({
@@ -420,6 +446,24 @@ describe('#documents.search', async () => {
expect(body.data.length).toEqual(0);
});
it('should not return archived documents', async () => {
const { user } = await seed();
const document = await buildDocument({
title: 'search term',
text: 'search term',
teamId: user.teamId,
});
await document.archive(user.id);
const res = await server.post('/api/documents.search', {
body: { token: user.getJwtToken(), query: 'search term' },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(0);
});
it('should not return documents in private collections not a member of', async () => {
const { user } = await seed();
const collection = await buildCollection({ private: true });
@@ -449,6 +493,66 @@ describe('#documents.search', async () => {
});
});
describe('#documents.archived', async () => {
it('should return archived documents', async () => {
const { user } = await seed();
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
await document.archive(user.id);
const res = await server.post('/api/documents.archived', {
body: { token: user.getJwtToken() },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(1);
});
it('should not return deleted documents', async () => {
const { user } = await seed();
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
await document.delete();
const res = await server.post('/api/documents.archived', {
body: { token: user.getJwtToken() },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(0);
});
it('should not return documents in private collections not a member of', async () => {
const { user } = await seed();
const collection = await buildCollection({ private: true });
const document = await buildDocument({
teamId: user.teamId,
collectionId: collection.id,
});
await document.archive(user.id);
const res = await server.post('/api/documents.archived', {
body: { token: user.getJwtToken() },
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.length).toEqual(0);
});
it('should require authentication', async () => {
const res = await server.post('/api/documents.archived');
expect(res.status).toEqual(401);
});
});
describe('#documents.viewed', async () => {
it('should return empty result if no views', async () => {
const { user } = await seed();
@@ -577,7 +681,37 @@ describe('#documents.pin', async () => {
});
});
describe('#documents.restore', async () => {
describe('#documents.restore', () => {
it('should allow restore of archived documents', async () => {
const { user, document } = await seed();
await document.archive(user.id);
const res = await server.post('/api/documents.restore', {
body: { token: user.getJwtToken(), id: document.id },
});
const body = await res.json();
expect(body.data.archivedAt).toEqual(null);
});
it('should restore archived when previous parent is archived', async () => {
const { user, document } = await seed();
const childDocument = await buildDocument({
userId: user.id,
teamId: user.teamId,
collectionId: document.collectionId,
parentDocumentId: document.id,
});
await childDocument.archive(user.id);
await document.archive(user.id);
const res = await server.post('/api/documents.restore', {
body: { token: user.getJwtToken(), id: childDocument.id },
});
const body = await res.json();
expect(body.data.parentDocumentId).toEqual(undefined);
expect(body.data.archivedAt).toEqual(null);
});
it('should restore the document to a previous version', async () => {
const { user, document } = await seed();
const revision = await Revision.findOne({
@@ -855,6 +989,22 @@ describe('#documents.update', async () => {
expect(body.data.collection.documents[0].title).toBe('Updated title');
});
it('should not edit archived document', async () => {
const { user, document } = await seed();
await document.archive();
const res = await server.post('/api/documents.update', {
body: {
token: user.getJwtToken(),
id: document.id,
title: 'Updated title',
text: 'Updated text',
lastRevision: document.revision,
},
});
expect(res.status).toEqual(403);
});
it('should not create new version when autosave=true', async () => {
const { user, document } = await seed();
@@ -974,6 +1124,24 @@ describe('#documents.update', async () => {
});
});
describe('#documents.archive', async () => {
it('should allow archiving document', async () => {
const { user, document } = await seed();
const res = await server.post('/api/documents.archive', {
body: { token: user.getJwtToken(), id: document.id },
});
expect(res.status).toEqual(200);
});
it('should require authentication', async () => {
const { document } = await seed();
const res = await server.post('/api/documents.archive', {
body: { id: document.id },
});
expect(res.status).toEqual(401);
});
});
describe('#documents.delete', async () => {
it('should allow deleting document', async () => {
const { user, document } = await seed();