diff --git a/apps/api/src/routes/reports.ts b/apps/api/src/routes/reports.ts index a3f09ef1a..789bfc82a 100644 --- a/apps/api/src/routes/reports.ts +++ b/apps/api/src/routes/reports.ts @@ -62,6 +62,8 @@ const createReportSchema = z.object({ .min(-180, "Longitude must be between -180 and 180") .max(180, "Longitude must be between -180 and 180") .optional(), + scannedBarcode: z.string().optional(), + medicineId: z.string().uuid().optional(), }); const buildReportLocation = (latitude?: number, longitude?: number) => { @@ -134,6 +136,8 @@ reportsRouter.post( is_escalated: !validation.passed, duplicate_group_id: validation.duplicateGroupId ?? null, status: "pending", + scanned_barcode: data.scannedBarcode ?? null, + medicine_id: data.medicineId ?? null, }) .select() .single(); diff --git a/apps/api/tests/reports.test.ts b/apps/api/tests/reports.test.ts index b69534b11..9dd69674a 100644 --- a/apps/api/tests/reports.test.ts +++ b/apps/api/tests/reports.test.ts @@ -28,6 +28,7 @@ jest.mock("../src/services/reportValidation.service", () => ({ computeReportHash: jest .fn() .mockReturnValue("abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"), + anonymizeIp: jest.fn().mockReturnValue("127.0.0.1"), })); jest.mock("../src/middleware/auth", () => { diff --git a/apps/web/components/reports/ReportWizard.tsx b/apps/web/components/reports/ReportWizard.tsx index 20df37f5d..40cd4328d 100644 --- a/apps/web/components/reports/ReportWizard.tsx +++ b/apps/web/components/reports/ReportWizard.tsx @@ -8,6 +8,7 @@ */ import React, { useState, useEffect, useId } from "react"; +import { useSearchParams } from "next/navigation"; import { useForm, FormProvider, useFormContext } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; @@ -78,6 +79,8 @@ const schema = z.object({ "Enter a valid 6-digit Indian Pincode (cannot start with 0)" ) ), + scannedBarcode: z.string().optional(), + medicineId: z.string().optional(), }); export type FormValues = z.infer; @@ -91,6 +94,8 @@ const EMPTY: FormValues = { city: "", state: "", pincode: "", + scannedBarcode: undefined, + medicineId: undefined, }; // ─── Per-step field keys ──────────────────────────────────────────────────────── @@ -823,13 +828,14 @@ export default function ReportWizard() { const [pendingCount, setPendingCount] = useState(0); const [restoredDraft, setRestoredDraft] = useState(false); const submitErrorId = useId(); + const searchParams = useSearchParams(); const methods = useForm({ resolver: zodResolver(schema), defaultValues: EMPTY, mode: "onTouched", }); - const { trigger, handleSubmit, reset } = methods; + const { trigger, handleSubmit, reset, setValue } = methods; // Cleanup blob URLs on unmount to prevent memory leaks useEffect(() => { @@ -861,6 +867,14 @@ export default function ReportWizard() { })(); }, []); + // Extract barcode and medicineId from URL query parameters + useEffect(() => { + const barcode = searchParams.get("barcode"); + const medicineId = searchParams.get("medicineId"); + if (barcode) setValue("scannedBarcode", barcode); + if (medicineId) setValue("medicineId", medicineId); + }, [searchParams, setValue]); + // Background sync of queued offline reports useEffect(() => { const cleanup = initBackgroundSync((count) => { diff --git a/apps/web/lib/api.ts b/apps/web/lib/api.ts index c5d99071d..797a3b1f0 100644 --- a/apps/web/lib/api.ts +++ b/apps/web/lib/api.ts @@ -34,6 +34,8 @@ export type ReportPayload = { pincode: string; latitude?: number; longitude?: number; + scannedBarcode?: string; + medicineId?: string; }; export type SubmittedReport = {