2026-07-17 12:01:56 -03:00
|
|
|
import type { Location } from "@/domain/locations/types/location";
|
|
|
|
|
import type { CompDocSiteInfo } from "@/domain/work-orders/types/completion-doc";
|
|
|
|
|
import type { WorkOrderTableRow } from "@/domain/work-orders/types/work-order-table-row";
|
2026-09-10 15:41:01 -03:00
|
|
|
import { effectiveSite } from "@/domain/work-orders/utils/effective-completion-snapshot";
|
2026-07-17 12:01:56 -03:00
|
|
|
|
|
|
|
|
function buildLabel(city?: string, state?: string): string {
|
|
|
|
|
return [city, state].filter(Boolean).join(", ");
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 18:15:02 -03:00
|
|
|
function toCompDocSite(street: string, city: string, state: string, zip: string): CompDocSiteInfo {
|
|
|
|
|
const label = buildLabel(city, state);
|
|
|
|
|
const fullAddress = [street, label, zip].filter(Boolean).join(", ") || "—";
|
|
|
|
|
return { street, city, state, zip, label, fullAddress };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resolveLiveLocationSite(location: Location): CompDocSiteInfo {
|
|
|
|
|
return toCompDocSite(
|
|
|
|
|
location.address ?? "",
|
|
|
|
|
location.city ?? "",
|
|
|
|
|
location.state ?? "",
|
|
|
|
|
location.zipCode ?? "",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resolveRowLocationSite(row: Pick<WorkOrderTableRow, "location">): CompDocSiteInfo {
|
|
|
|
|
const locationParts = row.location.split(",").map((part) => part.trim());
|
|
|
|
|
const city = locationParts[0] ?? "";
|
|
|
|
|
const state = locationParts[1] ?? "";
|
|
|
|
|
const label = buildLabel(city, state);
|
|
|
|
|
const street = row.location && !label ? row.location : "";
|
|
|
|
|
return toCompDocSite(street, city, state, "");
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-17 12:01:56 -03:00
|
|
|
export function resolveCompDocSite(
|
2026-09-10 15:41:01 -03:00
|
|
|
row: Pick<WorkOrderTableRow, "site" | "location" | "status" | "frozenSite">,
|
2026-07-17 12:01:56 -03:00
|
|
|
location?: Location | null,
|
|
|
|
|
): CompDocSiteInfo {
|
2026-09-10 15:41:01 -03:00
|
|
|
const frozen = effectiveSite(row);
|
|
|
|
|
if (frozen != null) {
|
2026-09-10 18:15:02 -03:00
|
|
|
return toCompDocSite(
|
|
|
|
|
frozen.address ?? "",
|
|
|
|
|
frozen.city ?? "",
|
|
|
|
|
frozen.state ?? "",
|
|
|
|
|
frozen.zip ?? "",
|
|
|
|
|
);
|
2026-09-10 15:41:01 -03:00
|
|
|
}
|
2026-07-17 12:01:56 -03:00
|
|
|
if (location?.address || location?.city) {
|
2026-09-10 18:15:02 -03:00
|
|
|
return resolveLiveLocationSite(location);
|
2026-07-17 12:01:56 -03:00
|
|
|
}
|
2026-09-10 18:15:02 -03:00
|
|
|
return resolveRowLocationSite(row);
|
2026-07-17 12:01:56 -03:00
|
|
|
}
|