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 }, ) { 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.profileId); } 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 }, ) { const profileSalary = await this.profileSalaryRepository.findOne({ where: { id: id } }); if (!profileSalary) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลไอดี: " + id); } 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, "ไม่พบข้อมูลไอดี: " + id); } try { await this.profileSalaryRepository.delete({ id: id }); return new HttpSuccess(); } catch (error) { return error; } } /** * API GetById ProfileSalary * * @summary GetById ProfileSalary * * @param {string} id Id ProfileSalaryId */ @Get("{id}") async GetById(@Path() id: string) { const profileSalary = await this.profileSalaryRepository.findOne({ where: { id }, select: ["id", "date", "amount", "positionSalaryAmount", "mouthSalaryAmount", "profileId"], }); if (!profileSalary) { throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูล"); } try { return new HttpSuccess(profileSalary); } catch (error) { return error; } } /** * 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([]); } try { return new HttpSuccess(profileSalary); } catch (error) { return error; } } }