import { BadRequestException, Body, Controller, Delete, Get, Param, Put, Query, Req, UseGuards, } from '@nestjs/common'; import { QuotaSubject, QuotaSubjectView, SetQuotaOverrideInput, setQuotaOverrideSchema, } from '@dorfteich/shared'; import { AuthedRequest } from '../auth/auth.guard'; import { ZodValidationPipe } from '../common/zod-validation.pipe'; import { SiteAdminGuard } from './site-admin.guard'; import { QuotaAdminService } from './quota-admin.service'; function asSubject(type: string): QuotaSubject { if (type !== 'user' && type !== 'pond') throw new BadRequestException({ code: 'bad_request' }); return type; } /** Site-Admin quota override management (issue #58). */ @Controller('admin/quotas') @UseGuards(SiteAdminGuard) export class QuotaAdminController { constructor(private readonly quotaAdmin: QuotaAdminService) {} /** Resolve a user (username/e-mail) or pond (slug) to id + label. */ @Get('lookup') async lookup( @Query('type') type: string, @Query('q') q: string, ): Promise<{ id: string; label: string }> { return this.quotaAdmin.lookup(asSubject(type), q ?? ''); } @Get(':type/:id') async subject(@Param('type') type: string, @Param('id') id: string): Promise { return this.quotaAdmin.subject(asSubject(type), id); } @Put(':type/:id/:key') async set( @Param('type') type: string, @Param('id') id: string, @Param('key') key: string, @Body(new ZodValidationPipe(setQuotaOverrideSchema)) input: SetQuotaOverrideInput, @Req() request: AuthedRequest, ): Promise { return this.quotaAdmin.setOverride(request.user!, asSubject(type), id, key, input.value); } @Delete(':type/:id/:key') async clear( @Param('type') type: string, @Param('id') id: string, @Param('key') key: string, @Req() request: AuthedRequest, ): Promise { return this.quotaAdmin.clearOverride(request.user!, asSubject(type), id, key); } }