Skip to content
Snippets Groups Projects

Story/alokasi topik dosbim

Merged Chiquita Ahsanunnisa requested to merge story/alokasi-topik-dosbim into development
Compare and
4 files
+ 111
26
Preferences
Compare changes
Files
4
import {
import {
 
BadRequestException,
Body,
Body,
Controller,
Controller,
Delete,
Delete,
@@ -8,12 +9,15 @@ import {
@@ -8,12 +9,15 @@ import {
Post,
Post,
Put,
Put,
Query,
Query,
 
Req,
UseGuards,
UseGuards,
} from "@nestjs/common";
} from "@nestjs/common";
import {
import {
ApiBearerAuth,
ApiBearerAuth,
ApiCookieAuth,
ApiCookieAuth,
 
ApiCreatedResponse,
ApiOkResponse,
ApiOkResponse,
 
ApiOperation,
ApiTags,
ApiTags,
} from "@nestjs/swagger";
} from "@nestjs/swagger";
import { RoleEnum } from "src/entities/pengguna.entity";
import { RoleEnum } from "src/entities/pengguna.entity";
@@ -22,7 +26,7 @@ import { Roles } from "src/middlewares/roles.decorator";
@@ -22,7 +26,7 @@ import { Roles } from "src/middlewares/roles.decorator";
import { RolesGuard } from "src/middlewares/roles.guard";
import { RolesGuard } from "src/middlewares/roles.guard";
import {
import {
CreateBulkTopikDto,
CreateBulkTopikDto,
CreateRespDto,
TopikIdRespDto,
CreateTopikDto,
CreateTopikDto,
GetAllRespDto,
GetAllRespDto,
OmittedTopik,
OmittedTopik,
@@ -32,6 +36,9 @@ import {
@@ -32,6 +36,9 @@ import {
createBulkRespDto,
createBulkRespDto,
} from "./alokasi-topik.dto";
} from "./alokasi-topik.dto";
import { AlokasiTopikService } from "./alokasi-topik.service";
import { AlokasiTopikService } from "./alokasi-topik.service";
 
import { Request } from "express";
 
import { AuthDto } from "src/auth/auth.dto";
 
import { HIGH_AUTHORITY_ROLES, isHighAuthority } from "src/helper/roles";
@ApiTags("Alokasi Topik")
@ApiTags("Alokasi Topik")
@ApiCookieAuth()
@ApiCookieAuth()
@@ -41,22 +48,41 @@ import { AlokasiTopikService } from "./alokasi-topik.service";
@@ -41,22 +48,41 @@ import { AlokasiTopikService } from "./alokasi-topik.service";
export class AlokasiTopikController {
export class AlokasiTopikController {
constructor(private alokasiTopikService: AlokasiTopikService) {}
constructor(private alokasiTopikService: AlokasiTopikService) {}
@ApiOkResponse({ type: CreateRespDto })
@ApiOperation({
@Roles(RoleEnum.S2_TIM_TESIS, RoleEnum.ADMIN)
summary: "Create new topik. Roles: S2_TIM_TESIS, ADMIN, S2_PEMBIMBING",
 
})
 
@ApiCreatedResponse({ type: TopikIdRespDto })
 
@Roles(...HIGH_AUTHORITY_ROLES, RoleEnum.S2_PEMBIMBING)
@Post()
@Post()
async create(@Body() createDto: CreateTopikDto) {
async create(
return await this.alokasiTopikService.create({ ...createDto });
@Body() createDto: CreateTopikDto,
 
@Req() req: Request,
 
): Promise<TopikIdRespDto> {
 
const { roles, id } = req.user as AuthDto;
 
// user only has S2_PEMBIMBING role
 
if (!isHighAuthority(roles) && createDto.idPengaju !== id) {
 
throw new BadRequestException("Pengaju ID harus sama dengan user ID");
 
}
 
 
return await this.alokasiTopikService.create(createDto);
}
}
 
@ApiOperation({
 
summary: "Create multiple topik. Roles: S2_TIM_TESIS, ADMIN",
 
})
@ApiOkResponse({ type: createBulkRespDto })
@ApiOkResponse({ type: createBulkRespDto })
@Roles(RoleEnum.S2_TIM_TESIS, RoleEnum.ADMIN)
@Roles(...HIGH_AUTHORITY_ROLES)
@Post("/bulk")
@Post("/bulk")
async createBulk(@Body() createDto: CreateBulkTopikDto) {
async createBulk(@Body() createDto: CreateBulkTopikDto) {
return await this.alokasiTopikService.createBulk(createDto);
return await this.alokasiTopikService.createBulk(createDto);
}
}
 
@ApiOperation({
 
summary:
 
"Get topik by ID. Roles: S2_TIM_TESIS, ADMIN, S2_PEMBIMBING, S2_MAHASISWA",
 
})
@ApiOkResponse({ type: OmittedTopik })
@ApiOkResponse({ type: OmittedTopik })
@Roles(RoleEnum.S2_TIM_TESIS, RoleEnum.ADMIN)
@Roles(...HIGH_AUTHORITY_ROLES, RoleEnum.S2_PEMBIMBING, RoleEnum.S2_MAHASISWA)
@Get("/:id")
@Get("/:id")
async getById(@Param() params: TopikParamDto) {
async getById(@Param() params: TopikParamDto) {
const res = await this.alokasiTopikService.findActiveTopikById(params.id);
const res = await this.alokasiTopikService.findActiveTopikById(params.id);
@@ -64,8 +90,12 @@ export class AlokasiTopikController {
@@ -64,8 +90,12 @@ export class AlokasiTopikController {
return res as OmittedTopik;
return res as OmittedTopik;
}
}
 
@ApiOperation({
 
summary:
 
"Get all topik. Roles: S2_TIM_TESIS, ADMIN, S2_MAHASISWA, S2_PEMBIMBING",
 
})
@ApiOkResponse({ type: GetAllRespDto })
@ApiOkResponse({ type: GetAllRespDto })
@Roles(RoleEnum.S2_TIM_TESIS, RoleEnum.ADMIN, RoleEnum.S2_MAHASISWA)
@Roles(...HIGH_AUTHORITY_ROLES, RoleEnum.S2_MAHASISWA, RoleEnum.S2_PEMBIMBING)
@Get()
@Get()
async getAll(
async getAll(
@Query()
@Query()
@@ -73,28 +103,72 @@ export class AlokasiTopikController {
@@ -73,28 +103,72 @@ export class AlokasiTopikController {
) {
) {
return await this.alokasiTopikService.findAllActiveTopikCreatedByPembimbing(
return await this.alokasiTopikService.findAllActiveTopikCreatedByPembimbing(
{
{
page: query.page || 1,
...query,
...query,
 
page: query.page || 1,
},
},
);
);
}
}
@Roles(RoleEnum.S2_TIM_TESIS, RoleEnum.ADMIN)
@ApiOperation({
 
summary: "Update topik. Roles: S2_TIM_TESIS, ADMIN, S2_PEMBIMBING",
 
})
 
@ApiOkResponse({ type: TopikIdRespDto })
 
@Roles(...HIGH_AUTHORITY_ROLES, RoleEnum.S2_PEMBIMBING)
@Put("/:id")
@Put("/:id")
async update(
async update(
@Param() params: TopikParamDto,
@Param() params: TopikParamDto,
@Body() updateDto: UpdateTopikDto,
@Body() updateDto: UpdateTopikDto,
) {
@Req() req: Request,
const res = await this.alokasiTopikService.update(params.id, updateDto);
): Promise<TopikIdRespDto> {
if (!res.affected) throw new NotFoundException();
let idPengaju = undefined;
return res;
const { roles, id } = req.user as AuthDto;
 
// user only has S2_PEMBIMBING role
 
if (!isHighAuthority(roles)) {
 
if (updateDto.idPengaju !== id) {
 
throw new BadRequestException("Pengaju ID harus sama dengan user ID");
 
}
 
idPengaju = id;
 
}
 
 
const res = await this.alokasiTopikService.update(
 
params.id,
 
updateDto,
 
idPengaju,
 
);
 
if (!res.affected)
 
throw new NotFoundException(
 
"Topik tidak ditemukan di antara topik yang dapat Anda akses",
 
);
 
 
const resp: TopikIdRespDto = { id: params.id };
 
 
return resp;
}
}
@Roles(RoleEnum.S2_TIM_TESIS, RoleEnum.ADMIN)
@ApiOperation({
 
summary: "Delete topik. Roles: S2_TIM_TESIS, ADMIN, S2_PEMBIMBING",
 
})
 
@ApiOkResponse({ type: TopikIdRespDto })
 
@Roles(...HIGH_AUTHORITY_ROLES, RoleEnum.S2_PEMBIMBING)
@Delete("/:id")
@Delete("/:id")
async delete(@Param() params: TopikParamDto) {
async delete(
const res = await this.alokasiTopikService.remove(params.id);
@Param() params: TopikParamDto,
if (!res.affected) throw new NotFoundException();
@Req() req: Request,
return res;
): Promise<TopikIdRespDto> {
 
let idPengaju = undefined;
 
const { roles, id } = req.user as AuthDto;
 
// user only has S2_PEMBIMBING role
 
if (!isHighAuthority(roles)) {
 
idPengaju = id;
 
}
 
 
const res = await this.alokasiTopikService.remove(params.id, idPengaju);
 
if (!res.affected)
 
throw new NotFoundException(
 
"Topik tidak ditemukan di antara topik yang dapat Anda akses",
 
);
 
 
const resp: TopikIdRespDto = { id: params.id };
 
return resp;
}
}
}
}