From 8a4a627f801adfd556d410dd610b9952b1fa5be6 Mon Sep 17 00:00:00 2001
From: BryannYeap <e0543723@u.nus.edu>
Date: Sat, 15 Oct 2022 08:21:01 +0800
Subject: [PATCH] [offers][fix] Fix offer analysis API percentile calculation

---
 .../pages/offers/test/generateAnalysis.tsx    |  2 +-
 .../src/pages/offers/test/getAnalysis.tsx     |  2 +-
 .../router/offers/offers-analysis-router.ts   | 56 ++++++-------------
 3 files changed, 18 insertions(+), 42 deletions(-)

diff --git a/apps/portal/src/pages/offers/test/generateAnalysis.tsx b/apps/portal/src/pages/offers/test/generateAnalysis.tsx
index fcb969bd..07c66e77 100644
--- a/apps/portal/src/pages/offers/test/generateAnalysis.tsx
+++ b/apps/portal/src/pages/offers/test/generateAnalysis.tsx
@@ -5,7 +5,7 @@ import { trpc } from '~/utils/trpc';
 function GenerateAnalysis() {
   const analysis = trpc.useQuery([
     'offers.analysis.generate',
-    { profileId: 'cl98yxuei002htx1s8lrmwzmy' },
+    { profileId: 'cl98ywtbv0000tx1s4p18eol1' },
   ]);
 
   return <div>{JSON.stringify(analysis.data)}</div>;
diff --git a/apps/portal/src/pages/offers/test/getAnalysis.tsx b/apps/portal/src/pages/offers/test/getAnalysis.tsx
index d7ae6797..ed96f74d 100644
--- a/apps/portal/src/pages/offers/test/getAnalysis.tsx
+++ b/apps/portal/src/pages/offers/test/getAnalysis.tsx
@@ -5,7 +5,7 @@ import { trpc } from '~/utils/trpc';
 function GetAnalysis() {
   const analysis = trpc.useQuery([
     'offers.analysis.get',
-    { profileId: 'cl98yxuei002htx1s8lrmwzmy' },
+    { profileId: 'cl98ywtbv0000tx1s4p18eol1' },
   ]);
 
   return <div>{JSON.stringify(analysis.data)}</div>;
diff --git a/apps/portal/src/server/router/offers/offers-analysis-router.ts b/apps/portal/src/server/router/offers/offers-analysis-router.ts
index 7ed9028d..efa684af 100644
--- a/apps/portal/src/server/router/offers/offers-analysis-router.ts
+++ b/apps/portal/src/server/router/offers/offers-analysis-router.ts
@@ -13,7 +13,7 @@ import { TRPCError } from '@trpc/server';
 
 import { createRouter } from '../context';
 
-const binarySearchOfferPercentile = (
+const searchOfferPercentile = (
   offer: OffersOffer & {
     OffersFullTime:
       | (OffersFullTime & {
@@ -29,40 +29,13 @@ const binarySearchOfferPercentile = (
   },
   similarOffers: Array<any> | string,
 ) => {
-  let start = 0;
-  let end = similarOffers.length - 1;
 
-  const salary =
-    offer.jobType === JobType.FULLTIME
-      ? offer.OffersFullTime?.totalCompensation.value
-      : offer.OffersIntern?.monthlySalary.value;
-
-  if (!salary) {
-    throw new TRPCError({
-      code: 'BAD_REQUEST',
-      message: 'Cannot analyse without salary',
-    });
-  }
-
-  while (start <= end) {
-    const mid = Math.floor((start + end) / 2);
-
-    const similarOffer = similarOffers[mid];
-    const similarSalary =
-      similarOffer.jobType === JobType.FULLTIME
-        ? similarOffer.OffersFullTime?.totalCompensation.value
-        : similarOffer.OffersIntern?.monthlySalary.value;
-
-    if (similarSalary === salary) {
-      return mid;
-    }
-
-    if (salary < similarSalary) {
-      end = mid - 1;
-    } else {
-      start = mid + 1;
+  for (let i = 0; i < similarOffers.length; i++) {
+    if (similarOffers[i].id === offer.id) {
+      return i;
     }
   }
+
   return -1;
 };
 
@@ -335,29 +308,32 @@ export const offersAnalysisRouter = createRouter()
       });
 
       let similarCompanyOffers = similarOffers.filter(
-        (offer: { companyId: string }) =>
-          offer.companyId === overallHighestOffer.companyId,
+        (offer) => offer.companyId === overallHighestOffer.companyId,
       );
 
       // CALCULATE PERCENTILES
-      const overallIndex = binarySearchOfferPercentile(
+      const overallIndex = searchOfferPercentile(
         overallHighestOffer,
         similarOffers,
       );
-      const overallPercentile = overallIndex / similarOffers.length;
+      const overallPercentile =
+        similarOffers.length === 0 ? 0 : overallIndex / similarOffers.length;
 
-      const companyIndex = binarySearchOfferPercentile(
+      const companyIndex = searchOfferPercentile(
         overallHighestOffer,
         similarCompanyOffers,
       );
-      const companyPercentile = companyIndex / similarCompanyOffers.length;
+      const companyPercentile =
+        similarCompanyOffers.length === 0
+          ? 0
+          : companyIndex / similarCompanyOffers.length;
 
       // FIND TOP >=90 PERCENTILE OFFERS
       similarOffers = similarOffers.filter(
-        (offer: { id: string }) => offer.id !== overallHighestOffer.id,
+        (offer) => offer.id !== overallHighestOffer.id,
       );
       similarCompanyOffers = similarCompanyOffers.filter(
-        (offer: { id: string }) => offer.id !== overallHighestOffer.id,
+        (offer) => offer.id !== overallHighestOffer.id,
       );
 
       const noOfSimilarOffers = similarOffers.length;