fanavaran configs seeding

This commit is contained in:
2026-08-03 09:55:30 +03:30
parent ca5c1900ff
commit f7b7cd13e8
5 changed files with 285 additions and 18 deletions

View File

@@ -0,0 +1,98 @@
import { Injectable, Logger, OnModuleInit } from "@nestjs/common";
import { InjectModel } from "@nestjs/mongoose";
import { Model } from "mongoose";
import {
FANAVARAN_CLIENT_KEYS,
SEED_FANAVARAN_CLIENT_PROFILES,
setFanavaranClientProfilesCache,
type FanavaranClientKey,
type FanavaranClientProfile,
} from "src/core/config/fanavaran-client.config";
import {
FanavaranClientConfig,
FanavaranClientConfigDocument,
} from "./schema/fanavaran-client-config.schema";
/**
* Loads / seeds per-tenant Fanavaran auth+defaults from Mongo and exposes them
* through the sync helpers in fanavaran-client.config.ts.
*
* Boot behaviour: for each known client key, if no document exists, insert the
* seed profile. Existing documents are never overwritten (ops can edit Mongo).
*/
@Injectable()
export class FanavaranClientConfigService implements OnModuleInit {
private readonly logger = new Logger(FanavaranClientConfigService.name);
constructor(
@InjectModel(FanavaranClientConfig.name)
private readonly configModel: Model<FanavaranClientConfigDocument>,
) {}
async onModuleInit(): Promise<void> {
await this.seedMissingProfiles();
await this.reloadCache();
}
async seedMissingProfiles(): Promise<void> {
for (const key of FANAVARAN_CLIENT_KEYS) {
const seed = SEED_FANAVARAN_CLIENT_PROFILES[key];
const existing = await this.configModel
.findOne({ key })
.select({ _id: 1 })
.lean()
.exec();
if (existing) {
continue;
}
await this.configModel.create({
key: seed.key,
auth: seed.auth,
defaults: seed.defaults,
});
this.logger.log(
`Seeded fanavaranClientConfigs for client "${key}" (ClaimExpertId=${seed.defaults.ClaimExpertId}, ExpertiseClaimExpertId=${seed.defaults.ExpertiseClaimExpertId})`,
);
}
}
async reloadCache(): Promise<void> {
const docs = await this.configModel.find().lean().exec();
const cache: Partial<Record<FanavaranClientKey, FanavaranClientProfile>> =
{};
for (const key of FANAVARAN_CLIENT_KEYS) {
const doc = docs.find((row) => row.key === key);
if (doc?.auth && doc?.defaults) {
cache[key] = {
key,
auth: {
appName: doc.auth.appName,
secret: doc.auth.secret,
username: doc.auth.username,
password: doc.auth.password,
corpId: doc.auth.corpId,
contractId: doc.auth.contractId,
location: doc.auth.location,
},
defaults: { ...doc.defaults },
};
} else {
cache[key] = SEED_FANAVARAN_CLIENT_PROFILES[key];
}
}
setFanavaranClientProfilesCache(cache);
this.logger.log(
`Loaded ${docs.length} Fanavaran client config(s) from Mongo into runtime cache`,
);
}
async findByKey(key: FanavaranClientKey) {
return this.configModel.findOne({ key }).lean().exec();
}
async list() {
return this.configModel.find().lean().exec();
}
}

View File

@@ -5,11 +5,16 @@ import { MongooseModule } from "@nestjs/mongoose";
import { createHttpModuleOptions } from "src/core/config/http-proxy.factory";
import { FanavaranAuditModule } from "./fanavaran-audit.module";
import { FanavaranAuthService } from "./fanavaran-auth.service";
import { FanavaranClientConfigService } from "./fanavaran-client-config.service";
import { FanavaranLookupService } from "./fanavaran-lookup.service";
import {
FanavaranAuthToken,
FanavaranAuthTokenSchema,
} from "./schema/fanavaran-auth-token.schema";
import {
FanavaranClientConfig,
FanavaranClientConfigSchema,
} from "./schema/fanavaran-client-config.schema";
@Module({
imports: [
@@ -20,10 +25,22 @@ import {
}),
MongooseModule.forFeature([
{ name: FanavaranAuthToken.name, schema: FanavaranAuthTokenSchema },
{
name: FanavaranClientConfig.name,
schema: FanavaranClientConfigSchema,
},
]),
FanavaranAuditModule,
],
providers: [FanavaranAuthService, FanavaranLookupService],
exports: [FanavaranAuthService, FanavaranLookupService],
providers: [
FanavaranClientConfigService,
FanavaranAuthService,
FanavaranLookupService,
],
exports: [
FanavaranClientConfigService,
FanavaranAuthService,
FanavaranLookupService,
],
})
export class FanavaranLookupModule {}

View File

@@ -0,0 +1,111 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { HydratedDocument } from "mongoose";
import type { FanavaranClientKey } from "src/core/config/fanavaran-client.config";
@Schema({ _id: false })
export class FanavaranAuthConfigEmbed {
@Prop({ type: String, required: true })
appName: string;
@Prop({ type: String, required: true })
secret: string;
@Prop({ type: String, required: true })
username: string;
@Prop({ type: String, required: true })
password: string;
@Prop({ type: String, required: true })
corpId: string;
@Prop({ type: String, required: true })
contractId: string;
@Prop({ type: String, required: true })
location: string;
}
export const FanavaranAuthConfigEmbedSchema = SchemaFactory.createForClass(
FanavaranAuthConfigEmbed,
);
@Schema({ _id: false })
export class FanavaranPayloadDefaultsEmbed {
@Prop({ type: Number, required: true })
AccidentCityId: number;
@Prop({ type: Number, required: true })
AccidentReportTypeId: number;
@Prop({ type: Number, required: true })
AccidentVehicleUsedId: number;
/**
* GEN.03 base claim — کارشناس مسئول پرونده مالی
* (Parsian example: 154 / 4662 — NOT the assessor role).
*/
@Prop({ type: Number, required: true })
ClaimExpertId: number;
/**
* GEN.08 expertise — کارشناس ارزیاب خسارت ثالث مالی (or وکیل/تحقیق).
* (Parsian example: 29; Tejaratno proven send: 2709).
*/
@Prop({ type: Number, required: true })
ExpertiseClaimExpertId: number;
@Prop({ type: Number, required: true })
CompensationReferenceId: number;
@Prop({ type: Number, required: true })
CulpritLicenceTypeId: number;
@Prop({ type: Number, required: true })
CulpritTypeId: number;
@Prop({ type: Number, required: true })
DmgCaseTypeId: number;
@Prop({ type: Number, required: true })
DmgHistoryStatus: number;
@Prop({ type: Number, required: true })
PlaqueKindId: number;
@Prop({ type: Number, required: true })
PlaqueSampleId: number;
@Prop({ type: Number, required: true })
DriverIsOwner: number;
@Prop({ type: Number, required: true })
FaultPercent: number;
@Prop({ type: Number, required: true })
ClaimFileTypeId: number;
}
export const FanavaranPayloadDefaultsEmbedSchema = SchemaFactory.createForClass(
FanavaranPayloadDefaultsEmbed,
);
/**
* Per-tenant Fanavaran auth + payload defaults.
* Seeded on boot when missing; edit in Mongo without redeploying code.
*/
@Schema({ collection: "fanavaranClientConfigs", timestamps: true })
export class FanavaranClientConfig {
@Prop({ type: String, required: true, unique: true, index: true })
key: FanavaranClientKey;
@Prop({ type: FanavaranAuthConfigEmbedSchema, required: true })
auth: FanavaranAuthConfigEmbed;
@Prop({ type: FanavaranPayloadDefaultsEmbedSchema, required: true })
defaults: FanavaranPayloadDefaultsEmbed;
}
export type FanavaranClientConfigDocument =
HydratedDocument<FanavaranClientConfig>;
export const FanavaranClientConfigSchema = SchemaFactory.createForClass(
FanavaranClientConfig,
);