Merge branch 'develop' into develop-Bright
This commit is contained in:
commit
2455f4a7d2
3 changed files with 261 additions and 0 deletions
121
src/controllers/ProfileLeaveController.ts
Normal file
121
src/controllers/ProfileLeaveController.ts
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Example,
|
||||
Get,
|
||||
Patch,
|
||||
Path,
|
||||
Post,
|
||||
Request,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
} from "tsoa";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import { CreateProfileLeave, ProfileLeave, UpdateProfileLeave } from "../entities/ProfileLeave";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import { ProfileLeaveHistory } from "../entities/ProfileLeaveHistory";
|
||||
import { RequestWithUser } from "../middlewares/user";
|
||||
import { Profile } from "../entities/Profile";
|
||||
|
||||
@Route("api/v1/org/profile/leave")
|
||||
@Tags("ProfileLeave")
|
||||
@Security("bearerAuth")
|
||||
export class ProfileLeaveController extends Controller {
|
||||
private profileRepo = AppDataSource.getRepository(Profile);
|
||||
private leaveRepo = AppDataSource.getRepository(ProfileLeave);
|
||||
private leaveHistoryRepo = AppDataSource.getRepository(ProfileLeaveHistory);
|
||||
|
||||
@Get("{profileId}")
|
||||
@Example({
|
||||
status: 200,
|
||||
message: "สำเร็จ",
|
||||
result: [],
|
||||
})
|
||||
public async getLeave(@Path() profileId: string) {
|
||||
const record = await this.leaveRepo.findBy({ profileId });
|
||||
return new HttpSuccess(record);
|
||||
}
|
||||
|
||||
@Get("history/{leaveId}")
|
||||
@Example({
|
||||
status: 200,
|
||||
message: "สำเร็จ",
|
||||
result: [],
|
||||
})
|
||||
public async leaveHistory(@Path() leaveId: string) {
|
||||
const record = await this.leaveHistoryRepo.findBy({
|
||||
profileLeaveId: leaveId,
|
||||
});
|
||||
return new HttpSuccess(record);
|
||||
}
|
||||
|
||||
@Post()
|
||||
public async newLeave(@Request() req: RequestWithUser, @Body() body: CreateProfileLeave) {
|
||||
if (!body.profileId) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
|
||||
}
|
||||
|
||||
const profile = await this.profileRepo.findOneBy({ id: body.profileId });
|
||||
|
||||
if (!profile) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||
}
|
||||
|
||||
const data = new ProfileLeave();
|
||||
|
||||
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.leaveRepo.save(data);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
@Patch("{leaveId}")
|
||||
public async editLeave(
|
||||
@Request() req: RequestWithUser,
|
||||
@Body() body: UpdateProfileLeave,
|
||||
@Path() leaveId: string,
|
||||
) {
|
||||
const record = await this.leaveRepo.findOneBy({ id: leaveId });
|
||||
|
||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
|
||||
const history = new ProfileLeaveHistory();
|
||||
|
||||
Object.assign(history, { ...record, id: undefined });
|
||||
Object.assign(record, body);
|
||||
history.profileLeaveId = leaveId;
|
||||
record.lastUpdateFullName = req.user.name;
|
||||
history.lastUpdateFullName = req.user.name;
|
||||
|
||||
await Promise.all([this.leaveRepo.save(record), this.leaveHistoryRepo.save(history)]);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
@Delete("{leaveId}")
|
||||
public async deleteTraning(@Path() leaveId: string) {
|
||||
await this.leaveHistoryRepo.delete({
|
||||
profileLeaveId: leaveId,
|
||||
});
|
||||
|
||||
const result = await this.leaveRepo.delete({ id: leaveId });
|
||||
|
||||
if (result.affected && result.affected <= 0) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
}
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue