fix: typo and handle delete

This commit is contained in:
Methapon Metanipat 2024-09-17 12:46:19 +07:00
parent a0168ee4fb
commit ae7392dc18
4 changed files with 43 additions and 31 deletions

View file

@ -17,6 +17,7 @@ import HttpStatus from "../interfaces/http-status";
import { connectOrDisconnect, connectOrNot } from "../utils/relation";
import { notFoundError, relationError } from "../utils/error";
import { permissionCheck } from "../middlewares/customer-branch";
import { deleteFile, fileLocation } from "../utils/minio";
const MANAGE_ROLES = [
"system",
@ -154,6 +155,8 @@ export class CustomerBranchCitizenController extends Controller {
if (!record) throw notFoundError("Citizen");
await deleteFile(fileLocation.customerBranch.citizen(branchId, citizenId));
return await prisma.customerBranchCitizen.delete({
where: { id: citizenId, customerBranchId: branchId },
});

View file

@ -16,6 +16,7 @@ import prisma from "../db";
import HttpStatus from "../interfaces/http-status";
import { permissionCheck } from "../middlewares/employee";
import { notFoundError } from "../utils/error";
import { deleteFile, fileLocation } from "../utils/minio";
const MANAGE_ROLES = [
"system",
@ -54,13 +55,13 @@ export class EmployeeInCountryNoticeController extends Controller {
});
}
@Get("{passportId}")
@Get("{noticeId}")
@Security("keycloak")
async getById(@Path() employeeId: string, @Path() passportId: string) {
async getById(@Path() employeeId: string, @Path() noticeId: string) {
const record = await prisma.employeeInCountryNotice.findFirst({
where: { id: passportId, employeeId },
where: { id: noticeId, employeeId },
});
if (!record) throw notFoundError("Employee Work");
if (!record) throw notFoundError("Notice");
return record;
}
@ -79,21 +80,21 @@ export class EmployeeInCountryNoticeController extends Controller {
return record;
}
@Put("{passportId}")
@Put("{noticeId}")
@Security("keycloak", MANAGE_ROLES)
async editById(
@Path() employeeId: string,
@Path() passportId: string,
@Path() noticeId: string,
@Body() body: EmployeeInCountryNoticePayload,
) {
const work = await prisma.employeeInCountryNotice.findUnique({
where: { id: passportId, employeeId },
where: { id: noticeId, employeeId },
});
if (!work) throw notFoundError("Employee Work");
if (!work) throw notFoundError("Notice");
const record = await prisma.employeeInCountryNotice.update({
where: { id: passportId, employeeId },
where: { id: noticeId, employeeId },
data: { ...body },
});
@ -102,15 +103,17 @@ export class EmployeeInCountryNoticeController extends Controller {
return record;
}
@Delete("{passportId}")
@Delete("{noticeId}")
@Security("keycloak", MANAGE_ROLES)
async deleteById(@Path() employeeId: string, @Path() passportId: string) {
async deleteById(@Path() employeeId: string, @Path() noticeId: string) {
const record = await prisma.employeeInCountryNotice.findFirst({
where: { id: passportId, employeeId },
where: { id: noticeId, employeeId },
});
if (!record) throw notFoundError("Employee Work");
if (!record) throw notFoundError("Notice");
return await prisma.employeeInCountryNotice.delete({ where: { id: passportId, employeeId } });
await deleteFile(fileLocation.employee.inCountryNotice(employeeId, noticeId));
return await prisma.employeeInCountryNotice.delete({ where: { id: noticeId, employeeId } });
}
}

View file

@ -16,6 +16,7 @@ import prisma from "../db";
import HttpStatus from "../interfaces/http-status";
import { permissionCheck } from "../middlewares/employee";
import { notFoundError } from "../utils/error";
import { deleteFile, fileLocation } from "../utils/minio";
const MANAGE_ROLES = [
"system",
@ -60,7 +61,7 @@ export class EmployeePassportController extends Controller {
const record = await prisma.employeePassport.findFirst({
where: { id: passportId, employeeId },
});
if (!record) throw notFoundError("Employee Work");
if (!record) throw notFoundError("Passport");
return record;
}
@ -90,7 +91,7 @@ export class EmployeePassportController extends Controller {
where: { id: passportId, employeeId },
});
if (!work) throw notFoundError("Employee Work");
if (!work) throw notFoundError("Passport");
const record = await prisma.employeePassport.update({
where: { id: passportId, employeeId },
@ -109,7 +110,9 @@ export class EmployeePassportController extends Controller {
where: { id: passportId, employeeId },
});
if (!record) throw notFoundError("Employee Work");
if (!record) throw notFoundError("Passport");
await deleteFile(fileLocation.employee.passport(employeeId, passportId));
return await prisma.employeePassport.delete({ where: { id: passportId, employeeId } });
}

View file

@ -16,6 +16,7 @@ import prisma from "../db";
import HttpStatus from "../interfaces/http-status";
import { permissionCheck } from "../middlewares/employee";
import { notFoundError } from "../utils/error";
import { deleteFile, fileLocation } from "../utils/minio";
const MANAGE_ROLES = [
"system",
@ -56,13 +57,13 @@ export class EmployeeVisaController extends Controller {
});
}
@Get("{passportId}")
@Get("{visaId}")
@Security("keycloak")
async getById(@Path() employeeId: string, @Path() passportId: string) {
async getById(@Path() employeeId: string, @Path() visaId: string) {
const record = await prisma.employeeVisa.findFirst({
where: { id: passportId, employeeId },
where: { id: visaId, employeeId },
});
if (!record) throw notFoundError("Employee Work");
if (!record) throw notFoundError("Visa");
return record;
}
@ -81,21 +82,21 @@ export class EmployeeVisaController extends Controller {
return record;
}
@Put("{passportId}")
@Put("{visaId}")
@Security("keycloak", MANAGE_ROLES)
async editById(
@Path() employeeId: string,
@Path() passportId: string,
@Path() visaId: string,
@Body() body: EmployeeVisaPayload,
) {
const work = await prisma.employeeVisa.findUnique({
where: { id: passportId, employeeId },
where: { id: visaId, employeeId },
});
if (!work) throw notFoundError("Employee Work");
if (!work) throw notFoundError("Visa");
const record = await prisma.employeeVisa.update({
where: { id: passportId, employeeId },
where: { id: visaId, employeeId },
data: { ...body },
});
@ -104,15 +105,17 @@ export class EmployeeVisaController extends Controller {
return record;
}
@Delete("{passportId}")
@Delete("{visaId}")
@Security("keycloak", MANAGE_ROLES)
async deleteById(@Path() employeeId: string, @Path() passportId: string) {
async deleteById(@Path() employeeId: string, @Path() visaId: string) {
const record = await prisma.employeeVisa.findFirst({
where: { id: passportId, employeeId },
where: { id: visaId, employeeId },
});
if (!record) throw notFoundError("Employee Work");
if (!record) throw notFoundError("Visa");
return await prisma.employeeVisa.delete({ where: { id: passportId, employeeId } });
await deleteFile(fileLocation.employee.visa(employeeId, visaId));
return await prisma.employeeVisa.delete({ where: { id: visaId, employeeId } });
}
}