328 lines
12 KiB
TypeScript
328 lines
12 KiB
TypeScript
import { Body, Controller, Example, Get, Patch, Path, Request, Route, Security, Tags } from "tsoa";
|
|
import { AppDataSource } from "../database/data-source";
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
import HttpStatus from "../interfaces/http-status";
|
|
import HttpError from "../interfaces/http-error";
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
import { Profile } from "../entities/Profile";
|
|
import { ProfileGovernment, UpdateProfileGovernment } from "../entities/ProfileGovernment";
|
|
import { Position } from "../entities/Position";
|
|
import { PosMaster } from "../entities/PosMaster";
|
|
import { calculateAge, calculateRetireDate } from "../interfaces/utils";
|
|
import permission from "../interfaces/permission";
|
|
@Route("api/v1/org/profile/government")
|
|
@Tags("ProfileGovernment")
|
|
@Security("bearerAuth")
|
|
export class ProfileGovernmentHistoryController extends Controller {
|
|
private profileRepo = AppDataSource.getRepository(Profile);
|
|
private govRepo = AppDataSource.getRepository(ProfileGovernment);
|
|
private positionRepo = AppDataSource.getRepository(Position);
|
|
private posMasterRepo = AppDataSource.getRepository(PosMaster);
|
|
|
|
/**
|
|
*
|
|
* @summary ข้อมูลราชการ
|
|
*
|
|
*/
|
|
@Get("user")
|
|
public async getGovHistoryUser(@Request() request: { user: Record<string, any> }) {
|
|
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
}
|
|
const record = await this.profileRepo.findOne({
|
|
where: { id: profile.id },
|
|
relations: {
|
|
posType: true,
|
|
posLevel: true,
|
|
},
|
|
});
|
|
const posMaster = await this.posMasterRepo.findOne({
|
|
where: {
|
|
orgRevision: {
|
|
orgRevisionIsCurrent: true,
|
|
orgRevisionIsDraft: false,
|
|
},
|
|
current_holderId: profile.id,
|
|
},
|
|
order: { createdAt: "DESC" },
|
|
relations: {
|
|
orgRoot: true,
|
|
orgChild1: true,
|
|
orgChild2: true,
|
|
orgChild3: true,
|
|
orgChild4: true,
|
|
},
|
|
});
|
|
const position = await this.positionRepo.findOne({
|
|
where: {
|
|
positionIsSelected: true,
|
|
posMaster: {
|
|
orgRevision: {
|
|
orgRevisionIsCurrent: true,
|
|
orgRevisionIsDraft: false,
|
|
},
|
|
current_holderId: profile.id,
|
|
},
|
|
},
|
|
order: { createdAt: "DESC" },
|
|
relations: {
|
|
posExecutive: true,
|
|
},
|
|
});
|
|
|
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
const fullNameParts = [
|
|
posMaster == null || posMaster.orgChild4 == null ? null : posMaster.orgChild4.orgChild4Name,
|
|
posMaster == null || posMaster.orgChild3 == null ? null : posMaster.orgChild3.orgChild3Name,
|
|
posMaster == null || posMaster.orgChild2 == null ? null : posMaster.orgChild2.orgChild2Name,
|
|
posMaster == null || posMaster.orgChild1 == null ? null : posMaster.orgChild1.orgChild1Name,
|
|
posMaster == null || posMaster.orgRoot == null ? null : posMaster.orgRoot.orgRootName,
|
|
];
|
|
const org = fullNameParts.filter((part) => part !== undefined && part !== null).join("/");
|
|
let orgShortName = "";
|
|
if (posMaster != null) {
|
|
if (posMaster.orgChild1Id === null) {
|
|
orgShortName = posMaster.orgRoot?.orgRootShortName;
|
|
} else if (posMaster.orgChild2Id === null) {
|
|
orgShortName = posMaster.orgChild1?.orgChild1ShortName;
|
|
} else if (posMaster.orgChild3Id === null) {
|
|
orgShortName = posMaster.orgChild2?.orgChild2ShortName;
|
|
} else if (posMaster.orgChild4Id === null) {
|
|
orgShortName = posMaster.orgChild3?.orgChild3ShortName;
|
|
} else {
|
|
orgShortName = posMaster.orgChild4?.orgChild4ShortName;
|
|
}
|
|
}
|
|
const data = {
|
|
org: org, //สังกัด
|
|
positionField: position == null ? null : position.positionField, //สายงาน
|
|
position: record.position, //ตำแหน่ง
|
|
posLevel: record.posLevel == null ? null : record.posLevel.posLevelName, //ระดับ
|
|
posMasterNo: posMaster == null ? null : `${orgShortName} ${posMaster.posMasterNo}`, //เลขที่ตำแหน่ง
|
|
posType: record.posType == null ? null : record.posType.posTypeName, //ประเภท
|
|
posExecutive:
|
|
position == null || position.posExecutive == null
|
|
? null
|
|
: position.posExecutive.posExecutiveName, //ตำแหน่งทางการบริหาร
|
|
positionArea: position == null ? null : position.positionArea, //ด้าน/สาขา
|
|
positionExecutiveField: position == null ? null : position.positionExecutiveField, //ด้านทางการบริหาร
|
|
dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate),
|
|
dateRetireLaw: record.dateRetireLaw ?? null,
|
|
govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
|
|
dateAppoint: record.dateAppoint,
|
|
dateStart: record.dateStart,
|
|
govAgeAbsent: record.govAgeAbsent,
|
|
govAgePlus: record.govAgePlus,
|
|
reasonSameDate: record.reasonSameDate,
|
|
};
|
|
|
|
return new HttpSuccess(data);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @summary ข้อมูลราชการ
|
|
*
|
|
*/
|
|
@Get("{profileId}")
|
|
@Example({})
|
|
public async getGovHistory(@Path() profileId: string) {
|
|
const record = await this.profileRepo.findOne({
|
|
where: { id: profileId },
|
|
relations: {
|
|
posType: true,
|
|
posLevel: true,
|
|
},
|
|
});
|
|
const posMaster = await this.posMasterRepo.findOne({
|
|
where: {
|
|
orgRevision: {
|
|
orgRevisionIsCurrent: true,
|
|
orgRevisionIsDraft: false,
|
|
},
|
|
current_holderId: profileId,
|
|
},
|
|
order: { createdAt: "DESC" },
|
|
relations: {
|
|
orgRoot: true,
|
|
orgChild1: true,
|
|
orgChild2: true,
|
|
orgChild3: true,
|
|
orgChild4: true,
|
|
},
|
|
});
|
|
const position = await this.positionRepo.findOne({
|
|
where: {
|
|
positionIsSelected: true,
|
|
posMaster: {
|
|
orgRevision: {
|
|
orgRevisionIsCurrent: true,
|
|
orgRevisionIsDraft: false,
|
|
},
|
|
current_holderId: profileId,
|
|
},
|
|
},
|
|
order: { createdAt: "DESC" },
|
|
relations: {
|
|
posExecutive: true,
|
|
},
|
|
});
|
|
|
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
const fullNameParts = [
|
|
posMaster == null || posMaster.orgChild4 == null ? null : posMaster.orgChild4.orgChild4Name,
|
|
posMaster == null || posMaster.orgChild3 == null ? null : posMaster.orgChild3.orgChild3Name,
|
|
posMaster == null || posMaster.orgChild2 == null ? null : posMaster.orgChild2.orgChild2Name,
|
|
posMaster == null || posMaster.orgChild1 == null ? null : posMaster.orgChild1.orgChild1Name,
|
|
posMaster == null || posMaster.orgRoot == null ? null : posMaster.orgRoot.orgRootName,
|
|
];
|
|
const org = fullNameParts.filter((part) => part !== undefined && part !== null).join("/");
|
|
let orgShortName = "";
|
|
if (posMaster != null) {
|
|
if (posMaster.orgChild1Id === null) {
|
|
orgShortName = posMaster.orgRoot?.orgRootShortName;
|
|
} else if (posMaster.orgChild2Id === null) {
|
|
orgShortName = posMaster.orgChild1?.orgChild1ShortName;
|
|
} else if (posMaster.orgChild3Id === null) {
|
|
orgShortName = posMaster.orgChild2?.orgChild2ShortName;
|
|
} else if (posMaster.orgChild4Id === null) {
|
|
orgShortName = posMaster.orgChild3?.orgChild3ShortName;
|
|
} else {
|
|
orgShortName = posMaster.orgChild4?.orgChild4ShortName;
|
|
}
|
|
}
|
|
const data = {
|
|
org: org, //สังกัด
|
|
positionField: position == null ? null : position.positionField, //สายงาน
|
|
position: record.position, //ตำแหน่ง
|
|
posLevel: record.posLevel == null ? null : record.posLevel.posLevelName, //ระดับ
|
|
posMasterNo: posMaster == null ? null : `${orgShortName} ${posMaster.posMasterNo}`, //เลขที่ตำแหน่ง
|
|
posType: record.posType == null ? null : record.posType.posTypeName, //ประเภท
|
|
posExecutive:
|
|
position == null || position.posExecutive == null
|
|
? null
|
|
: position.posExecutive.posExecutiveName, //ตำแหน่งทางการบริหาร
|
|
positionArea: position == null ? null : position.positionArea, //ด้าน/สาขา
|
|
positionExecutiveField: position == null ? null : position.positionExecutiveField, //ด้านทางการบริหาร
|
|
dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate),
|
|
dateRetireLaw: record.dateRetireLaw ?? null,
|
|
govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
|
|
dateAppoint: record.dateAppoint,
|
|
dateStart: record.dateStart,
|
|
govAgeAbsent: record.govAgeAbsent,
|
|
govAgePlus: record.govAgePlus,
|
|
reasonSameDate: record.reasonSameDate,
|
|
};
|
|
|
|
return new HttpSuccess(data);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @summary ประวัติข้อมูลราชการ by keycloak
|
|
*
|
|
*/
|
|
@Get("history/user")
|
|
public async govHistoryUser(@Request() request: RequestWithUser) {
|
|
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
}
|
|
const record = await this.govRepo.find({
|
|
order: { lastUpdatedAt: "DESC" },
|
|
where: { profileId: profile.id },
|
|
});
|
|
return new HttpSuccess(record);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @summary ประวัติข้อมูลราชการ
|
|
*
|
|
*/
|
|
@Get("history/{profileId}")
|
|
@Example({})
|
|
public async govHistory(@Path() profileId: string) {
|
|
const record = await this.govRepo.find({
|
|
order: { lastUpdatedAt: "DESC" },
|
|
where: { profileId: profileId },
|
|
});
|
|
|
|
// record.pop();
|
|
|
|
return new HttpSuccess(record);
|
|
}
|
|
|
|
// @Post()
|
|
// public async newGov(@Request() req: RequestWithUser, @Body() body: CreateProfileGovernment) {
|
|
// if (!body.profileId) {
|
|
// throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
|
|
// }
|
|
|
|
// const profile = await this.profileRepo.findOneBy({ id: body.profileId });
|
|
|
|
// if (!profile) {
|
|
// throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
// }
|
|
|
|
// const data = new ProfileGovernment();
|
|
|
|
// const meta = {
|
|
// createdUserId: req.user.sub,
|
|
// createdFullName: req.user.name,
|
|
// lastUpdateUserId: req.user.sub,
|
|
// lastUpdateFullName: req.user.name,
|
|
// };
|
|
|
|
// Object.assign(data, { ...body, ...meta });
|
|
|
|
// await this.govRepo.save(data);
|
|
|
|
// return new HttpSuccess();
|
|
// }
|
|
|
|
/**
|
|
*
|
|
* @summary แก้ไขข้อมูลราชการ
|
|
*
|
|
*/
|
|
@Patch("{profileId}")
|
|
public async editGov(
|
|
@Request() req: RequestWithUser,
|
|
@Body() body: UpdateProfileGovernment,
|
|
@Path() profileId: string,
|
|
) {
|
|
await new permission().PermissionUpdate(req, "SYS_REGISTRY_OFFICER");
|
|
const record = await this.profileRepo.findOne({
|
|
where: { id: profileId },
|
|
});
|
|
|
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
const historyData = new ProfileGovernment();
|
|
|
|
Object.assign(historyData, { ...record, ...body, id: undefined });
|
|
Object.assign(record, body);
|
|
record.lastUpdateFullName = req.user.name;
|
|
record.lastUpdateFullName = req.user.name;
|
|
historyData.profileId = profileId;
|
|
historyData.lastUpdateFullName = req.user.name;
|
|
historyData.lastUpdateFullName = req.user.name;
|
|
|
|
await Promise.all([this.profileRepo.save(record), this.govRepo.save(historyData)]);
|
|
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
// @Delete("{profileId}")
|
|
// public async deleteGov(@Path() profileId: string) {
|
|
// const result = await this.govRepo.delete({ profileId: profileId });
|
|
|
|
// if (result.affected == undefined || result.affected <= 0) {
|
|
// throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
// }
|
|
|
|
// return new HttpSuccess();
|
|
// }
|
|
}
|