From 4e2d599d4e1c2b1505be3d0eca05ed930ac4e133 Mon Sep 17 00:00:00 2001 From: Stuart Long Chay Boon Date: Wed, 12 Oct 2022 16:07:02 +0800 Subject: [PATCH] [offers][chore] refactor isEditable code --- .../server/router/offers-profile-router.ts | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/apps/portal/src/server/router/offers-profile-router.ts b/apps/portal/src/server/router/offers-profile-router.ts index 20343ac6..92b53f24 100644 --- a/apps/portal/src/server/router/offers-profile-router.ts +++ b/apps/portal/src/server/router/offers-profile-router.ts @@ -51,6 +51,20 @@ const education = z.object({ type: z.string().optional(), }); +type WithIsEditable = T & { + isEditable: boolean +} + +function computeIsEditable( + profileInput: offersProfile, + editToken?: string +): WithIsEditable { + return { + ...profileInput, + isEditable: profileInput.editToken === editToken, + } +} + export const offersProfileRouter = createRouter() .query('listOne', { input: z.object({ @@ -102,21 +116,8 @@ export const offersProfileRouter = createRouter() id: input.profileId, } }); - // Extend the T generic with the fullName attribute - type WithIsEditable = T & { - isEditable: boolean - } - // Take objects that satisfy FirstLastName and computes a full name - function computeIsEditable( - profileInput: offersProfile - ): WithIsEditable { - return { - ...profileInput, - isEditable: profileInput["editToken" as keyof typeof profileInput] === input.token, - } - } - return result ? computeIsEditable(result) : result; + return result ? computeIsEditable(result, input.token) : result; }, }) .mutation('create', {