From 7eea1a90afb1314278f2f92fc973b2ef71033298 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 23 May 2018 23:09:20 -0700 Subject: [PATCH] One share link per user, per doc --- server/api/shares.js | 12 +++++++----- server/api/shares.test.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/server/api/shares.js b/server/api/shares.js index b50f5c1a4..49bbe84fe 100644 --- a/server/api/shares.js +++ b/server/api/shares.js @@ -5,7 +5,6 @@ import pagination from './middlewares/pagination'; import { presentShare } from '../presenters'; import { Document, User, Share } from '../models'; import policy from '../policies'; -import { constants } from 'os'; const { authorize } = policy; const router = new Router(); @@ -53,11 +52,14 @@ router.post('shares.create', auth(), async ctx => { const document = await Document.findById(id); authorize(user, 'share', document); - const share = await Share.create({ - documentId: document.id, - userId: user.id, - teamId: user.teamId, + const [share, created] = await Share.findOrCreate({ + where: { + documentId: document.id, + userId: user.id, + teamId: user.teamId, + }, }); + console.log('created', created); share.user = user; share.document = document; diff --git a/server/api/shares.test.js b/server/api/shares.test.js index 1229e8ca4..30df40f64 100644 --- a/server/api/shares.test.js +++ b/server/api/shares.test.js @@ -70,6 +70,22 @@ describe('#shares.create', async () => { expect(body.data.documentTitle).toBe(document.title); }); + it('should return existing share link for document and user', async () => { + const { user, document } = await seed(); + const share = await buildShare({ + documentId: document.id, + teamId: user.teamId, + userId: user.id, + }); + const res = await server.post('/api/shares.create', { + body: { token: user.getJwtToken(), id: document.id }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.id).toBe(share.id); + }); + it('should require authentication', async () => { const { document } = await seed(); const res = await server.post('/api/shares.create', {