2024-03-13 15:33:02 +07:00
|
|
|
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";
|
2024-08-08 17:15:21 +07:00
|
|
|
import permission from "../interfaces/permission";
|
2024-03-21 17:11:01 +07:00
|
|
|
@Route("api/v1/org/profile/discipline")
|
2024-03-13 15:33:02 +07:00
|
|
|
@Tags("ProfileDiscipline")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
export class ProfileDisciplineController extends Controller {
|
|
|
|
|
private profileRepository = AppDataSource.getRepository(Profile);
|
|
|
|
|
private disciplineRepository = AppDataSource.getRepository(ProfileDiscipline);
|
|
|
|
|
private disciplineHistoryRepository = AppDataSource.getRepository(ProfileDisciplineHistory);
|
|
|
|
|
|
2024-05-23 16:44:37 +07:00
|
|
|
@Get("user")
|
|
|
|
|
public async getDisciplineUser(@Request() request: { user: Record<string, any> }) {
|
|
|
|
|
const profile = await this.profileRepository.findOneBy({ keycloak: request.user.sub });
|
|
|
|
|
if (!profile) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
|
|
|
}
|
|
|
|
|
const lists = await this.disciplineRepository.find({
|
|
|
|
|
where: { profileId: profile.id },
|
|
|
|
|
select: [
|
|
|
|
|
"id",
|
|
|
|
|
"date",
|
|
|
|
|
"level",
|
|
|
|
|
"detail",
|
|
|
|
|
"unStigma",
|
|
|
|
|
"refCommandNo",
|
|
|
|
|
"refCommandDate",
|
|
|
|
|
"lastUpdateFullName",
|
|
|
|
|
"lastUpdatedAt",
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
return new HttpSuccess(lists);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 15:33:02 +07:00
|
|
|
@Get("{profileId}")
|
2024-08-22 17:25:25 +07:00
|
|
|
public async getDiscipline(@Path() profileId: string, @Request() req: RequestWithUser) {
|
|
|
|
|
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
|
2024-03-13 15:33:02 +07:00
|
|
|
const lists = await this.disciplineRepository.find({
|
2024-05-16 14:10:17 +07:00
|
|
|
where: { profileId: profileId },
|
2024-03-29 12:00:12 +07:00
|
|
|
select: [
|
|
|
|
|
"id",
|
|
|
|
|
"date",
|
|
|
|
|
"level",
|
|
|
|
|
"detail",
|
|
|
|
|
"unStigma",
|
|
|
|
|
"refCommandNo",
|
|
|
|
|
"refCommandDate",
|
|
|
|
|
"lastUpdateFullName",
|
|
|
|
|
"lastUpdatedAt",
|
|
|
|
|
],
|
2024-03-13 15:33:02 +07:00
|
|
|
});
|
|
|
|
|
return new HttpSuccess(lists);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("history/{disciplineId}")
|
|
|
|
|
@Example({
|
|
|
|
|
status: 200,
|
|
|
|
|
message: "สำเร็จ",
|
|
|
|
|
result: [
|
|
|
|
|
{
|
|
|
|
|
id: "debfa8a7-83fb-4801-a940-8ae74e7638d3",
|
|
|
|
|
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",
|
|
|
|
|
date: "2024-03-12T10:09:47.000Z",
|
|
|
|
|
level: "string",
|
|
|
|
|
detail: "string",
|
|
|
|
|
unStigma: "string",
|
|
|
|
|
refCommandNo: "string",
|
|
|
|
|
refCommandDate: "2024-03-12T10:09:47.000Z",
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
})
|
2024-08-22 17:25:25 +07:00
|
|
|
public async disciplineHistory(@Path() disciplineId: string, @Request() req: RequestWithUser) {
|
|
|
|
|
const _record = await this.disciplineRepository.findOneBy({ id: disciplineId });
|
|
|
|
|
if (_record) {
|
|
|
|
|
await new permission().PermissionOrgUserList(req, "SYS_REGISTRY_OFFICER", _record.profileId);
|
|
|
|
|
}
|
2024-03-13 15:33:02 +07:00
|
|
|
const record = await this.disciplineHistoryRepository.find({
|
|
|
|
|
where: { profileDisciplineId: disciplineId },
|
2024-03-29 12:00:12 +07:00
|
|
|
select: [
|
|
|
|
|
"id",
|
|
|
|
|
"date",
|
|
|
|
|
"level",
|
|
|
|
|
"detail",
|
|
|
|
|
"unStigma",
|
|
|
|
|
"refCommandNo",
|
|
|
|
|
"refCommandDate",
|
|
|
|
|
"lastUpdateFullName",
|
|
|
|
|
"lastUpdatedAt",
|
|
|
|
|
],
|
2024-03-21 14:19:42 +07:00
|
|
|
order: { createdAt: "DESC" },
|
2024-03-13 15:33:02 +07:00
|
|
|
});
|
|
|
|
|
return new HttpSuccess(record);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Post()
|
|
|
|
|
public async newDiscipline(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body() body: CreateProfileDiscipline,
|
|
|
|
|
) {
|
|
|
|
|
if (!body.profileId) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
|
|
|
|
|
}
|
2024-08-22 17:25:25 +07:00
|
|
|
|
2024-03-13 15:33:02 +07:00
|
|
|
const profile = await this.profileRepository.findOneBy({ id: body.profileId });
|
2024-08-22 17:25:25 +07:00
|
|
|
|
2024-03-13 15:33:02 +07:00
|
|
|
if (!profile) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
|
|
|
}
|
2024-08-27 12:11:31 +07:00
|
|
|
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id);
|
2024-03-13 15:33:02 +07:00
|
|
|
|
|
|
|
|
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 });
|
2024-08-13 17:43:30 +07:00
|
|
|
const history = new ProfileDisciplineHistory();
|
2024-08-13 20:01:53 +07:00
|
|
|
Object.assign(history, { ...data, id: undefined });
|
2024-08-13 17:43:30 +07:00
|
|
|
|
2024-08-13 20:01:53 +07:00
|
|
|
await this.disciplineRepository.save(data);
|
|
|
|
|
history.profileDisciplineId = data.id;
|
|
|
|
|
await this.disciplineHistoryRepository.save(history);
|
2024-03-13 15:33:02 +07:00
|
|
|
|
|
|
|
|
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, "ไม่พบข้อมูล");
|
2024-08-22 17:25:25 +07:00
|
|
|
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId);
|
2024-03-13 15:33:02 +07:00
|
|
|
|
|
|
|
|
const history = new ProfileDisciplineHistory();
|
|
|
|
|
|
|
|
|
|
Object.assign(record, body);
|
2024-08-13 17:43:30 +07:00
|
|
|
Object.assign(history, body);
|
|
|
|
|
|
2024-03-13 15:33:02 +07:00
|
|
|
history.profileDisciplineId = disciplineId;
|
2024-08-13 17:43:30 +07:00
|
|
|
record.lastUpdateUserId = req.user.sub;
|
2024-03-13 15:33:02 +07:00
|
|
|
record.lastUpdateFullName = req.user.name;
|
2024-08-13 17:43:30 +07:00
|
|
|
history.lastUpdateUserId = req.user.sub;
|
2024-03-13 15:33:02 +07:00
|
|
|
history.lastUpdateFullName = req.user.name;
|
2024-08-13 17:43:30 +07:00
|
|
|
history.createdUserId = req.user.sub;
|
|
|
|
|
history.createdFullName = req.user.name;
|
2024-03-13 15:33:02 +07:00
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
|
this.disciplineRepository.save(record),
|
|
|
|
|
this.disciplineHistoryRepository.save(history),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("{disciplineId}")
|
2024-08-08 17:15:21 +07:00
|
|
|
public async deleteDiscipline(@Path() disciplineId: string, @Request() req: RequestWithUser) {
|
2024-08-22 17:25:25 +07:00
|
|
|
const _record = await this.disciplineRepository.findOneBy({ id: disciplineId });
|
|
|
|
|
if (_record) {
|
|
|
|
|
await new permission().PermissionOrgUserDelete(req, "SYS_REGISTRY_OFFICER", _record.profileId);
|
|
|
|
|
}
|
2024-03-13 15:33:02 +07:00
|
|
|
await this.disciplineHistoryRepository.delete({
|
|
|
|
|
profileDisciplineId: disciplineId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const result = await this.disciplineRepository.delete({ id: disciplineId });
|
|
|
|
|
|
2024-06-10 10:17:37 +07:00
|
|
|
if (result.affected == undefined || result.affected <= 0) {
|
2024-03-13 15:33:02 +07:00
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
}
|