api รักษาการแทน
This commit is contained in:
parent
f7c553ec1d
commit
9de005c7ae
8 changed files with 752 additions and 69 deletions
241
src/controllers/PosMasterActController.ts
Normal file
241
src/controllers/PosMasterActController.ts
Normal file
|
|
@ -0,0 +1,241 @@
|
|||
import {
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Delete,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
Body,
|
||||
Path,
|
||||
Request,
|
||||
SuccessResponse,
|
||||
Response,
|
||||
} from "tsoa";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpStatusCode from "../interfaces/http-status";
|
||||
import { PosLevel, CreatePosLevel } from "../entities/PosLevel";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import { PosMasterAct } from "../entities/PosMasterAct";
|
||||
import { PosMaster } from "../entities/PosMaster";
|
||||
import { LessThan, MoreThan } from "typeorm";
|
||||
|
||||
@Route("api/v1/org/pos/act")
|
||||
@Tags("PosMasterAct")
|
||||
@Security("bearerAuth")
|
||||
@Response(
|
||||
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
||||
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
||||
)
|
||||
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
||||
export class PosMasterActController extends Controller {
|
||||
private posMasterActRepository = AppDataSource.getRepository(PosMasterAct);
|
||||
private posMasterRepository = AppDataSource.getRepository(PosMaster);
|
||||
|
||||
/**
|
||||
* API เพิ่มรักษาการในตำแหน่ง
|
||||
*
|
||||
* @summary เพิ่มรักษาการในตำแหน่ง (ADMIN)
|
||||
*
|
||||
*/
|
||||
@Post()
|
||||
async createPosMasterAct(
|
||||
@Body() requestBody: { posMasterId: string; posMasterChildId: string },
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const posMaster = await this.posMasterRepository.findOne({
|
||||
where: {
|
||||
id: requestBody.posMasterId,
|
||||
},
|
||||
});
|
||||
if (posMaster == null) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
|
||||
}
|
||||
|
||||
const posMasterChild = await this.posMasterRepository.findOne({
|
||||
where: {
|
||||
id: requestBody.posMasterChildId,
|
||||
},
|
||||
});
|
||||
if (posMasterChild == null) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
|
||||
}
|
||||
|
||||
const posMasterActOld = await this.posMasterActRepository.findOne({
|
||||
where: {
|
||||
posMasterId: requestBody.posMasterId,
|
||||
},
|
||||
order: { posMasterOrder: "DESC" },
|
||||
});
|
||||
let num = 0;
|
||||
if (posMasterActOld != null) {
|
||||
num = posMasterActOld.posMasterOrder;
|
||||
}
|
||||
|
||||
const posMasterAct = new PosMasterAct();
|
||||
posMasterAct.posMasterOrder = num + 1;
|
||||
posMasterAct.posMasterId = requestBody.posMasterId;
|
||||
posMasterAct.posMasterChildId = requestBody.posMasterChildId;
|
||||
posMasterAct.createdUserId = request.user.sub;
|
||||
posMasterAct.createdFullName = request.user.name;
|
||||
posMasterAct.lastUpdateUserId = request.user.sub;
|
||||
posMasterAct.lastUpdateFullName = request.user.name;
|
||||
await this.posMasterActRepository.save(posMasterAct);
|
||||
return new HttpSuccess(posMasterAct);
|
||||
}
|
||||
|
||||
/**
|
||||
* API ลบรักษาการในตำแหน่ง
|
||||
*
|
||||
* @summary ลบรักษาการในตำแหน่ง (ADMIN)
|
||||
*
|
||||
* @param {string} id Id รักษาการในตำแหน่ง
|
||||
*/
|
||||
@Delete("{id}")
|
||||
async deletePosMasterAct(@Path() id: string) {
|
||||
let result: any;
|
||||
const posMasterAct = await this.posMasterActRepository.findOne({
|
||||
where: {
|
||||
id: id,
|
||||
},
|
||||
});
|
||||
try {
|
||||
result = await this.posMasterActRepository.delete({ id: id });
|
||||
} catch {
|
||||
throw new HttpError(
|
||||
HttpStatusCode.NOT_FOUND,
|
||||
"ไม่สามารถลบได้เนื่องจากมีการใช้งานระดับตำแหน่งนี้อยู่",
|
||||
);
|
||||
}
|
||||
if (result.affected == undefined || result.affected <= 0) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
|
||||
if (posMasterAct != null) {
|
||||
const posMasterActList = await this.posMasterActRepository.find({
|
||||
where: {
|
||||
posMasterId: posMasterAct.posMasterId,
|
||||
},
|
||||
});
|
||||
let num = 0;
|
||||
posMasterActList.forEach(async (p) => {
|
||||
p.posMasterOrder = num + 1;
|
||||
await this.posMasterActRepository.save(p);
|
||||
num = num + 1;
|
||||
});
|
||||
}
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API สลับรักษาการในตำแหน่ง
|
||||
*
|
||||
* @summary สลับรักษาการในตำแหน่ง (ADMIN)
|
||||
*
|
||||
*/
|
||||
@Get("swap/{direction}/{posMasterActId}")
|
||||
async swapPosMasterAct(
|
||||
@Path() direction: string,
|
||||
posMasterActId: string,
|
||||
@Request() request: { user: Record<string, any> },
|
||||
) {
|
||||
const source_item = await this.posMasterActRepository.findOne({
|
||||
where: {
|
||||
id: posMasterActId,
|
||||
},
|
||||
});
|
||||
if (source_item == null) throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลตำแหน่งนี้");
|
||||
const sourceOrder = source_item.posMasterOrder;
|
||||
|
||||
if (direction.trim().toUpperCase() == "UP") {
|
||||
const dest_item = await this.posMasterActRepository.findOne({
|
||||
where: { posMasterId: source_item.posMasterId, posMasterOrder: LessThan(sourceOrder) },
|
||||
order: { posMasterOrder: "DESC" },
|
||||
});
|
||||
if (dest_item == null) return new HttpSuccess();
|
||||
var destOrder = dest_item.posMasterOrder;
|
||||
dest_item.posMasterOrder = sourceOrder;
|
||||
source_item.posMasterOrder = destOrder;
|
||||
await Promise.all([
|
||||
this.posMasterActRepository.save(source_item),
|
||||
this.posMasterActRepository.save(dest_item),
|
||||
]);
|
||||
} else {
|
||||
const dest_item = await this.posMasterActRepository.findOne({
|
||||
where: { posMasterId: source_item.posMasterId, posMasterOrder: MoreThan(sourceOrder) },
|
||||
order: { posMasterOrder: "ASC" },
|
||||
});
|
||||
if (dest_item == null) return new HttpSuccess();
|
||||
var destOrder = dest_item.posMasterOrder;
|
||||
dest_item.posMasterOrder = sourceOrder;
|
||||
source_item.posMasterOrder = destOrder;
|
||||
await Promise.all([
|
||||
this.posMasterActRepository.save(source_item),
|
||||
this.posMasterActRepository.save(dest_item),
|
||||
]);
|
||||
}
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* API รายละเอียดรักษาการในตำแหน่ง
|
||||
*
|
||||
* @summary รายละเอียดรักษาการในตำแหน่ง (ADMIN)
|
||||
*
|
||||
* @param {string} id Id รักษาการในตำแหน่ง
|
||||
*/
|
||||
@Get("{id}")
|
||||
async GetPosMasterActDetail(@Path() id: string) {
|
||||
const posMaster = await this.posMasterRepository.findOne({
|
||||
relations: [
|
||||
"posMasterActs",
|
||||
"posMasterActs.posMasterChild",
|
||||
"posMasterActs.posMasterChild.orgRoot",
|
||||
"posMasterActs.posMasterChild.orgChild1",
|
||||
"posMasterActs.posMasterChild.orgChild2",
|
||||
"posMasterActs.posMasterChild.orgChild3",
|
||||
"posMasterActs.posMasterChild.orgChild4",
|
||||
"posMasterActs.posMasterChild.current_holder",
|
||||
"posMasterActs.posMasterChild.current_holder.posLevel",
|
||||
"posMasterActs.posMasterChild.current_holder.posType",
|
||||
],
|
||||
where: { id: id },
|
||||
});
|
||||
if (!posMaster) {
|
||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลประเภทตำแหน่งนี้");
|
||||
}
|
||||
|
||||
const data = await Promise.all(
|
||||
posMaster.posMasterActs
|
||||
.sort((a, b) => a.posMasterOrder - b.posMasterOrder)
|
||||
.map((item) => {
|
||||
const shortName =
|
||||
item.posMasterChild != null && item.posMasterChild.orgChild4 != null
|
||||
? `${item.posMasterChild.orgChild4.orgChild4ShortName}${item.posMasterChild.posMasterNo}`
|
||||
: item.posMasterChild != null && item.posMasterChild?.orgChild3 != null
|
||||
? `${item.posMasterChild.orgChild3.orgChild3ShortName}${item.posMasterChild.posMasterNo}`
|
||||
: item.posMasterChild != null && item.posMasterChild?.orgChild2 != null
|
||||
? `${item.posMasterChild.orgChild2.orgChild2ShortName}${item.posMasterChild.posMasterNo}`
|
||||
: item.posMasterChild != null && item.posMasterChild?.orgChild1 != null
|
||||
? `${item.posMasterChild.orgChild1.orgChild1ShortName}${item.posMasterChild.posMasterNo}`
|
||||
: item.posMasterChild != null && item.posMasterChild?.orgRoot != null
|
||||
? `${item.posMasterChild.orgRoot.orgRootShortName}${item.posMasterChild.posMasterNo}`
|
||||
: null;
|
||||
return {
|
||||
id: item.id,
|
||||
posMasterOrder: item.posMasterOrder,
|
||||
citizenId: item.posMasterChild?.current_holder?.citizenId ?? null,
|
||||
prefix: item.posMasterChild?.current_holder?.prefix ?? null,
|
||||
firstName: item.posMasterChild?.current_holder?.firstName ?? null,
|
||||
lastName: item.posMasterChild?.current_holder?.lastName ?? null,
|
||||
posLevel: item.posMasterChild?.current_holder?.posLevel?.posLevelName ?? null,
|
||||
posType: item.posMasterChild?.current_holder?.posType?.posTypeName ?? null,
|
||||
position: item.posMasterChild?.current_holder?.position ?? null,
|
||||
posNo: shortName,
|
||||
};
|
||||
}),
|
||||
);
|
||||
return new HttpSuccess(data);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue