fanavaran locations getting from branches

This commit is contained in:
2026-11-11 15:29:37 +03:30
parent d469cce463
commit 54459877a2
7 changed files with 163 additions and 22 deletions
@@ -14,6 +14,7 @@ describe("FanavaranLocationService", () => {
expert?: Record<string, unknown> | null;
damage?: Record<string, unknown> | null;
field?: Record<string, unknown> | null;
branch?: Record<string, unknown> | null;
}) =>
new FanavaranLocationService(
{
@@ -44,6 +45,7 @@ describe("FanavaranLocationService", () => {
return null;
}),
} as any,
{ findById: jest.fn().mockResolvedValue(opts?.branch ?? null) } as any,
);
describe("branch scope", () => {
@@ -92,6 +94,66 @@ describe("FanavaranLocationService", () => {
});
describe("resolveBusinessLocation", () => {
it.each(["maker", "reviewer", "expert", "damage", "field"])(
"uses the branch code for %s staff without their own locations",
async (role) => {
const service = createService({
[role]: { branchId: "branch-1", locations: null },
branch: { code: " 110011 " },
});
const staffId =
role === "maker"
? makerId
: role === "reviewer"
? reviewerId
: role === "field"
? fieldExpertId
: "expert-1";
await expect(
service.resolveBusinessLocation({
clientKey: "parsian",
staffCandidateIds: [staffId],
requireStaffLocation: true,
}),
).resolves.toBe("110011");
},
);
it("prefers staff locations over the branch code", async () => {
const service = createService({
field: { branchId: "branch-1", locations: [{ id: "110011" }] },
branch: { code: "210050" },
});
await expect(
service.resolveBusinessLocation({
clientKey: "parsian",
staffCandidateIds: [fieldExpertId],
requireStaffLocation: true,
}),
).resolves.toBe("110011");
});
it.each([null, { code: "" }, { code: " " }])(
"reports missing configuration when neither staff nor branch supplies a location: %j",
async (branch) => {
const service = createService({
field: { branchId: "branch-1", locations: null },
branch,
});
await expect(
service.resolveBusinessLocation({
clientKey: "parsian",
staffCandidateIds: [fieldExpertId],
requireStaffLocation: true,
}),
).rejects.toMatchObject({
response: expect.objectContaining({
code: "FANAVARAN_STAFF_LOCATION_MISSING",
}),
});
},
);
it("uses the first staff candidate location for any tenant", async () => {
const service = createService({
field: { locations: [{ id: "210050", name: "غرب" }] },
+16 -5
View File
@@ -4,6 +4,7 @@ import {
Injectable,
Logger,
} from "@nestjs/common";
import { BranchDbService } from "src/client/entities/db-service/branch.db.service";
import {
FanavaranClientKey,
getFanavaranClientProfile,
@@ -19,7 +20,7 @@ type LocationEntry = { id?: string; name?: string };
/**
* Resolve Fanavaran HTTP `Location` (OpBUId) from assigned staff
* `locations[].id` for every tenant, and enforce maker/reviewer match on
* `locations[].id` or their branch code for every tenant, and enforce maker/reviewer match on
* Parsian FileMaker flows.
*/
@Injectable()
@@ -32,6 +33,7 @@ export class FanavaranLocationService {
private readonly expertDbService: ExpertDbService,
private readonly damageExpertDbService: DamageExpertDbService,
private readonly fieldExpertDbService: FieldExpertDbService,
private readonly branchDbService: BranchDbService,
) {}
isParsianClient(clientKey?: FanavaranClientKey): boolean {
@@ -49,7 +51,8 @@ export class FanavaranLocationService {
}
/**
* First staff candidate with a stored `locations[].id` wins.
* First staff candidate with a stored `locations[].id` or branch code wins.
* A staff member's own locations take priority over their branch's code.
* Tenant auth.location is allowed only when requireStaffLocation is false.
*/
async resolveBusinessLocation(input: {
@@ -90,13 +93,13 @@ export class FanavaranLocationService {
throw new BadRequestException({
code: "FANAVARAN_STAFF_LOCATION_MISSING",
message:
"Cannot submit this claim to Fanavaran: no attached expert has a configured locations[].id (OpBUId). Configure the responsible staff member's location and retry.",
"Cannot submit this claim to Fanavaran: no attached expert has a configured locations[].id or branch code (OpBUId). Configure the responsible staff member's location or branch and retry.",
staffCandidateIds: [...seen],
});
}
this.logger.warn(
`[${input.clientKey}] Fanavaran Location fallback to tenant default=${defaultLocation}; no stored locations[].id found for staff candidates=${[...seen].join(",") || "none"}`,
`[${input.clientKey}] Fanavaran Location fallback to tenant default=${defaultLocation}; no staff location or branch code found for staff candidates=${[...seen].join(",") || "none"}`,
);
return defaultLocation;
}
@@ -197,7 +200,15 @@ export class FanavaranLocationService {
try {
const doc = await load();
if (doc) {
return this.extractLocationIds((doc as any)?.locations);
const locationIds = this.extractLocationIds(doc.locations);
if (locationIds.length > 0) return locationIds;
if (!doc.branchId) return [];
const branch = await this.branchDbService.findById(
String(doc.branchId),
);
const branchCode = String(branch?.code ?? "").trim();
return branchCode ? [branchCode] : [];
}
} catch {
// invalid ObjectId / cast errors — try next collection
+7
View File
@@ -2,6 +2,11 @@ import { Module } from "@nestjs/common";
import { HttpModule } from "@nestjs/axios";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { MongooseModule } from "@nestjs/mongoose";
import { BranchDbService } from "src/client/entities/db-service/branch.db.service";
import {
BranchModel,
BranchSchema,
} from "src/client/entities/schema/branch.schema";
import { createHttpModuleOptions } from "src/core/config/http-proxy.factory";
import {
LookupModel,
@@ -36,11 +41,13 @@ import {
schema: FanavaranClientConfigSchema,
},
{ name: LookupModel.name, schema: LookupSchema },
{ name: BranchModel.name, schema: BranchSchema },
]),
FanavaranAuditModule,
UsersModule,
],
providers: [
BranchDbService,
FanavaranClientConfigService,
FanavaranAuthService,
FanavaranLookupService,