forked from Yara724/api
feat(claims): enforce insurer working hours
This commit is contained in:
@@ -0,0 +1,263 @@
|
||||
import { ForbiddenException } from "@nestjs/common";
|
||||
import { Reflector } from "@nestjs/core";
|
||||
import { ExecutionContextHost } from "@nestjs/core/helpers/execution-context-host";
|
||||
import { lastValueFrom, defer, of } from "rxjs";
|
||||
import { RoleEnum } from "src/Types&Enums/role.enum";
|
||||
import {
|
||||
CaseWorkingHoursInterceptor,
|
||||
checkDiscoveredBlameInsurer,
|
||||
EnforceCaseWorkingHours,
|
||||
} from "./case-working-hours.interceptor";
|
||||
import {
|
||||
ClientWorkingHoursService,
|
||||
responsibleBlameInsurer,
|
||||
} from "./client-working-hours.service";
|
||||
import { getNextOpening, parseWorkingHours } from "./working-hours";
|
||||
|
||||
const open = { alwaysOpen: true, windows: [] };
|
||||
const saturday = { weekday: 6, opensAt: "08:00", closesAt: "16:00" };
|
||||
const schedule = { alwaysOpen: false, windows: [saturday] };
|
||||
const afterClosing = new Date("2026-11-21T12:30:00Z");
|
||||
|
||||
describe("Tehran working hours", () => {
|
||||
it("includes the opening minute, excludes closing, and reports the next open day", () => {
|
||||
expect(getNextOpening(open, afterClosing)).toBeNull();
|
||||
expect(getNextOpening(schedule, new Date("2026-11-21T04:29:59Z"))).toEqual({
|
||||
weekday: 6,
|
||||
time: "08:00",
|
||||
});
|
||||
expect(
|
||||
getNextOpening(schedule, new Date("2026-11-21T04:30:00Z")),
|
||||
).toBeNull();
|
||||
expect(
|
||||
getNextOpening(schedule, new Date("2026-11-21T12:29:59Z")),
|
||||
).toBeNull();
|
||||
expect(getNextOpening(schedule, afterClosing)).toEqual({
|
||||
weekday: 6,
|
||||
time: "08:00",
|
||||
});
|
||||
});
|
||||
|
||||
it("handles the overnight week boundary and a day ending at midnight", () => {
|
||||
const overnight = {
|
||||
alwaysOpen: false,
|
||||
windows: [{ weekday: 6, opensAt: "23:00", closesAt: "02:00" }],
|
||||
};
|
||||
expect(getNextOpening(overnight, new Date("2026-11-21T19:29:59Z"))).toEqual(
|
||||
{ weekday: 6, time: "23:00" },
|
||||
);
|
||||
expect(
|
||||
getNextOpening(overnight, new Date("2026-11-21T21:30:00Z")),
|
||||
).toBeNull();
|
||||
expect(getNextOpening(overnight, new Date("2026-11-21T22:30:00Z"))).toEqual(
|
||||
{ weekday: 6, time: "23:00" },
|
||||
);
|
||||
const allSaturday = parseWorkingHours({
|
||||
alwaysOpen: false,
|
||||
windows: [{ weekday: 6, opensAt: "00:00", closesAt: "24:00" }],
|
||||
});
|
||||
expect(
|
||||
getNextOpening(allSaturday, new Date("2026-11-21T20:29:59Z")),
|
||||
).toBeNull();
|
||||
expect(
|
||||
getNextOpening(allSaturday, new Date("2026-11-21T20:30:00Z")),
|
||||
).toEqual({ weekday: 6, time: "00:00" });
|
||||
});
|
||||
|
||||
it("rejects duplicate, invalid, and empty restricted schedules", () => {
|
||||
for (const value of [
|
||||
{ alwaysOpen: false, windows: [] },
|
||||
{ ...schedule, windows: [saturday, saturday] },
|
||||
{ ...schedule, windows: [{ ...saturday, weekday: 7 }] },
|
||||
{ ...schedule, windows: [{ ...saturday, opensAt: "24:00" }] },
|
||||
{ ...schedule, windows: [{ ...saturday, closesAt: "08:00" }] },
|
||||
{ ...schedule, windows: [{ ...saturday, closesAt: "25:00" }] },
|
||||
{ ...schedule, clientId: "another insurer" },
|
||||
]) {
|
||||
expect(() => parseWorkingHours(value)).toThrow();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@EnforceCaseWorkingHours()
|
||||
class CaseController {
|
||||
write() {}
|
||||
}
|
||||
|
||||
const first = "aaaaaaaaaaaaaaaaaaaaaaaa";
|
||||
const second = "bbbbbbbbbbbbbbbbbbbbbbbb";
|
||||
const caseId = "cccccccccccccccccccccccc";
|
||||
|
||||
function context(
|
||||
params: Record<string, string> = {},
|
||||
role = RoleEnum.USER,
|
||||
method = "POST",
|
||||
) {
|
||||
const req = { method, params, user: { sub: "user", role, clientKey: first } };
|
||||
return new ExecutionContextHost(
|
||||
[req],
|
||||
CaseController,
|
||||
CaseController.prototype.write,
|
||||
);
|
||||
}
|
||||
|
||||
describe("case working-hours enforcement", () => {
|
||||
const findWorkingHours = jest.fn();
|
||||
const findByIdAndUpdate = jest.fn();
|
||||
const hours = new ClientWorkingHoursService({
|
||||
findWorkingHours,
|
||||
findByIdAndUpdate,
|
||||
} as any);
|
||||
const assertOpen = jest.spyOn(hours, "assertOpen");
|
||||
const claims = { find: jest.fn() };
|
||||
const blames = { find: jest.fn() };
|
||||
const interceptor = new CaseWorkingHoursInterceptor(
|
||||
new Reflector(),
|
||||
hours,
|
||||
claims as any,
|
||||
blames as any,
|
||||
);
|
||||
let deployment: string | undefined;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
deployment = process.env.CLIENT_ID;
|
||||
jest.useFakeTimers({ now: afterClosing });
|
||||
findWorkingHours.mockImplementation(async (filter) =>
|
||||
filter._id === first
|
||||
? { settings: {} }
|
||||
: { settings: { workingHours: schedule } },
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (deployment === undefined) delete process.env.CLIENT_ID;
|
||||
else process.env.CLIENT_ID = deployment;
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it("uses the second party's insurer when the first party reports being injured", () => {
|
||||
const injured = {
|
||||
role: "FIRST",
|
||||
person: { clientId: first },
|
||||
statement: { claimsDamage: true },
|
||||
};
|
||||
const atFault = { role: "SECOND", person: { clientId: second } };
|
||||
expect(
|
||||
responsibleBlameInsurer({
|
||||
type: "THIRD_PARTY",
|
||||
blameStatus: "AGREED",
|
||||
parties: [injured],
|
||||
}),
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
responsibleBlameInsurer({
|
||||
type: "THIRD_PARTY",
|
||||
blameStatus: "AGREED",
|
||||
parties: [injured, atFault],
|
||||
}),
|
||||
).toBe(second);
|
||||
});
|
||||
|
||||
it("blocks public intake using the deployment insurer instead of the public JWT key", async () => {
|
||||
process.env.CLIENT_ID = "8";
|
||||
const handle = jest.fn(() => of(true));
|
||||
await expect(
|
||||
interceptor.intercept(context(), { handle }),
|
||||
).rejects.toMatchObject({
|
||||
status: 403,
|
||||
response: {
|
||||
code: "OUTSIDE_WORKING_HOURS",
|
||||
nextOpening: { weekday: 6, time: "08:00" },
|
||||
},
|
||||
});
|
||||
expect(findWorkingHours).toHaveBeenCalledWith({ clientCode: 8 });
|
||||
expect(handle).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("uses a case's payer over the actor's insurer, including Fanavaran writes", async () => {
|
||||
claims.find.mockResolvedValue([{ owner: { clientId: second } }]);
|
||||
const handle = jest.fn(() => of(true));
|
||||
await expect(
|
||||
interceptor.intercept(
|
||||
context({ claimCaseId: caseId }, RoleEnum.DAMAGE_EXPERT),
|
||||
{ handle },
|
||||
),
|
||||
).rejects.toBeInstanceOf(ForbiddenException);
|
||||
expect(findWorkingHours).toHaveBeenCalledWith({ _id: second });
|
||||
expect(handle).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("uses the initiating insurer for a call-center case before policy details are known", async () => {
|
||||
blames.find.mockResolvedValue([
|
||||
{ type: "CAR_BODY", workingHoursIntakeClientId: second },
|
||||
]);
|
||||
await expect(
|
||||
interceptor.intercept(context({ requestId: caseId }), {
|
||||
handle: () => of(true),
|
||||
}),
|
||||
).rejects.toBeInstanceOf(ForbiddenException);
|
||||
expect(findWorkingHours).toHaveBeenCalledWith({ _id: second });
|
||||
});
|
||||
|
||||
it("checks a newly discovered payer inside the handler before it persists a transition", async () => {
|
||||
const save = jest.fn();
|
||||
const result = await interceptor.intercept(
|
||||
context({}, RoleEnum.FIELD_EXPERT),
|
||||
{
|
||||
handle: () =>
|
||||
defer(async () => {
|
||||
await checkDiscoveredBlameInsurer({
|
||||
type: "THIRD_PARTY",
|
||||
parties: [
|
||||
{ role: "FIRST", person: { clientId: first } },
|
||||
{
|
||||
role: "SECOND",
|
||||
person: { clientId: second },
|
||||
statement: { admitsGuilt: true },
|
||||
},
|
||||
],
|
||||
});
|
||||
save();
|
||||
}),
|
||||
},
|
||||
);
|
||||
await expect(lastValueFrom(result)).rejects.toBeInstanceOf(
|
||||
ForbiddenException,
|
||||
);
|
||||
expect(assertOpen).toHaveBeenNthCalledWith(1, first, afterClosing);
|
||||
expect(assertOpen).toHaveBeenNthCalledWith(2, second, afterClosing);
|
||||
expect(save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("defaults old insurers to always open and updates only the schedule", async () => {
|
||||
expect(await hours.read(first)).toEqual(open);
|
||||
findByIdAndUpdate.mockResolvedValueOnce({
|
||||
settings: { workingHours: schedule },
|
||||
});
|
||||
expect(await hours.update(first, schedule)).toEqual(schedule);
|
||||
expect(findByIdAndUpdate).toHaveBeenCalledWith(first, {
|
||||
$set: { "settings.workingHours": schedule },
|
||||
});
|
||||
});
|
||||
|
||||
it("leaves reads and the existing missing-case error path alone", async () => {
|
||||
blames.find.mockResolvedValue([]);
|
||||
const read = await interceptor.intercept(
|
||||
context({ requestId: caseId }, RoleEnum.USER, "GET"),
|
||||
{
|
||||
handle: () => of("read"),
|
||||
},
|
||||
);
|
||||
expect(await lastValueFrom(read)).toBe("read");
|
||||
expect(blames.find).not.toHaveBeenCalled();
|
||||
const missing = await interceptor.intercept(
|
||||
context({ requestId: caseId }),
|
||||
{
|
||||
handle: () => of("existing not found handling"),
|
||||
},
|
||||
);
|
||||
expect(await lastValueFrom(missing)).toBe("existing not found handling");
|
||||
expect(assertOpen).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user