All files / src/modules/admin admin.controller.ts

86.04% Statements 37/43
41.66% Branches 15/36
66.66% Functions 10/15
85% Lines 34/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 16312x                       12x 12x 12x 12x 12x 12x 12x 12x             12x 12x       12x 1x         12x       1x           12x         1x         12x 1x         12x       1x           12x         1x                 12x 1x         12x       1x           12x         1x           12x                   12x                     12x                                   12x           12x      
import {
  Controller,
  Get,
  Post,
  Param,
  UseGuards,
  ParseIntPipe,
  Req,
  Body,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { AdminService } from '@app/modules/admin/admin.service';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from '@app/common/guards/jwt-auth.guard';
import { RolesGuard } from '@app/common/guards/roles.guard';
import { Roles } from '@app/common/decorators/roles.decorator';
import { Role, User } from '@prisma/client';
import { RejectDto } from '@app/modules/admin/dto/reject.dto';
import { AuditService } from '@app/modules/audit/audit.service';
 
@ApiTags('Admin')
@ApiBearerAuth()
@Controller('admin')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(Role.ADMIN, Role.MODERATOR)
export class AdminController {
  constructor(private readonly adminService: AdminService, private readonly audit: AuditService) {}
 
  @Get('agency-requests')
  @ApiOperation({ summary: 'List agency creation requests', description: 'Roles: ADMIN or MODERATOR.' })
  getAgencyRequests() {
    return this.adminService.getAgencyRequests();
  }
 
  @Post('agency-requests/:id/approve')
  @ApiOperation({ summary: 'Approve agency creation request', description: 'Roles: ADMIN or MODERATOR.' })
  approveAgencyRequest(
    @Param('id', ParseIntPipe) id: number,
    @Req() req: { user: User },
  ) {
    return this.adminService.approveAgencyRequest(id, req.user.id);
  }
 
  @Post('agency-requests/:id/reject')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Reject agency creation request', description: 'Roles: ADMIN or MODERATOR.' })
  rejectAgencyRequest(
    @Param('id', ParseIntPipe) id: number,
    @Req() req: { user: User },
    @Body() rejectDto: RejectDto,
  ) {
    return this.adminService.rejectAgencyRequest(id, req.user.id, rejectDto);
  }
 
  @Get('agency-updates')
  @ApiOperation({ summary: 'List requested agency profile updates', description: 'Roles: ADMIN or MODERATOR.' })
  getAgencyUpdateRequests() {
    return this.adminService.getAgencyUpdateRequests();
  }
 
  @Post('agency-updates/:id/approve')
  @ApiOperation({ summary: 'Approve agency update request', description: 'Roles: ADMIN or MODERATOR.' })
  approveAgencyUpdateRequest(
    @Param('id', ParseIntPipe) id: number,
    @Req() req: { user: User },
  ) {
    return this.adminService.approveAgencyUpdateRequest(id, req.user.id);
  }
 
  @Post('agency-updates/:id/reject')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Reject agency update request', description: 'Roles: ADMIN or MODERATOR.' })
  rejectAgencyUpdateRequest(
    @Param('id', ParseIntPipe) id: number,
    @Req() req: { user: User },
    @Body() rejectDto: RejectDto,
  ) {
    return this.adminService.rejectAgencyUpdateRequest(
      id,
      req.user.id,
      rejectDto,
    );
  }
 
  @Get('reported-listings')
  @ApiOperation({ summary: 'List reported listings', description: 'Roles: ADMIN or MODERATOR.' })
  getReportedListings() {
    return this.adminService.getReportedListings();
  }
 
  @Post('reported-listings/:id/approve')
  @ApiOperation({ summary: 'Approve reported listing', description: 'Roles: ADMIN or MODERATOR.' })
  approveListingReport(
    @Param('id', ParseIntPipe) id: number,
    @Req() req: { user: User },
  ) {
    return this.adminService.approveListingReport(id, req.user.id);
  }
 
  @Post('reported-listings/:id/reject')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Reject reported listing', description: 'Roles: ADMIN or MODERATOR.' })
  rejectListingReport(
    @Param('id', ParseIntPipe) id: number,
    @Req() req: { user: User },
    @Body() rejectDto: RejectDto,
  ) {
    return this.adminService.rejectListingReport(id, req.user.id, rejectDto);
  }
 
  // ────── Moderation queue bulk actions ─────────────────────────────────
  @Post('moderation/:type/bulk/approve')
  @ApiOperation({ summary: 'Bulk approve moderation items by type' })
  bulkApprove(
    @Param('type') type: string,
    @Body() body: { ids: number[] },
    @Req() req: { user: User },
  ) {
    return this.adminService.bulkModerationAction(type as any, body.ids || [], 'APPROVED', req.user.id);
  }
 
  @Post('moderation/:type/bulk/reject')
  @ApiOperation({ summary: 'Bulk reject moderation items by type' })
  bulkReject(
    @Param('type') type: string,
    @Body() body: { ids: number[]; reason?: string },
    @Req() req: { user: User },
  ) {
    return this.adminService.bulkModerationAction(type as any, body.ids || [], 'REJECTED', req.user.id, body.reason);
  }
 
  // ────── Audit Explorer ────────────────────────────────────────────────
  @Get('audit')
  @ApiOperation({ summary: 'Query audit logs' })
  getAudit(
    @Req() req: any,
  ) {
    const q = req.query as { entityType?: string; entityId?: string; startDate?: string; endDate?: string; userId?: string; page?: string; limit?: string };
    return this.audit.getAuditLogs({
      entityType: q.entityType,
      entityId: q.entityId ? Number(q.entityId) : undefined,
      startDate: q.startDate ? new Date(q.startDate) : undefined,
      endDate  : q.endDate ? new Date(q.endDate) : undefined,
      userId   : q.userId ? Number(q.userId) : undefined,
      page     : q.page ? Number(q.page) : 1,
      limit    : q.limit ? Number(q.limit) : 20,
    });
  }
 
  // ────── Feature Flags ────────────────────────────────────────────────
  @Get('flags')
  @ApiOperation({ summary: 'List feature flags' })
  listFlags(@Req() _req: any) {
    return this.adminService.listFeatureFlags();
  }
 
  @Post('flags/:key/toggle')
  @ApiOperation({ summary: 'Toggle a feature flag' })
  toggleFlag(@Param('key') key: string) {
    return this.adminService.toggleFeatureFlag(key);
  }
}