Commit 877f3f7a by Augusto

version 0.0.1

parent 8bbe41f2
......@@ -11,7 +11,7 @@ export class CandidatesService {
constructor(private prisma: PrismaService) { }
async create(createCandidateDto: CreateCandidateDto, userId?: number) {
const { comment, ...candidateData } = createCandidateDto;
const { comment, siteIds, ...candidateData } = createCandidateDto;
// Create the candidate with a transaction to ensure both operations succeed or fail together
return this.prisma.$transaction(async (prisma) => {
......@@ -21,8 +21,20 @@ export class CandidatesService {
...candidateData,
createdById: userId,
updatedById: userId,
sites: {
create: siteIds.map(siteId => ({
site: {
connect: { id: siteId }
}
}))
}
},
include: {
sites: {
include: {
site: true
}
},
comments: {
include: {
createdBy: {
......@@ -54,6 +66,11 @@ export class CandidatesService {
return prisma.candidate.findUnique({
where: { id: candidate.id },
include: {
sites: {
include: {
site: true
}
},
comments: {
include: {
createdBy: {
......@@ -172,10 +189,29 @@ export class CandidatesService {
async update(id: number, updateCandidateDto: UpdateCandidateDto) {
try {
const { siteIds, ...candidateData } = updateCandidateDto;
return await this.prisma.candidate.update({
where: { id },
data: updateCandidateDto,
data: {
...candidateData,
...(siteIds && {
sites: {
deleteMany: {}, // Remove all existing site associations
create: siteIds.map(siteId => ({
site: {
connect: { id: siteId }
}
}))
}
})
},
include: {
sites: {
include: {
site: true
}
},
comments: {
include: {
createdBy: {
......
......@@ -12,6 +12,11 @@ export enum CandidateStatus {
PENDING = 'PENDING',
APPROVED = 'APPROVED',
REJECTED = 'REJECTED',
NEGOTIATION_ONGOING = 'NEGOTIATION_ONGOING',
MNO_VALIDATION = 'MNO_VALIDATION',
CLOSING = 'CLOSING',
SEARCH_AREA = 'SEARCH_AREA',
}
export class CreateCandidateDto {
......@@ -43,9 +48,9 @@ export class CreateCandidateDto {
@IsBoolean()
onGoing: boolean;
@ApiProperty({ description: 'ID of the site this candidate belongs to' })
@IsNumber()
siteId: number;
@ApiProperty({ description: 'IDs of the sites this candidate belongs to', type: [Number] })
@IsNumber({}, { each: true })
siteIds: number[];
@ApiPropertyOptional({ description: 'Initial comment for the candidate' })
@IsString()
......
......@@ -38,8 +38,8 @@ export class UpdateCandidateDto {
@IsBoolean()
onGoing?: boolean;
@ApiPropertyOptional({ description: 'ID of the site this candidate belongs to' })
@ApiPropertyOptional({ description: 'IDs of the sites this candidate belongs to', type: [Number] })
@IsOptional()
@IsNumber()
siteId?: number;
@IsNumber({}, { each: true })
siteIds?: number[];
}
\ No newline at end of file
import { Controller, Get, Post, Body, Param, Delete, UseGuards, ParseIntPipe } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { Controller, Get, Post, Body, Param, Delete, UseGuards, ParseIntPipe, Req, Put } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiBody } from '@nestjs/swagger';
import { CommentsService } from './comments.service';
import { CreateCommentDto } from './dto/create-comment.dto';
import { UpdateCommentDto } from './dto/update-comment.dto';
import { CommentResponseDto } from './dto/comment-response.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles } from '../auth/decorators/roles.decorator';
import { Role } from '@prisma/client';
import { Request } from 'express';
@ApiTags('comments')
@Controller('comments')
......@@ -16,11 +18,14 @@ export class CommentsController {
constructor(private readonly commentsService: CommentsService) { }
@Post()
@Roles(Role.ADMIN, Role.MANAGER, Role.OPERATOR)
@Roles(Role.ADMIN, Role.MANAGER, Role.OPERATOR, Role.SUPERADMIN)
@ApiOperation({ summary: 'Create a new comment' })
@ApiBody({ type: CreateCommentDto })
@ApiResponse({ status: 201, description: 'The comment has been successfully created.', type: CommentResponseDto })
@ApiResponse({ status: 400, description: 'Bad Request.' })
create(@Body() createCommentDto: CreateCommentDto) {
create(@Body() createCommentDto: CreateCommentDto, @Req() req: Request) {
const user = req.user as any;
createCommentDto.createdById = user.id;
return this.commentsService.create(createCommentDto);
}
......@@ -44,11 +49,21 @@ export class CommentsController {
}
@Delete(':id')
@Roles(Role.ADMIN, Role.MANAGER)
@Roles(Role.ADMIN, Role.MANAGER, Role.SUPERADMIN)
@ApiOperation({ summary: 'Delete a comment' })
@ApiResponse({ status: 200, description: 'The comment has been successfully deleted.', type: CommentResponseDto })
@ApiResponse({ status: 404, description: 'Comment not found.' })
remove(@Param('id', ParseIntPipe) id: number) {
return this.commentsService.remove(id);
}
@Put(':id')
@Roles(Role.ADMIN, Role.MANAGER, Role.OPERATOR, Role.SUPERADMIN)
@ApiOperation({ summary: 'Update a comment' })
@ApiBody({ type: UpdateCommentDto })
@ApiResponse({ status: 200, description: 'The comment has been successfully updated.', type: CommentResponseDto })
@ApiResponse({ status: 404, description: 'Comment not found.' })
update(@Param('id', ParseIntPipe) id: number, @Body() updateCommentDto: UpdateCommentDto) {
return this.commentsService.update(id, updateCommentDto);
}
}
\ No newline at end of file
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from '../../common/prisma/prisma.service';
import { CreateCommentDto } from './dto/create-comment.dto';
import { UpdateCommentDto } from './dto/update-comment.dto';
@Injectable()
export class CommentsService {
......@@ -65,4 +66,27 @@ export class CommentsService {
where: { id },
});
}
async update(id: number, updateCommentDto: UpdateCommentDto) {
try {
return await this.prisma.comment.update({
where: { id },
data: {
content: updateCommentDto.content,
updatedAt: new Date(),
},
include: {
createdBy: {
select: {
id: true,
name: true,
email: true,
},
},
},
});
} catch (error) {
throw new NotFoundException(`Comment with ID ${id} not found`);
}
}
}
\ No newline at end of file
import { IsString, IsNotEmpty, IsInt, IsOptional } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
export class CreateCommentDto {
@ApiProperty({
description: 'The content of the comment',
example: 'This is a comment about the candidate'
})
@IsString()
@IsNotEmpty()
content: string;
@ApiProperty({
description: 'The ID of the candidate this comment is for',
example: 64
})
@IsInt()
@IsNotEmpty()
candidateId: number;
@ApiProperty({
description: 'The ID of the user creating the comment (optional, will be set automatically)',
example: 1,
required: false
})
@IsInt()
@IsOptional()
createdById?: number;
......
import { IsString, IsNotEmpty, IsOptional } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
export class UpdateCommentDto {
@ApiProperty({
description: 'The updated content of the comment',
example: 'This is an updated comment about the candidate'
})
@IsString()
@IsNotEmpty()
content: string;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment