forked from Yara724/api
dynamic locations
This commit is contained in:
@@ -4,14 +4,18 @@ import {
|
||||
getFanavaranClientProfile,
|
||||
resolveFanavaranClientKey,
|
||||
} from "src/core/config/fanavaran-client.config";
|
||||
import { DamageExpertDbService } from "src/users/entities/db-service/damage-expert.db.service";
|
||||
import { ExpertDbService } from "src/users/entities/db-service/expert.db.service";
|
||||
import { FieldExpertDbService } from "src/users/entities/db-service/field-expert.db.service";
|
||||
import { FileMakerDbService } from "src/users/entities/db-service/file-maker.db.service";
|
||||
import { FileReviewerDbService } from "src/users/entities/db-service/file-reviewer.db.service";
|
||||
|
||||
type LocationEntry = { id?: string; name?: string };
|
||||
|
||||
/**
|
||||
* Parsian V4/V5 FileMaker flows: resolve Fanavaran HTTP `Location` from
|
||||
* file-maker / file-reviewer `locations[].id`, and enforce maker/reviewer match.
|
||||
* Resolve Fanavaran HTTP `Location` (OpBUId) from assigned staff
|
||||
* `locations[].id` for every tenant, and enforce maker/reviewer match on
|
||||
* Parsian FileMaker flows.
|
||||
*/
|
||||
@Injectable()
|
||||
export class FanavaranLocationService {
|
||||
@@ -20,6 +24,9 @@ export class FanavaranLocationService {
|
||||
constructor(
|
||||
private readonly fileMakerDbService: FileMakerDbService,
|
||||
private readonly fileReviewerDbService: FileReviewerDbService,
|
||||
private readonly expertDbService: ExpertDbService,
|
||||
private readonly damageExpertDbService: DamageExpertDbService,
|
||||
private readonly fieldExpertDbService: FieldExpertDbService,
|
||||
) {}
|
||||
|
||||
isParsianClient(clientKey?: FanavaranClientKey): boolean {
|
||||
@@ -37,11 +44,13 @@ export class FanavaranLocationService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Maker primary location → reviewer primary → tenant auth.location.
|
||||
* Non-parsian or non-FileMaker flows always use the tenant default.
|
||||
* First staff candidate with a stored `locations[].id` wins.
|
||||
* Fallback: tenant auth.location (lookups / cases with no staff locations).
|
||||
*/
|
||||
async resolveBusinessLocation(input: {
|
||||
clientKey: FanavaranClientKey;
|
||||
staffCandidateIds?: Array<string | null | undefined>;
|
||||
/** @deprecated Prefer staffCandidateIds — kept for older call sites. */
|
||||
isMadeByFileMaker?: boolean;
|
||||
fileMakerId?: string | null;
|
||||
fileReviewerId?: string | null;
|
||||
@@ -49,30 +58,25 @@ export class FanavaranLocationService {
|
||||
const defaultLocation = getFanavaranClientProfile(input.clientKey).auth
|
||||
.location;
|
||||
|
||||
if (input.clientKey !== "parsian" || input.isMadeByFileMaker !== true) {
|
||||
return defaultLocation;
|
||||
}
|
||||
|
||||
const makerLocation = await this.loadPrimaryLocationId(
|
||||
const candidateIds = [
|
||||
...(input.staffCandidateIds ?? []),
|
||||
input.fileMakerId,
|
||||
"maker",
|
||||
);
|
||||
if (makerLocation) {
|
||||
this.logger.debug(
|
||||
`Fanavaran Location from file-maker id=${input.fileMakerId} → ${makerLocation}`,
|
||||
);
|
||||
return makerLocation;
|
||||
}
|
||||
|
||||
const reviewerLocation = await this.loadPrimaryLocationId(
|
||||
input.fileReviewerId,
|
||||
"reviewer",
|
||||
);
|
||||
if (reviewerLocation) {
|
||||
this.logger.debug(
|
||||
`Fanavaran Location from file-reviewer id=${input.fileReviewerId} → ${reviewerLocation}`,
|
||||
);
|
||||
return reviewerLocation;
|
||||
];
|
||||
|
||||
const seen = new Set<string>();
|
||||
for (const rawId of candidateIds) {
|
||||
const staffId = rawId != null ? String(rawId).trim() : "";
|
||||
if (!staffId || seen.has(staffId)) continue;
|
||||
seen.add(staffId);
|
||||
|
||||
const location = await this.loadPrimaryLocationIdFromAnyStaff(staffId);
|
||||
if (location) {
|
||||
this.logger.debug(
|
||||
`Fanavaran Location from staff id=${staffId} → ${location}`,
|
||||
);
|
||||
return location;
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.debug(
|
||||
@@ -158,14 +162,34 @@ export class FanavaranLocationService {
|
||||
}
|
||||
}
|
||||
|
||||
private async loadPrimaryLocationId(
|
||||
userId: string | null | undefined,
|
||||
kind: "maker" | "reviewer",
|
||||
private async loadPrimaryLocationIdFromAnyStaff(
|
||||
userId: string,
|
||||
): Promise<string | undefined> {
|
||||
const ids = await this.loadLocationIds(userId, kind);
|
||||
const ids = await this.loadLocationIdsFromAnyStaff(userId);
|
||||
return ids[0];
|
||||
}
|
||||
|
||||
private async loadLocationIdsFromAnyStaff(userId: string): Promise<string[]> {
|
||||
const loaders = [
|
||||
() => this.fileMakerDbService.findById(userId),
|
||||
() => this.fileReviewerDbService.findById(userId),
|
||||
() => this.expertDbService.findById(userId),
|
||||
() => this.damageExpertDbService.findById(userId),
|
||||
() => this.fieldExpertDbService.findById(userId),
|
||||
];
|
||||
for (const load of loaders) {
|
||||
try {
|
||||
const doc = await load();
|
||||
if (doc) {
|
||||
return this.extractLocationIds((doc as any)?.locations);
|
||||
}
|
||||
} catch {
|
||||
// invalid ObjectId / cast errors — try next collection
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
private async loadLocationIds(
|
||||
userId: string | null | undefined,
|
||||
kind: "maker" | "reviewer",
|
||||
|
||||
Reference in New Issue
Block a user