From 726719784b65550b1e3c0e56c67fd82781692a02 Mon Sep 17 00:00:00 2001 From: Joey Zhou Date: Tue, 3 Dec 2024 11:22:01 -0800 Subject: [PATCH 1/2] feat: enable unresolve --- .../[onerepScanResultId]/resolution/route.ts | 62 ++++++++++++++++++- src/db/tables/onerep_scans.ts | 7 ++- 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/app/api/v1/user/scan-result/[onerepScanResultId]/resolution/route.ts b/src/app/api/v1/user/scan-result/[onerepScanResultId]/resolution/route.ts index 7ede6745cad..960f1c7c97b 100644 --- a/src/app/api/v1/user/scan-result/[onerepScanResultId]/resolution/route.ts +++ b/src/app/api/v1/user/scan-result/[onerepScanResultId]/resolution/route.ts @@ -8,7 +8,7 @@ import { getServerSession } from "../../../../../../functions/server/getServerSe import { logger } from "../../../../../../functions/server/logging"; import { isOnerepScanResultForSubscriber, - markOnerepScanResultAsResolved, + setOnerepScanResultManualResolution, } from "../../../../../../../db/tables/onerep_scans"; import { markQaCustomBrokerAsResolved } from "../../../../../../../db/tables/qa_customs"; import { isAdmin } from "../../../../../utils/auth"; @@ -55,14 +55,72 @@ export async function POST( subscriberId: session.user.subscriber.id, }); if (!isAllowedToResolve) { + return new NextResponse( + JSON.stringify({ success: false, message: "Unauthorized" }), + { status: 403 }, + ); + } + + try { + await setOnerepScanResultManualResolution(scanResultId, true); + return new NextResponse( + JSON.stringify({ success: true }), + { status: 200 }, + ); + } catch (e) { + logger.error(e); + return new NextResponse( + JSON.stringify({ + success: false, + message: "Something went wrong, please try again.", + }), + { status: 500 }, + ); + } +} + +export async function PUT( + params: { onerepScanResultId: string }, + body: { resolved: boolean }, +): Promise> { + const scanResultId = Number.parseInt(params.onerepScanResultId, 10); + const { resolved } = body; + const session = await getServerSession(); + + if (!session?.user?.subscriber) { + return new NextResponse( + JSON.stringify({ success: false, message: "Unauthenticated" }), + { status: 401 }, + ); + } + + if (typeof scanResultId !== "number" || Number.isNaN(scanResultId)) { return new NextResponse( JSON.stringify({ success: false, message: "Invalid scan result ID" }), + { status: 400 }, + ); + } + + if (typeof resolved !== "boolean") { + return new NextResponse( + JSON.stringify({ success: false, message: "Invalid resolution value" }), + { status: 400 }, + ); + } + + const isAllowedToResolve = await isOnerepScanResultForSubscriber({ + onerepScanResultId: scanResultId, + subscriberId: session.user.subscriber.id, + }); + if (!isAllowedToResolve) { + return new NextResponse( + JSON.stringify({ success: false, message: "Unauthorized" }), { status: 403 }, ); } try { - await markOnerepScanResultAsResolved(scanResultId); + await setOnerepScanResultManualResolution(scanResultId, resolved); return new NextResponse( JSON.stringify({ success: true }), { status: 200 }, diff --git a/src/db/tables/onerep_scans.ts b/src/db/tables/onerep_scans.ts index b6ba56b79e2..37c9b05a9c5 100644 --- a/src/db/tables/onerep_scans.ts +++ b/src/db/tables/onerep_scans.ts @@ -301,15 +301,16 @@ async function isOnerepScanResultForSubscriber(params: { return typeof result?.onerep_scan_result_id === "number"; } -async function markOnerepScanResultAsResolved( +async function setOnerepScanResultManualResolution( onerepScanResultId: number, + resolved: boolean, ): Promise { logger.info("scan_resolved", { onerepScanResultId, }); await knex("onerep_scan_results") .update({ - manually_resolved: true, + manually_resolved: resolved, // @ts-ignore knex.fn.now() results in it being set to a date, // even if it's not typed as a JS date object: updated_at: knex.fn.now(), @@ -448,7 +449,7 @@ export { addOnerepScanResults, getScansCount, isOnerepScanResultForSubscriber, - markOnerepScanResultAsResolved, + setOnerepScanResultManualResolution, getScansCountForProfile, deleteScansForProfile, deleteScanResultsForProfile, From 7096816d490793ce1a8e437455dd5c73f675fe9f Mon Sep 17 00:00:00 2001 From: Joey Zhou Date: Tue, 3 Dec 2024 11:30:46 -0800 Subject: [PATCH 2/2] feat: update PUT body --- .../scan-result/[onerepScanResultId]/resolution/route.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/app/api/v1/user/scan-result/[onerepScanResultId]/resolution/route.ts b/src/app/api/v1/user/scan-result/[onerepScanResultId]/resolution/route.ts index 960f1c7c97b..f0e0d6eec53 100644 --- a/src/app/api/v1/user/scan-result/[onerepScanResultId]/resolution/route.ts +++ b/src/app/api/v1/user/scan-result/[onerepScanResultId]/resolution/route.ts @@ -80,11 +80,10 @@ export async function POST( } export async function PUT( - params: { onerepScanResultId: string }, - body: { resolved: boolean }, + req: NextRequest, ): Promise> { - const scanResultId = Number.parseInt(params.onerepScanResultId, 10); - const { resolved } = body; + // const scanResultId = Number.parseInt(params.onerepScanResultId, 10); + // const { resolved } = body; const session = await getServerSession(); if (!session?.user?.subscriber) { @@ -94,6 +93,8 @@ export async function PUT( ); } + const { resolved, scanResultId } = await req.json(); + if (typeof scanResultId !== "number" || Number.isNaN(scanResultId)) { return new NextResponse( JSON.stringify({ success: false, message: "Invalid scan result ID" }),