hrms-api-org/src/controllers/OrgChild2Controller.ts

166 lines
5.9 KiB
TypeScript
Raw Normal View History

2024-01-25 14:35:19 +07:00
import { AppDataSource } from "../database/data-source";
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Path,
Body,
Request,
Example,
} from "tsoa";
import HttpStatusCode from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import { CreateOrgChild2, OrgChild2 } from "../entities/OrgChild2";
2024-01-25 15:45:57 +07:00
import { OrgChild1 } from "../entities/OrgChild1";
import { OrgChild3 } from "../entities/OrgChild3";
2024-01-25 14:35:19 +07:00
@Route("organization")
@Tags("OrgChild2")
@Security("bearerAuth")
export class OrgChild2Controller extends Controller {
2024-01-25 15:45:57 +07:00
private orgChild1Repository = AppDataSource.getRepository(OrgChild1);
2024-01-25 14:35:19 +07:00
private orgChild2Repository = AppDataSource.getRepository(OrgChild2);
2024-01-25 15:45:57 +07:00
private orgChild3Repository = AppDataSource.getRepository(OrgChild3);
2024-01-25 14:35:19 +07:00
/**
* 2 Child2
*
* @summary ORG_007 - 2 (ADMIN) #7
*
*/
2024-01-25 15:45:57 +07:00
@Post("child2")
2024-01-25 14:35:19 +07:00
@Example([
{
orgChild2Name: "string", //ชื่อหน่วยงาน
orgChild2ShortName: "string", //อักษรย่อ
orgChild2Code: "string", //รหัสหน่วยงาน
// orgChild2Order: "number", //ลำดับที่ของหน่วยงาน
2024-01-25 14:35:19 +07:00
orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
orgChild2Fax: "string", //หมายเลขโทรสาร
orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
orgChild1Id: "Guid", //id Child1
},
])
async create(
@Body()
requestBody: CreateOrgChild2,
@Request() request: { user: Record<string, any> },
) {
try {
2024-01-25 15:45:57 +07:00
const orgChild1 = await this.orgChild1Repository.findOne({
2024-01-25 14:35:19 +07:00
where: { id: requestBody.orgChild1Id },
});
const orgChild2 = Object.assign(new OrgChild2(), requestBody);
if (!orgChild2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-01-25 15:45:57 +07:00
if (orgChild1) {
orgChild2.createdUserId = request.user.sub;
orgChild2.createdFullName = request.user.name;
2024-01-26 15:54:07 +07:00
orgChild2.createdAt = new Date();
2024-01-25 15:45:57 +07:00
orgChild2.lastUpdateUserId = request.user.sub;
orgChild2.lastUpdateFullName = request.user.name;
2024-01-26 15:54:07 +07:00
orgChild2.lastUpdatedAt = new Date();
2024-01-25 15:45:57 +07:00
await this.orgChild2Repository.save(orgChild2);
} else {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีโครงสร้างระดับ1");
2024-01-25 14:35:19 +07:00
}
return new HttpSuccess();
} catch (error) {
return error;
}
}
/**
2024-01-25 15:45:57 +07:00
* 2 Child2
2024-01-25 14:35:19 +07:00
*
2024-01-25 15:45:57 +07:00
* @summary ORG_008 - 2 (ADMIN) #8
2024-01-25 14:35:19 +07:00
*
* @param {string} id Guid, *Id Child2
*/
2024-01-25 15:45:57 +07:00
@Put("child2/{id}")
@Example([
{
orgChild2Name: "string", //ชื่อหน่วยงาน
orgChild2ShortName: "string", //อักษรย่อ
orgChild2Code: "string", //รหัสหน่วยงาน
// orgChild2Order: "number", //ลำดับที่ของหน่วยงาน
2024-01-25 15:45:57 +07:00
orgChild2PhoneEx: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายนอก
orgChild2PhoneIn: "string", //หมายเลขโทรศัพท์ที่ติดต่อจากภายใน
orgChild2Fax: "string", //หมายเลขโทรสาร
orgChild2IsNormal: "boolean", //สถานะของหน่วยงาน
orgChild1Id: "Guid", //id Child1
},
])
async update(
@Path() id: string,
@Body()
requestBody: CreateOrgChild2,
@Request() request: { user: Record<string, any> },
) {
try {
const orgChild1 = await this.orgChild1Repository.findOne({
where: { id: requestBody.orgChild1Id },
});
const orgChild2 = await this.orgChild2Repository.findOne({ where: { id } });
if (!orgChild2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-01-25 15:45:57 +07:00
if (orgChild1) {
orgChild2.lastUpdateUserId = request.user.sub;
orgChild2.lastUpdateFullName = request.user.name;
2024-01-26 15:54:07 +07:00
orgChild2.lastUpdatedAt = new Date();
2024-01-25 15:45:57 +07:00
await this.orgChild2Repository.save(orgChild2);
} else {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดีโครงสร้างระดับ1");
}
return new HttpSuccess();
} catch (error) {
return error;
}
}
2024-01-25 14:35:19 +07:00
/**
* Child2
*
2024-01-25 15:45:57 +07:00
* @summary ORG_009 - 2 (ADMIN) #9
2024-01-25 14:35:19 +07:00
*
* @param {string} id Guid, *Id Child2
*/
2024-01-25 15:45:57 +07:00
@Delete("Child2/{id}")
async delete(@Path() id: string) {
try {
const orgChild2 = await this.orgChild2Repository.findOne({ where: { id } });
2024-01-26 12:00:43 +07:00
const orgChild3 = await this.orgChild3Repository.findOne({ where: { orgChild2Id: id } });
2024-01-25 14:35:19 +07:00
2024-01-25 15:45:57 +07:00
if (!orgChild2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
2024-01-25 14:35:19 +07:00
2024-01-25 15:45:57 +07:00
if (!orgChild3) {
await this.orgChild2Repository.remove(orgChild2);
} else {
throw new HttpError(
HttpStatusCode.NOT_FOUND,
"ไม่สามารถลบข้อมูลได้เมื่อมีข้อมูลโครงสร้างระดับ3",
);
2024-01-25 15:45:57 +07:00
}
2024-01-25 14:35:19 +07:00
2024-01-25 15:45:57 +07:00
return new HttpSuccess();
} catch (error) {
return error;
}
}
2024-01-25 14:35:19 +07:00
}