forked from Ynov/ptitspas-ynov-back
parents route
This commit is contained in:
parent
3a111cce70
commit
fd10c87c94
@ -4,10 +4,11 @@ import { Parents } from 'src/entities/parents.entity';
|
||||
import { ParentsService } from './parents.service';
|
||||
import { Roles } from 'src/common/decorators/roles.decorator';
|
||||
import { RoleType } from 'src/entities/users.entity';
|
||||
import { CreateParentDto } from './dto/create_parents.dto';
|
||||
import * as typeorm from 'typeorm';
|
||||
import { UpdateParentsDto } from './dto/update_parents.dto';
|
||||
import { ApiBody, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { CreateParentDto } from '../user/dto/create_parent.dto';
|
||||
import { UpdateParentsDto } from '../user/dto/update_parent.dto';
|
||||
|
||||
|
||||
@ApiTags('Parents')
|
||||
@Controller('parents')
|
||||
@ -20,7 +21,7 @@ export class ParentsController extends BaseController<Parents> {
|
||||
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE) //Seul les utilisateurs super admin et gestionnaire peuvent accéder à cette route
|
||||
@Get()
|
||||
@ApiResponse({ status: 200, description: 'Liste des parents' })
|
||||
@ApiResponse({ status: 403, description: 'Accès refusé : Reservé aux parents' })
|
||||
@ApiResponse({ status: 403, description: 'Accès refusé : Reservé aux super_admins' })
|
||||
override getAll(): Promise<Parents[]> {
|
||||
return this.parentsService.findAll();
|
||||
}
|
||||
@ -35,15 +36,18 @@ export class ParentsController extends BaseController<Parents> {
|
||||
}
|
||||
|
||||
//Creation de parents
|
||||
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE) //Seul les utilisateurs super admin et gestionnaire peuvent accéder à cette route
|
||||
@Post()
|
||||
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
|
||||
@ApiBody({ type: CreateParentDto })
|
||||
@ApiResponse({ status: 201, description: 'Parent créé avec succès' })
|
||||
@ApiResponse({ status: 403, description: 'Acces invalide' })
|
||||
create(@Body() data: typeorm.DeepPartial<Parents>): Promise<Parents> {
|
||||
return this.parentsService.create(data as CreateParentDto);
|
||||
create(data: typeorm.DeepPartial<Parents>): Promise<Parents> {
|
||||
const dto = data as CreateParentDto;
|
||||
return this.parentsService.create({ ...dto,
|
||||
user: { ...(dto as any).user, role: RoleType.PARENT } });
|
||||
}
|
||||
|
||||
|
||||
|
||||
//Modifier un parent
|
||||
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE) //Seul les utilisateurs super admin et gestionnaire peuvent accéder à cette route
|
||||
@Patch(':id')
|
||||
|
||||
@ -4,7 +4,7 @@ import { BaseService } from 'src/common/base.service';
|
||||
import { Parents } from 'src/entities/parents.entity';
|
||||
import { RoleType, Users } from 'src/entities/users.entity';
|
||||
import { DeepPartial, Repository } from 'typeorm';
|
||||
import { CreateParentDto } from './dto/create_parents.dto';
|
||||
import { CreateParentDto } from '../user/dto/create_parent.dto';
|
||||
|
||||
@Injectable()
|
||||
export class ParentsService extends BaseService<Parents> {
|
||||
@ -17,28 +17,30 @@ export class ParentsService extends BaseService<Parents> {
|
||||
|
||||
override async create(data: DeepPartial<Parents>): Promise<Parents> {
|
||||
const dto = data as CreateParentDto;
|
||||
|
||||
const user = await this.usersRepository.findOneBy({ id: dto.user_id });
|
||||
if (!user) throw new NotFoundException('Utilisateur introuvable');
|
||||
if (user.role !== RoleType.PARENT) throw new BadRequestException('Acces reserve aux parents');
|
||||
if (user.role !== RoleType.PARENT) throw new BadRequestException('Accès réservé aux parents');
|
||||
|
||||
const exist = await this.parentsRepository.findOneBy({ user_id: dto.user_id });
|
||||
if (exist) throw new ConflictException('Ce parent existe deja');
|
||||
if (exist) throw new ConflictException('Ce parent existe déjà');
|
||||
|
||||
let co_parent: Users | null = null;
|
||||
if (dto.co_parent_id) {
|
||||
co_parent = await this.usersRepository.findOneBy({ id: dto.co_parent_id });
|
||||
if (!co_parent) throw new NotFoundException('Co-parent introuvable');
|
||||
if (co_parent.role !== RoleType.PARENT) throw new BadRequestException('Acces reserve aux parents');
|
||||
if (co_parent.role !== RoleType.PARENT) throw new BadRequestException('Accès réservé aux parents');
|
||||
}
|
||||
|
||||
const entity = this.parentsRepository.create({
|
||||
user_id: dto.user_id,
|
||||
user,
|
||||
co_parent: co_parent ?? undefined,
|
||||
}) as DeepPartial<Parents>;
|
||||
return this.parentsRepository.save(entity)
|
||||
});
|
||||
|
||||
return this.parentsRepository.save(entity);
|
||||
}
|
||||
|
||||
|
||||
override async findAll(): Promise<Parents[]> {
|
||||
return this.parentsRepository.find({
|
||||
relations: ['user', 'co_parent',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user