diff --git a/packages/doorman-api/src/functions/api/door/logCall.ts b/packages/doorman-api/src/functions/api/door/logCall.ts index a3ccc40..0e33abd 100644 --- a/packages/doorman-api/src/functions/api/door/logCall.ts +++ b/packages/doorman-api/src/functions/api/door/logCall.ts @@ -12,7 +12,8 @@ import { z } from "zod"; import { UserAgentHeader } from "../../../utils/blockUserAgent"; import { setResponseJson } from "../../../utils/responseUtils"; import { LOG_CALL_SK, LogCallSchema } from "../../../schema/LogCall"; -import { isTTLInFuture } from "../../../common/TTLHelper"; + +import crypto from "crypto"; export const LogCallRequestSchema = z.object({ caller: z.string(), @@ -27,8 +28,24 @@ export const LogCallResponseSchema = z.object({ export type LogCallResponse = z.infer; -function getCode() { - return `${Math.floor(Math.random() * 10000)}`.padStart(4, '0'); +// hash is 4 digit number based on todays date + phone number caller +// cost saving so we don't generate a new OTP for every caller even if its the same caller +function getCode(caller: string) { + const hash = crypto.createHash("sha256"); + const today = new Date(); + + hash.update(today.toLocaleDateString('en-US')); + hash.update(caller); + + const hashHex = hash.digest('hex'); + + // 2. Convert the hexadecimal string to a BigInt + // This is necessary for large hash values that exceed JavaScript's Number limit. + const hashBigInt = BigInt(`0x${hashHex}`); + + // 3. Convert the BigInt to a decimal string + const hashDecimal = hashBigInt.toString(); + return hashDecimal.substring(hashDecimal.length - 4,); }; export const handler: ServerlessFunctionSignature = withMetrics("logCall", async (context, event, callback, metricsRegistry) => { @@ -47,17 +64,16 @@ export const handler: ServerlessFunctionSignature !isTTLInFuture(log)); - console.log(`There are ${toRemove.length} old call logs to remove`); - await db.entities.logCall.deleteBatch(toRemove); - console.log("done cleaning up logged calls"); + // TODO: best efforts cleanup + // console.log("Attempting best efforts cleanup of logged calls") + // const items = await db.entities.logCall.findAll(); + // const toRemove = items.filter(item => item.SK === LOG_CALL_SK && !isTTLInFuture(item)); + // console.log(`There are ${toRemove.length} old call logs to remove`); + // await db.entities.logCall.deleteBatch(toRemove); + // console.log("done cleaning up logged calls"); // log this caller - const otp = getCode(); + const otp = getCode(caller); const logCall = LogCallSchema.parse({ PK: otp, SK: LOG_CALL_SK, diff --git a/packages/doorman-ui/src/pages/OnboardingPage.tsx b/packages/doorman-ui/src/pages/OnboardingPage.tsx index e248c88..8315955 100644 --- a/packages/doorman-ui/src/pages/OnboardingPage.tsx +++ b/packages/doorman-ui/src/pages/OnboardingPage.tsx @@ -4,6 +4,7 @@ import { AppLayout, BreadcrumbGroup, Container, Flashbar, FlashbarProps, Header, import OtpInput from 'react-otp-input'; import { CountdownBar } from "../components/CountdownBar"; import { fetchUrlEncoded } from "../helpers/FetchHelper"; +import { readSync } from "fs"; export function OnboardingPage() { const navigate = useNavigate(); @@ -111,10 +112,13 @@ export function OnboardingPage() { } if (detail.requestedStepIndex === 2) { - fetch('/api/door/auth?door=onboardingflag&key=1234') + fetchUrlEncoded('/api/door/auth', { + door: "onboardingflag", + key: 1234 + }) .then(res => res.json()) .then(res => { - if (res.status === "CLOSED") { + if (res.status === "CLOSED" || !res.msg) { addAlert('error', 'Something went wrong, please try again'); } else { addAlert("in-progress", (