From bca6e85f0a1149070dc806e053b65049a74461fe Mon Sep 17 00:00:00 2001 From: BryannYeap Date: Wed, 19 Oct 2022 05:10:54 +0800 Subject: [PATCH] [offers][chore] Add typed response for get offers API --- .../components/offers/table/OffersTable.tsx | 6 +-- apps/portal/src/mappers/offers-mappers.ts | 52 +++++++++++++++++++ .../portal/src/server/router/offers/offers.ts | 19 ++++--- apps/portal/src/types/offers.d.ts | 11 ++-- 4 files changed, 73 insertions(+), 15 deletions(-) diff --git a/apps/portal/src/components/offers/table/OffersTable.tsx b/apps/portal/src/components/offers/table/OffersTable.tsx index 848098d7..ca49fc9b 100644 --- a/apps/portal/src/components/offers/table/OffersTable.tsx +++ b/apps/portal/src/components/offers/table/OffersTable.tsx @@ -73,10 +73,10 @@ export default function OffersTable({ }); setOffers(filteredData); setPagination({ - currentPage: (response.paging.currPage as number) + 1, - numOfItems: response.paging.numOfItemsInPage, + currentPage: response.paging.currentPage + 1, + numOfItems: response.paging.numOfItems, numOfPages: response.paging.numOfPages, - totalItems: response.paging.totalNumberOfOffers, + totalItems: response.paging.totalItems, }); }, }, diff --git a/apps/portal/src/mappers/offers-mappers.ts b/apps/portal/src/mappers/offers-mappers.ts index 0e78d0f0..6b570323 100644 --- a/apps/portal/src/mappers/offers-mappers.ts +++ b/apps/portal/src/mappers/offers-mappers.ts @@ -22,9 +22,12 @@ import type { AnalysisOffer, Background, CreateOfferProfileResponse, + DashboardOffer, Education, Experience, + GetOffersResponse, OffersCompany, + Paging, Profile, ProfileAnalysis, ProfileOffer, @@ -522,3 +525,52 @@ export const addToProfileResponseMapper = (updatedProfile: { return addToProfileResponse; }; + +export const dashboardOfferDtoMapper = ( + offer: OffersOffer & { + OffersFullTime: + | (OffersFullTime & { + baseSalary: OffersCurrency; + bonus: OffersCurrency; + stocks: OffersCurrency; + totalCompensation: OffersCurrency; + }) + | null; + OffersIntern: (OffersIntern & { monthlySalary: OffersCurrency }) | null; + company: Company; + profile: OffersProfile & { background: OffersBackground | null }; + }, +) => { + const dashboardOfferDto: DashboardOffer = { + company: offersCompanyDtoMapper(offer.company), + id: offer.id, + income: valuationDtoMapper({ currency: '', value: -1 }), + monthYearReceived: offer.monthYearReceived, + profileId: offer.profileId, + title: offer.OffersFullTime?.title ?? '', + totalYoe: offer.profile.background?.totalYoe ?? -1, + }; + + if (offer.OffersFullTime) { + dashboardOfferDto.income = valuationDtoMapper( + offer.OffersFullTime.totalCompensation, + ); + } else if (offer.OffersIntern) { + dashboardOfferDto.income = valuationDtoMapper( + offer.OffersIntern.monthlySalary, + ); + } + + return dashboardOfferDto; +}; + +export const getOffersResponseMapper = ( + data: Array, + paging: Paging, +) => { + const getOffersResponse: GetOffersResponse = { + data, + paging, + }; + return getOffersResponse; +}; diff --git a/apps/portal/src/server/router/offers/offers.ts b/apps/portal/src/server/router/offers/offers.ts index ceb1367c..a35e2d2e 100644 --- a/apps/portal/src/server/router/offers/offers.ts +++ b/apps/portal/src/server/router/offers/offers.ts @@ -1,6 +1,11 @@ import { z } from 'zod'; import { TRPCError } from '@trpc/server'; +import { + dashboardOfferDtoMapper, + getOffersResponseMapper, +} from '~/mappers/offers-mappers'; + import { createRouter } from '../context'; const yoeCategoryMap: Record = { @@ -299,14 +304,14 @@ export const offersRouter = createRouter().query('list', { : data.length; const paginatedData = data.slice(startRecordIndex, endRecordIndex); - return { - data: paginatedData, - paging: { - currPage: input.offset, - numOfItemsInPage: paginatedData.length, + return getOffersResponseMapper( + paginatedData.map((offer) => dashboardOfferDtoMapper(offer)), + { + currentPage: input.offset, + numOfItems: paginatedData.length, numOfPages: Math.ceil(data.length / input.limit), - totalNumberOfOffers: data.length, + totalItems: data.length, }, - }; + ); }, }); diff --git a/apps/portal/src/types/offers.d.ts b/apps/portal/src/types/offers.d.ts index 291c44ba..c6724611 100644 --- a/apps/portal/src/types/offers.d.ts +++ b/apps/portal/src/types/offers.d.ts @@ -65,6 +65,7 @@ export type DashboardOffer = { id: string; income: Valuation; monthYearReceived: Date; + profileId: string; title: string; totalYoe: number; }; @@ -124,10 +125,10 @@ export type GetOffersResponse = { }; export type Paging = { - currPage: number; - numOfItemsInPage: number; + currentPage: number; + numOfItems: number; numOfPages: number; - totalNumberOfOffers: number; + totalItems: number; }; export type CreateOfferProfileResponse = { @@ -136,7 +137,7 @@ export type CreateOfferProfileResponse = { }; export type OffersDiscussion = { - data: Array; + data: Array; }; export type ProfileAnalysis = { @@ -182,4 +183,4 @@ export type AddToProfileResponse = { id: string; profileName: string; userId: string; -}; \ No newline at end of file +};