apps/recallassess/recallassess-api/src/api/client/reports/participant-group-reports.controller.ts
api/client/reports/participant-group
Methods |
|
| Async createGroupForReports | |||||||||
createGroupForReports(data: AddParticipantGroupDto, req: Request)
|
|||||||||
Decorators :
@HttpCode(HttpStatus.CREATED)
|
|||||||||
|
Create a new participant group (reports alias) POST /api/reports/client/participant-group
Parameters :
Returns :
Promise<CLParticipantGroupDto>
|
| Async deleteGroupForReports | |||||||||
deleteGroupForReports(id: number, req: Request)
|
|||||||||
Decorators :
@HttpCode(HttpStatus.NO_CONTENT)
|
|||||||||
|
Delete a participant group (reports alias) DELETE /api/reports/client/participant-group/:id
Parameters :
Returns :
Promise<void>
|
| Async getAllGroupsForReports | |||||||||
getAllGroupsForReports(query: ParticipantGroupQueryDto, req: Request)
|
|||||||||
Decorators :
@HttpCode(HttpStatus.OK)
|
|||||||||
|
Get all participant groups for client reports with optional filtering GET /api/reports/client/participant-group?sq=sales
Parameters :
Returns :
Promise<CLParticipantGroupDto[]>
|
| Private Async getAuthInfoFromRequest | ||||||||
getAuthInfoFromRequest(request: Request)
|
||||||||
|
Get company_id and participant_id from authenticated participant token
Parameters :
Returns :
Promise<literal type>
Object with companyId and participantId |
| Async getGroupByIdForReports | |||||||||
getGroupByIdForReports(id: number, req: Request)
|
|||||||||
Decorators :
@HttpCode(HttpStatus.OK)
|
|||||||||
|
Get a single participant group by ID (reports alias) GET /api/reports/client/participant-group/:id
Parameters :
Returns :
Promise<CLParticipantGroupDto>
|
| Async updateGroupForReports | ||||||||||||
updateGroupForReports(id: number, data: UpdateParticipantGroupDto, req: Request)
|
||||||||||||
Decorators :
@HttpCode(HttpStatus.OK)
|
||||||||||||
|
Update a participant group (reports alias) PUT /api/reports/client/participant-group/:id
Parameters :
Returns :
Promise<CLParticipantGroupDto>
|
import { Public } from "@bish-nest/core/auth/decorator/public.decorator";
import {
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
NotFoundException,
Param,
ParseIntPipe,
Post,
Put,
Query,
Req,
} from "@nestjs/common";
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
import { Request } from "express";
import {
AddParticipantGroupDto,
CLParticipantGroupDto,
ParticipantGroupQueryDto,
UpdateParticipantGroupDto,
} from "../participant-group/dto";
import { CLParticipantGroupService } from "../participant-group/participant-group.service";
import { BNestPrismaService } from "@bish-nest/core/services";
import { ConfigService } from "@nestjs/config";
import { JwtService } from "@nestjs/jwt";
import { UnauthorizedException } from "@nestjs/common";
@ApiTags("Client - Reports - Participant Groups")
@Controller("api/client/reports/participant-group")
export class CLParticipantGroupReportsController {
constructor(
private participantGroupService: CLParticipantGroupService,
private prisma: BNestPrismaService,
private jwtService: JwtService,
private configService: ConfigService,
) {}
/**
* Extract token from request header
* Reuses the same logic as CLParticipantGroupController
*/
private extractTokenFromHeader(request: Request): string | undefined {
const [, token] = request.headers.authorization?.split(" ") ?? [];
let accessToken = token;
// accessToken can also come in the query param
if (!accessToken) {
accessToken = request.query["accessToken"] as string;
}
return accessToken;
}
/**
* Get company_id and participant_id from authenticated participant token
* @param request Express request object
* @returns Object with companyId and participantId
*/
private async getAuthInfoFromRequest(request: Request): Promise<{ companyId: number; participantId: number }> {
const token = this.extractTokenFromHeader(request);
if (!token) {
throw new UnauthorizedException("Authentication token required");
}
try {
const payload = await this.jwtService.verifyAsync(token, {
secret: this.configService.get("JWT_SECRET") || "secret",
});
if (!payload || !payload.sub) {
throw new UnauthorizedException("Invalid authentication token");
}
const participantId = payload.sub as number;
const participant = await this.prisma.client.participant.findUnique({
where: { id: participantId },
select: { company_id: true },
});
if (!participant) {
throw new UnauthorizedException("Participant not found");
}
return {
companyId: participant.company_id,
participantId,
};
} catch (error) {
if (error instanceof UnauthorizedException) {
throw error;
}
throw new UnauthorizedException("Invalid or expired authentication token");
}
}
/**
* Get all participant groups for client reports with optional filtering
* GET /api/reports/client/participant-group?sq=sales
*/
@HttpCode(HttpStatus.OK)
@Public()
@Get()
@ApiOperation({
summary: "Get all participant groups with optional search (reports)",
description: "Returns participant groups filtered by search query and company_id under the reports namespace",
})
@ApiResponse({
status: 200,
description: "Returns array of participant groups",
type: [CLParticipantGroupDto],
})
async getAllGroupsForReports(
@Query() query: ParticipantGroupQueryDto,
@Req() req: Request,
): Promise<CLParticipantGroupDto[]> {
const { companyId } = await this.getAuthInfoFromRequest(req);
return this.participantGroupService.getFilteredGroups(query.sq, companyId);
}
/**
* Get a single participant group by ID (reports alias)
* GET /api/reports/client/participant-group/:id
*/
@HttpCode(HttpStatus.OK)
@Public()
@Get(":id")
@ApiOperation({ summary: "Get a participant group by ID (reports)" })
@ApiResponse({
status: 200,
description: "Returns a single participant group",
type: CLParticipantGroupDto,
})
@ApiResponse({
status: 404,
description: "Participant group not found",
})
async getGroupByIdForReports(
@Param("id", ParseIntPipe) id: number,
@Req() req: Request,
): Promise<CLParticipantGroupDto> {
const { companyId } = await this.getAuthInfoFromRequest(req);
const group = await this.participantGroupService.getGroupById(id, companyId);
if (!group) {
throw new NotFoundException(`Participant group with ID ${id} not found`);
}
return group;
}
/**
* Create a new participant group (reports alias)
* POST /api/reports/client/participant-group
*/
@HttpCode(HttpStatus.CREATED)
@Public()
@Post()
@ApiOperation({ summary: "Create a new participant group (reports)" })
@ApiResponse({
status: 201,
description: "Participant group created successfully",
type: CLParticipantGroupDto,
})
async createGroupForReports(
@Body() data: AddParticipantGroupDto,
@Req() req: Request,
): Promise<CLParticipantGroupDto> {
const { companyId, participantId } = await this.getAuthInfoFromRequest(req);
return this.participantGroupService.createGroup(data, companyId, participantId);
}
/**
* Update a participant group (reports alias)
* PUT /api/reports/client/participant-group/:id
*/
@HttpCode(HttpStatus.OK)
@Public()
@Put(":id")
@ApiOperation({ summary: "Update a participant group (reports)" })
@ApiResponse({
status: 200,
description: "Participant group updated successfully",
type: CLParticipantGroupDto,
})
@ApiResponse({
status: 404,
description: "Participant group not found",
})
async updateGroupForReports(
@Param("id", ParseIntPipe) id: number,
@Body() data: UpdateParticipantGroupDto,
@Req() req: Request,
): Promise<CLParticipantGroupDto> {
const { companyId } = await this.getAuthInfoFromRequest(req);
const group = await this.participantGroupService.updateGroup(id, data, companyId);
if (!group) {
throw new NotFoundException(`Participant group with ID ${id} not found`);
}
return group;
}
/**
* Delete a participant group (reports alias)
* DELETE /api/reports/client/participant-group/:id
*/
@HttpCode(HttpStatus.NO_CONTENT)
@Public()
@Delete(":id")
@ApiOperation({ summary: "Delete a participant group (reports)" })
@ApiResponse({
status: 204,
description: "Participant group deleted successfully",
})
@ApiResponse({
status: 404,
description: "Participant group not found",
})
async deleteGroupForReports(
@Param("id", ParseIntPipe) id: number,
@Req() req: Request,
): Promise<void> {
const { companyId } = await this.getAuthInfoFromRequest(req);
await this.participantGroupService.deleteGroup(id, companyId);
}
}