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

158 lines
4.6 KiB
TypeScript

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 { ProfileNopaidHistory } from "../entities/ProfileNopaidHistory";
import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import { CreateProfileNopaid, ProfileNopaid, UpdateProfileNopaid } from "../entities/ProfileNopaid";
@Route("api/v1/org/profile/nopaid")
@Tags("ProfileNopaid")
@Security("bearerAuth")
export class ProfileNopaidController extends Controller {
private profileRepository = AppDataSource.getRepository(Profile);
private nopaidRepository = AppDataSource.getRepository(ProfileNopaid);
private nopaidHistoryRepository = AppDataSource.getRepository(ProfileNopaidHistory);
@Get("{profileId}")
@Example({
status: 200,
message: "สำเร็จ",
result: [
{
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
isActive: true,
date: "2024-03-12T10:09:47.000Z",
reference: "string",
detail: "string",
refCommandNo: "string",
refCommandDate: "2024-03-12T10:09:47.000Z",
},
],
})
public async getNopaid(@Path() profileId: string) {
const lists = await this.nopaidRepository.find({
where: { profileId: profileId },
select: ["id", "isActive", "date", "reference", "detail", "refCommandNo", "refCommandDate"],
});
return new HttpSuccess(lists);
}
@Get("history/{nopaidId}")
@Example({
status: 200,
message: "สำเร็จ",
result: [
{
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
isActive: true,
date: "2024-03-12T10:09:47.000Z",
reference: "string",
detail: "string",
refCommandNo: "string",
refCommandDate: "2024-03-12T10:09:47.000Z",
},
{
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
isActive: true,
date: "2024-03-12T10:09:47.000Z",
reference: "string",
detail: "string",
refCommandNo: "string",
refCommandDate: "2024-03-12T10:09:47.000Z",
},
],
})
public async nopaidHistory(@Path() nopaidId: string) {
const record = await this.nopaidHistoryRepository.find({
where: { profileNopaidId: nopaidId },
select: ["id", "isActive", "date", "reference", "detail", "refCommandNo", "refCommandDate"],
order: { createdAt: "DESC" },
});
return new HttpSuccess(record);
}
@Post()
public async newNopaid(@Request() req: RequestWithUser, @Body() body: CreateProfileNopaid) {
if (!body.profileId) {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
}
const profile = await this.profileRepository.findOneBy({ id: body.profileId });
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const data = new ProfileNopaid();
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.nopaidRepository.save(data);
return new HttpSuccess();
}
@Patch("{nopaidId}")
public async editNopaid(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileNopaid,
@Path() nopaidId: string,
) {
const record = await this.nopaidRepository.findOneBy({ id: nopaidId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileNopaidHistory();
Object.assign(history, { ...record, id: undefined });
Object.assign(record, body);
history.profileNopaidId = nopaidId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([
this.nopaidRepository.save(record),
this.nopaidHistoryRepository.save(history),
]);
return new HttpSuccess();
}
@Delete("{nopaidId}")
public async deleteTraning(@Path() nopaidId: string) {
await this.nopaidHistoryRepository.delete({
profileNopaidId: nopaidId,
});
const result = await this.nopaidRepository.delete({ id: nopaidId });
if (result.affected && result.affected <= 0) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
}
return new HttpSuccess();
}
}