Spaces:
Sleeping
Sleeping
| import { | |
| Controller, | |
| Get, | |
| Put, | |
| Param, | |
| Body, | |
| Query, | |
| UseGuards, | |
| Request, | |
| ParseIntPipe, | |
| } from '@nestjs/common'; | |
| import { | |
| ApiTags, | |
| ApiBearerAuth, | |
| ApiOperation, | |
| ApiResponse, | |
| ApiParam, | |
| ApiQuery, | |
| } from '@nestjs/swagger'; | |
| import { JwtAuthGuard } from '../../auth/guards/jwt-auth.guard'; | |
| import { RolesGuard } from '../../auth/guards/roles.guard'; | |
| import { Roles } from '../../../common/decorators/roles.decorator'; | |
| import { RoleName } from '../../auth/entities/role.entity'; | |
| import { SettingsService } from '../services/settings.service'; | |
| import { UpdateSettingsDto, UpdateSingleSettingDto } from '../dto/update-settings.dto'; | |
| import { UpdateBrandingDto } from '../dto/update-branding.dto'; | |
| import { UpdateRateLimitsDto } from '../dto/update-rate-limits.dto'; | |
| ('βοΈ Settings') | |
| ('JWT-auth') | |
| ('api/settings') | |
| (JwtAuthGuard, RolesGuard) | |
| export class SettingsController { | |
| constructor(private readonly settingsService: SettingsService) {} | |
| // ββ Static routes FIRST ββββββββββββββββββββββββββββββββββββ | |
| ('branding') | |
| ({ | |
| summary: 'Get branding settings (public)', | |
| description: 'Retrieves publicly accessible branding configuration like logos, UI colors, and custom CSS. No auth required. Uses table: `branding_settings`.', | |
| }) | |
| ({ status: 200, description: 'Branding settings returned' }) | |
| ({ name: 'campusId', required: false, type: Number }) | |
| async getBranding(('campusId') campusId?: number) { | |
| return this.settingsService.getBranding(campusId ? Number(campusId) : undefined); | |
| } | |
| ('branding/:campusId') | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Update branding settings', | |
| description: 'Updates UI configurations like primary and secondary UI colors. Access rules: ADMIN, IT_ADMIN. Uses table: `branding_settings`.', | |
| }) | |
| ({ name: 'campusId', type: Number }) | |
| ({ status: 200, description: 'Branding updated' }) | |
| async updateBranding( | |
| ('campusId', ParseIntPipe) campusId: number, | |
| () dto: UpdateBrandingDto, | |
| () req, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.settingsService.updateBranding(campusId, dto, userId); | |
| } | |
| ('rate-limits') | |
| (RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Get rate limit configurations', | |
| description: 'Fetches configured thresholds mapping maximum requests dynamically loaded to endpoint routers. Access rules: IT_ADMIN. Uses table: `api_rate_limits`.', | |
| }) | |
| ({ status: 200, description: 'Rate limits returned' }) | |
| async getRateLimits() { | |
| return this.settingsService.getRateLimits(); | |
| } | |
| ('rate-limits/:id') | |
| (RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Update a rate limit configuration', | |
| description: 'Updates limits applied to specified API route matching endpoints. Access rules: IT_ADMIN. Uses table: `api_rate_limits`.', | |
| }) | |
| ({ name: 'id', type: Number }) | |
| ({ status: 200, description: 'Rate limit updated' }) | |
| async updateRateLimit( | |
| ('id', ParseIntPipe) id: number, | |
| () dto: UpdateRateLimitsDto, | |
| ) { | |
| return this.settingsService.updateRateLimit(id, dto); | |
| } | |
| // ββ Generic settings routes ββββββββββββββββββββββββββββββββ | |
| () | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Get all system settings', | |
| description: 'Retrieves active universal application configurations across cache structures. Access rules: ADMIN, IT_ADMIN. Uses table: `system_settings`.', | |
| }) | |
| ({ status: 200, description: 'All settings returned' }) | |
| async getAllSettings() { | |
| return this.settingsService.getAllSettings(); | |
| } | |
| () | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Update multiple system settings', | |
| description: 'Batch updates generic property map array of system settings. Access rules: ADMIN, IT_ADMIN. Uses table: `system_settings`.', | |
| }) | |
| ({ status: 200, description: 'Settings updated' }) | |
| async updateSettings(() dto: UpdateSettingsDto, () req) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.settingsService.updateSettings(dto, userId); | |
| } | |
| // ββ Parameter routes LAST ββββββββββββββββββββββββββββββββββ | |
| (':key') | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Get a specific setting by key', | |
| description: 'Fast retrieval query locating single string cache index key setting. Access rules: ADMIN, IT_ADMIN. Uses table: `system_settings`.', | |
| }) | |
| ({ name: 'key', description: 'Setting key' }) | |
| ({ status: 200, description: 'Setting returned' }) | |
| async getSetting(('key') key: string) { | |
| return this.settingsService.getSetting(key); | |
| } | |
| (':key') | |
| (RoleName.ADMIN, RoleName.IT_ADMIN) | |
| ({ | |
| summary: 'Update a specific setting by key', | |
| description: 'Upserts singular setting instance modifying existing string configurations. Access rules: ADMIN, IT_ADMIN. Uses table: `system_settings`.', | |
| }) | |
| ({ name: 'key', description: 'Setting key' }) | |
| ({ status: 200, description: 'Setting updated' }) | |
| async updateSetting( | |
| ('key') key: string, | |
| () dto: UpdateSingleSettingDto, | |
| () req, | |
| ) { | |
| const userId = req.user.userId || req.user.id; | |
| return this.settingsService.updateSetting(key, dto, userId); | |
| } | |
| } | |