150 lines
4.5 KiB
TypeScript
150 lines
4.5 KiB
TypeScript
import {
|
|
Controller,
|
|
Post,
|
|
Put,
|
|
Delete,
|
|
Route,
|
|
Security,
|
|
Tags,
|
|
Body,
|
|
Path,
|
|
Request,
|
|
SuccessResponse,
|
|
Response,
|
|
Get,
|
|
} from "tsoa";
|
|
import { AppDataSource } from "../database/data-source";
|
|
import HttpSuccess from "../interfaces/http-success";
|
|
import HttpStatusCode from "../interfaces/http-status";
|
|
import HttpError from "../interfaces/http-error";
|
|
import { ProfileSalary, CreateProfileSalary, UpdateProfileSalary } from "../entities/ProfileSalary";
|
|
import { Profile } from "../entities/Profile";
|
|
import { Not } from "typeorm";
|
|
|
|
@Route("api/v1/org/profileSalary")
|
|
@Tags("ProfileSalary")
|
|
@Security("bearerAuth")
|
|
@Response(
|
|
HttpStatusCode.INTERNAL_SERVER_ERROR,
|
|
"เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
|
|
)
|
|
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
|
|
export class ProfileSalaryController extends Controller {
|
|
private profileSalaryRepository = AppDataSource.getRepository(ProfileSalary);
|
|
private profileRepository = AppDataSource.getRepository(Profile);
|
|
|
|
/**
|
|
* API Create profile salary
|
|
*
|
|
* @summary Create profile salary
|
|
*
|
|
*/
|
|
@Post()
|
|
async CreateProfileSalary(
|
|
@Body()
|
|
requestBody: CreateProfileSalary,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const profileSalary = Object.assign(new ProfileSalary(), requestBody);
|
|
if (!profileSalary) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
|
}
|
|
|
|
const profile = await this.profileRepository.findOne({
|
|
where: { id: profileSalary.profileId },
|
|
});
|
|
if (!profile) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");
|
|
}
|
|
|
|
profileSalary.createdUserId = request.user.sub;
|
|
profileSalary.createdFullName = request.user.name;
|
|
profileSalary.lastUpdateUserId = request.user.sub;
|
|
profileSalary.lastUpdateFullName = request.user.name;
|
|
await this.profileSalaryRepository.save(profileSalary);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API Update profile salary
|
|
*
|
|
* @summary Update profile salary
|
|
*
|
|
* @param {string} id Id ProfileSalaryId
|
|
*/
|
|
@Put("{id}")
|
|
async UpdateProfileSalary(
|
|
@Path() id: string,
|
|
@Body()
|
|
requestBody: UpdateProfileSalary,
|
|
@Request() request: { user: Record<string, any> },
|
|
) {
|
|
const profileSalary = await this.profileSalaryRepository.findOne({ where: { id: id } });
|
|
if (!profileSalary) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");
|
|
}
|
|
|
|
profileSalary.lastUpdateUserId = request.user.sub;
|
|
profileSalary.lastUpdateFullName = request.user.name;
|
|
this.profileSalaryRepository.merge(profileSalary, requestBody);
|
|
await this.profileSalaryRepository.save(profileSalary);
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API Delete profile salary
|
|
*
|
|
* @summary Delete profile salary
|
|
*
|
|
* @param {string} id Id ProfileSalaryId
|
|
*/
|
|
@Delete("{id}")
|
|
async DeleteProfileSalary(@Path() id: string) {
|
|
const delprofileSalary = await this.profileSalaryRepository.findOne({
|
|
where: { id },
|
|
});
|
|
if (!delprofileSalary) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");
|
|
}
|
|
await this.profileSalaryRepository.delete({ id: id });
|
|
return new HttpSuccess();
|
|
}
|
|
|
|
/**
|
|
* API GetById ProfileSalary
|
|
*
|
|
* @summary GetById Profile
|
|
*
|
|
* @param {string} id Id ProfileId
|
|
*/
|
|
@Get("{id}")
|
|
async GetById(@Path() id: string) {
|
|
const profileSalary = await this.profileSalaryRepository.find({
|
|
where: { profileId:id },
|
|
select: ["id", "date", "amount", "positionSalaryAmount", "mouthSalaryAmount", "profileId"],
|
|
});
|
|
if (!profileSalary) {
|
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล");
|
|
}
|
|
return new HttpSuccess(profileSalary);
|
|
}
|
|
|
|
// /**
|
|
// * API GetLists profile salary
|
|
// *
|
|
// * @summary GetLists profile salary
|
|
// *
|
|
// */
|
|
// @Get()
|
|
// async GetLists() {
|
|
// const profileSalary = await this.profileSalaryRepository.find({
|
|
// select: ["id", "date", "amount", "positionSalaryAmount", "mouthSalaryAmount", "profileId"],
|
|
// order: { createdAt: "ASC" },
|
|
// });
|
|
|
|
// if (!profileSalary) {
|
|
// return new HttpSuccess([]);
|
|
// }
|
|
// return new HttpSuccess(profileSalary);
|
|
// }
|
|
}
|