forked from Ynov/ptitspas-ynov-back
init parents route
This commit is contained in:
parent
16aed5a76d
commit
de36fa8068
10
src/routes/parents/dto/create_parents.dto.ts
Normal file
10
src/routes/parents/dto/create_parents.dto.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { IsUUID, IsOptional } from 'class-validator';
|
||||||
|
|
||||||
|
export class CreateParentDto {
|
||||||
|
@IsUUID()
|
||||||
|
id_utilisateur: string;
|
||||||
|
|
||||||
|
@IsOptional()
|
||||||
|
@IsUUID()
|
||||||
|
co_parent_id?: string;
|
||||||
|
}
|
||||||
18
src/routes/parents/parents.controller.spec.ts
Normal file
18
src/routes/parents/parents.controller.spec.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { ParentsController } from './parents.controller';
|
||||||
|
|
||||||
|
describe('ParentsController', () => {
|
||||||
|
let controller: ParentsController;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
controllers: [ParentsController],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
controller = module.get<ParentsController>(ParentsController);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(controller).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
19
src/routes/parents/parents.controller.ts
Normal file
19
src/routes/parents/parents.controller.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { Controller, Get } from '@nestjs/common';
|
||||||
|
import { BaseController } from 'src/common/base.controller';
|
||||||
|
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';
|
||||||
|
|
||||||
|
@Controller('parents')
|
||||||
|
export class ParentsController extends BaseController<Parents> {
|
||||||
|
constructor(private readonly parentsService: ParentsService) {
|
||||||
|
super(parentsService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Roles(RoleType.SUPER_ADMIN, RoleType.GESTIONNAIRE)
|
||||||
|
@Get()
|
||||||
|
override getAll(): Promise<Parents[]> {
|
||||||
|
return this.parentsService.findAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/routes/parents/parents.module.ts
Normal file
13
src/routes/parents/parents.module.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { Parents } from 'src/entities/parents.entity';
|
||||||
|
import { ParentsController } from './parents.controller';
|
||||||
|
import { ParentsService } from './parents.service';
|
||||||
|
import { Users } from 'src/entities/users.entity';
|
||||||
|
|
||||||
|
@Module({
|
||||||
|
imports: [TypeOrmModule.forFeature([Parents, Users])],
|
||||||
|
controllers: [ParentsController],
|
||||||
|
providers: [ParentsService],
|
||||||
|
})
|
||||||
|
export class ParentsModule {}
|
||||||
18
src/routes/parents/parents.service.spec.ts
Normal file
18
src/routes/parents/parents.service.spec.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { ParentsService } from './parents.service';
|
||||||
|
|
||||||
|
describe('ParentsService', () => {
|
||||||
|
let service: ParentsService;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [ParentsService],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
service = module.get<ParentsService>(ParentsService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(service).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
57
src/routes/parents/parents.service.ts
Normal file
57
src/routes/parents/parents.service.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import { BadRequestException, ConflictException, Injectable, NotFoundException } from '@nestjs/common';
|
||||||
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
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';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class ParentsService extends BaseService<Parents> {
|
||||||
|
constructor(
|
||||||
|
@InjectRepository(Parents) private readonly parentsRepository: Repository<Parents>,
|
||||||
|
@InjectRepository(Users) private readonly usersRepository: Repository<Users>,
|
||||||
|
) {
|
||||||
|
super(parentsRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
override async create(data: DeepPartial<Parents>): Promise<Parents> {
|
||||||
|
const dto = data as CreateParentDto;
|
||||||
|
|
||||||
|
const user = await this.usersRepository.findOneBy({ id: dto.id_utilisateur });
|
||||||
|
if (!user) throw new NotFoundException('Utilisateur introuvable');
|
||||||
|
if (user.role !== RoleType.PARENT) throw new BadRequestException('Acces reserve aux parents');
|
||||||
|
|
||||||
|
const exist = await this.parentsRepository.findOneBy({ user_id: dto.id_utilisateur });
|
||||||
|
if (exist) throw new ConflictException('Ce parent existe deja');
|
||||||
|
|
||||||
|
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');
|
||||||
|
}
|
||||||
|
const entity = this.parentsRepository.create({
|
||||||
|
user_id: dto.id_utilisateur,
|
||||||
|
user,
|
||||||
|
co_parent: co_parent ?? undefined,
|
||||||
|
}) as DeepPartial<Parents>;
|
||||||
|
return this.parentsRepository.save(entity)
|
||||||
|
}
|
||||||
|
|
||||||
|
override async findAll(): Promise<Parents[]> {
|
||||||
|
return this.parentsRepository.find({
|
||||||
|
relations: ['user', 'co_parent',
|
||||||
|
'parentChildren', 'dossiers'],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
override async findOne(user_id: string): Promise<Parents> {
|
||||||
|
const parent = await this.parentsRepository.findOne({
|
||||||
|
where: { user_id },
|
||||||
|
relations: ['user', 'co_parent', 'parentChildren', 'dossiers'],
|
||||||
|
});
|
||||||
|
if (!parent) throw new NotFoundException('Parent introuvable');
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user