Skip to content

Commit

Permalink
chore: verification data handling functions
Browse files Browse the repository at this point in the history
chore: cleanup

chore: cleanup

chore: cleanup

chore: cleanup
  • Loading branch information
shon-button committed Jan 31, 2025
1 parent da18f41 commit 6bc31ee
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,98 +12,8 @@ import {
import { actionHandler } from "@bciers/actions";
import { lfoUiSchema } from "@reporting/src/data/jsonSchema/verification/verification";
import { sfoUiSchema } from "@reporting/src/data/jsonSchema/verification/verification";

// 🛠️ Function to update the report_verification_visits property in a given formData object
function updateReportVerificationVisits(formData: any): void {
// Initialize the report_verification_visits array
formData.report_verification_visits = [];

// Check if "None" is selected in visit_names
if (
Array.isArray(formData.visit_names) &&
formData.visit_names.includes("None")
) {
formData.report_verification_visits = [
{
visit_name: "None",
is_other_visit: false,
visit_coordinates: "",
visit_type: "",
},
];
return; // Exit early as "None" overrides all other data
} else if (formData.visit_names === "None") {
formData.report_verification_visits = [
{
visit_name: "None",
is_other_visit: false,
visit_coordinates: "",
visit_type: "",
},
];
return; // Exit early as "None" overrides all other data
}

// Handle visit_types
if (Array.isArray(formData.visit_types)) {
formData.report_verification_visits.push(
...formData.visit_types.map((type: any) => ({
visit_name: type.visit_name || "",
visit_type: type.visit_type || "",
is_other_visit: false,
visit_coordinates: "", // Default for non-other visits
})),
);
} else if (formData.visit_types) {
// Handle single object scenario for visit_types
formData.report_verification_visits.push({
visit_name: formData.visit_types.visit_name || "",
visit_type: formData.visit_types.visit_type || "",
is_other_visit: false,
visit_coordinates: "", // Default for non-other visits
});
}

// Handle visit_others
if (Array.isArray(formData.visit_others)) {
formData.report_verification_visits.push(
...formData.visit_others.map((other: any) => ({
visit_name: other.visit_name || "",
visit_type: other.visit_type || "",
is_other_visit: true,
visit_coordinates: other.visit_coordinates || "",
})),
);
} else if (formData.visit_others) {
// Handle single object scenario for visit_others
formData.report_verification_visits.push({
visit_name: formData.visit_others.visit_name || "",
visit_type: formData.visit_others.visit_type || "",
is_other_visit: true,
visit_coordinates: formData.visit_others.visit_coordinates || "",
});
}

// If "None" is found in visit_types or visit_others, override with "None"
const noneSelectedInVisitTypes = Array.isArray(formData.visit_types)
? formData.visit_types.some((type: any) => type.visit_name === "None")
: formData.visit_types?.visit_name === "None";

const noneSelectedInVisitOthers = Array.isArray(formData.visit_others)
? formData.visit_others.some((other: any) => other.visit_name === "None")
: formData.visit_others?.visit_name === "None";

if (noneSelectedInVisitTypes || noneSelectedInVisitOthers) {
formData.report_verification_visits = [
{
visit_name: "None",
is_other_visit: false,
visit_coordinates: "",
visit_type: "",
},
];
}
}
import { handleVerificationData } from "@reporting/src/app/utils/verification/handleVerificationData";
import { mergeVerificationData } from "@reporting/src/app/utils/verification/mergeVerificationData";

interface Props {
version_id: number;
Expand Down Expand Up @@ -133,93 +43,8 @@ export default function VerificationForm({
const handleChange = (e: IChangeEvent) => {
const updatedData = { ...e.formData };

// Detect if `visit_names` is an array or a single value
const isVisitNamesArray = Array.isArray(updatedData.visit_names);

if (isVisitNamesArray) {
// LFO scenario
const selectedValues = updatedData.visit_names;

if (selectedValues.includes("None")) {
// If "None" is selected:
// - Lock selection to only "None"
updatedData.visit_names = ["None"];

// - Clear `visit_types` and `visit_others`
updatedData.visit_types = [];
updatedData.visit_others = [];
} else {
// If "None" is not selected:
updatedData.visit_names = selectedValues.filter(
(value: string) => value !== "None",
);

// Update `visit_types` for facilities except "Other"
updatedData.visit_types = updatedData.visit_names
.filter((visit_name: string) => visit_name !== "Other")
.map((visit_name: string) => {
const existingVisitType = updatedData.visit_types?.find(
(item: { visit_name: string }) => item.visit_name === visit_name,
);
return (
existingVisitType || {
visit_name,
visit_type: "",
}
);
});

// If "Other" is selected, prepare `visit_others`
if (selectedValues.includes("Other")) {
updatedData.visit_others = updatedData.visit_others ?? [
{
visit_name: "",
visit_coordinates: "",
visit_type: "",
},
];
} else {
updatedData.visit_others = [{}];
}
}
} else {
// SFO scenario
const selectedVisitName = updatedData.visit_names;

if (selectedVisitName === "None") {
// If "None" is selected:
// - Lock selection to "None"
updatedData.visit_names = "None";

// - Clear `visit_types` and `visit_others`
updatedData.visit_types = null;
updatedData.visit_others = [{}];
} else if (selectedVisitName) {
// If a specific visit name is selected (e.g., "Facility X"):
if (selectedVisitName !== "Other") {
// Prepare or retain `visit_types`
updatedData.visit_types = updatedData.visit_types ?? {
visit_type: "",
};
// - Clear `visit_others`
updatedData.visit_others = [{}];
} else {
// If "Other" is selected, clear `visit_types`
updatedData.visit_types = null;

// Prepare `visit_others`
updatedData.visit_others = updatedData.visit_others ?? {
visit_name: "",
visit_coordinates: "",
visit_type: "",
};
}
} else {
// If no selection is made:
updatedData.visit_types = null;
updatedData.visit_others = [{}];
}
}
// LFO;SFO visit_names handling logic
handleVerificationData(updatedData, operationType);

// 🔄 Update the form data state with the modified data
setFormData(updatedData);
Expand All @@ -229,9 +54,9 @@ export default function VerificationForm({
const handleSubmit = async () => {
// 📷 Clone formData as payload
const payload = { ...formData };

debugger;
// ➕ Update report_verification_visits property based on visit_types and visit_others
updateReportVerificationVisits(payload);
mergeVerificationData(payload);

// 🧼 Remove unnecessary properties from payload
delete payload.visit_names;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { HasReportVersion } from "@reporting/src/app/utils/defaultPageFactoryTypes";
import { getReportNeedsVerification } from "@reporting/src/app/utils/getReportNeedsVerification";
import { getReportingOperation } from "@reporting/src/app/utils/getReportingOperation";
import { extendVerificationData } from "@reporting/src/app/utils/verification/extendVerificationData";

// import { verificationSchema } from "@reporting/src/data/jsonSchema/verification/verification";
export default async function VerificationPage({
Expand All @@ -26,82 +27,7 @@ export default async function VerificationPage({

// 🚀 Fetch initial form data
const initialData = (await getReportVerification(version_id)) || {};

// 🔄 Add properties conditionally based on operationType
if (operationType === "LFO") {
// Add the visit_names property
initialData.visit_names = (initialData.report_verification_visits || [])
.filter((visit: { is_other_visit: boolean }) => !visit.is_other_visit)
.map((visit: { visit_name: string }) => visit.visit_name);
if (
(initialData.report_verification_visits || []).some(
(visit: { is_other_visit: boolean }) => visit.is_other_visit,
)
) {
initialData.visit_names.push("Other");
}
// Add the visit_types property
initialData.visit_types = (initialData.report_verification_visits || [])
.filter(
(visit: { is_other_visit: boolean; visit_name: string }) =>
!visit.is_other_visit && visit.visit_name !== "None",
)
.map((visit: { visit_name: string; visit_type: string }) => ({
visit_name: visit.visit_name,
visit_type: visit.visit_type,
}));

// Add the visit_others property
initialData.visit_others = (
initialData.report_verification_visits || []
).some((visit: { is_other_visit: boolean }) => visit.is_other_visit)
? (initialData.report_verification_visits || [])
.filter((visit: { is_other_visit: boolean }) => visit.is_other_visit)
.map(
(visit: {
visit_name: string;
visit_coordinates: string;
visit_type: string;
}) => ({
visit_name: visit.visit_name,
visit_coordinates: visit.visit_coordinates,
visit_type: visit.visit_type,
}),
)
: [{}];
} else {
const visit = initialData.report_verification_visits?.[0];
// Add the visit_names property
if (visit) {
if (visit.is_other_visit) {
initialData.visit_names = "Other";
} else {
initialData.visit_names = visit.visit_name;
}
// Add the visit_others property
if (visit && !visit.is_other_visit && visit.visit_name !== "None") {
initialData.visit_types = visit.visit_type;
} else {
initialData.visit_types = undefined;
}
// Add the visit_others property
if (visit && visit.is_other_visit) {
initialData.visit_others = [
{
visit_name: visit.visit_name,
visit_coordinates: visit.visit_coordinates,
visit_type: visit.visit_type,
},
];
} else {
initialData.visit_others = [{}];
}
} else {
initialData.visit_names = undefined;
initialData.visit_types = undefined;
initialData.visit_others = [{}];
}
}
const transformedData = extendVerificationData(initialData);

// 🚀 Fetch the list of facilities associated with the specified version ID
const facilityList = await getReportFacilityList(version_id);
Expand All @@ -127,7 +53,7 @@ export default async function VerificationPage({
version_id={version_id}
operationType={operationType}
verificationSchema={verificationSchema}
initialData={initialData}
initialData={transformedData}
taskListElements={taskListElements}
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,10 @@ export const createVerificationSchema = (
schemaType === "SFO" ? { ...sfoSchema } : { ...lfoSchema };
const defaultVisistValues = ["None", "Other"];

// Dynamically populate the "visit_names" field's enum with the facilities
switch (schemaType) {
case "SFO":
(localSchema.properties?.visit_names as any).enum = [
...defaultVisistValues,
...facilities,
];
break;
case "LFO":
(localSchema.properties?.visit_names as any).items.enum = [
...defaultVisistValues,
...facilities,
];
break;
}
(localSchema.properties?.visit_names as any).items.enum = [
...defaultVisistValues,
...facilities,
];

// Return the customized schema.
return localSchema;
Expand Down
Loading

0 comments on commit 6bc31ee

Please sign in to comment.