Merge branch 'develop' into adiDev

This commit is contained in:
AdisakKanthawilang 2024-02-15 17:20:27 +07:00
commit 8c4d2e4d1a
5 changed files with 649 additions and 4 deletions

View file

@ -0,0 +1,107 @@
import {
Controller,
Get,
Post,
Put,
Delete,
Patch,
Route,
Security,
Tags,
Body,
Path,
Request,
Example,
} from "tsoa";
import { Salarys, CreateSalary, UpdateSalary } from "../entities/Salarys";
import { PosType } from "../entities/PosType";
import { PosLevel } from "../entities/PosLevel";
import { AppDataSource } from "../database/data-source";
import { In, IsNull, Not } from "typeorm";
import HttpSuccess from "../interfaces/http-success";
import HttpError from "../interfaces/http-error";
import HttpStatusCode from "../interfaces/http-status";
@Route("api/v1/salary")
@Tags("Salary")
@Security("bearerAuth")
export class Salary extends Controller {
private salaryRepository = AppDataSource.getRepository(Salarys);
private poTypeRepository = AppDataSource.getRepository(PosType);
private posLevelRepository = AppDataSource.getRepository(PosLevel);
/**
* API
*
* @summary SLR_001 - #1
*
*/
@Post()
@Example(
{
salaryType: "string", //*ประเภทผัง (OFFICER->"ข้าราชการกรุงเทพมหานครสามัญ",EMPLOYEE->"ลูกจ้างประจำกรุงเทพมหานคร")
posType: "string", //*ระดับของตำแหน่ง
posLevel: "string", //*ประเภทของตำแหน่ง
isActive: "boolean", //*สถานะการใช้งาน
date: "datetime", //ให้ไว้ ณ วันที่
startDate: "datetime", //วันที่มีผลบังคับใช้
endDate: "datetime", //วันที่สิ้นสุดบังคับใช้
detail: "string", //คำอธิบาย
}
)
async create(
@Body() requestBody: CreateSalary,
@Request() request: { user: Record<string, any> },
){
const salarys = Object.assign(new Salarys(), requestBody);
if (!salarys) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
}
const chk_salaryType = ["OFFICER", "EMPLOYEE"];
if (!chk_salaryType.includes(salarys.salaryType.toUpperCase())) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทผัง ไม่ถูกต้อง");
}
const chk_posTypeId = await this.poTypeRepository.findOne({
where: { id: salarys.posTypeId }
})
if(!chk_posTypeId){
throw new HttpError(HttpStatusCode.NOT_FOUND, "ประเภทของตำแหน่ง ไม่ถูกต้อง");
}
const chk_posLevelId = await this.posLevelRepository.findOne({
where: { id: salarys.posLevelId }
});
if(!chk_posLevelId){
throw new HttpError(HttpStatusCode.NOT_FOUND, "ระดับของตำแหน่ง ไม่ถูกต้อง");
}
const chk_3fields = await this.salaryRepository.findOne({
where: {
salaryType: salarys.salaryType,
posTypeId: salarys.posTypeId,
posLevelId: salarys.posLevelId
}
})
if(chk_3fields && salarys.isActive){
salarys.isActive = false;
}
try {
salarys.salaryType = salarys.salaryType.toUpperCase();
salarys.createdUserId = request.user.sub;
salarys.createdFullName = request.user.name;
salarys.lastUpdateUserId = request.user.sub;
salarys.lastUpdateFullName = request.user.name;
await this.salaryRepository.save(salarys);
return new HttpSuccess(salarys.id);
} catch (error) {
return error;
}
}
}

View file

@ -0,0 +1,280 @@
import {
Body,
Controller,
Delete,
Example,
Get,
Patch,
Path,
Post,
Route,
Security,
SuccessResponse,
Tags,
} from "tsoa";
import HttpStatus from "../interfaces/http-status";
import {
createFile,
createFolder,
deleteFile,
deleteFolder,
downloadFile,
listFile,
updateFile,
} from "../services/edm";
@Route("api/v1/salary/file")
@Security("bearerAuth")
@Tags("Document")
export class DocumentController extends Controller {
/**
* @example id "00000000-0000-0000-0000-000000000000"
*
* @summary SLR_015 -
*/
@Get("{id}")
@SuccessResponse(200, "สำเร็จ")
@Example([
{
pathname: "ระบบเงินเดือน/00000000-0000-0000-0000-000000000000/เอกสาร 1.docx",
path: "ระบบเงินเดือน/00000000-0000-0000-0000-000000000000",
title: "เอกสาร 1",
description: "",
author: "นายก",
metadata: { tag: 1 },
keyword: [],
category: [],
fileName: "เอกสาร 1.docx",
fileType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
fileSize: 1024,
upload: true,
createdAt: "2021-07-20T12:33:13.018Z",
createdBy: "service-account-ext-api",
updatedAt: "2021-07-20T12:33:13.018Z",
updatedBy: "service-account-ext-api",
},
])
public async getFile(@Path() id: string) {
const list = await listFile(["ระบบเงินเดือน", "เอกสารอ้างอิงผังเงินเดือน", id]);
if (!list) throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
return list;
}
/**
* ข้อควรระวัง: หากลิงก์ยาวเกินไปอาจทำให้ไม่สามารถอัปโหลดได้
*
* @example id "00000000-0000-0000-0000-000000000000"
* @example file "เอกสาร 1.docx"
*
* @summary
*/
@Get("/{id}/{file}")
@SuccessResponse(200, "สำเร็จ")
@Example({
pathname: "ระบบเงินเดือน/00000000-0000-0000-0000-000000000000/เอกสาร 1.docx",
path: "ระบบเงินเดือน/00000000-0000-0000-0000-000000000000",
title: "เอกสาร 1",
description: "",
author: "นายก",
keyword: [],
category: [],
metadata: { tag: 1 },
fileName: "เอกสาร 1.docx",
fileType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
fileSize: 1024,
hidden: false,
upload: true,
createdAt: "2021-07-20T12:33:13.018Z",
createdBy: "service-account-ext-api",
updatedAt: "2021-07-20T12:33:13.018Z",
updatedBy: "service-account-ext-api",
downloadUrl: "https://.../...", // Presigned Download URL 7 Days Exp
})
public async getFileDownload(@Path() id: string, @Path() file: string) {
const data = await downloadFile(["ระบบเงินเดือน", "เอกสารอ้างอิงผังเงินเดือน", id], file);
if (!data) throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์");
return data;
}
/**
* ข้อควรระวัง: หากลิงก์ภาษาไทยยาวเกินไปอาจทำให้ไม่สามารถอัปโหลดได้ ( DNS)
*
* PUT Method Content-Type
* Content-Type mime-type docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
* Content-Type
*
* Browser Object File attribute type mime-type
*
* title EDM title
*
* @example id "00000000-0000-0000-0000-000000000000"
*
* @summary SLR_013 -
*/
@Post("{id}")
@Example([
{
pathname: "ระบบเงินเดือน/00000000-0000-0000-0000-000000000000/เอกสาร 1.docx",
path: "ระบบเงินเดือน/00000000-0000-0000-0000-000000000000",
title: "เอกสาร 1",
description: "",
author: "นายก",
keyword: [],
category: [],
metadata: {
tag: 1,
},
fileName: "เอกสาร 1.docx",
fileSize: 0,
fileType: "",
hidden: false,
upload: false,
createdAt: "2021-07-20T12:33:13.018Z",
createdBy: "service-account-ext-api",
updatedAt: "2021-07-20T12:33:13.018Z",
updatedBy: "service-account-ext-api",
uploadUrl: "https://.../...", // Presigned Upload URL 7 Days Exp
},
])
@SuccessResponse(200, "Success")
public async uploadFile(
@Path() id: string,
@Body()
body: {
/**
* @example [
* { "fileName": "เอกสาร 1.docx" },
* { "fileName": "เอกสาร 2.docx", "title": "เอกสาร 2" }
* ]
*/
fileList: {
fileName: string;
title?: string;
description?: string;
keyword?: string[];
category?: string[];
author?: string;
metadata?: {
[key: string]: unknown;
};
}[];
/**
* @example false
*/
replace?: boolean;
},
) {
const path = ["ระบบเงินเดือน", "เอกสารอ้างอิงผังเงินเดือน", id];
if (!(await createFolder(path, id, true))) {
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถสร้างแฟ้มได้");
}
const list = await listFile(path);
if (!list || !Array.isArray(list)) {
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถสร้างแฟ้มได้");
}
let used: string[] = [];
let fileList = !body.replace
? body.fileList.map(({ fileName, ...props }) => {
const dotIndex = fileName.lastIndexOf(".");
const originalName =
dotIndex !== -1 && !fileName.startsWith(".") ? fileName.slice(0, dotIndex) : fileName;
const extension =
dotIndex !== -1 && !fileName.startsWith(".") ? fileName.slice(dotIndex) : "";
let i = 1;
while (list.findIndex((v) => v.fileName === fileName) !== -1 || used.includes(fileName)) {
fileName = `${originalName} (${i++})`;
if (dotIndex !== -1) fileName += extension;
}
props.author = "ไม่พบข้อมูล";
used.push(fileName);
return { fileName: fileName, ...props };
})
: body.fileList;
const map = fileList.map(async ({ fileName, ...props }) => [
fileName,
await createFile(path, fileName, props),
]);
const result = await Promise.all(map).catch((e) =>
console.error(`Storage Service Error: ${e}`),
);
if (!result || result.some((v) => !v[1])) {
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถอัปโหลดไฟล์ได้");
}
return Object.fromEntries(result);
}
/**
* @example id "00000000-0000-0000-0000-000000000000"
* @example file "เอกสาร 1.docx"
*
* @summary id
*/
@Patch("{id}/{file}")
public async updateFile(
@Path() id: string,
@Path() file: string,
@Body()
body: {
title?: string;
description?: string;
keyword?: string[];
category?: string[];
author?: string;
metadata?: { [key: string]: unknown };
},
) {
const props = body;
const result = await updateFile(
["ระบบเงินเดือน", "เอกสารอ้างอิงผังเงินเดือน", id],
file,
props,
);
if (!result) {
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถแก้ไขไฟล์ได้");
}
return this.setStatus(HttpStatus.NO_CONTENT);
}
/**
* @example id "00000000-0000-0000-0000-000000000000"
*
* @summary id
*/
@Delete("{id}")
public async deleteFolder(@Path() id: string) {
const result = await deleteFolder(["ระบบเงินเดือน", "เอกสารอ้างอิงผังเงินเดือน"], id);
if (!result) {
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถลบแฟ้มได้");
}
return this.setStatus(HttpStatus.NO_CONTENT);
}
/**
* @example id "00000000-0000-0000-0000-000000000000"
* @example file "เอกสาร 1.docx"
*
* @summary SLR_014 -
*/
@Delete("{id}/{file}")
public async deleteFile(@Path() id: string, @Path() file: string) {
const result = await deleteFile(["ระบบเงินเดือน", "เอกสารอ้างอิงผังเงินเดือน", id], file);
if (!result) {
throw new Error("เกิดข้อผิดพลาดกับระบบจัดการไฟล์ ไม่สามารถลบไฟล์ ได้");
}
return this.setStatus(HttpStatus.NO_CONTENT);
}
}