hrms-api-org/src/controllers/ProfileEditController.ts
2024-07-19 11:08:47 +07:00

190 lines
5.9 KiB
TypeScript

import {
Body,
Controller,
Delete,
Get,
Patch,
Path,
Post,
Query,
Request,
Route,
Security,
Tags,
} from "tsoa";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import { AppDataSource } from "../database/data-source";
import { Profile } from "../entities/Profile";
import { CreateProfileEdit, EditProfileEdit, ProfileEdit } from "../entities/ProfileEdit";
import { RequestWithUser } from "../middlewares/user";
import { IsNull, Not } from "typeorm";
@Route("api/v1/org/profile/edit")
@Tags("ProfileEdit")
@Security("bearerAuth")
export class ProfileEditController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private profileEditRepo = AppDataSource.getRepository(ProfileEdit);
@Get("user")
public async detailProfileEditUser(
@Request() request: { user: Record<string, any> },
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword: string = "",
) {
const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit)
.createQueryBuilder("ProfileEdit")
.leftJoinAndSelect("ProfileEdit.profile", "profile")
.where({
profileId: profile.id,
})
.orderBy("ProfileEdit.createdAt", "ASC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const _data = getProfileEdit.map((item) => ({
id: item.id,
topic: item.topic,
detail: item.detail,
status: item.status,
remark: item.remark,
createdAt: item.createdAt,
createdFullName: item.createdFullName,
lastUpdatedAt: item.lastUpdatedAt,
lastUpdateFullName: item.lastUpdateFullName,
fullname:
(item?.profile?.prefix ?? "") +
"" +
(item?.profile?.firstName ?? "") +
" " +
(item?.profile?.lastName ?? ""),
}));
return new HttpSuccess({ data: _data, total: total });
}
@Get("admin")
public async detailProfileEditAdmin(
@Request() request: { user: Record<string, any> },
@Query("page") page: number = 1,
@Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword: string = "",
) {
let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit)
.createQueryBuilder("ProfileEdit")
.leftJoinAndSelect("ProfileEdit.profile", "profile")
.where({
profileId: Not(IsNull()),
})
.orderBy("ProfileEdit.createdAt", "ASC")
.skip((page - 1) * pageSize)
.take(pageSize)
.getManyAndCount();
const _data = getProfileEdit.map((item) => ({
id: item.id,
topic: item.topic,
detail: item.detail,
status: item.status,
remark: item.remark,
createdAt: item.createdAt,
createdFullName: item.createdFullName,
lastUpdatedAt: item.lastUpdatedAt,
lastUpdateFullName: item.lastUpdateFullName,
fullname:
(item?.profile?.prefix ?? "") +
"" +
(item?.profile?.firstName ?? "") +
" " +
(item?.profile?.lastName ?? ""),
}));
return new HttpSuccess({ data: _data, total: total });
}
@Get("{profileId}")
public async detailProfileEdit(@Path() profileId: string) {
const getProfileEdit = await this.profileEditRepo.findOne({
where: { profileId: profileId },
relations: ["profile"],
});
if (!getProfileEdit) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
const _data = {
id: getProfileEdit.id,
topic: getProfileEdit.topic,
detail: getProfileEdit.detail,
status: getProfileEdit.status,
remark: getProfileEdit.remark,
createdAt: getProfileEdit.createdAt,
createdFullName: getProfileEdit.createdFullName,
lastUpdatedAt: getProfileEdit.lastUpdatedAt,
lastUpdateFullName: getProfileEdit.lastUpdateFullName,
fullname:
(getProfileEdit?.profile?.prefix ?? "") +
"" +
(getProfileEdit?.profile?.firstName ?? "") +
" " +
(getProfileEdit?.profile?.lastName ?? ""),
};
return new HttpSuccess(_data);
}
@Post()
public async newProfileEdit(@Request() req: RequestWithUser, @Body() body: CreateProfileEdit) {
const profile = await this.profileRepo.findOneBy({ keycloak: req.user.sub });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const data = new ProfileEdit();
const meta = {
createdUserId: req.user.sub,
createdFullName: req.user.name,
lastUpdateUserId: req.user.sub,
lastUpdateFullName: req.user.name,
};
Object.assign(data, { ...body, ...meta });
data.status = "PENDING";
await this.profileEditRepo.save(data);
return new HttpSuccess(data.id);
}
@Patch("{editId}")
public async editProfileEdit(
@Body() requestBody: EditProfileEdit,
@Request() req: RequestWithUser,
@Path() editId: string,
) {
const record = await this.profileEditRepo.findOneBy({ id: editId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
Object.assign(record, requestBody);
record.lastUpdateFullName = req.user.name;
record.lastUpdateUserId = req.user.sub;
record.lastUpdatedAt = new Date();
await Promise.all([this.profileEditRepo.save(record)]);
return new HttpSuccess();
}
@Delete("{editId}")
public async deleteProfileEdit(@Path() editId: string) {
const result = await this.profileEditRepo.delete({ id: editId });
if (result.affected == undefined || result.affected <= 0)
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
return new HttpSuccess();
}
}