crud profile discip,duty,nopaid,other
This commit is contained in:
parent
1d48bb06ee
commit
13fc7945e5
9 changed files with 706 additions and 5 deletions
186
src/controllers/ProfileDisciplineController.ts
Normal file
186
src/controllers/ProfileDisciplineController.ts
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Example,
|
||||
Get,
|
||||
Patch,
|
||||
Path,
|
||||
Post,
|
||||
Request,
|
||||
Route,
|
||||
Security,
|
||||
Tags,
|
||||
} from "tsoa";
|
||||
import { AppDataSource } from "../database/data-source";
|
||||
import HttpSuccess from "../interfaces/http-success";
|
||||
import HttpStatus from "../interfaces/http-status";
|
||||
import HttpError from "../interfaces/http-error";
|
||||
import { ProfileDisciplineHistory } from "../entities/ProfileDisciplineHistory";
|
||||
import { RequestWithUser } from "../middlewares/user";
|
||||
import { Profile } from "../entities/Profile";
|
||||
import {
|
||||
CreateProfileDiscipline,
|
||||
ProfileDiscipline,
|
||||
UpdateProfileDiscipline,
|
||||
} from "../entities/ProfileDiscipline";
|
||||
|
||||
@Route("api/v1/org/profile/disipline")
|
||||
@Tags("ProfileDiscipline")
|
||||
@Security("bearerAuth")
|
||||
export class ProfileDisciplineController extends Controller {
|
||||
private profileRepository = AppDataSource.getRepository(Profile);
|
||||
private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline);
|
||||
private disciplineHistoryRepository = AppDataSource.getRepository(ProfileDisciplineHistory);
|
||||
|
||||
@Get("{profileId}")
|
||||
@Example({
|
||||
status: 200,
|
||||
message: "สำเร็จ",
|
||||
result: [
|
||||
{
|
||||
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||
isActive: true,
|
||||
date: "2024-03-12T10:09:47.000Z",
|
||||
level: "string",
|
||||
detail: "string",
|
||||
unStigma: "string",
|
||||
refCommandNo: "string",
|
||||
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||
},
|
||||
],
|
||||
})
|
||||
public async getDiscipline(@Path() profileId: string) {
|
||||
const lists = await this.disciplineRepository.find({
|
||||
where: { profileId: profileId },
|
||||
select: [
|
||||
"id",
|
||||
"isActive",
|
||||
"date",
|
||||
"level",
|
||||
"detail",
|
||||
"unStigma",
|
||||
"refCommandNo",
|
||||
"refCommandDate",
|
||||
],
|
||||
});
|
||||
return new HttpSuccess(lists);
|
||||
}
|
||||
|
||||
@Get("history/{disciplineId}")
|
||||
@Example({
|
||||
status: 200,
|
||||
message: "สำเร็จ",
|
||||
result: [
|
||||
{
|
||||
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
||||
isActive: true,
|
||||
date: "2024-03-12T10:09:47.000Z",
|
||||
level: "string",
|
||||
detail: "string",
|
||||
unStigma: "string",
|
||||
refCommandNo: "string",
|
||||
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||
},
|
||||
{
|
||||
id: "ba0e2f82-014e-46c6-8b82-a7c28eb5325f",
|
||||
isActive: true,
|
||||
date: "2024-03-12T10:09:47.000Z",
|
||||
level: "string",
|
||||
detail: "string",
|
||||
unStigma: "string",
|
||||
refCommandNo: "string",
|
||||
refCommandDate: "2024-03-12T10:09:47.000Z",
|
||||
},
|
||||
],
|
||||
})
|
||||
public async disciplineHistory(@Path() disciplineId: string) {
|
||||
const record = await this.disciplineHistoryRepository.find({
|
||||
where: { profileDisciplineId: disciplineId },
|
||||
select: [
|
||||
"id",
|
||||
"isActive",
|
||||
"date",
|
||||
"level",
|
||||
"detail",
|
||||
"unStigma",
|
||||
"refCommandNo",
|
||||
"refCommandDate",
|
||||
],
|
||||
order: { createdAt: "DESC" }
|
||||
});
|
||||
return new HttpSuccess(record);
|
||||
}
|
||||
|
||||
@Post()
|
||||
public async newDiscipline(
|
||||
@Request() req: RequestWithUser,
|
||||
@Body() body: CreateProfileDiscipline,
|
||||
) {
|
||||
if (!body.profileId) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
|
||||
}
|
||||
|
||||
const profile = await this.profileRepository.findOneBy({ id: body.profileId });
|
||||
|
||||
if (!profile) {
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
||||
}
|
||||
|
||||
const data = new ProfileDiscipline();
|
||||
|
||||
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.disciplineRepository.save(data);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
@Patch("{disciplineId}")
|
||||
public async editDiscipline(
|
||||
@Request() req: RequestWithUser,
|
||||
@Body() body: UpdateProfileDiscipline,
|
||||
@Path() disciplineId: string,
|
||||
) {
|
||||
const record = await this.disciplineRepository.findOneBy({ id: disciplineId });
|
||||
|
||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||
|
||||
const history = new ProfileDisciplineHistory();
|
||||
|
||||
Object.assign(history, { ...record, id: undefined });
|
||||
Object.assign(record, body);
|
||||
history.profileDisciplineId = disciplineId;
|
||||
record.lastUpdateFullName = req.user.name;
|
||||
history.lastUpdateFullName = req.user.name;
|
||||
|
||||
await Promise.all([
|
||||
this.disciplineRepository.save(record),
|
||||
this.disciplineHistoryRepository.save(history),
|
||||
]);
|
||||
|
||||
return new HttpSuccess();
|
||||
}
|
||||
|
||||
@Delete("{disciplineId}")
|
||||
public async deleteTraning(@Path() disciplineId: string) {
|
||||
await this.disciplineHistoryRepository.delete({
|
||||
profileDisciplineId: disciplineId,
|
||||
});
|
||||
|
||||
const result = await this.disciplineRepository.delete({ id: disciplineId });
|
||||
|
||||
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