refactor: shorten var name

This commit is contained in:
Methapon2001 2024-03-21 10:41:18 +07:00
parent ed7b15ebbe
commit 4b61617123

View file

@ -35,11 +35,11 @@ import { EmployeePosType } from "../entities/EmployeePosType";
) )
@SuccessResponse(HttpStatusCode.OK, "สำเร็จ") @SuccessResponse(HttpStatusCode.OK, "สำเร็จ")
export class ProfileEmployeeController extends Controller { export class ProfileEmployeeController extends Controller {
private orgRevisionRepository = AppDataSource.getRepository(OrgRevision); private orgRevisionRepo = AppDataSource.getRepository(OrgRevision);
private posMasterRepository = AppDataSource.getRepository(EmployeePosMaster); private posMasterRepo = AppDataSource.getRepository(EmployeePosMaster);
private profileRepository = AppDataSource.getRepository(ProfileEmployee); private profileRepo = AppDataSource.getRepository(ProfileEmployee);
private posLevelRepository = AppDataSource.getRepository(EmployeePosLevel); private posLevelRepo = AppDataSource.getRepository(EmployeePosLevel);
private posTypeRepository = AppDataSource.getRepository(EmployeePosType); private posTypeRepo = AppDataSource.getRepository(EmployeePosType);
/** /**
* API * API
@ -53,7 +53,7 @@ export class ProfileEmployeeController extends Controller {
requestBody: CreateProfileEmployee, requestBody: CreateProfileEmployee,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const _profile = await this.profileRepository.findOne({ const _profile = await this.profileRepo.findOne({
where: { citizenId: requestBody.citizenId }, where: { citizenId: requestBody.citizenId },
}); });
if (_profile) { if (_profile) {
@ -66,7 +66,7 @@ export class ProfileEmployeeController extends Controller {
requestBody.posLevelId = null; requestBody.posLevelId = null;
} }
if (requestBody.posLevelId) { if (requestBody.posLevelId) {
const checkPosLevel = await this.posLevelRepository.findOne({ const checkPosLevel = await this.posLevelRepo.findOne({
where: { id: requestBody.posLevelId }, where: { id: requestBody.posLevelId },
}); });
if (!checkPosLevel) { if (!checkPosLevel) {
@ -78,7 +78,7 @@ export class ProfileEmployeeController extends Controller {
requestBody.posTypeId = null; requestBody.posTypeId = null;
} }
if (requestBody.posTypeId) { if (requestBody.posTypeId) {
const checkPosType = await this.posTypeRepository.findOne({ const checkPosType = await this.posTypeRepo.findOne({
where: { id: requestBody.posTypeId }, where: { id: requestBody.posTypeId },
}); });
if (!checkPosType) { if (!checkPosType) {
@ -86,7 +86,7 @@ export class ProfileEmployeeController extends Controller {
} }
} }
const checkCitizenId = await this.profileRepository.findOne({ const checkCitizenId = await this.profileRepo.findOne({
where: { citizenId: requestBody.citizenId }, where: { citizenId: requestBody.citizenId },
}); });
@ -99,7 +99,7 @@ export class ProfileEmployeeController extends Controller {
profile.createdFullName = request.user.name; profile.createdFullName = request.user.name;
profile.lastUpdateUserId = request.user.sub; profile.lastUpdateUserId = request.user.sub;
profile.lastUpdateFullName = request.user.name; profile.lastUpdateFullName = request.user.name;
await this.profileRepository.save(profile); await this.profileRepo.save(profile);
return new HttpSuccess(); return new HttpSuccess();
} }
@ -117,12 +117,12 @@ export class ProfileEmployeeController extends Controller {
requestBody: CreateProfileEmployee, requestBody: CreateProfileEmployee,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const profile = await this.profileRepository.findOne({ where: { id: id } }); const profile = await this.profileRepo.findOne({ where: { id: id } });
if (!profile) { if (!profile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");
} }
const _profile = await this.profileRepository.findOne({ const _profile = await this.profileRepo.findOne({
where: { id: Not(id), citizenId: requestBody.citizenId }, where: { id: Not(id), citizenId: requestBody.citizenId },
}); });
if (_profile) { if (_profile) {
@ -136,7 +136,7 @@ export class ProfileEmployeeController extends Controller {
requestBody.posLevelId = null; requestBody.posLevelId = null;
} }
if (requestBody.posLevelId) { if (requestBody.posLevelId) {
const checkPosLevel = await this.posLevelRepository.findOne({ const checkPosLevel = await this.posLevelRepo.findOne({
where: { id: requestBody.posLevelId }, where: { id: requestBody.posLevelId },
}); });
if (!checkPosLevel) { if (!checkPosLevel) {
@ -148,7 +148,7 @@ export class ProfileEmployeeController extends Controller {
requestBody.posTypeId = null; requestBody.posTypeId = null;
} }
if (requestBody.posTypeId) { if (requestBody.posTypeId) {
const checkPosType = await this.posTypeRepository.findOne({ const checkPosType = await this.posTypeRepo.findOne({
where: { id: requestBody.posTypeId }, where: { id: requestBody.posTypeId },
}); });
if (!checkPosType) { if (!checkPosType) {
@ -156,7 +156,7 @@ export class ProfileEmployeeController extends Controller {
} }
} }
const checkCitizenId = await this.profileRepository.findOne({ const checkCitizenId = await this.profileRepo.findOne({
where: { where: {
id: Not(id), id: Not(id),
citizenId: requestBody.citizenId, citizenId: requestBody.citizenId,
@ -169,8 +169,8 @@ export class ProfileEmployeeController extends Controller {
profile.lastUpdateUserId = request.user.sub; profile.lastUpdateUserId = request.user.sub;
profile.lastUpdateFullName = request.user.name; profile.lastUpdateFullName = request.user.name;
this.profileRepository.merge(profile, requestBody); this.profileRepo.merge(profile, requestBody);
await this.profileRepository.save(profile); await this.profileRepo.save(profile);
return new HttpSuccess(); return new HttpSuccess();
} }
@ -183,13 +183,13 @@ export class ProfileEmployeeController extends Controller {
*/ */
@Delete("{id}") @Delete("{id}")
async deleteProfile(@Path() id: string) { async deleteProfile(@Path() id: string) {
const delProfile = await this.profileRepository.findOne({ const delProfile = await this.profileRepo.findOne({
where: { id }, where: { id },
}); });
if (!delProfile) { if (!delProfile) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์นี้");
} }
await this.profileRepository.delete({ id: id }); await this.profileRepo.delete({ id: id });
return new HttpSuccess(); return new HttpSuccess();
} }
@ -202,7 +202,7 @@ export class ProfileEmployeeController extends Controller {
*/ */
@Get("{id}") @Get("{id}")
async detailProfile(@Path() id: string) { async detailProfile(@Path() id: string) {
const profile = await this.profileRepository.findOne({ const profile = await this.profileRepo.findOne({
where: { id }, where: { id },
select: [ select: [
"id", "id",
@ -233,7 +233,7 @@ export class ProfileEmployeeController extends Controller {
@Query("pageSize") pageSize: number = 10, @Query("pageSize") pageSize: number = 10,
@Query("keyword") keyword?: string, @Query("keyword") keyword?: string,
) { ) {
const [profile, total] = await this.profileRepository.findAndCount({ const [profile, total] = await this.profileRepo.findAndCount({
select: [ select: [
"id", "id",
"prefix", "prefix",
@ -302,7 +302,7 @@ export class ProfileEmployeeController extends Controller {
keyword?: string; keyword?: string;
}, },
) { ) {
const orgRevision = await this.orgRevisionRepository.findOne({ const orgRevision = await this.orgRevisionRepo.findOne({
where: { where: {
orgRevisionIsDraft: true, orgRevisionIsDraft: true,
orgRevisionIsCurrent: false, orgRevisionIsCurrent: false,
@ -312,7 +312,7 @@ export class ProfileEmployeeController extends Controller {
if (!orgRevision) { if (!orgRevision) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบแบบร่างโครงสร้าง");
} }
const [profiles, total] = await this.profileRepository const [profiles, total] = await this.profileRepo
.createQueryBuilder("profileEmployee") .createQueryBuilder("profileEmployee")
.leftJoinAndSelect("profileEmployee.next_holders", "next_holders") .leftJoinAndSelect("profileEmployee.next_holders", "next_holders")
.leftJoinAndSelect("profileEmployee.posLevel", "posLevel") .leftJoinAndSelect("profileEmployee.posLevel", "posLevel")
@ -412,7 +412,7 @@ export class ProfileEmployeeController extends Controller {
*/ */
@Get("keycloak/position") @Get("keycloak/position")
async getProfileByKeycloak(@Request() request: { user: Record<string, any> }) { async getProfileByKeycloak(@Request() request: { user: Record<string, any> }) {
const profile = await this.profileRepository.findOne({ const profile = await this.profileRepo.findOne({
where: { keycloak: request.user.sub }, where: { keycloak: request.user.sub },
relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"], relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"],
}); });
@ -420,7 +420,7 @@ export class ProfileEmployeeController extends Controller {
throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ"); throw new HttpError(HttpStatusCode.NOT_FOUND, "ไม่พบข้อมูลบุคคลนี้ในระบบ");
} }
const orgRevisionPublish = await this.orgRevisionRepository const orgRevisionPublish = await this.orgRevisionRepo
.createQueryBuilder("orgRevision") .createQueryBuilder("orgRevision")
.where("orgRevision.orgRevisionIsDraft = false") .where("orgRevision.orgRevisionIsDraft = false")
.andWhere("orgRevision.orgRevisionIsCurrent = true") .andWhere("orgRevision.orgRevisionIsCurrent = true")
@ -533,28 +533,28 @@ export class ProfileEmployeeController extends Controller {
let findProfile: any; let findProfile: any;
switch (body.fieldName) { switch (body.fieldName) {
case "idcard": case "idcard":
findProfile = await this.profileRepository.find({ findProfile = await this.profileRepo.find({
where: { citizenId: Like(`%${body.keyword}%`) }, where: { citizenId: Like(`%${body.keyword}%`) },
relations: ["posType", "posLevel"], relations: ["posType", "posLevel"],
}); });
break; break;
case "firstname": case "firstname":
findProfile = await this.profileRepository.find({ findProfile = await this.profileRepo.find({
where: { firstName: Like(`%${body.keyword}%`) }, where: { firstName: Like(`%${body.keyword}%`) },
relations: ["posType", "posLevel"], relations: ["posType", "posLevel"],
}); });
break; break;
case "lastname": case "lastname":
findProfile = await this.profileRepository.find({ findProfile = await this.profileRepo.find({
where: { lastName: Like(`%${body.keyword}%`) }, where: { lastName: Like(`%${body.keyword}%`) },
relations: ["posType", "posLevel"], relations: ["posType", "posLevel"],
}); });
break; break;
default: default:
findProfile = await this.profileRepository.find({ findProfile = await this.profileRepo.find({
relations: ["posType", "posLevel"], relations: ["posType", "posLevel"],
}); });
break; break;
@ -593,17 +593,17 @@ export class ProfileEmployeeController extends Controller {
let commanderFullname_: any = {}; let commanderFullname_: any = {};
let commanderPosition_: any = {}; let commanderPosition_: any = {};
const findProfile = await this.profileRepository.findOne({ const findProfile = await this.profileRepo.findOne({
where: { keycloak: request.user.sub }, where: { keycloak: request.user.sub },
}); });
const findRevision = await this.orgRevisionRepository.findOne({ const findRevision = await this.orgRevisionRepo.findOne({
where: { where: {
orgRevisionIsCurrent: true, orgRevisionIsCurrent: true,
}, },
}); });
const findPosMaster = await this.posMasterRepository.findOne({ const findPosMaster = await this.posMasterRepo.findOne({
where: { where: {
current_holderId: findProfile?.id, current_holderId: findProfile?.id,
orgRevisionId: findRevision?.id, orgRevisionId: findRevision?.id,
@ -632,7 +632,7 @@ export class ProfileEmployeeController extends Controller {
condition = { orgRootId: childId, orgChild1Id: IsNull() }; condition = { orgRootId: childId, orgChild1Id: IsNull() };
} }
const findCmd = await this.posMasterRepository.findOne({ const findCmd = await this.posMasterRepo.findOne({
where: { where: {
current_holderId: Not(IsNull()) || Not(""), current_holderId: Not(IsNull()) || Not(""),
orgRevisionId: findRevision?.id, orgRevisionId: findRevision?.id,
@ -775,7 +775,7 @@ export class ProfileEmployeeController extends Controller {
@Body() @Body()
requestBody: { citizenId: string }, requestBody: { citizenId: string },
) { ) {
const profile = await this.profileRepository.findOne({ const profile = await this.profileRepo.findOne({
where: { id: Not(id), citizenId: requestBody.citizenId }, where: { id: Not(id), citizenId: requestBody.citizenId },
}); });
if (profile) { if (profile) {
@ -822,7 +822,7 @@ export class ProfileEmployeeController extends Controller {
.take(body.pageSize) .take(body.pageSize)
.getManyAndCount(); .getManyAndCount();
const orgRevisionActive = await this.orgRevisionRepository.findOne({ const orgRevisionActive = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false }, where: { orgRevisionIsCurrent: true, orgRevisionIsDraft: false },
}); });
@ -912,7 +912,7 @@ export class ProfileEmployeeController extends Controller {
period: string; period: string;
}, },
) { ) {
const findRevision = await this.orgRevisionRepository.findOne({ const findRevision = await this.orgRevisionRepo.findOne({
where: { orgRevisionIsCurrent: true }, where: { orgRevisionIsCurrent: true },
}); });
if (!findRevision) { if (!findRevision) {
@ -1114,7 +1114,7 @@ export class ProfileEmployeeController extends Controller {
@Path() revisionId: string, @Path() revisionId: string,
@Request() request: { user: Record<string, any> }, @Request() request: { user: Record<string, any> },
) { ) {
const profile = await this.profileRepository.findOne({ const profile = await this.profileRepo.findOne({
where: { keycloak: request.user.sub }, where: { keycloak: request.user.sub },
relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"], relations: ["posLevel", "posType", "current_holders", "current_holders.orgRoot"],
}); });