|
| 1 | +// [BE-49] Add document statistics endpoint |
| 2 | +import { Controller, Get, Param, UseGuards } from '@nestjs/common'; |
| 3 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 4 | +import { Repository } from 'typeorm'; |
| 5 | +import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; |
| 6 | +import { Document, DocumentStatus } from '../documents/entities/document.entity'; |
| 7 | + |
| 8 | +@Controller('documents/stats') |
| 9 | +@UseGuards(JwtAuthGuard) |
| 10 | +export class DocumentStatsController { |
| 11 | + constructor( |
| 12 | + @InjectRepository(Document) |
| 13 | + private readonly documentRepo: Repository<Document>, |
| 14 | + ) {} |
| 15 | + |
| 16 | + @Get() |
| 17 | + async getOverallStats() { |
| 18 | + const total = await this.documentRepo.count(); |
| 19 | + const byStatus = await this.documentRepo |
| 20 | + .createQueryBuilder('doc') |
| 21 | + .select('doc.status', 'status') |
| 22 | + .addSelect('COUNT(*)', 'count') |
| 23 | + .groupBy('doc.status') |
| 24 | + .getRawMany(); |
| 25 | + |
| 26 | + return { total, byStatus }; |
| 27 | + } |
| 28 | + |
| 29 | + @Get('owner/:ownerId') |
| 30 | + async getStatsByOwner(@Param('ownerId') ownerId: string) { |
| 31 | + const total = await this.documentRepo.count({ where: { ownerId } }); |
| 32 | + const verified = await this.documentRepo.count({ |
| 33 | + where: { ownerId, status: DocumentStatus.VERIFIED }, |
| 34 | + }); |
| 35 | + const flagged = await this.documentRepo.count({ |
| 36 | + where: { ownerId, status: DocumentStatus.FLAGGED }, |
| 37 | + }); |
| 38 | + const pending = await this.documentRepo.count({ |
| 39 | + where: { ownerId, status: DocumentStatus.PENDING }, |
| 40 | + }); |
| 41 | + |
| 42 | + return { ownerId, total, verified, flagged, pending }; |
| 43 | + } |
| 44 | +} |
0 commit comments