2024-08-27 09:21:22 +07:00
|
|
|
import {
|
|
|
|
|
Body,
|
|
|
|
|
Controller,
|
|
|
|
|
Delete,
|
|
|
|
|
Get,
|
|
|
|
|
Patch,
|
|
|
|
|
Path,
|
|
|
|
|
Post,
|
|
|
|
|
Request,
|
|
|
|
|
Route,
|
|
|
|
|
Security,
|
|
|
|
|
Tags,
|
2024-10-22 08:20:45 +07:00
|
|
|
Query,
|
2024-08-27 09:21:22 +07:00
|
|
|
} 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 { ProfileDevelopmentHistory } from "../entities/ProfileDevelopmentHistory";
|
|
|
|
|
import { RequestWithUser } from "../middlewares/user";
|
|
|
|
|
import { Profile } from "../entities/Profile";
|
2024-08-27 13:21:13 +07:00
|
|
|
import {
|
|
|
|
|
CreateProfileDevelopment,
|
|
|
|
|
ProfileDevelopment,
|
|
|
|
|
UpdateProfileDevelopment,
|
|
|
|
|
} from "../entities/ProfileDevelopment";
|
2024-08-27 09:21:22 +07:00
|
|
|
import permission from "../interfaces/permission";
|
2024-10-01 00:56:57 +07:00
|
|
|
import { DevelopmentProject } from "../entities/DevelopmentProject";
|
2024-10-10 17:58:34 +07:00
|
|
|
import { In, Brackets } from "typeorm";
|
2024-12-19 15:23:05 +07:00
|
|
|
import { DevelopmentRequest } from "../entities/DevelopmentRequest";
|
2024-08-27 09:21:22 +07:00
|
|
|
@Route("api/v1/org/profile/development")
|
|
|
|
|
@Tags("ProfileDevelopment")
|
|
|
|
|
@Security("bearerAuth")
|
|
|
|
|
export class ProfileDevelopmentController extends Controller {
|
|
|
|
|
private profileRepository = AppDataSource.getRepository(Profile);
|
|
|
|
|
private developmentRepository = AppDataSource.getRepository(ProfileDevelopment);
|
|
|
|
|
private developmentHistoryRepository = AppDataSource.getRepository(ProfileDevelopmentHistory);
|
2024-10-01 00:56:57 +07:00
|
|
|
private developmentProjectRepository = AppDataSource.getRepository(DevelopmentProject);
|
2024-12-19 15:23:05 +07:00
|
|
|
private developmentRequestRepository = AppDataSource.getRepository(DevelopmentRequest);
|
2024-08-27 09:21:22 +07:00
|
|
|
|
|
|
|
|
@Get("user")
|
|
|
|
|
public async getDevelopmentUser(@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.developmentRepository.find({
|
|
|
|
|
where: { profileId: profile.id },
|
2024-08-30 18:02:34 +07:00
|
|
|
order: { createdAt: "ASC" },
|
2024-08-27 09:21:22 +07:00
|
|
|
});
|
2024-08-27 17:30:19 +07:00
|
|
|
return new HttpSuccess(lists);
|
2024-08-27 09:21:22 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("{profileId}")
|
2024-10-10 17:58:34 +07:00
|
|
|
public async getDevelopment(
|
2024-10-22 08:20:45 +07:00
|
|
|
@Path() profileId: string,
|
2024-10-10 17:58:34 +07:00
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Query("page") page: number = 1,
|
|
|
|
|
@Query("pageSize") pageSize: number = 10,
|
|
|
|
|
@Query() searchKeyword: string = "",
|
|
|
|
|
) {
|
2024-10-22 08:20:45 +07:00
|
|
|
let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_OFFICER");
|
|
|
|
|
if (_workflow == false)
|
|
|
|
|
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
|
2024-10-10 17:58:34 +07:00
|
|
|
const [profileDevelopment, total] = await AppDataSource.getRepository(ProfileDevelopment)
|
|
|
|
|
.createQueryBuilder("profileDevelopment")
|
|
|
|
|
.where({ profileId: profileId })
|
|
|
|
|
.andWhere(
|
|
|
|
|
new Brackets((qb) => {
|
|
|
|
|
qb.where(
|
|
|
|
|
searchKeyword != undefined && searchKeyword != null && searchKeyword != ""
|
|
|
|
|
? "profileDevelopment.name LIKE :keyword"
|
|
|
|
|
: "1=1",
|
2024-10-22 08:20:45 +07:00
|
|
|
{
|
|
|
|
|
keyword: `%${searchKeyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
2024-10-10 17:58:34 +07:00
|
|
|
.orWhere(
|
|
|
|
|
searchKeyword != undefined && searchKeyword != null && searchKeyword != ""
|
|
|
|
|
? "profileDevelopment.developmentTarget LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${searchKeyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
searchKeyword != undefined && searchKeyword != null && searchKeyword != ""
|
|
|
|
|
? "profileDevelopment.developmentResults LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${searchKeyword}%`,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.orWhere(
|
|
|
|
|
searchKeyword != undefined && searchKeyword != null && searchKeyword != ""
|
|
|
|
|
? "profileDevelopment.developmentReport LIKE :keyword"
|
|
|
|
|
: "1=1",
|
|
|
|
|
{
|
|
|
|
|
keyword: `%${searchKeyword}%`,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.orderBy("profileDevelopment.createdAt", "ASC")
|
|
|
|
|
.skip((page - 1) * pageSize)
|
|
|
|
|
.take(pageSize)
|
|
|
|
|
.getManyAndCount();
|
|
|
|
|
return new HttpSuccess({ data: profileDevelopment, total });
|
2024-08-27 09:21:22 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("history/{developmentId}")
|
|
|
|
|
public async developmentHistory(@Path() developmentId: string, @Request() req: RequestWithUser) {
|
|
|
|
|
const _record = await this.developmentRepository.findOneBy({ id: developmentId });
|
|
|
|
|
if (_record) {
|
2024-10-22 08:20:45 +07:00
|
|
|
let _workflow = await new permission().Workflow(req, developmentId, "SYS_REGISTRY_OFFICER");
|
|
|
|
|
if (_workflow == false)
|
|
|
|
|
await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", _record.profileId);
|
2024-08-27 09:21:22 +07:00
|
|
|
}
|
|
|
|
|
const record = await this.developmentHistoryRepository.find({
|
|
|
|
|
where: { profileDevelopmentId: developmentId },
|
|
|
|
|
order: { createdAt: "DESC" },
|
|
|
|
|
});
|
2024-08-27 17:30:19 +07:00
|
|
|
return new HttpSuccess(record);
|
2024-08-27 09:21:22 +07:00
|
|
|
}
|
|
|
|
|
|
2024-12-19 15:23:05 +07:00
|
|
|
@Get("registry/{type}/{developmentId}")
|
|
|
|
|
public async developmentDetail(
|
|
|
|
|
@Path() developmentId: string,
|
|
|
|
|
@Path() type: string,
|
|
|
|
|
@Request() req: RequestWithUser
|
|
|
|
|
) {
|
|
|
|
|
const data = await this.developmentRequestRepository.findOne({
|
|
|
|
|
where: { id: developmentId },
|
|
|
|
|
relations: ["developmentProjects"],
|
|
|
|
|
});
|
|
|
|
|
if (!data)
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบรายการพัฒนารายบุคคลดังกล่าว");
|
|
|
|
|
|
|
|
|
|
if (type.trim().toLocaleUpperCase() == "OFFICER") {
|
|
|
|
|
let _workflow = await new permission().Workflow(req, developmentId, "SYS_REGISTRY_OFFICER");
|
|
|
|
|
if (_workflow == false)
|
|
|
|
|
await new permission().PermissionOrgUserGet(
|
|
|
|
|
req,
|
|
|
|
|
"SYS_REGISTRY_OFFICER",
|
|
|
|
|
data.profileId,
|
|
|
|
|
);
|
|
|
|
|
} else if (type.trim().toLocaleUpperCase() == "EMPLOYEE") {
|
|
|
|
|
let _workflow = await new permission().Workflow(req, developmentId, "SYS_REGISTRY_EMP");
|
|
|
|
|
if (_workflow == false)
|
|
|
|
|
await new permission().PermissionOrgUserGet(
|
|
|
|
|
req,
|
|
|
|
|
"SYS_REGISTRY_EMP",
|
|
|
|
|
data.profileEmployeeId,
|
|
|
|
|
);
|
|
|
|
|
} else if (type.trim().toLocaleUpperCase() == "TEMP") {
|
|
|
|
|
let _workflow = await new permission().Workflow(req, developmentId, "SYS_REGISTRY_TEMP");
|
|
|
|
|
if (_workflow == false)
|
|
|
|
|
await new permission().PermissionOrgUserGet(
|
|
|
|
|
req,
|
|
|
|
|
"SYS_REGISTRY_TEMP",
|
|
|
|
|
data.profileEmployeeId,
|
|
|
|
|
);
|
|
|
|
|
} else if (type.trim().toLocaleUpperCase() == "USER") {
|
|
|
|
|
} else {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่สามารถเข้าถึงข้อมูลนี้ได้");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const _mapData = {
|
|
|
|
|
id: data.id,
|
|
|
|
|
evaluationId: null,
|
|
|
|
|
target: data.developmentTarget,
|
|
|
|
|
summary: null,
|
|
|
|
|
point: null,
|
|
|
|
|
name: data.name,
|
|
|
|
|
achievement10: "มีผลการพัฒนาและมีการดำเนินการตามเป้าหมายการนำไปพัฒนางาน1",
|
|
|
|
|
achievement5: "มีผลการพัฒนาแต่ยังไม่ได้ดำเนินการตามเป้าหมายการนำไปพัฒนางาน",
|
|
|
|
|
achievement0: "ไม่ได้ดำเนินการพัฒนา",
|
|
|
|
|
isDevelopment70: data.isDevelopment70,
|
|
|
|
|
isDevelopment20: data.isDevelopment20,
|
|
|
|
|
isDevelopment10: data.isDevelopment10,
|
|
|
|
|
reasonDevelopment70: data.reasonDevelopment70,
|
|
|
|
|
reasonDevelopment20: data.reasonDevelopment20,
|
|
|
|
|
reasonDevelopment10: data.reasonDevelopment10,
|
|
|
|
|
selectType: null,
|
|
|
|
|
selectTypeYear: null,
|
|
|
|
|
selectTypeId: null
|
|
|
|
|
}
|
|
|
|
|
const _data = {
|
|
|
|
|
..._mapData,
|
|
|
|
|
developmentProjects: data.developmentProjects.map((x) => x.name),
|
|
|
|
|
};
|
|
|
|
|
return new HttpSuccess(_data);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 09:21:22 +07:00
|
|
|
@Post()
|
2024-08-27 13:21:13 +07:00
|
|
|
public async newDevelopment(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body() body: CreateProfileDevelopment,
|
|
|
|
|
) {
|
2024-08-27 09:21:22 +07:00
|
|
|
if (!body.profileId) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "กรุณากรอก profileId");
|
|
|
|
|
}
|
2024-08-27 13:21:13 +07:00
|
|
|
|
2024-08-27 09:21:22 +07:00
|
|
|
const profile = await this.profileRepository.findOneBy({ id: body.profileId });
|
|
|
|
|
if (!profile) {
|
|
|
|
|
throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
|
|
|
|
|
}
|
2024-08-28 17:05:27 +07:00
|
|
|
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profile.id);
|
2024-08-27 09:21:22 +07:00
|
|
|
|
|
|
|
|
const data = new ProfileDevelopment();
|
|
|
|
|
|
|
|
|
|
const meta = {
|
|
|
|
|
createdUserId: req.user.sub,
|
|
|
|
|
createdFullName: req.user.name,
|
|
|
|
|
lastUpdateUserId: req.user.sub,
|
|
|
|
|
lastUpdateFullName: req.user.name,
|
2024-08-30 18:02:34 +07:00
|
|
|
createdAt: new Date(),
|
|
|
|
|
lastUpdatedAt: new Date(),
|
2024-08-27 09:21:22 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Object.assign(data, { ...body, ...meta });
|
|
|
|
|
const history = new ProfileDevelopmentHistory();
|
|
|
|
|
Object.assign(history, { ...data, id: undefined });
|
|
|
|
|
|
|
|
|
|
await this.developmentRepository.save(data);
|
|
|
|
|
history.profileDevelopmentId = data.id;
|
|
|
|
|
await this.developmentHistoryRepository.save(history);
|
|
|
|
|
|
2024-10-01 00:56:57 +07:00
|
|
|
if (body.developmentProjects != null) {
|
|
|
|
|
await Promise.all(
|
|
|
|
|
body.developmentProjects.map(async (x) => {
|
|
|
|
|
let developmentProject = new DevelopmentProject();
|
|
|
|
|
let developmentProjectHistory = new DevelopmentProject();
|
|
|
|
|
Object.assign(developmentProject, {
|
|
|
|
|
...meta,
|
|
|
|
|
name: x,
|
|
|
|
|
profileDevelopmentId: data.id,
|
|
|
|
|
});
|
|
|
|
|
Object.assign(developmentProject, {
|
|
|
|
|
...meta,
|
|
|
|
|
name: x,
|
|
|
|
|
profileDevelopmentHistoryId: history.id,
|
|
|
|
|
});
|
|
|
|
|
await Promise.all([
|
|
|
|
|
this.developmentProjectRepository.save(developmentProject),
|
|
|
|
|
this.developmentProjectRepository.save(developmentProjectHistory),
|
|
|
|
|
]);
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 09:21:22 +07:00
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Patch("{developmentId}")
|
|
|
|
|
public async editDevelopment(
|
|
|
|
|
@Request() req: RequestWithUser,
|
|
|
|
|
@Body() body: UpdateProfileDevelopment,
|
|
|
|
|
@Path() developmentId: string,
|
|
|
|
|
) {
|
2024-08-27 13:21:13 +07:00
|
|
|
const record = await this.developmentRepository.findOne({
|
|
|
|
|
where: { id: developmentId },
|
|
|
|
|
});
|
2024-08-27 09:21:22 +07:00
|
|
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", record.profileId);
|
|
|
|
|
|
|
|
|
|
const history = new ProfileDevelopmentHistory();
|
|
|
|
|
|
|
|
|
|
Object.assign(record, body);
|
2024-09-01 22:44:23 +07:00
|
|
|
Object.assign(history, { ...record, id: undefined });
|
2024-08-27 09:21:22 +07:00
|
|
|
|
|
|
|
|
history.profileDevelopmentId = developmentId;
|
|
|
|
|
record.lastUpdateUserId = req.user.sub;
|
|
|
|
|
record.lastUpdateFullName = req.user.name;
|
2024-08-30 18:02:34 +07:00
|
|
|
record.lastUpdatedAt = new Date();
|
2024-08-27 09:21:22 +07:00
|
|
|
history.lastUpdateUserId = req.user.sub;
|
|
|
|
|
history.lastUpdateFullName = req.user.name;
|
|
|
|
|
history.createdUserId = req.user.sub;
|
|
|
|
|
history.createdFullName = req.user.name;
|
2024-08-30 18:02:34 +07:00
|
|
|
history.createdAt = new Date();
|
|
|
|
|
history.lastUpdatedAt = new Date();
|
2024-08-27 09:21:22 +07:00
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
|
this.developmentRepository.save(record),
|
|
|
|
|
this.developmentHistoryRepository.save(history),
|
|
|
|
|
]);
|
|
|
|
|
|
2024-10-01 00:56:57 +07:00
|
|
|
await this.developmentProjectRepository.delete({ profileDevelopmentId: record.id });
|
|
|
|
|
if (body.developmentProjects != null) {
|
|
|
|
|
const meta = {
|
|
|
|
|
createdUserId: req.user.sub,
|
|
|
|
|
createdFullName: req.user.name,
|
|
|
|
|
lastUpdateUserId: req.user.sub,
|
|
|
|
|
lastUpdateFullName: req.user.name,
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
lastUpdatedAt: new Date(),
|
|
|
|
|
};
|
|
|
|
|
await Promise.all(
|
|
|
|
|
body.developmentProjects.map(async (x) => {
|
|
|
|
|
let developmentProject = new DevelopmentProject();
|
|
|
|
|
let developmentProjectHistory = new DevelopmentProject();
|
|
|
|
|
Object.assign(developmentProject, {
|
|
|
|
|
...meta,
|
|
|
|
|
name: x,
|
|
|
|
|
profileDevelopmentId: record.id,
|
|
|
|
|
});
|
|
|
|
|
Object.assign(developmentProject, {
|
|
|
|
|
...meta,
|
|
|
|
|
name: x,
|
|
|
|
|
profileDevelopmentHistoryId: history.id,
|
|
|
|
|
});
|
|
|
|
|
await Promise.all([
|
|
|
|
|
this.developmentProjectRepository.save(developmentProject),
|
|
|
|
|
this.developmentProjectRepository.save(developmentProjectHistory),
|
|
|
|
|
]);
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-08-27 09:21:22 +07:00
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Delete("{developmentId}")
|
|
|
|
|
public async deleteDevelopment(@Path() developmentId: string, @Request() req: RequestWithUser) {
|
2024-08-27 13:21:13 +07:00
|
|
|
const _record = await this.developmentRepository.findOneBy({ id: developmentId });
|
|
|
|
|
if (_record) {
|
|
|
|
|
await new permission().PermissionOrgUserDelete(
|
|
|
|
|
req,
|
|
|
|
|
"SYS_REGISTRY_OFFICER",
|
|
|
|
|
_record.profileId,
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-10-01 00:56:57 +07:00
|
|
|
const history = await this.developmentHistoryRepository.find({
|
|
|
|
|
where: { profileDevelopmentId: developmentId },
|
|
|
|
|
});
|
|
|
|
|
await this.developmentProjectRepository.delete({ profileDevelopmentHistoryId: In(history) });
|
2024-08-27 09:21:22 +07:00
|
|
|
await this.developmentHistoryRepository.delete({
|
|
|
|
|
profileDevelopmentId: developmentId,
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-01 00:56:57 +07:00
|
|
|
await this.developmentProjectRepository.delete({ profileDevelopmentId: developmentId });
|
2024-08-27 09:21:22 +07:00
|
|
|
const result = await this.developmentRepository.delete({ id: developmentId });
|
|
|
|
|
|
|
|
|
|
if (result.affected == undefined || result.affected <= 0) {
|
|
|
|
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new HttpSuccess();
|
|
|
|
|
}
|
|
|
|
|
}
|