1
0
forked from Yara724/api
Files
yara724-api/src/lookups/lookups.service.ts
2026-06-16 11:31:41 +03:30

86 lines
2.8 KiB
TypeScript

import { readFile } from "node:fs/promises";
import { Injectable, Logger, NotFoundException } from "@nestjs/common";
import { LookupDbService } from "./entities/db-service/lookup.db.service";
@Injectable()
export class LookupsService {
private readonly logger = new Logger(LookupsService.name);
constructor(private readonly lookupDbService: LookupDbService) {}
/**
* Returns stored `response` for a lookup document by `name` in the lookups collection.
*/
async getLookup(lookupName: string): Promise<any> {
const doc = await this.lookupDbService.findOne({ name: lookupName });
if (!doc) {
this.logger.warn(`Lookup not found in database: ${lookupName}`);
throw new NotFoundException(`Lookup "${lookupName}" not found`);
}
return doc.response;
}
async getAccidentCauses(): Promise<any> {
return await this.getLookup("accident-causes");
}
async getAccidentReportType(): Promise<any> {
return await this.getLookup("accident-report-type");
}
async getVehicleUseTypes(): Promise<any> {
return await this.getLookup("vehicle-use-types");
}
async getDmgPayMethod(): Promise<any> {
return await this.getLookup("dmg-pay-method");
}
async getDrivingLicenceTypes(): Promise<any> {
return await this.getLookup("driving-licence-types");
}
async getAccidentCulpritType(): Promise<any> {
return await this.getLookup("accident-culprit-type");
}
async getAccidentWay(): Promise<{ id: number; label: string }[]> {
const raw: { id: number; persianLabel: string }[] = JSON.parse(
await readFile("src/static/ACCIDENT_WAY.json", "utf-8"),
);
return raw.map((item) => ({ id: item.id, label: item.persianLabel }));
}
async getAccidentReason(): Promise<
{ id: number; label: string; fanavaran: number }[]
> {
const raw: { id: number; persianLabel: string; fanavaranID: number }[] =
JSON.parse(await readFile("src/static/ACCIDENT_REASON.json", "utf-8"));
return raw.map((item) => ({
id: item.id,
label: item.persianLabel,
fanavaran: item.fanavaranID,
}));
}
async getAccidentType(): Promise<{ id: number; label: string }[]> {
const raw: { id: number; persianLabel: string }[] = JSON.parse(
await readFile("src/static/ACCIDENT_TYPE.json", "utf-8"),
);
return raw.map((item) => ({ id: item.id, label: item.persianLabel }));
}
async getAccidentFields(): Promise<{
accidentWay: { id: number; label: string }[];
accidentReason: { id: number; label: string; fanavaran: number }[];
accidentType: { id: number; label: string }[];
}> {
const [accidentWay, accidentReason, accidentType] = await Promise.all([
this.getAccidentWay(),
this.getAccidentReason(),
this.getAccidentType(),
]);
return { accidentWay, accidentReason, accidentType };
}
}