From 0f1e46bd7e6acc53999c99757959ef2246e7dc70 Mon Sep 17 00:00:00 2001 From: Stuart Long Chay Boon Date: Mon, 31 Oct 2022 14:45:02 +0800 Subject: [PATCH] [offers][feat] add isSaved endpoint --- .../router/offers/offers-profile-router.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/apps/portal/src/server/router/offers/offers-profile-router.ts b/apps/portal/src/server/router/offers/offers-profile-router.ts index 48c8fb74..0699db4f 100644 --- a/apps/portal/src/server/router/offers/offers-profile-router.ts +++ b/apps/portal/src/server/router/offers/offers-profile-router.ts @@ -117,6 +117,43 @@ export const offersProfileRouter = createRouter() return profile?.editToken === input.token } }) + .query('isSaved', { + input: z.object({ + profileId: z.string(), + userId: z.string().nullish(), + }), + async resolve({ ctx, input }) { + + if (!input.userId) { + return false + } + + const profile = await ctx.prisma.offersProfile.findFirst({ + include: { + users: true + }, + where: { + id: input.profileId + } + }) + + const users = profile?.users + + if (!users) { + return false + } + + let isSaved = false + + for (let i = 0; i < users.length; i++) { + if (users[i].id === input.userId) { + isSaved = true + } + } + + return isSaved + } + }) .query('listOne', { input: z.object({ profileId: z.string(),