แก้apiข้อมูลหลัก

This commit is contained in:
Kittapath 2024-03-26 23:07:55 +07:00
parent 73e07dfed6
commit 6b78a365fa
26 changed files with 1816 additions and 988 deletions

View file

@ -1,150 +1,189 @@
import {
Body,
Controller,
Post,
Put,
Delete,
Example,
Get,
Patch,
Path,
Post,
Request,
Route,
Security,
Tags,
Body,
Path,
Request,
SuccessResponse,
Response,
Get,
} from "tsoa";
import { AppDataSource } from "../database/data-source";
import { CreateProfileSalary, ProfileSalary, UpdateProfileSalary } from "../entities/ProfileSalary";
import HttpSuccess from "../interfaces/http-success";
import HttpStatusCode from "../interfaces/http-status";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { ProfileSalary, CreateProfileSalary, UpdateProfileSalary } from "../entities/ProfileSalary";
import { ProfileSalaryHistory } from "../entities/ProfileSalaryHistory";
import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import { Not } from "typeorm";
@Route("api/v1/org/profileSalary")
@Route("api/v1/org/profile/salary")
@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);
private profileRepo = AppDataSource.getRepository(Profile);
private salaryRepo = AppDataSource.getRepository(ProfileSalary);
private salaryHistoryRepo = AppDataSource.getRepository(ProfileSalaryHistory);
@Get("{profileId}")
@Example({
status: 200,
message: "สำเร็จ",
result: [
{
id: "3cf02fb7-2f0c-471b-b641-51d557375c0a",
createdAt: "2024-03-12T02:55:56.915Z",
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
lastUpdatedAt: "2024-03-12T02:55:56.915Z",
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
createdFullName: "สาวิตรี ศรีสมัย",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
isActive: true,
startDate: "2024-03-12T09:55:23.000Z",
endDate: "2024-03-12T09:55:23.000Z",
numberOrder: "string",
topic: "string",
place: "string",
dateOrder: "2024-03-12T09:55:23.000Z",
department: "string",
duration: "string",
name: "string",
yearly: 0,
isDate: true,
},
],
})
public async getSalary(@Path() profileId: string) {
const record = await this.salaryRepo.findBy({ profileId });
return new HttpSuccess(record);
}
@Get("history/{salaryId}")
@Example({
status: 200,
message: "สำเร็จ",
result: [
{
id: "6d4e9dbe-8697-4546-9651-0a1c3ea0a82d",
createdAt: "2024-03-12T02:55:56.971Z",
createdUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
lastUpdatedAt: "2024-03-12T02:55:56.971Z",
lastUpdateUserId: "59134ef9-9e62-41d0-aac5-339be727f2b0",
createdFullName: "สาวิตรี ศรีสมัย",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
isActive: true,
startDate: "2024-03-12T09:55:23.000Z",
endDate: "2024-03-12T09:55:23.000Z",
numberOrder: "string",
topic: "string",
place: "string",
dateOrder: "2024-03-12T09:55:23.000Z",
department: "string",
duration: "string",
name: "string",
yearly: 0,
isDate: true,
profileSalaryId: "3cf02fb7-2f0c-471b-b641-51d557375c0a",
},
{
id: "a251c176-3dac-4d09-9813-38c8db1127e3",
createdAt: "2024-03-12T02:58:17.917Z",
createdUserId: "00000000-0000-0000-0000-000000000000",
lastUpdatedAt: "2024-03-12T02:58:17.917Z",
lastUpdateUserId: "00000000-0000-0000-0000-000000000000",
createdFullName: "string",
lastUpdateFullName: "สาวิตรี ศรีสมัย",
isActive: true,
startDate: "2024-03-12T09:57:44.000Z",
endDate: "2024-03-12T09:57:44.000Z",
numberOrder: "string",
topic: "topic",
place: "place",
dateOrder: "2024-03-12T09:57:44.000Z",
department: "department",
duration: "string",
name: "name",
yearly: 0,
isDate: true,
profileSalaryId: "3cf02fb7-2f0c-471b-b641-51d557375c0a",
},
],
})
public async salaryHistory(@Path() salaryId: string) {
const record = await this.salaryHistoryRepo.findBy({
profileSalaryId: salaryId,
});
return new HttpSuccess(record);
}
/**
* 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, "ไม่พบข้อมูล");
public async newSalary(@Request() req: RequestWithUser, @Body() body: CreateProfileSalary) {
if (!body.profileId) {
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
}
const profile = await this.profileRepository.findOne({
where: { id: profileSalary.profileId },
});
const profile = await this.profileRepo.findOneBy({ id: body.profileId });
if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
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);
const data = new ProfileSalary();
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.salaryRepo.save(data);
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> },
@Patch("{salaryId}")
public async editSalary(
@Request() req: RequestWithUser,
@Body() body: UpdateProfileSalary,
@Path() salaryId: string,
) {
const profileSalary = await this.profileSalaryRepository.findOne({ where: { id: id } });
if (!profileSalary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");
}
const record = await this.salaryRepo.findOneBy({ id: salaryId });
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const history = new ProfileSalaryHistory();
Object.assign(history, { ...record, id: undefined });
Object.assign(record, body);
history.profileSalaryId = salaryId;
record.lastUpdateFullName = req.user.name;
history.lastUpdateFullName = req.user.name;
await Promise.all([this.salaryRepo.save(record), this.salaryHistoryRepo.save(history)]);
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 },
@Delete("{salaryId}")
public async deleteSalary(@Path() salaryId: string) {
await this.salaryHistoryRepo.delete({
profileSalaryId: salaryId,
});
if (!delprofileSalary) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");
const result = await this.salaryRepo.delete({ id: salaryId });
if (result.affected && result.affected <= 0) {
throw new HttpError(HttpStatus.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);
// }
}