Toggle External API

This commit is contained in:
SepehrYahyaee
2026-05-16 16:23:01 +03:30
parent f2848a6179
commit 094816ce8f
11 changed files with 358 additions and 14 deletions

View File

@@ -0,0 +1,35 @@
import { Injectable } from "@nestjs/common";
import { InjectModel } from "@nestjs/mongoose";
import { Model, UpdateQuery } from "mongoose";
import {
SYSTEM_SETTINGS_GLOBAL_KEY,
SystemSettingsModel,
} from "../schema/system-settings.schema";
@Injectable()
export class SystemSettingsDbService {
constructor(
@InjectModel(SystemSettingsModel.name)
private readonly model: Model<SystemSettingsModel>,
) {}
findGlobal() {
return this.model
.findOne({ key: SYSTEM_SETTINGS_GLOBAL_KEY })
.lean()
.exec();
}
async upsertGlobal(update: UpdateQuery<SystemSettingsModel>) {
return this.model
.findOneAndUpdate(
{ key: SYSTEM_SETTINGS_GLOBAL_KEY },
{
$setOnInsert: { key: SYSTEM_SETTINGS_GLOBAL_KEY },
...update,
},
{ new: true, upsert: true, lean: true },
)
.exec();
}
}

View File

@@ -0,0 +1,30 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
/** Singleton document key — only `global` is used today. */
export const SYSTEM_SETTINGS_GLOBAL_KEY = "global";
/**
* Toggle outbound integrations (SandHub / Tejarat inquiry gateways, etc.).
* When `sandHubUseLiveApi` is false, {@link SandHubService} skips HTTP and
* returns permissive mocks (including default plate/insurance inquiry data).
*/
export class ExternalApisSettings {
/**
* `true` → live SandHub/Tejarat HTTP calls.
* `false` → no external requests; mocks pass validation checks.
*/
@Prop({ type: Boolean, required: false, default: false })
sandHubUseLiveApi?: boolean;
}
@Schema({ collection: "system_settings", versionKey: false })
export class SystemSettingsModel {
@Prop({ required: true, unique: true, default: SYSTEM_SETTINGS_GLOBAL_KEY })
key: string;
@Prop({ type: ExternalApisSettings, required: false, default: {} })
externalApis?: ExternalApisSettings;
}
export const SystemSettingsSchema =
SchemaFactory.createForClass(SystemSettingsModel);