From 661f68aaba920b3ef6ed6a415a17238d13c645a3 Mon Sep 17 00:00:00 2001 From: usirin Date: Fri, 11 Aug 2023 16:39:04 -0700 Subject: [PATCH] feat(apps/gql): make sure upvote doesn't exist before creating it --- apps/gql/schema/resolvers/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/apps/gql/schema/resolvers/index.ts b/apps/gql/schema/resolvers/index.ts index e0f17a0c..924a006f 100644 --- a/apps/gql/schema/resolvers/index.ts +++ b/apps/gql/schema/resolvers/index.ts @@ -438,7 +438,7 @@ export const resolvers = { return { edge: { node, cursor: node.id }, error: null }; }, - createPanoUpvote: async (_, { input }, { actions, pasaport: { session } }) => { + createPanoUpvote: async (_, { input }, { loaders, actions, pasaport: { session } }) => { if (!session?.user?.id) { return { node: null, error: NotAuthorized() }; } @@ -447,7 +447,16 @@ export const resolvers = { switch (type) { case "PanoPost": { - const upvote = await actions.pano.postUpvote + let upvote = await loaders.pano.upvote.byUserAndPostID.load({ + postID: value, + userID: session.user.id, + }); + + if (upvote) { + return { node: null, error: InvalidInput() }; + } + + upvote = await actions.pano.postUpvote .create({ postID: value, userID: session.user.id }) .then(transformPanoUpvote);