One share link per user, per doc

This commit is contained in:
Tom Moor
2018-05-23 23:09:20 -07:00
parent aeb97ddcae
commit 7eea1a90af
2 changed files with 23 additions and 5 deletions

View File

@@ -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;

View File

@@ -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', {