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

153 lines
5.4 KiB
TypeScript
Raw Normal View History

2024-05-14 17:26:06 +07:00
import {
Body,
Controller,
Delete,
Example,
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";
import Extension from "../interfaces/extension";
import permission from "../interfaces/permission";
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({
where: { profileId: profile.id },
});
return new HttpSuccess(lists);
}
2024-05-14 17:26:06 +07:00
@Get("{profileId}")
public async getChildren(@Path() profileId: string) {
const lists = await this.childrenRepository.find({
where: { profileId: profileId },
});
return new HttpSuccess(lists);
}
@Get("history/{childrenId}")
public async childrenHistory(@Path() childrenId: string) {
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) {
2024-08-13 10:25:58 +07:00
await new permission().PermissionCreate(req, "SYS_REGISTRY_OFFICER");
2024-05-14 17:26:06 +07:00
const profile = await this.profileRepository.findOneBy({ id: body.profileId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const data = new ProfileChildren();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
2024-05-23 16:44:37 +07:00
data.childrenCitizenId = Extension.CheckCitizen(String(data.childrenCitizenId));
2024-05-14 17:26:06 +07:00
await this.childrenRepository.save(data);
2024-05-23 16:44:37 +07:00
if (data) {
2024-05-15 18:02:16 +07:00
const history: ProfileChildrenHistory = Object.assign(new ProfileChildrenHistory(), {
profileChildrenId: data.id,
childrenCareer: data.childrenCareer,
childrenFirstName: data.childrenFirstName,
childrenLastName: data.childrenLastName,
childrenPrefix: data.childrenPrefix,
childrenLive: data.childrenLive,
childrenCitizenId: data.childrenCitizenId,
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
});
await this.childrenHistoryRepository.save(history);
}
2024-05-14 17:26:06 +07:00
return new HttpSuccess();
}
@Patch("{childrenId}")
public async editChildren(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileChildren,
@Path() childrenId: string,
) {
2024-08-13 10:25:58 +07:00
await new permission().PermissionUpdate(req, "SYS_REGISTRY_OFFICER");
2024-05-14 17:26:06 +07:00
const record = await this.childrenRepository.findOneBy({ id: childrenId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileChildrenHistory();
Object.assign(history, { ...record, id: undefined });
Object.assign(record, body);
2024-05-15 18:02:16 +07:00
record.lastUpdateUserId = req.user.sub;
2024-05-14 17:26:06 +07:00
record.lastUpdateFullName = req.user.name;
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;
history.childrenCareer = record.childrenCareer;
history.childrenFirstName = record.childrenFirstName;
history.childrenLastName = record.childrenLastName;
2024-05-23 16:44:37 +07:00
history.childrenPrefix = record.childrenPrefix;
2024-05-15 18:02:16 +07:00
history.childrenLive = record.childrenLive;
history.childrenCitizenId = record.childrenCitizenId;
2024-05-23 16:44:37 +07:00
(history.lastUpdateUserId = req.user.sub), (history.lastUpdateFullName = req.user.name);
2024-05-14 17:26:06 +07:00
await Promise.all([
this.childrenRepository.save(record),
this.childrenHistoryRepository.save(history),
]);
return new HttpSuccess();
}
@Delete("{childrenId}")
2024-08-13 10:25:58 +07:00
public async deleteTraning(@Path() childrenId: string, @Request() req: RequestWithUser) {
await new permission().PermissionDelete(req, "SYS_REGISTRY_OFFICER");
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();
}
}