แก้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

@ -25,6 +25,7 @@ import HttpError from "../interfaces/http-error";
import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import { LeaveType } from "../entities/LeaveType";
@Route("api/v1/org/profile/leave")
@Tags("ProfileLeave")
@ -33,6 +34,7 @@ export class ProfileLeaveController extends Controller {
private profileRepo = AppDataSource.getRepository(Profile);
private leaveRepo = AppDataSource.getRepository(ProfileLeave);
private leaveHistoryRepo = AppDataSource.getRepository(ProfileLeaveHistory);
private leaveTypeRepository = AppDataSource.getRepository(LeaveType);
@Get("{profileId}")
@Example({
@ -48,7 +50,8 @@ export class ProfileLeaveController extends Controller {
lastUpdateFullName: "สาวิตรี ศรีสมัย",
profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
leaveTypeId: "8dc5e672-b416-4323-b086-06dde8c4353c",
dateLeave: "2024-03-21T06:39:46.000Z",
dateLeaveStart: "2024-03-21T06:39:46.000Z",
dateLeaveEnd: "2024-03-21T06:39:46.000Z",
leaveDays: 0,
leaveCount: null,
totalLeave: 0,
@ -69,7 +72,7 @@ export class ProfileLeaveController extends Controller {
},
})
public async getLeave(@Path() profileId: string) {
const record = await this.leaveRepo.findOne({
const record = await this.leaveRepo.find({
relations: { leaveType: true },
where: { profileId },
});
@ -91,7 +94,8 @@ export class ProfileLeaveController extends Controller {
lastUpdateFullName: "สาวิตรี ศรีสมัย",
profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
leaveTypeId: "8dc5e672-b416-4323-b086-06dde8c4353c",
dateLeave: "2024-03-21T06:39:46.000Z",
dateLeaveStart: "2024-03-21T06:39:46.000Z",
dateLeaveEnd: "2024-03-21T06:39:46.000Z",
leaveDays: 0,
leaveCount: null,
totalLeave: 0,
@ -121,7 +125,8 @@ export class ProfileLeaveController extends Controller {
lastUpdateFullName: "สาวิตรี ศรีสมัย",
profileId: "1526d9d3-d8b1-43ab-81b5-a84dfbe99201",
leaveTypeId: "7dc4e314-b456-4323-b086-06dde8c4353c",
dateLeave: "2024-03-21T06:34:49.000Z",
dateLeaveStart: "2024-03-21T06:34:49.000Z",
dateLeaveEnd: "2024-03-21T06:34:49.000Z",
leaveDays: 2,
leaveCount: null,
totalLeave: 200,
@ -162,6 +167,12 @@ export class ProfileLeaveController extends Controller {
if (!profile) {
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
}
const leaveType = await this.leaveTypeRepository.findOne({
where: { id: body.leaveTypeId },
});
if (!leaveType) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทลานี้");
}
const data = new ProfileLeave();
@ -189,6 +200,13 @@ export class ProfileLeaveController extends Controller {
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
const leaveType = await this.leaveTypeRepository.findOne({
where: { id: body.leaveTypeId },
});
if (!leaveType) {
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลประเภทลานี้");
}
const history = new ProfileLeaveHistory();
Object.assign(history, { ...record, id: undefined });