| 56 | } |
| 57 | |
| 58 | async function vote(parent, args, context, info) { |
| 59 | const { userId } = context; |
| 60 | const vote = await context.prisma.vote.findUnique({ |
| 61 | where: { |
| 62 | linkId_userId: { |
| 63 | linkId: Number(args.linkId), |
| 64 | userId: userId |
| 65 | } |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | if (Boolean(vote)) { |
| 70 | throw new Error( |
| 71 | `Already voted for link: ${args.linkId}` |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | const newVote = context.prisma.vote.create({ |
| 76 | data: { |
| 77 | user: { connect: { id: userId } }, |
| 78 | link: { connect: { id: Number(args.linkId) } } |
| 79 | } |
| 80 | }); |
| 81 | context.pubsub.publish('NEW_VOTE', newVote); |
| 82 | |
| 83 | return newVote; |
| 84 | } |
| 85 | |
| 86 | module.exports = { |
| 87 | post, |