diff --git a/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/fix/data-broker-profiles/removal-under-maintenance/page.tsx b/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/fix/data-broker-profiles/removal-under-maintenance/page.tsx index 62b4aef9879..1ee09c1636e 100644 --- a/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/fix/data-broker-profiles/removal-under-maintenance/page.tsx +++ b/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/fix/data-broker-profiles/removal-under-maintenance/page.tsx @@ -8,7 +8,10 @@ import { RemovalUnderMaintenanceView } from "./RemovalUnderMaintenanceView"; import { StepDeterminationData } from "../../../../../../../../../functions/server/getRelevantGuidedSteps"; import { getCountryCode } from "../../../../../../../../../functions/server/getCountryCode"; import { headers } from "next/headers"; -import { getLatestOnerepScanResults } from "../../../../../../../../../../db/tables/onerep_scans"; +import { + getLatestOnerepScanResults, + getScanResultsWithBrokerUnderMaintenance, +} from "../../../../../../../../../../db/tables/onerep_scans"; import { getOnerepProfileId } from "../../../../../../../../../../db/tables/subscribers"; import { getSubscriberBreaches } from "../../../../../../../../../functions/server/getSubscriberBreaches"; import { getSubscriberEmails } from "../../../../../../../../../functions/server/getSubscriberEmails"; @@ -22,6 +25,8 @@ export default async function RemovalUnderMaintenance() { const countryCode = getCountryCode(headers()); const profileId = await getOnerepProfileId(session.user.subscriber.id); const latestScan = await getLatestOnerepScanResults(profileId); + // TODO: remove after testing + await getScanResultsWithBrokerUnderMaintenance(profileId); const data: StepDeterminationData = { countryCode, diff --git a/src/db/tables/onerep_scans.ts b/src/db/tables/onerep_scans.ts index 8bde717c627..23ff3f139d1 100644 --- a/src/db/tables/onerep_scans.ts +++ b/src/db/tables/onerep_scans.ts @@ -17,12 +17,13 @@ import { } from "knex/types/tables"; import { RemovalStatus } from "../../app/functions/universal/scanResult.js"; import { getQaCustomBrokers, getQaToggleRow } from "./qa_customs.ts"; +import { DataBrokerRow } from "../../knex-tables"; const knex = createDbConnection(); export interface LatestOnerepScanData { scan: OnerepScanRow | null; - results: OnerepScanResultRow[]; + results: OnerepScanResultRow[] | (OnerepScanResultRow & DataBrokerRow)[]; } async function getAllScansForProfile( @@ -398,28 +399,39 @@ async function getEmailForProfile(onerepProfileId: number) { } async function getScanResultsWithBrokerUnderMaintenance( - onerepProfileId: number, + onerepProfileId: number | null, ) { - const scanResults = await knex("onerep_scan_results") - .innerJoin( - "onerep_data_brokers", - "onerep_scan_results.data_broker", - "=", - "onerep_data_brokers.data_broker", + if (onerepProfileId === null) { + return null; + } + + let scanResults = await knex("onerep_scan_results as sr") + .select( + "sr.*", + "s.*", + "sr.status as scan_result_status", // rename to avoid collision + "db.status as broker_status", // rename to avoid collision ) - .where("onerep_scan_results.onerep_profile_id", onerepProfileId) // profile Id match - .where("onerep_data_brokers.status", "removal_under_maintenance") // data broker needs to be under maintenance - .andWhereRaw( - "\"onerep_scan_results.updated_at\" < NOW() - INTERVAL '200 day'", - ) // scan result needs to be 200 days or younger - .andWhere("onerep_scan_results.manually_resolved", "false") // not already manually removed - .andWhereNot("onerep_scan_results.status", "removed") // not auto removed - .orderBy("onerep_scan_result_id"); + .innerJoin("onerep_scans as s", "sr.onerep_scan_id", "s.onerep_scan_id") + .where("s.onerep_profile_id", onerepProfileId) + .andWhere("sr.manually_resolved", "false") + .andWhereNot("sr.status", "removed") + .join("onerep_data_brokers as db", "sr.data_broker", "db.data_broker") + .orderBy("sr.onerep_scan_result_id"); console.log("\n\n scan results broker maintenance:"); + console.log(scanResults.length); + + scanResults = scanResults.filter( + (result) => + result.broker_status === "removal_under_maintenance" || + new Date().getTime() - new Date(result.updated_at).getTime() > + 200 * 24 * 60 * 60 * 1000, + ); console.log({ scanResults }); + console.log(scanResults.length); - return scanResults; + return { results: scanResults } as LatestOnerepScanData; } export {