From c6b4d86f57a178b73301415b86d52c39fc43cfc3 Mon Sep 17 00:00:00 2001 From: rma1403 <ravamaulana14@gmail.com> Date: Thu, 18 Apr 2024 18:35:24 +0700 Subject: [PATCH] feat: adjust pendaftaran --- src/entities/pendaftaranTesis.entity.ts | 7 +- .../registrasi-tesis.controller.ts | 25 +++++- src/registrasi-tesis/registrasi-tesis.dto.ts | 25 +++--- .../registrasi-tesis.service.ts | 85 +++++++++++++------ 4 files changed, 98 insertions(+), 44 deletions(-) diff --git a/src/entities/pendaftaranTesis.entity.ts b/src/entities/pendaftaranTesis.entity.ts index 637a05e..e9a8fa3 100644 --- a/src/entities/pendaftaranTesis.entity.ts +++ b/src/entities/pendaftaranTesis.entity.ts @@ -1,6 +1,7 @@ import { Column, Entity, + JoinColumn, ManyToOne, OneToMany, PrimaryGeneratedColumn, @@ -56,13 +57,17 @@ export class PendaftaranTesis { status: RegStatus; @ApiProperty({ type: Topik }) - @ManyToOne(() => Topik, (topik) => topik.id) + @ManyToOne(() => Topik, (topik) => topik.id, { cascade: true }) topik: Topik; @ApiProperty() @ManyToOne(() => Pengguna, (pengguna) => pengguna.id) + @JoinColumn({ name: "mahasiswaId" }) mahasiswa: Pengguna; + @Column() + mahasiswaId: string; + @ApiProperty() @ManyToOne(() => Pengguna, (pengguna) => pengguna.id) penerima: Pengguna; diff --git a/src/registrasi-tesis/registrasi-tesis.controller.ts b/src/registrasi-tesis/registrasi-tesis.controller.ts index e41920c..9802752 100644 --- a/src/registrasi-tesis/registrasi-tesis.controller.ts +++ b/src/registrasi-tesis/registrasi-tesis.controller.ts @@ -12,8 +12,11 @@ import { UseGuards, } from "@nestjs/common"; import { + ApiBadRequestResponse, ApiBearerAuth, ApiCookieAuth, + ApiCreatedResponse, + ApiNotFoundResponse, ApiOkResponse, ApiOperation, ApiTags, @@ -52,21 +55,35 @@ export class RegistrasiTesisController { ) {} @ApiOperation({ - summary: - "Create new registration. Roles: S2_MAHASISWA, ADMIN, S2_TIM_TESIS", + summary: "Create new registration. Roles: S2_MAHASISWA, ADMIN", + }) + @ApiCreatedResponse({ type: IdDto }) + @ApiNotFoundResponse({ description: "Penerima atau topik tidak ditemukan" }) + @ApiBadRequestResponse({ + description: + "Mahasiswa sedang memiliki pendaftaran aktif atau judul dan deskripsi topik baru tidak ada", }) @UseGuards(CustomAuthGuard, RolesGuard) - @Roles(RoleEnum.S2_MAHASISWA, RoleEnum.ADMIN, RoleEnum.S2_TIM_TESIS) + @Roles(RoleEnum.S2_MAHASISWA, RoleEnum.ADMIN) @Post() async createTopicRegistration( @Body() topicRegistrationDto: RegDto, @Req() req: Request, - ) { + ): Promise<IdDto> { const { id } = req.user as AuthDto; + const periode = await this.konfService.getKonfigurasiByKey( + process.env.KONF_PERIODE_KEY, + ); + + if (!periode) { + throw new BadRequestException("Periode belum dikonfigurasi."); + } + return this.registrasiTesisService.createTopicRegistration( id, topicRegistrationDto, + periode, ); } diff --git a/src/registrasi-tesis/registrasi-tesis.dto.ts b/src/registrasi-tesis/registrasi-tesis.dto.ts index 330f31d..cc9541b 100644 --- a/src/registrasi-tesis/registrasi-tesis.dto.ts +++ b/src/registrasi-tesis/registrasi-tesis.dto.ts @@ -16,25 +16,28 @@ import { import { Pengguna, RoleEnum } from "src/entities/pengguna.entity"; export class RegDto { - @IsUUID() - @ApiProperty() - idMahasiswa: string; - @IsUUID() @ApiProperty() idPenerima: string; - @IsString() - @ApiProperty() - judulTopik: string; - - @IsString() - @ApiProperty() - deskripsi: string; + @IsUUID() + @IsOptional() + @ApiPropertyOptional() + idTopik?: string; @IsEnum(JalurEnum) @ApiProperty({ enum: JalurEnum }) jalurPilihan: JalurEnum; + + @IsString() + @IsOptional() + @ApiPropertyOptional() + judulTopik?: string; + + @IsString() + @IsOptional() + @ApiPropertyOptional() + deskripsiTopik?: string; } export class RegByMhsParamDto { diff --git a/src/registrasi-tesis/registrasi-tesis.service.ts b/src/registrasi-tesis/registrasi-tesis.service.ts index 74afb3e..5e90697 100644 --- a/src/registrasi-tesis/registrasi-tesis.service.ts +++ b/src/registrasi-tesis/registrasi-tesis.service.ts @@ -14,7 +14,6 @@ import { import { Pengguna, RoleEnum } from "src/entities/pengguna.entity"; import { Topik } from "src/entities/topik.entity"; import { generateQueryBuilderOrderByObj } from "src/helper/sorting"; -import { validateId } from "src/helper/validation"; import { ArrayContains, Brackets, DataSource, In, Repository } from "typeorm"; import { FindAllNewestRegRespDto, @@ -44,47 +43,77 @@ export class RegistrasiTesisService { async createTopicRegistration( userId: string, topicRegistrationDto: RegDto, - ): Promise<PendaftaranTesis> { - // TODO: Proper validations - - // Validate id - validateId([ - { id: userId, object: "Pengguna" }, - { id: topicRegistrationDto.idPenerima, object: "Pembimbing" }, - ]); - - // Validate user id, supervisor id - const [user, supervisor, topic] = await Promise.all([ - this.penggunaRepository.findOne({ - where: { id: userId }, - }), + periode: string, + ): Promise<IdDto> { + const queries: ( + | Promise<PendaftaranTesis> + | Promise<Pengguna> + | Promise<Topik> + )[] = [ + this.getNewestRegByMhs(userId, periode), this.penggunaRepository.findOne({ where: { id: topicRegistrationDto.idPenerima }, }), - this.topicRepostitory.findOne({ - where: { judul: topicRegistrationDto.judulTopik }, - }), - ]); + ]; + + if (topicRegistrationDto.idTopik) { + queries.push( + this.topicRepostitory.findOne({ + where: { id: topicRegistrationDto.idTopik }, + }), + ); + } + + const queryResult = await Promise.all(queries); + const lastPendaftaran = queryResult[0] as PendaftaranTesis; + const penerima = queryResult[1] as Pengguna; + let topik = topicRegistrationDto.idTopik ? (queryResult[2] as Topik) : null; - if (!user) { - throw new NotFoundException("User not found."); - } else if (!supervisor) { - throw new NotFoundException("Supervisor not found."); - } else if (!topic) { + if (!penerima) { + throw new NotFoundException("Penerima not found."); + } + + if (topicRegistrationDto.idTopik && !topik) { throw new NotFoundException("Topic not found."); } + if (lastPendaftaran && lastPendaftaran.status !== RegStatus.REJECTED) { + throw new BadRequestException( + "Mahasiswa already has pending registration in this period", + ); + } + + if (!topik) { + if ( + !topicRegistrationDto.judulTopik || + !topicRegistrationDto.deskripsiTopik + ) { + throw new BadRequestException( + "Judul dan deskripsi topik tidak boleh kosong.", + ); + } + + topik = this.topicRepostitory.create({ + judul: topicRegistrationDto.judulTopik, + deskripsi: topicRegistrationDto.deskripsiTopik, + idPengaju: userId, + periode, + }); + } + // Create new registration const createdRegistration = this.pendaftaranTesisRepository.create({ ...topicRegistrationDto, - mahasiswa: user, - penerima: supervisor, - topik: topic, + mahasiswaId: userId, + penerima, + topik, }); await this.pendaftaranTesisRepository.save(createdRegistration); - return createdRegistration; + return { + id: createdRegistration.id, + }; } async findByUserId( -- GitLab