setup
This commit is contained in:
155
src/lib/helpers/location-enrichment.ts
Normal file
155
src/lib/helpers/location-enrichment.ts
Normal file
@@ -0,0 +1,155 @@
|
||||
import { db } from "@/database/db";
|
||||
import { locations, type Location } from "@/database/schema/location";
|
||||
import {
|
||||
industrialEstates,
|
||||
type IndustrialEstate,
|
||||
} from "@/database/schema/industrialEstate";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
/**
|
||||
* Load location data by ID
|
||||
* @param locationId - Location ID
|
||||
* @returns Location data or null
|
||||
*/
|
||||
export async function loadLocation(
|
||||
locationId: string,
|
||||
): Promise<Location | null> {
|
||||
const [location] = await db
|
||||
.select()
|
||||
.from(locations)
|
||||
.where(eq(locations.id, locationId))
|
||||
.limit(1);
|
||||
|
||||
return location || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load location data by type and code
|
||||
* @param code - Location code
|
||||
* @param type - Location type (country, province, district, subdistrict)
|
||||
* @returns Location data or null
|
||||
*/
|
||||
export async function loadLocationByCode(
|
||||
code: string,
|
||||
type: string,
|
||||
): Promise<Location | null> {
|
||||
const [location] = await db
|
||||
.select()
|
||||
.from(locations)
|
||||
.where(eq(locations.code, code))
|
||||
.limit(1);
|
||||
|
||||
if (!location || location.type !== type) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load industrial estate by ID
|
||||
* @param industrialEstateId - Industrial estate ID
|
||||
* @returns Industrial estate with location data or null
|
||||
*/
|
||||
export async function loadIndustrialEstate(
|
||||
industrialEstateId: string,
|
||||
): Promise<(IndustrialEstate & { location?: Location | null }) | null> {
|
||||
const [industrialEstate] = await db
|
||||
.select()
|
||||
.from(industrialEstates)
|
||||
.where(eq(industrialEstates.id, industrialEstateId))
|
||||
.limit(1);
|
||||
|
||||
if (!industrialEstate) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Load location data
|
||||
const location = await loadLocation(industrialEstate.locationId);
|
||||
|
||||
return {
|
||||
...industrialEstate,
|
||||
location: location || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Load industrial estate by code
|
||||
* @param code - Industrial estate code
|
||||
* @returns Industrial estate with location data or null
|
||||
*/
|
||||
export async function loadIndustrialEstateByCode(
|
||||
code: string,
|
||||
): Promise<(IndustrialEstate & { location?: Location }) | null> {
|
||||
const [industrialEstate] = await db
|
||||
.select()
|
||||
.from(industrialEstates)
|
||||
.where(eq(industrialEstates.code, code))
|
||||
.limit(1);
|
||||
|
||||
if (!industrialEstate) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Load location data
|
||||
const location = await loadLocation(industrialEstate.locationId);
|
||||
|
||||
return {
|
||||
...industrialEstate,
|
||||
location,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Load location hierarchy data
|
||||
* Returns full path from country to subdistrict
|
||||
* @param locationId - Location ID
|
||||
* @returns Array of locations in hierarchy order (country -> province -> district -> subdistrict)
|
||||
*/
|
||||
export async function loadLocationHierarchy(
|
||||
locationId: string,
|
||||
): Promise<Location[]> {
|
||||
const hierarchy: Location[] = [];
|
||||
let currentLocation = await loadLocation(locationId);
|
||||
|
||||
while (currentLocation) {
|
||||
hierarchy.unshift(currentLocation); // Add to beginning
|
||||
|
||||
if (currentLocation.parentId) {
|
||||
currentLocation = await loadLocation(currentLocation.parentId);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return hierarchy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enrich quotation with location data
|
||||
* @param quotation - Quotation object
|
||||
* @param locationId - Location ID (optional)
|
||||
* @param industrialEstateId - Industrial estate ID (optional)
|
||||
* @returns Enriched quotation with location data
|
||||
*/
|
||||
export async function enrichQuotationWithLocation(
|
||||
quotation: any,
|
||||
locationId?: string,
|
||||
industrialEstateId?: string,
|
||||
): Promise<any> {
|
||||
const enriched = { ...quotation };
|
||||
|
||||
// Load industrial estate data
|
||||
if (industrialEstateId) {
|
||||
const industrialEstate = await loadIndustrialEstate(industrialEstateId);
|
||||
enriched.locationIndustrialData = industrialEstate;
|
||||
}
|
||||
|
||||
// Load location data (province)
|
||||
if (locationId) {
|
||||
const location = await loadLocation(locationId);
|
||||
enriched.locationProvinceData = location;
|
||||
}
|
||||
|
||||
return enriched;
|
||||
}
|
||||
Reference in New Issue
Block a user