2024-05-14 17:26:06 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Patch,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
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 { ProfileChildrenHistory } from "../entities/ProfileChildrenHistory";
|
|
|
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
|
|
|
import { Profile } from "../entities/Profile";
|
|
|
|
|
import {
|
|
|
|
|
CreateProfileChildren,
|
|
|
|
|
ProfileChildren,
|
|
|
|
|
UpdateProfileChildren,
|
|
|
|
|
} from "../entities/ProfileChildren";
|
2024-05-17 12:54:18 +07:00
|
|
|
import Extension from "../interfaces/extension";
|
2024-08-08 17:15:21 +07:00
|
|
|
import permission from "../interfaces/permission";
|
2024-10-03 15:57:44 +07:00
|
|
|
import { setLogDataDiff } from "../interfaces/utils";
|
2024-05-15 18:02:16 +07:00
|
|
|
@Route("api/v1/org/profile/family/children")
|
2024-05-14 17:26:06 +07:00
|
|
|
@Tags("ProfileChildren")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
export class ProfileChildrenController extends Controller {
|
|
|
|
|
private profileRepository = AppDataSource.getRepository(Profile);
|
|
|
|
|
private childrenRepository = AppDataSource.getRepository(ProfileChildren);
|
|
|
|
|
private childrenHistoryRepository = AppDataSource.getRepository(ProfileChildrenHistory);
|
|
|
|
|
|
2024-05-23 16:44:37 +07:00
|
|
|
@Get("user")
|
|
|
|
|
public async getChildrenUser(@Request() request: { user: Record<string, any> }) {
|
|
|
|
|
const profile = await this.profileRepository.findOneBy({ keycloak: request.user.sub });
|
|
|
|
|
if (!profile) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
|
|
|
}
|
|
|
|
|
const lists = await this.childrenRepository.find({
|
2026-02-12 11:47:53 +07:00
|
|
|
where: { profileId: profile.id, isDeleted: false },
|
2024-08-30 18:02:34 +07:00
|
|
|
order: { createdAt: "ASC" },
|
2024-05-23 16:44:37 +07:00
|
|
|
});
|
|
|
|
|
return new HttpSuccess(lists);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-14 17:26:06 +07:00
|
|
|
@Get("{profileId}")
|
2024-08-22 17:25:25 +07:00
|
|
|
public async getChildren(@Path() profileId: string, @Request() req: RequestWithUser) {
|
2024-10-22 08:20:45 +07:00
|
|
|
let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_OFFICER");
|
|
|
|
|
if (_workflow == false)
|
|
|
|
|
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
|
2024-05-14 17:26:06 +07:00
|
|
|
const lists = await this.childrenRepository.find({
|
2026-02-12 11:47:53 +07:00
|
|
|
where: { profileId: profileId, isDeleted: false },
|
2024-08-30 18:02:34 +07:00
|
|
|
order: { createdAt: "ASC" },
|
2024-05-14 17:26:06 +07:00
|
|
|
});
|
|
|
|
|
return new HttpSuccess(lists);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("history/{childrenId}")
|
2024-08-22 17:25:25 +07:00
|
|
|
public async childrenHistory(@Path() childrenId: string, @Request() req: RequestWithUser) {
|
|
|
|
|
const _record = await this.childrenRepository.findOneBy({ id: childrenId });
|
|
|
|
|
if (_record) {
|
2024-10-22 08:20:45 +07:00
|
|
|
let _workflow = await new permission().Workflow(req, childrenId, "SYS_REGISTRY_OFFICER");
|
|
|
|
|
if (_workflow == false)
|
|
|
|
|
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", _record.profileId);
|
2024-08-22 17:25:25 +07:00
|
|
|
}
|
2024-05-14 17:26:06 +07:00
|
|
|
const record = await this.childrenHistoryRepository.find({
|
|
|
|
|
where: { profileChildrenId: childrenId },
|
|
|
|
|
order: { createdAt: "DESC" },
|
|
|
|
|
});
|
|
|
|
|
return new HttpSuccess(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
|
|
|
|
public async newChildren(@Request() req: RequestWithUser, @Body() body: CreateProfileChildren) {
|
|
|
|
|
const profile = await this.profileRepository.findOneBy({ id: body.profileId });
|
|
|
|
|
if (!profile) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
|
|
|
}
|
2024-08-27 12:11:31 +07:00
|
|
|
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id);
|
2024-10-03 15:57:44 +07:00
|
|
|
const before = null;
|
2024-05-14 17:26:06 +07:00
|
|
|
const data = new ProfileChildren();
|
|
|
|
|
const meta = {
|
|
|
|
|
createdUserId: req.user.sub,
|
|
|
|
|
createdFullName: req.user.name,
|
|
|
|
|
lastUpdateUserId: req.user.sub,
|
|
|
|
|
lastUpdateFullName: req.user.name,
|
2024-08-30 18:02:34 +07:00
|
|
|
createdAt: new Date(),
|
|
|
|
|
lastUpdatedAt: new Date(),
|
2024-05-14 17:26:06 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Object.assign(data, { ...body, ...meta });
|
2024-05-23 16:44:37 +07:00
|
|
|
data.childrenCitizenId = Extension.CheckCitizen(String(data.childrenCitizenId));
|
2024-08-13 17:43:30 +07:00
|
|
|
|
|
|
|
|
const history = new ProfileChildrenHistory();
|
2024-08-13 20:01:53 +07:00
|
|
|
Object.assign(history, { ...data, id: undefined });
|
2024-08-13 17:43:30 +07:00
|
|
|
|
2024-10-03 15:57:44 +07:00
|
|
|
await this.childrenRepository.save(data, { data: req });
|
|
|
|
|
setLogDataDiff(req, { before, after: data });
|
2024-08-13 20:01:53 +07:00
|
|
|
history.profileChildrenId = data.id;
|
2024-10-03 15:57:44 +07:00
|
|
|
await this.childrenHistoryRepository.save(history, { data: req });
|
|
|
|
|
//setLogDataDiff(req, { before, after: history });
|
2024-08-13 17:43:30 +07:00
|
|
|
|
2025-01-29 10:15:19 +07:00
|
|
|
return new HttpSuccess(data.id);
|
2024-05-14 17:26:06 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch("{childrenId}")
|
|
|
|
|
public async editChildren(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body() body: UpdateProfileChildren,
|
|
|
|
|
@Path() childrenId: string,
|
|
|
|
|
) {
|
|
|
|
|
const record = await this.childrenRepository.findOneBy({ id: childrenId });
|
|
|
|
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
2024-08-22 17:25:25 +07:00
|
|
|
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId);
|
2024-10-03 15:57:44 +07:00
|
|
|
const before = structuredClone(record);
|
|
|
|
|
// const before_null = null;
|
2024-05-14 17:26:06 +07:00
|
|
|
const history = new ProfileChildrenHistory();
|
|
|
|
|
Object.assign(record, body);
|
2024-09-01 22:44:23 +07:00
|
|
|
Object.assign(history, { ...record, id: undefined });
|
2024-08-13 17:43:30 +07:00
|
|
|
|
2024-05-23 16:44:37 +07:00
|
|
|
record.childrenCitizenId = Extension.CheckCitizen(String(record.childrenCitizenId));
|
2024-05-15 18:02:16 +07:00
|
|
|
history.profileChildrenId = record.id;
|
2024-08-13 17:43:30 +07:00
|
|
|
record.lastUpdateUserId = req.user.sub;
|
|
|
|
|
record.lastUpdateFullName = req.user.name;
|
2024-08-30 18:02:34 +07:00
|
|
|
record.lastUpdatedAt = new Date();
|
2024-08-13 17:43:30 +07:00
|
|
|
history.lastUpdateUserId = req.user.sub;
|
|
|
|
|
history.lastUpdateFullName = req.user.name;
|
|
|
|
|
history.createdUserId = req.user.sub;
|
|
|
|
|
history.createdFullName = req.user.name;
|
2024-08-30 18:02:34 +07:00
|
|
|
history.createdAt = new Date();
|
|
|
|
|
history.lastUpdatedAt = new Date();
|
2024-05-14 17:26:06 +07:00
|
|
|
await Promise.all([
|
2024-10-03 15:57:44 +07:00
|
|
|
this.childrenRepository.save(record, { data: req }),
|
|
|
|
|
setLogDataDiff(req, { before, after: record }),
|
|
|
|
|
this.childrenHistoryRepository.save(history, { data: req }),
|
|
|
|
|
// setLogDataDiff(req, { before: before_null, after: history }),
|
2024-05-14 17:26:06 +07:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("{childrenId}")
|
2024-08-13 10:25:58 +07:00
|
|
|
public async deleteTraning(@Path() childrenId: string, @Request() req: RequestWithUser) {
|
2024-08-22 17:25:25 +07:00
|
|
|
const _record = await this.childrenRepository.findOneBy({ id: childrenId });
|
|
|
|
|
if (_record) {
|
2024-08-30 18:02:34 +07:00
|
|
|
await new permission().PermissionOrgUserDelete(
|
|
|
|
|
req,
|
|
|
|
|
"SYS_REGISTRY_OFFICER",
|
|
|
|
|
_record.profileId,
|
|
|
|
|
);
|
2024-08-22 17:25:25 +07:00
|
|
|
}
|
2024-05-14 17:26:06 +07:00
|
|
|
await this.childrenHistoryRepository.delete({
|
|
|
|
|
profileChildrenId: childrenId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await this.childrenRepository.delete({ id: childrenId });
|
|
|
|
|
|
2024-05-15 18:02:16 +07:00
|
|
|
if (result.affected == undefined || result.affected <= 0) {
|
2024-05-14 17:26:06 +07:00
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
}
|