diff --git a/server/api/__snapshots__/documents.test.js.snap b/server/api/__snapshots__/documents.test.js.snap index 2c0ac5201..3a157720a 100644 --- a/server/api/__snapshots__/documents.test.js.snap +++ b/server/api/__snapshots__/documents.test.js.snap @@ -107,6 +107,15 @@ Object { } `; +exports[`#documents.update should require text while appending 1`] = ` +Object { + "error": "param_required", + "message": "Text is required while appending", + "ok": false, + "status": 400, +} +`; + exports[`#documents.viewed should require authentication 1`] = ` Object { "error": "authentication_required", diff --git a/server/api/documents.js b/server/api/documents.js index e890e0c67..9ecaa2c14 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -600,9 +600,19 @@ router.post('documents.create', auth(), async ctx => { }); router.post('documents.update', auth(), async ctx => { - const { id, title, text, publish, autosave, done, lastRevision } = ctx.body; + const { + id, + title, + text, + publish, + autosave, + done, + lastRevision, + append, + } = ctx.body; ctx.assertPresent(id, 'id is required'); ctx.assertPresent(title || text, 'title or text is required'); + if (append) ctx.assertPresent(text, 'Text is required while appending'); const user = ctx.state.user; const document = await Document.findById(id); @@ -615,7 +625,12 @@ router.post('documents.update', auth(), async ctx => { // Update document if (title) document.title = title; - if (text) document.text = text; + //append to document + if (append) { + document.text += text; + } else if (text) { + document.text = text; + } document.lastModifiedById = user.id; if (publish) { diff --git a/server/api/documents.test.js b/server/api/documents.test.js index 8f3254997..041964d23 100644 --- a/server/api/documents.test.js +++ b/server/api/documents.test.js @@ -1212,6 +1212,42 @@ describe('#documents.update', async () => { }); expect(res.status).toEqual(403); }); + + it('should append document with text', async () => { + const { user, document } = await seed(); + + const res = await server.post('/api/documents.update', { + body: { + token: user.getJwtToken(), + id: document.id, + text: 'Additional text', + lastRevision: document.revision, + append: true, + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.text).toBe(document.text + 'Additional text'); + }); + + it('should require text while appending', async () => { + const { user, document } = await seed(); + + const res = await server.post('/api/documents.update', { + body: { + token: user.getJwtToken(), + id: document.id, + lastRevision: document.revision, + title: 'Updated Title', + append: true, + }, + }); + const body = await res.json(); + + expect(res.status).toEqual(400); + expect(body).toMatchSnapshot(); + }); }); describe('#documents.archive', async () => {