forked from Yara724/api
Enforce claim beneficiary access and retire unused legacy APIs
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import { Injectable, Logger } from "@nestjs/common";
|
||||
import { InjectModel } from "@nestjs/mongoose";
|
||||
import { isAxiosError, type AxiosResponse } from "axios";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { Model, Types } from "mongoose";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import type { FanavaranAuditSession } from "./fanavaran-audit.types";
|
||||
import {
|
||||
FanavaranAuditLog,
|
||||
FanavaranAuditLogDocument,
|
||||
FanavaranAuditStatus,
|
||||
FanavaranAuditStep,
|
||||
} from "./schema/fanavaran-audit-log.schema";
|
||||
import type { FanavaranAuditSession } from "./fanavaran-audit.types";
|
||||
|
||||
export interface RecordFanavaranAuditStepInput {
|
||||
session: FanavaranAuditSession;
|
||||
@@ -104,9 +104,7 @@ export class FanavaranAuditService {
|
||||
const lower = key.toLowerCase();
|
||||
if (FanavaranAuditService.SENSITIVE_HEADER_KEYS.has(lower)) {
|
||||
out[key] =
|
||||
typeof rawValue === "string"
|
||||
? this.maskToken(rawValue)
|
||||
: "***";
|
||||
typeof rawValue === "string" ? this.maskToken(rawValue) : "***";
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
@@ -301,14 +299,6 @@ export class FanavaranAuditService {
|
||||
}
|
||||
}
|
||||
|
||||
async findByTrackingCode(trackingCode: string) {
|
||||
return this.auditModel
|
||||
.find({ trackingCode })
|
||||
.sort({ createdAt: 1 })
|
||||
.lean()
|
||||
.exec();
|
||||
}
|
||||
|
||||
private maskSensitiveDeep(value: unknown, depth = 0): unknown {
|
||||
if (depth > 8) {
|
||||
return "[max-depth]";
|
||||
|
||||
@@ -174,7 +174,6 @@ describe("FanavaranAuthService", () => {
|
||||
"parsian",
|
||||
"کد پیگیری خطا: 1\r\n.لطفا پس از چند لحظه مجدد تلاش فرمایید.",
|
||||
);
|
||||
expect(service.isInBackoff("parsian")).toBe(true);
|
||||
expect(() => service.assertNotInBackoff("parsian")).toThrow(/backoff/i);
|
||||
});
|
||||
|
||||
|
||||
@@ -58,7 +58,10 @@ export class FanavaranAuthService {
|
||||
*/
|
||||
static readonly TOKEN_TIME_ZONE = "Asia/Tehran";
|
||||
|
||||
private readonly tokenCache = new Map<FanavaranClientKey, CachedFanavaranAuth>();
|
||||
private readonly tokenCache = new Map<
|
||||
FanavaranClientKey,
|
||||
CachedFanavaranAuth
|
||||
>();
|
||||
private readonly inflightLogin = new Map<
|
||||
FanavaranClientKey,
|
||||
Promise<string>
|
||||
@@ -94,12 +97,7 @@ export class FanavaranAuthService {
|
||||
| string
|
||||
| undefined;
|
||||
if (typeof data === "string") return data;
|
||||
return (
|
||||
data?.Message ||
|
||||
data?.message ||
|
||||
errorOrMessage.message ||
|
||||
""
|
||||
);
|
||||
return data?.Message || data?.message || errorOrMessage.message || "";
|
||||
}
|
||||
if (errorOrMessage instanceof Error) return errorOrMessage.message;
|
||||
return errorOrMessage == null ? "" : String(errorOrMessage);
|
||||
@@ -145,10 +143,6 @@ export class FanavaranAuthService {
|
||||
return Math.max(0, state.until - Date.now());
|
||||
}
|
||||
|
||||
isInBackoff(clientKey: FanavaranClientKey): boolean {
|
||||
return this.getBackoffRemainingMs(clientKey) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call after any Fanavaran HTTP failure. Sets a tenant-wide pause when the
|
||||
* error is the familiar “try again later” overload response.
|
||||
@@ -172,12 +166,15 @@ export class FanavaranAuthService {
|
||||
|
||||
invalidateToken(clientKey: FanavaranClientKey): void {
|
||||
this.tokenCache.delete(clientKey);
|
||||
void this.authTokenModel.deleteOne({ clientKey }).exec().catch((error) => {
|
||||
this.logger.warn(
|
||||
`[${clientKey}] Failed to clear persisted Fanavaran auth token`,
|
||||
error,
|
||||
);
|
||||
});
|
||||
void this.authTokenModel
|
||||
.deleteOne({ clientKey })
|
||||
.exec()
|
||||
.catch((error) => {
|
||||
this.logger.warn(
|
||||
`[${clientKey}] Failed to clear persisted Fanavaran auth token`,
|
||||
error,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -331,7 +328,10 @@ export class FanavaranAuthService {
|
||||
fingerprint: string,
|
||||
): Promise<string | null> {
|
||||
try {
|
||||
const doc = await this.authTokenModel.findOne({ clientKey }).lean().exec();
|
||||
const doc = await this.authTokenModel
|
||||
.findOne({ clientKey })
|
||||
.lean()
|
||||
.exec();
|
||||
if (!doc?.authenticationToken || !doc.expiresAt) {
|
||||
return null;
|
||||
}
|
||||
@@ -454,11 +454,10 @@ export class FanavaranAuthService {
|
||||
}
|
||||
|
||||
if (auditSession) {
|
||||
const exchange =
|
||||
this.fanavaranAuditService.captureAxiosExchange(
|
||||
response,
|
||||
requestHeaders,
|
||||
);
|
||||
const exchange = this.fanavaranAuditService.captureAxiosExchange(
|
||||
response,
|
||||
requestHeaders,
|
||||
);
|
||||
await this.fanavaranAuditService.recordStep({
|
||||
session: auditSession,
|
||||
step: FanavaranAuditStep.GET_APP_TOKEN,
|
||||
@@ -498,7 +497,11 @@ export class FanavaranAuthService {
|
||||
durationMs: Date.now() - startedAt,
|
||||
});
|
||||
}
|
||||
throw this.toGatewayError(error, "Failed to get appToken from external API", auditSession);
|
||||
throw this.toGatewayError(
|
||||
error,
|
||||
"Failed to get appToken from external API",
|
||||
auditSession,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -551,11 +554,10 @@ export class FanavaranAuthService {
|
||||
}
|
||||
|
||||
if (auditSession) {
|
||||
const exchange =
|
||||
this.fanavaranAuditService.captureAxiosExchange(
|
||||
response,
|
||||
requestHeaders,
|
||||
);
|
||||
const exchange = this.fanavaranAuditService.captureAxiosExchange(
|
||||
response,
|
||||
requestHeaders,
|
||||
);
|
||||
await this.fanavaranAuditService.recordStep({
|
||||
session: auditSession,
|
||||
step: FanavaranAuditStep.LOGIN,
|
||||
@@ -598,7 +600,11 @@ export class FanavaranAuthService {
|
||||
durationMs: Date.now() - startedAt,
|
||||
});
|
||||
}
|
||||
throw this.toGatewayError(error, "Failed to login to external API", auditSession);
|
||||
throw this.toGatewayError(
|
||||
error,
|
||||
"Failed to login to external API",
|
||||
auditSession,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,10 +36,6 @@ export class FanavaranLocationService {
|
||||
return ids;
|
||||
}
|
||||
|
||||
primaryLocationId(locations?: LocationEntry[] | null): string | undefined {
|
||||
return this.extractLocationIds(locations)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Maker primary location → reviewer primary → tenant auth.location.
|
||||
* Non-parsian or non-FileMaker flows always use the tenant default.
|
||||
@@ -57,7 +53,10 @@ export class FanavaranLocationService {
|
||||
return defaultLocation;
|
||||
}
|
||||
|
||||
const makerLocation = await this.loadPrimaryLocationId(input.fileMakerId, "maker");
|
||||
const makerLocation = await this.loadPrimaryLocationId(
|
||||
input.fileMakerId,
|
||||
"maker",
|
||||
);
|
||||
if (makerLocation) {
|
||||
this.logger.debug(
|
||||
`Fanavaran Location from file-maker id=${input.fileMakerId} → ${makerLocation}`,
|
||||
@@ -155,9 +154,7 @@ export class FanavaranLocationService {
|
||||
);
|
||||
}
|
||||
if (!caseBranchId || caseBranchId !== reviewerBranchId) {
|
||||
throw new ForbiddenException(
|
||||
"This file belongs to another branch.",
|
||||
);
|
||||
throw new ForbiddenException("This file belongs to another branch.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user