Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/api/src/routes/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions apps/api/tests/reports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
16 changes: 15 additions & 1 deletion apps/web/components/reports/ReportWizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<typeof schema>;

Expand All @@ -91,6 +94,8 @@ const EMPTY: FormValues = {
city: "",
state: "",
pincode: "",
scannedBarcode: undefined,
medicineId: undefined,
};

// ─── Per-step field keys ────────────────────────────────────────────────────────
Expand Down Expand Up @@ -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<FormValues>({
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(() => {
Expand Down Expand Up @@ -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) => {
Expand Down
2 changes: 2 additions & 0 deletions apps/web/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export type ReportPayload = {
pincode: string;
latitude?: number;
longitude?: number;
scannedBarcode?: string;
medicineId?: string;
};

export type SubmittedReport = {
Expand Down
Loading