Skip to content

Commit 381f88c

Browse files
authored
Fix qa_custom_brokers bugs (#5659)
* separate results for different users * separate results for different users * separate results for different users * fix last optout date set * requested removal * lint * add profileid param to mocked results under maintenance * remove console log * replace onerepscanid with profile id * add helpful comment * rmv l10n file accidentally added * reorg comments * add todo comment
1 parent e3738c0 commit 381f88c

File tree

5 files changed

+77
-55
lines changed

5 files changed

+77
-55
lines changed

src/app/(proper_react)/(redesign)/(authenticated)/admin/qa-customs/onerepConfig.tsx

+18-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const OnerepConfigPage = ({
2929
}: Props) => {
3030
const [brokers, setBrokers] = useState<OnerepScanResultDataBrokerRow[]>([]);
3131
const [newBroker, setNewBroker] = useState<OnerepScanResultDataBrokerRow>({
32+
// TODO: MNTOR-4153 use onerep_profile_id instead
33+
// Using the onerep_scan_id as a placeholder for the profile ID
34+
onerep_scan_id: onerepProfileId,
3235
link: "",
3336
age: 30,
3437
data_broker: "",
@@ -46,7 +49,6 @@ const OnerepConfigPage = ({
4649
url: "",
4750
id: 0,
4851
onerep_scan_result_id: Math.floor(Math.random() * 2147483647),
49-
onerep_scan_id: 0,
5052
data_broker_id: 0,
5153
created_at: new Date(),
5254
updated_at: new Date(),
@@ -69,8 +71,10 @@ const OnerepConfigPage = ({
6971
const fetchBrokers = async () => {
7072
setBrokersFetchHappened(false);
7173
try {
74+
// TODO: MNTOR-4153 use onerep_profile_id instead
75+
// Using the onerep_scan_id as a placeholder for the profile ID
7276
const response = await fetch(
73-
`${endpointBase}?onerep_scan_result_id=${onerepProfileId}`,
77+
`${endpointBase}?onerep_scan_id=${onerepProfileId}`,
7478
);
7579
const data = await response.json();
7680
setBrokers(data);
@@ -86,9 +90,12 @@ const OnerepConfigPage = ({
8690
| React.ChangeEvent<HTMLSelectElement>,
8791
) => {
8892
const { name, value } = e.target;
93+
const updatedValue =
94+
name === "last_optout_at" ? (value ? new Date(value) : null) : value;
95+
8996
setNewBroker({
9097
...newBroker,
91-
[name]: name === "manually_resolved" ? value === "true" : value,
98+
[name]: name === "manually_resolved" ? value === "true" : updatedValue,
9299
});
93100
};
94101

@@ -361,6 +368,9 @@ const OnerepConfigPage = ({
361368
<option value="new">New</option>
362369
<option value="optout_in_progress">In Progress</option>
363370
<option value="removed">Removed</option>
371+
<option value="waiting_for_verification">
372+
Requested Removal
373+
</option>
364374
</select>
365375
</label>
366376

@@ -401,8 +411,11 @@ const OnerepConfigPage = ({
401411
className={styles.input}
402412
type="date"
403413
name="last_optout_at"
404-
placeholder={new Date().toISOString().split("T")[0]}
405-
value={newBroker.last_optout_at?.toDateString()}
414+
value={
415+
newBroker.last_optout_at
416+
? newBroker.last_optout_at.toISOString().split("T")[0]
417+
: ""
418+
}
406419
onChange={handleChange}
407420
/>
408421
</label>

src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/dashboard/fix/data-broker-profiles/removal-under-maintenance/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default async function RemovalUnderMaintenance() {
4848
process.env.NODE_ENV !== "production";
4949

5050
const scanResultsWithRemovalUnderMaintenance = useMockedScans
51-
? await getMockedScanResultsWithBrokerUnderMaintenance()
51+
? await getMockedScanResultsWithBrokerUnderMaintenance(profileId)
5252
: await getScanResultsWithBrokerUnderMaintenance(profileId);
5353

5454
const scanResults = useMockedScans

src/app/api/v1/admin/qa-customs/onerep/route.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from "../../../../../../db/tables/qa_customs";
1818
import { getServerSession } from "../../../../../functions/server/getServerSession";
1919
import { OnerepScanResultDataBrokerRow } from "knex/types/tables";
20+
import { getOnerepProfileId } from "../../../../../../db/tables/subscribers";
2021

2122
function successResponse() {
2223
return NextResponse.json(
@@ -54,7 +55,17 @@ export async function GET() {
5455
const prodErr = errorIfProduction();
5556
if (prodErr !== null) return prodErr;
5657

57-
return NextResponse.json(await getAllMockedScanResults());
58+
const session = await getServerSession();
59+
const email = session?.user.email;
60+
61+
const subscriberId = session?.user.subscriber?.id;
62+
if (!session || !email || !subscriberId) return unauthError();
63+
64+
const onerepProfileId = await getOnerepProfileId(subscriberId);
65+
if (!onerepProfileId)
66+
return internalServerError("Unable to fetch OneRep profile ID");
67+
68+
return NextResponse.json(await getAllMockedScanResults(onerepProfileId));
5869
}
5970

6071
export async function POST(req: NextRequest) {
@@ -85,6 +96,7 @@ export async function POST(req: NextRequest) {
8596
const scan_result_status = body.scan_result_status || "new";
8697
const broker_status = body.broker_status || "active";
8798
const url = "";
99+
const onerep_scan_id = body.onerep_scan_id;
88100

89101
const brokerData: OnerepScanResultDataBrokerRow = {
90102
link,
@@ -101,11 +113,11 @@ export async function POST(req: NextRequest) {
101113
manually_resolved,
102114
optout_attempts,
103115
last_optout_at,
104-
scan_result_status: scan_result_status,
105-
broker_status: broker_status,
106-
url: url,
116+
scan_result_status,
117+
broker_status,
118+
url,
107119
id: 0,
108-
onerep_scan_id: 0,
120+
onerep_scan_id,
109121
data_broker_id: 0,
110122
created_at: new Date(),
111123
updated_at: new Date(),

src/db/tables/onerep_scans.ts

+25-38
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
} from "knex/types/tables";
1919
import { RemovalStatus } from "../../app/functions/universal/scanResult.js";
2020
import { CONST_DAY_MILLISECONDS } from "../../constants.ts";
21-
import { getAllMockedScanResults, getQaToggleRow } from "./qa_customs.ts";
21+
import { getAllMockedScanResults } from "./qa_customs.ts";
2222

2323
const knex = createDbConnection();
2424

@@ -159,43 +159,20 @@ async function getLatestOnerepScanResults(
159159
onerepProfileId: number | null,
160160
): Promise<LatestOnerepScanDataOld> {
161161
const scan = await getLatestOnerepScan(onerepProfileId);
162-
163162
let results: OnerepScanResultRow[] = [];
164163

165-
if (scan !== null) {
166-
const qaToggles = await getQaToggleRow(onerepProfileId);
167-
let showCustomBrokers = false;
168-
let showRealBrokers = true;
169-
170-
if (qaToggles) {
171-
showCustomBrokers = qaToggles.show_custom_brokers;
172-
showRealBrokers = qaToggles.show_real_brokers;
173-
}
174-
175-
const qaBrokers = !showCustomBrokers ? [] : await getAllMockedScanResults();
176-
if (!showRealBrokers) {
177-
logger.info("get_latest_results_custom_brokers", {
178-
onerepProfileId,
179-
onerepScanId: scan?.onerep_scan_id,
180-
qaBrokers,
181-
});
182-
results = qaBrokers;
183-
} else {
184-
// Fetch initial results from onerep_scan_results
185-
const scanResults = (await knex("onerep_scan_results as sr")
186-
.select(
187-
"sr.*",
188-
"s.created_at as scan_created_at",
189-
"s.updated_at as scan_updated_at",
190-
)
191-
.distinctOn("link")
192-
.where("onerep_profile_id", onerepProfileId)
193-
.innerJoin("onerep_scans as s", "sr.onerep_scan_id", "s.onerep_scan_id")
194-
.orderBy("link")
195-
.orderBy("onerep_scan_result_id", "desc")) as OnerepScanResultRow[];
196-
results = [...scanResults];
197-
}
198-
}
164+
const scanResults = (await knex("onerep_scan_results as sr")
165+
.select(
166+
"sr.*",
167+
"s.created_at as scan_created_at",
168+
"s.updated_at as scan_updated_at",
169+
)
170+
.distinctOn("link")
171+
.where("onerep_profile_id", onerepProfileId)
172+
.innerJoin("onerep_scans as s", "sr.onerep_scan_id", "s.onerep_scan_id")
173+
.orderBy("link")
174+
.orderBy("onerep_scan_result_id", "desc")) as OnerepScanResultRow[];
175+
results = [...scanResults];
199176

200177
return {
201178
scan: scan ?? null,
@@ -501,15 +478,25 @@ async function getMockedScanResults(
501478

502479
const scan = await getLatestOnerepScan(onerepProfileId);
503480
const scanResults: OnerepScanResultDataBrokerRow[] | OnerepScanResultRow[] =
504-
await getAllMockedScanResults();
481+
await getAllMockedScanResults(onerepProfileId);
505482

506483
return { scan: scan ?? null, results: scanResults } as LatestOnerepScanData;
507484
}
508485

509-
async function getMockedScanResultsWithBrokerUnderMaintenance(): Promise<LatestOnerepScanData> {
486+
async function getMockedScanResultsWithBrokerUnderMaintenance(
487+
onerepProfileId: number | null,
488+
): Promise<LatestOnerepScanData> {
489+
if (onerepProfileId === null) {
490+
return {
491+
scan: null,
492+
results: [],
493+
} as LatestOnerepScanData;
494+
}
495+
510496
let scanResults = (await knex("qa_custom_brokers")
511497
.where("broker_status", "removal_under_maintenance")
512498
.where("manually_resolved", false)
499+
.where("onerep_scan_id", onerepProfileId)
513500
.select("*")) as OnerepScanResultDataBrokerRow[];
514501

515502
scanResults = scanResults.filter(

src/db/tables/qa_customs.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,22 @@ async function addQaCustomBroker(
135135
}
136136
}
137137

138-
async function getAllMockedScanResults(): Promise<
139-
OnerepScanResultDataBrokerRow[]
140-
> {
141-
const res = (await knex("qa_custom_brokers").select(
142-
"*",
143-
)) as OnerepScanResultDataBrokerRow[];
138+
async function getAllMockedScanResults(
139+
onerepProfileId: number | null,
140+
): Promise<OnerepScanResultDataBrokerRow[]> {
141+
if (onerepProfileId === null) {
142+
logger.error(`onerepProfileId not set`);
143+
}
144+
145+
// TODO: MNTOR-4153 use onerep_profile_id instead
146+
// Using the onerep_scan_id as a placeholder for the profile ID
147+
const res = (await knex("qa_custom_brokers")
148+
.select("*")
149+
.where(
150+
"onerep_scan_id",
151+
onerepProfileId,
152+
)) as OnerepScanResultDataBrokerRow[];
153+
144154
return res;
145155
}
146156

0 commit comments

Comments
 (0)