import { Body, Controller, Get, Patch, Post, UseGuards } from "@nestjs/common"; import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags, } from "@nestjs/swagger"; import { SettingsJwtGuard } from "src/auth/guards/settings-jwt.guard"; import { RolesGuard } from "src/auth/guards/role.guard"; import { Roles } from "src/decorators/roles.decorator"; import { RoleEnum } from "src/Types&Enums/role.enum"; import { OfflineInquirySettingsViewDto, SystemSettingsResponseDto, UpdateOfflineInquirySettingsDto, UpdateSystemSettingsDto, } from "./dto/system-settings.dto"; import { SystemSettingsService } from "./system-settings.service"; const EXPERT_AND_ADMIN_ROLES = [ RoleEnum.ADMIN, RoleEnum.SUPER_ADMIN, RoleEnum.EXPERT, RoleEnum.DAMAGE_EXPERT, RoleEnum.FIELD_EXPERT, RoleEnum.FILE_MAKER, RoleEnum.FILE_REVIEWER, ] as const; @ApiTags("system-settings") @ApiBearerAuth() @Controller("system-settings") export class SystemSettingsController { constructor(private readonly systemSettingsService: SystemSettingsService) {} @Get() @UseGuards(SettingsJwtGuard, RolesGuard) @Roles(RoleEnum.ADMIN, RoleEnum.COMPANY, RoleEnum.SUPER_ADMIN) @ApiOperation({ summary: "Get global system settings (external API toggles)", description: "Shows whether SandHub/Tejarat live HTTP is enabled, and whether offline seeded inquiries are enabled.", }) @ApiResponse({ status: 200, type: SystemSettingsResponseDto }) getSettings(): Promise { return this.systemSettingsService.getSettingsView(); } @Patch() @UseGuards(SettingsJwtGuard, RolesGuard) @Roles(RoleEnum.ADMIN, RoleEnum.SUPER_ADMIN) @ApiOperation({ summary: "Update global system settings", description: "Set `externalApis.sandHubUseLiveApi` and/or `offlineInquiry.enabled`.", }) @ApiResponse({ status: 200, type: SystemSettingsResponseDto }) updateSettings( @Body() body: UpdateSystemSettingsDto, ): Promise { return this.systemSettingsService.updateSettings(body); } @Post("fanavaran-config/reload") @UseGuards(SettingsJwtGuard, RolesGuard) @Roles(...EXPERT_AND_ADMIN_ROLES) @ApiOperation({ summary: "Reload Fanavaran client config cache from Mongo", description: "Re-reads `fanavaranClientConfigs` into the runtime profile cache. If auth fields changed, cached Fanavaran tokens are invalidated so the next call re-logins. Allowed for admin and expert roles.", }) @ApiResponse({ status: 200, description: "Cache reloaded", schema: { type: "object", properties: { ok: { type: "boolean", example: true }, message: { type: "string" }, }, }, }) reloadFanavaranConfigCache(): Promise<{ ok: true; message: string }> { return this.systemSettingsService.reloadFanavaranConfigCache(); } @Get("offline-inquiry") @UseGuards(SettingsJwtGuard, RolesGuard) @Roles(...EXPERT_AND_ADMIN_ROLES) @ApiOperation({ summary: "Get offline inquiry master switch", description: "When enabled, matching `offlineInquiries` seeds short-circuit live plate inquiry. When disabled, live/mock paths run as usual.", }) @ApiResponse({ status: 200, type: OfflineInquirySettingsViewDto }) getOfflineInquirySettings(): Promise { return this.systemSettingsService.getOfflineInquirySettings(); } @Patch("offline-inquiry") @UseGuards(SettingsJwtGuard, RolesGuard) @Roles(...EXPERT_AND_ADMIN_ROLES) @ApiOperation({ summary: "Enable or disable offline seeded plate inquiries", description: "Global master switch stored in `system_settings.offlineInquiry.enabled`. Per-seed `enabled` flags still apply when this is on.", }) @ApiResponse({ status: 200, type: OfflineInquirySettingsViewDto }) updateOfflineInquirySettings( @Body() body: UpdateOfflineInquirySettingsDto, ): Promise { return this.systemSettingsService.updateOfflineInquirySettings(body); } }