fix update prefix and profileId
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m45s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m45s
This commit is contained in:
parent
e4f46a1762
commit
2951630b7b
10 changed files with 201 additions and 69 deletions
|
|
@ -64,6 +64,7 @@ import {
|
||||||
getRoleMappings,
|
getRoleMappings,
|
||||||
removeUserRoles,
|
removeUserRoles,
|
||||||
getToken,
|
getToken,
|
||||||
|
updateUserAttributes,
|
||||||
} from "../keycloak";
|
} from "../keycloak";
|
||||||
import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation";
|
import { ProfileEducation, CreateProfileEducation } from "../entities/ProfileEducation";
|
||||||
import { ProfileEducationHistory } from "../entities/ProfileEducationHistory";
|
import { ProfileEducationHistory } from "../entities/ProfileEducationHistory";
|
||||||
|
|
@ -4242,6 +4243,12 @@ export class CommandController extends Controller {
|
||||||
profile.isActive = true;
|
profile.isActive = true;
|
||||||
}
|
}
|
||||||
await this.profileRepository.save(profile);
|
await this.profileRepository.save(profile);
|
||||||
|
// update user attribute in keycloak
|
||||||
|
await updateUserAttributes(profile.keycloak ?? "", {
|
||||||
|
profileId: [profile.id],
|
||||||
|
prefix: [profile.prefix || ""],
|
||||||
|
});
|
||||||
|
|
||||||
// Task #2190
|
// Task #2190
|
||||||
if (code && ["C-PM-17", "C-PM-18"].includes(code)) {
|
if (code && ["C-PM-17", "C-PM-18"].includes(code)) {
|
||||||
let organizeName = "";
|
let organizeName = "";
|
||||||
|
|
@ -6329,8 +6336,7 @@ export class CommandController extends Controller {
|
||||||
name: x.name,
|
name: x.name,
|
||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
userKeycloakId = checkUser[0].id;
|
userKeycloakId = checkUser[0].id;
|
||||||
const rolesData = await getRoleMappings(userKeycloakId);
|
const rolesData = await getRoleMappings(userKeycloakId);
|
||||||
if (rolesData) {
|
if (rolesData) {
|
||||||
|
|
@ -6393,9 +6399,9 @@ export class CommandController extends Controller {
|
||||||
if (profileEmployee.keycloak != null) {
|
if (profileEmployee.keycloak != null) {
|
||||||
// const delUserKeycloak = await deleteUser(profileEmployee.keycloak);
|
// const delUserKeycloak = await deleteUser(profileEmployee.keycloak);
|
||||||
// if (delUserKeycloak) {
|
// if (delUserKeycloak) {
|
||||||
profileEmployee.keycloak = _null;
|
profileEmployee.keycloak = _null;
|
||||||
profileEmployee.roleKeycloaks = [];
|
profileEmployee.roleKeycloaks = [];
|
||||||
profileEmployee.isActive = false;
|
profileEmployee.isActive = false;
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
profileEmployee.isLeave = true;
|
profileEmployee.isLeave = true;
|
||||||
|
|
@ -6448,6 +6454,11 @@ export class CommandController extends Controller {
|
||||||
profile.phone = item.bodyProfile.phone ?? null;
|
profile.phone = item.bodyProfile.phone ?? null;
|
||||||
|
|
||||||
await this.profileRepository.save(profile);
|
await this.profileRepository.save(profile);
|
||||||
|
// update user attribute in keycloak
|
||||||
|
await updateUserAttributes(profile.keycloak ?? "", {
|
||||||
|
profileId: [profile.id],
|
||||||
|
prefix: [profile.prefix || ""],
|
||||||
|
});
|
||||||
setLogDataDiff(req, { before, after: profile });
|
setLogDataDiff(req, { before, after: profile });
|
||||||
}
|
}
|
||||||
//ขรก.ในระบบ หรือ ขรก.ในระบบที่สถานะพ้นจากราชการ
|
//ขรก.ในระบบ หรือ ขรก.ในระบบที่สถานะพ้นจากราชการ
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import { profile } from "console";
|
|
||||||
import { Controller, Get, Post, Query, Route, Security, Tags } from "tsoa";
|
import { Controller, Get, Post, Query, Route, Security, Tags } from "tsoa";
|
||||||
import { calculateGovAge } from "../interfaces/utils";
|
import { calculateGovAge } from "../interfaces/utils";
|
||||||
import HttpSuccess from "../interfaces/http-success";
|
import HttpSuccess from "../interfaces/http-success";
|
||||||
|
|
@ -14,7 +13,7 @@ export class AppController extends Controller {
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
public async Post(@Query() profileId: string) {
|
public async Post(@Query() profileId: string) {
|
||||||
const result = calculateGovAge(profileId,"OFFICER");
|
const result = calculateGovAge(profileId, "OFFICER");
|
||||||
return new HttpSuccess(result);
|
return new HttpSuccess(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,8 +50,13 @@ import {
|
||||||
getUserByUsername,
|
getUserByUsername,
|
||||||
getRoles,
|
getRoles,
|
||||||
addUserRoles,
|
addUserRoles,
|
||||||
|
updateUserAttributes,
|
||||||
} from "../keycloak";
|
} from "../keycloak";
|
||||||
import { getPositionCountsAggregated, getPositionCount, PositionCountsByNode } from "../services/OrganizationService";
|
import {
|
||||||
|
getPositionCountsAggregated,
|
||||||
|
getPositionCount,
|
||||||
|
PositionCountsByNode,
|
||||||
|
} from "../services/OrganizationService";
|
||||||
import {
|
import {
|
||||||
BatchSavePosMasterHistoryOfficer,
|
BatchSavePosMasterHistoryOfficer,
|
||||||
CreatePosMasterHistoryEmployee,
|
CreatePosMasterHistoryEmployee,
|
||||||
|
|
@ -4688,7 +4693,12 @@ export class OrganizationController extends Controller {
|
||||||
case 2: {
|
case 2: {
|
||||||
const data = await this.child1Repository.findOne({
|
const data = await this.child1Repository.findOne({
|
||||||
where: { id: idNode },
|
where: { id: idNode },
|
||||||
relations: ["orgRevision", "orgChild2s", "orgChild2s.orgChild3s", "orgChild2s.orgChild3s.orgChild4s"],
|
relations: [
|
||||||
|
"orgRevision",
|
||||||
|
"orgChild2s",
|
||||||
|
"orgChild2s.orgChild3s",
|
||||||
|
"orgChild2s.orgChild3s.orgChild4s",
|
||||||
|
],
|
||||||
});
|
});
|
||||||
if (!data) {
|
if (!data) {
|
||||||
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id");
|
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found child1Id");
|
||||||
|
|
@ -4700,7 +4710,9 @@ export class OrganizationController extends Controller {
|
||||||
deptID: data.id,
|
deptID: data.id,
|
||||||
type: 2,
|
type: 2,
|
||||||
totalPositionCount: child1Counts.totalCount,
|
totalPositionCount: child1Counts.totalCount,
|
||||||
totalPositionVacant: isDraft ? child1Counts.nextVacantCount : child1Counts.currentVacantCount,
|
totalPositionVacant: isDraft
|
||||||
|
? child1Counts.nextVacantCount
|
||||||
|
: child1Counts.currentVacantCount,
|
||||||
children: this.buildOrgChild2s(data.orgChild2s, positionCounts, isDraft),
|
children: this.buildOrgChild2s(data.orgChild2s, positionCounts, isDraft),
|
||||||
};
|
};
|
||||||
return new HttpSuccess([formattedData]);
|
return new HttpSuccess([formattedData]);
|
||||||
|
|
@ -4720,7 +4732,9 @@ export class OrganizationController extends Controller {
|
||||||
deptID: data.id,
|
deptID: data.id,
|
||||||
type: 3,
|
type: 3,
|
||||||
totalPositionCount: child2Counts.totalCount,
|
totalPositionCount: child2Counts.totalCount,
|
||||||
totalPositionVacant: isDraft ? child2Counts.nextVacantCount : child2Counts.currentVacantCount,
|
totalPositionVacant: isDraft
|
||||||
|
? child2Counts.nextVacantCount
|
||||||
|
: child2Counts.currentVacantCount,
|
||||||
children: this.buildOrgChild3s(data.orgChild3s, positionCounts, isDraft),
|
children: this.buildOrgChild3s(data.orgChild3s, positionCounts, isDraft),
|
||||||
};
|
};
|
||||||
return new HttpSuccess([formattedData]);
|
return new HttpSuccess([formattedData]);
|
||||||
|
|
@ -4740,7 +4754,9 @@ export class OrganizationController extends Controller {
|
||||||
deptID: data.id,
|
deptID: data.id,
|
||||||
type: 4,
|
type: 4,
|
||||||
totalPositionCount: child3Counts.totalCount,
|
totalPositionCount: child3Counts.totalCount,
|
||||||
totalPositionVacant: isDraft ? child3Counts.nextVacantCount : child3Counts.currentVacantCount,
|
totalPositionVacant: isDraft
|
||||||
|
? child3Counts.nextVacantCount
|
||||||
|
: child3Counts.currentVacantCount,
|
||||||
children: this.buildOrgChild4s(data.orgChild4s, positionCounts, isDraft),
|
children: this.buildOrgChild4s(data.orgChild4s, positionCounts, isDraft),
|
||||||
};
|
};
|
||||||
return new HttpSuccess([formattedData]);
|
return new HttpSuccess([formattedData]);
|
||||||
|
|
@ -4760,7 +4776,9 @@ export class OrganizationController extends Controller {
|
||||||
deptID: data.id,
|
deptID: data.id,
|
||||||
type: 5,
|
type: 5,
|
||||||
totalPositionCount: child4Counts.totalCount,
|
totalPositionCount: child4Counts.totalCount,
|
||||||
totalPositionVacant: isDraft ? child4Counts.nextVacantCount : child4Counts.currentVacantCount,
|
totalPositionVacant: isDraft
|
||||||
|
? child4Counts.nextVacantCount
|
||||||
|
: child4Counts.currentVacantCount,
|
||||||
};
|
};
|
||||||
return new HttpSuccess([formattedData]);
|
return new HttpSuccess([formattedData]);
|
||||||
}
|
}
|
||||||
|
|
@ -8089,7 +8107,12 @@ export class OrganizationController extends Controller {
|
||||||
if (_item) {
|
if (_item) {
|
||||||
_item.roleKeycloaks = Array.from(new Set([..._item.roleKeycloaks, ...roleKeycloak]));
|
_item.roleKeycloaks = Array.from(new Set([..._item.roleKeycloaks, ...roleKeycloak]));
|
||||||
check += 1;
|
check += 1;
|
||||||
await this.profileEmployeeRepo.save(_item);
|
_item = await this.profileEmployeeRepo.save(_item);
|
||||||
|
// update user attribute in keycloak
|
||||||
|
await updateUserAttributes(_item.keycloak, {
|
||||||
|
profileId: [_item.id],
|
||||||
|
prefix: [_item.prefix || ""],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error processing ${_item.citizenId}:`, error);
|
console.error(`Error processing ${_item.citizenId}:`, error);
|
||||||
|
|
@ -9096,7 +9119,7 @@ export class OrganizationController extends Controller {
|
||||||
*/
|
*/
|
||||||
private sumAllVacantCounts(
|
private sumAllVacantCounts(
|
||||||
map: Map<string, { currentVacantCount: number; nextVacantCount: number }>,
|
map: Map<string, { currentVacantCount: number; nextVacantCount: number }>,
|
||||||
isDraft: boolean
|
isDraft: boolean,
|
||||||
): number {
|
): number {
|
||||||
let sum = 0;
|
let sum = 0;
|
||||||
for (const value of map.values()) {
|
for (const value of map.values()) {
|
||||||
|
|
@ -9111,7 +9134,7 @@ export class OrganizationController extends Controller {
|
||||||
private buildOrgRoots(
|
private buildOrgRoots(
|
||||||
orgRoots: OrgRoot[],
|
orgRoots: OrgRoot[],
|
||||||
positionCounts: PositionCountsByNode,
|
positionCounts: PositionCountsByNode,
|
||||||
isDraft: boolean
|
isDraft: boolean,
|
||||||
) {
|
) {
|
||||||
if (!orgRoots) return [];
|
if (!orgRoots) return [];
|
||||||
return orgRoots
|
return orgRoots
|
||||||
|
|
@ -9135,7 +9158,7 @@ export class OrganizationController extends Controller {
|
||||||
private buildOrgChild1s(
|
private buildOrgChild1s(
|
||||||
orgChild1s: OrgChild1[],
|
orgChild1s: OrgChild1[],
|
||||||
positionCounts: PositionCountsByNode,
|
positionCounts: PositionCountsByNode,
|
||||||
isDraft: boolean
|
isDraft: boolean,
|
||||||
) {
|
) {
|
||||||
if (!orgChild1s) return [];
|
if (!orgChild1s) return [];
|
||||||
return orgChild1s
|
return orgChild1s
|
||||||
|
|
@ -9159,7 +9182,7 @@ export class OrganizationController extends Controller {
|
||||||
private buildOrgChild2s(
|
private buildOrgChild2s(
|
||||||
orgChild2s: OrgChild2[],
|
orgChild2s: OrgChild2[],
|
||||||
positionCounts: PositionCountsByNode,
|
positionCounts: PositionCountsByNode,
|
||||||
isDraft: boolean
|
isDraft: boolean,
|
||||||
) {
|
) {
|
||||||
if (!orgChild2s) return [];
|
if (!orgChild2s) return [];
|
||||||
return orgChild2s
|
return orgChild2s
|
||||||
|
|
@ -9183,7 +9206,7 @@ export class OrganizationController extends Controller {
|
||||||
private buildOrgChild3s(
|
private buildOrgChild3s(
|
||||||
orgChild3s: OrgChild3[],
|
orgChild3s: OrgChild3[],
|
||||||
positionCounts: PositionCountsByNode,
|
positionCounts: PositionCountsByNode,
|
||||||
isDraft: boolean
|
isDraft: boolean,
|
||||||
) {
|
) {
|
||||||
if (!orgChild3s) return [];
|
if (!orgChild3s) return [];
|
||||||
return orgChild3s
|
return orgChild3s
|
||||||
|
|
@ -9207,7 +9230,7 @@ export class OrganizationController extends Controller {
|
||||||
private buildOrgChild4s(
|
private buildOrgChild4s(
|
||||||
orgChild4s: OrgChild4[],
|
orgChild4s: OrgChild4[],
|
||||||
positionCounts: PositionCountsByNode,
|
positionCounts: PositionCountsByNode,
|
||||||
isDraft: boolean
|
isDraft: boolean,
|
||||||
) {
|
) {
|
||||||
if (!orgChild4s) return [];
|
if (!orgChild4s) return [];
|
||||||
return orgChild4s
|
return orgChild4s
|
||||||
|
|
|
||||||
|
|
@ -116,7 +116,12 @@ export class ProfileChangeNameController extends Controller {
|
||||||
setLogDataDiff(req, { before, after: profile });
|
setLogDataDiff(req, { before, after: profile });
|
||||||
|
|
||||||
if (profile != null && profile.keycloak != null) {
|
if (profile != null && profile.keycloak != null) {
|
||||||
const result = await updateName(profile.keycloak, profile.firstName, profile.lastName);
|
const result = await updateName(
|
||||||
|
profile.keycloak,
|
||||||
|
profile.firstName,
|
||||||
|
profile.lastName,
|
||||||
|
profile.prefix,
|
||||||
|
);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw new Error(result.errorMessage);
|
throw new Error(result.errorMessage);
|
||||||
}
|
}
|
||||||
|
|
@ -182,7 +187,12 @@ export class ProfileChangeNameController extends Controller {
|
||||||
|
|
||||||
// ปิดไว้ก่อนเพราะ error ต้องใช้ keycloak ที่มีสิทธิ์ในการ update //update 17/07
|
// ปิดไว้ก่อนเพราะ error ต้องใช้ keycloak ที่มีสิทธิ์ในการ update //update 17/07
|
||||||
if (profile != null && profile.keycloak != null) {
|
if (profile != null && profile.keycloak != null) {
|
||||||
const result = await updateName(profile.keycloak, profile.firstName, profile.lastName);
|
const result = await updateName(
|
||||||
|
profile.keycloak,
|
||||||
|
profile.firstName,
|
||||||
|
profile.lastName,
|
||||||
|
profile.prefix,
|
||||||
|
);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw new Error(result.errorMessage);
|
throw new Error(result.errorMessage);
|
||||||
}
|
}
|
||||||
|
|
@ -197,10 +207,7 @@ export class ProfileChangeNameController extends Controller {
|
||||||
* @param trainingId คีย์ประวัติการเปลี่ยนชื่อ - นามสกุล
|
* @param trainingId คีย์ประวัติการเปลี่ยนชื่อ - นามสกุล
|
||||||
*/
|
*/
|
||||||
@Patch("update-delete/{changeNameId}")
|
@Patch("update-delete/{changeNameId}")
|
||||||
public async updateIsDeleted(
|
public async updateIsDeleted(@Request() req: RequestWithUser, @Path() changeNameId: string) {
|
||||||
@Request() req: RequestWithUser,
|
|
||||||
@Path() changeNameId: string,
|
|
||||||
) {
|
|
||||||
const record = await this.changeNameRepository.findOneBy({ id: changeNameId });
|
const record = await this.changeNameRepository.findOneBy({ id: changeNameId });
|
||||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
if (record.isDeleted === true) {
|
if (record.isDeleted === true) {
|
||||||
|
|
|
||||||
|
|
@ -122,7 +122,12 @@ export class ProfileChangeNameEmployeeController extends Controller {
|
||||||
setLogDataDiff(req, { before, after: profile });
|
setLogDataDiff(req, { before, after: profile });
|
||||||
|
|
||||||
if (profile != null && profile.keycloak != null) {
|
if (profile != null && profile.keycloak != null) {
|
||||||
const result = await updateName(profile.keycloak, profile.firstName, profile.lastName);
|
const result = await updateName(
|
||||||
|
profile.keycloak,
|
||||||
|
profile.firstName,
|
||||||
|
profile.lastName,
|
||||||
|
profile.prefix,
|
||||||
|
);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw new Error(result.errorMessage);
|
throw new Error(result.errorMessage);
|
||||||
}
|
}
|
||||||
|
|
@ -195,16 +200,17 @@ export class ProfileChangeNameEmployeeController extends Controller {
|
||||||
* @param trainingId คีย์ประวัติการเปลี่ยนชื่อ - นามสกุล
|
* @param trainingId คีย์ประวัติการเปลี่ยนชื่อ - นามสกุล
|
||||||
*/
|
*/
|
||||||
@Patch("update-delete/{changeNameId}")
|
@Patch("update-delete/{changeNameId}")
|
||||||
public async updateIsDeleted(
|
public async updateIsDeleted(@Request() req: RequestWithUser, @Path() changeNameId: string) {
|
||||||
@Request() req: RequestWithUser,
|
|
||||||
@Path() changeNameId: string,
|
|
||||||
) {
|
|
||||||
const record = await this.changeNameRepository.findOneBy({ id: changeNameId });
|
const record = await this.changeNameRepository.findOneBy({ id: changeNameId });
|
||||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
if (record.isDeleted === true) {
|
if (record.isDeleted === true) {
|
||||||
return new HttpSuccess();
|
return new HttpSuccess();
|
||||||
}
|
}
|
||||||
await new permission().PermissionOrgUserDelete(req, "SYS_REGISTRY_EMP", record.profileEmployeeId);
|
await new permission().PermissionOrgUserDelete(
|
||||||
|
req,
|
||||||
|
"SYS_REGISTRY_EMP",
|
||||||
|
record.profileEmployeeId,
|
||||||
|
);
|
||||||
const before = structuredClone(record);
|
const before = structuredClone(record);
|
||||||
const history = new ProfileChangeNameHistory();
|
const history = new ProfileChangeNameHistory();
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,12 @@ export class ProfileChangeNameEmployeeTempController extends Controller {
|
||||||
setLogDataDiff(req, { before, after: profile });
|
setLogDataDiff(req, { before, after: profile });
|
||||||
|
|
||||||
if (profile != null && profile.keycloak != null) {
|
if (profile != null && profile.keycloak != null) {
|
||||||
const result = await updateName(profile.keycloak, profile.firstName, profile.lastName);
|
const result = await updateName(
|
||||||
|
profile.keycloak,
|
||||||
|
profile.firstName,
|
||||||
|
profile.lastName,
|
||||||
|
profile.prefix,
|
||||||
|
);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw new Error(result.errorMessage);
|
throw new Error(result.errorMessage);
|
||||||
}
|
}
|
||||||
|
|
@ -187,10 +192,7 @@ export class ProfileChangeNameEmployeeTempController extends Controller {
|
||||||
* @param trainingId คีย์ประวัติการเปลี่ยนชื่อ - นามสกุล
|
* @param trainingId คีย์ประวัติการเปลี่ยนชื่อ - นามสกุล
|
||||||
*/
|
*/
|
||||||
@Patch("update-delete/{changeNameId}")
|
@Patch("update-delete/{changeNameId}")
|
||||||
public async updateIsDeleted(
|
public async updateIsDeleted(@Request() req: RequestWithUser, @Path() changeNameId: string) {
|
||||||
@Request() req: RequestWithUser,
|
|
||||||
@Path() changeNameId: string,
|
|
||||||
) {
|
|
||||||
const record = await this.changeNameRepository.findOneBy({ id: changeNameId });
|
const record = await this.changeNameRepository.findOneBy({ id: changeNameId });
|
||||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
if (record.isDeleted === true) {
|
if (record.isDeleted === true) {
|
||||||
|
|
|
||||||
|
|
@ -5092,7 +5092,12 @@ export class ProfileController extends Controller {
|
||||||
// setLogDataDiff(request, { before, after: record });
|
// setLogDataDiff(request, { before, after: record });
|
||||||
|
|
||||||
if (record != null && record.keycloak != null) {
|
if (record != null && record.keycloak != null) {
|
||||||
const result = await updateName(record.keycloak, record.firstName, record.lastName);
|
const result = await updateName(
|
||||||
|
record.keycloak,
|
||||||
|
record.firstName,
|
||||||
|
record.lastName,
|
||||||
|
record.prefix,
|
||||||
|
);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw new Error(result.errorMessage);
|
throw new Error(result.errorMessage);
|
||||||
}
|
}
|
||||||
|
|
@ -5406,7 +5411,12 @@ export class ProfileController extends Controller {
|
||||||
setLogDataDiff(request, { before, after: record });
|
setLogDataDiff(request, { before, after: record });
|
||||||
|
|
||||||
if (record != null && record.keycloak != null) {
|
if (record != null && record.keycloak != null) {
|
||||||
const result = await updateName(record.keycloak, record.firstName, record.lastName);
|
const result = await updateName(
|
||||||
|
record.keycloak,
|
||||||
|
record.firstName,
|
||||||
|
record.lastName,
|
||||||
|
record.prefix,
|
||||||
|
);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
throw new Error(result.errorMessage);
|
throw new Error(result.errorMessage);
|
||||||
}
|
}
|
||||||
|
|
@ -5507,13 +5517,13 @@ export class ProfileController extends Controller {
|
||||||
@Get("history/user")
|
@Get("history/user")
|
||||||
async getHistoryProfileByUser(@Request() request: RequestWithUser) {
|
async getHistoryProfileByUser(@Request() request: RequestWithUser) {
|
||||||
const profile = await this.profileRepo.findOne({
|
const profile = await this.profileRepo.findOne({
|
||||||
where: { keycloak: request.user.sub }
|
where: { keycloak: request.user.sub },
|
||||||
});
|
});
|
||||||
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
|
||||||
const profileHistory = await this.profileHistoryRepo.find({
|
const profileHistory = await this.profileHistoryRepo.find({
|
||||||
where: { profileId: profile.id },
|
where: { profileId: profile.id },
|
||||||
order: { createdAt: "ASC" }
|
order: { createdAt: "ASC" },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (profileHistory.length == 0) {
|
if (profileHistory.length == 0) {
|
||||||
|
|
@ -5522,12 +5532,12 @@ export class ProfileController extends Controller {
|
||||||
...profile,
|
...profile,
|
||||||
birthDateOld: profile?.birthDate,
|
birthDateOld: profile?.birthDate,
|
||||||
profileId: profile.id,
|
profileId: profile.id,
|
||||||
id: undefined
|
id: undefined,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
const firstRecord = await this.profileHistoryRepo.find({
|
const firstRecord = await this.profileHistoryRepo.find({
|
||||||
where: { profileId: profile.id },
|
where: { profileId: profile.id },
|
||||||
order: { createdAt: "ASC" }
|
order: { createdAt: "ASC" },
|
||||||
});
|
});
|
||||||
return new HttpSuccess(firstRecord);
|
return new HttpSuccess(firstRecord);
|
||||||
}
|
}
|
||||||
|
|
@ -6482,13 +6492,13 @@ export class ProfileController extends Controller {
|
||||||
async getProfileHistory(@Path() id: string, @Request() req: RequestWithUser) {
|
async getProfileHistory(@Path() id: string, @Request() req: RequestWithUser) {
|
||||||
//await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", id); //ไม่แน่ใจOFFปิดไว้ก่อน
|
//await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", id); //ไม่แน่ใจOFFปิดไว้ก่อน
|
||||||
const profile = await this.profileRepo.findOne({
|
const profile = await this.profileRepo.findOne({
|
||||||
where: { id: id }
|
where: { id: id },
|
||||||
});
|
});
|
||||||
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
|
||||||
|
|
||||||
const profileHistory = await this.profileHistoryRepo.find({
|
const profileHistory = await this.profileHistoryRepo.find({
|
||||||
where: { profileId: id },
|
where: { profileId: id },
|
||||||
order: { createdAt: "ASC" }
|
order: { createdAt: "ASC" },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (profileHistory.length == 0) {
|
if (profileHistory.length == 0) {
|
||||||
|
|
@ -6497,12 +6507,12 @@ export class ProfileController extends Controller {
|
||||||
...profile,
|
...profile,
|
||||||
birthDateOld: profile?.birthDate,
|
birthDateOld: profile?.birthDate,
|
||||||
profileId: id,
|
profileId: id,
|
||||||
id: undefined
|
id: undefined,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
const firstRecord = await this.profileHistoryRepo.find({
|
const firstRecord = await this.profileHistoryRepo.find({
|
||||||
where: { profileId: id },
|
where: { profileId: id },
|
||||||
order: { createdAt: "ASC" }
|
order: { createdAt: "ASC" },
|
||||||
});
|
});
|
||||||
return new HttpSuccess(firstRecord);
|
return new HttpSuccess(firstRecord);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ import {
|
||||||
getUserByUsername,
|
getUserByUsername,
|
||||||
changeUserPassword,
|
changeUserPassword,
|
||||||
resetPassword,
|
resetPassword,
|
||||||
|
updateUserAttributes,
|
||||||
} from "../keycloak";
|
} from "../keycloak";
|
||||||
import { AppDataSource } from "../database/data-source";
|
import { AppDataSource } from "../database/data-source";
|
||||||
import { Profile } from "../entities/Profile";
|
import { Profile } from "../entities/Profile";
|
||||||
|
|
@ -137,6 +138,13 @@ export class KeycloakController extends Controller {
|
||||||
}
|
}
|
||||||
profile.email = body.email == null ? _null : body.email;
|
profile.email = body.email == null ? _null : body.email;
|
||||||
await this.profileRepo.save(profile);
|
await this.profileRepo.save(profile);
|
||||||
|
|
||||||
|
// Update Keycloak with profile prefix after profile is loaded
|
||||||
|
await updateUserAttributes(userId, {
|
||||||
|
profileId: [profile.id],
|
||||||
|
prefix: [profile.prefix || ""],
|
||||||
|
});
|
||||||
|
|
||||||
if (body.roles != null && body.roles.length > 0) {
|
if (body.roles != null && body.roles.length > 0) {
|
||||||
const roleKeycloak = await this.roleKeycloakRepo.find({
|
const roleKeycloak = await this.roleKeycloakRepo.find({
|
||||||
where: { id: In(body.roles) },
|
where: { id: In(body.roles) },
|
||||||
|
|
@ -195,6 +203,12 @@ export class KeycloakController extends Controller {
|
||||||
}
|
}
|
||||||
profile.email = body.email == null ? _null : body.email;
|
profile.email = body.email == null ? _null : body.email;
|
||||||
await this.profileEmpRepo.save(profile);
|
await this.profileEmpRepo.save(profile);
|
||||||
|
// Update Keycloak with profile prefix after profile is loaded
|
||||||
|
await updateUserAttributes(userId, {
|
||||||
|
profileId: [profile.id],
|
||||||
|
prefix: [profile.prefix || ""],
|
||||||
|
});
|
||||||
|
|
||||||
if (body.roles != null && body.roles.length > 0) {
|
if (body.roles != null && body.roles.length > 0) {
|
||||||
const roleKeycloak = await this.roleKeycloakRepo.find({
|
const roleKeycloak = await this.roleKeycloakRepo.find({
|
||||||
where: { id: In(body.roles) },
|
where: { id: In(body.roles) },
|
||||||
|
|
@ -475,14 +489,14 @@ export class KeycloakController extends Controller {
|
||||||
@Request() req: RequestWithUser,
|
@Request() req: RequestWithUser,
|
||||||
@Body()
|
@Body()
|
||||||
body: {
|
body: {
|
||||||
page: number,
|
page: number;
|
||||||
pageSize: number,
|
pageSize: number;
|
||||||
keyword: string | null,
|
keyword: string | null;
|
||||||
type: string,
|
type: string;
|
||||||
isAll: boolean,
|
isAll: boolean;
|
||||||
node: number | null,
|
node: number | null;
|
||||||
nodeId: string | null,
|
nodeId: string | null;
|
||||||
}
|
},
|
||||||
) {
|
) {
|
||||||
let checkChildFromRole: any = {};
|
let checkChildFromRole: any = {};
|
||||||
|
|
||||||
|
|
@ -558,10 +572,14 @@ export class KeycloakController extends Controller {
|
||||||
.andWhere(
|
.andWhere(
|
||||||
new Brackets((qb) => {
|
new Brackets((qb) => {
|
||||||
qb.orWhere(
|
qb.orWhere(
|
||||||
body.keyword != null && body.keyword != "" ? `profile.citizenId like '%${body.keyword}%'` : "1=1",
|
body.keyword != null && body.keyword != ""
|
||||||
|
? `profile.citizenId like '%${body.keyword}%'`
|
||||||
|
: "1=1",
|
||||||
)
|
)
|
||||||
.orWhere(
|
.orWhere(
|
||||||
body.keyword != null && body.keyword != "" ? `profile.email like '%${body.keyword}%'` : "1=1",
|
body.keyword != null && body.keyword != ""
|
||||||
|
? `profile.email like '%${body.keyword}%'`
|
||||||
|
: "1=1",
|
||||||
)
|
)
|
||||||
.orWhere(
|
.orWhere(
|
||||||
body.keyword != null && body.keyword != ""
|
body.keyword != null && body.keyword != ""
|
||||||
|
|
@ -737,6 +755,12 @@ export class KeycloakController extends Controller {
|
||||||
}
|
}
|
||||||
profile.email = body.email == null ? _null : body.email;
|
profile.email = body.email == null ? _null : body.email;
|
||||||
await this.profileEmpRepo.save(profile);
|
await this.profileEmpRepo.save(profile);
|
||||||
|
// Update Keycloak with profile prefix after profile is loaded
|
||||||
|
await updateUserAttributes(userId, {
|
||||||
|
profileId: [profile.id],
|
||||||
|
prefix: [profile.prefix || ""],
|
||||||
|
});
|
||||||
|
|
||||||
if (body.roles != null && body.roles.length > 0) {
|
if (body.roles != null && body.roles.length > 0) {
|
||||||
const roleKeycloak = await this.roleKeycloakRepo.find({
|
const roleKeycloak = await this.roleKeycloakRepo.find({
|
||||||
where: { id: In(body.roles) },
|
where: { id: In(body.roles) },
|
||||||
|
|
@ -783,7 +807,6 @@ export class KeycloakController extends Controller {
|
||||||
|
|
||||||
@Get("user/role/{id}")
|
@Get("user/role/{id}")
|
||||||
async getRoleUser(@Request() req: RequestWithUser, @Path("id") id: string) {
|
async getRoleUser(@Request() req: RequestWithUser, @Path("id") id: string) {
|
||||||
|
|
||||||
const profile = await this.profileRepo.findOne({
|
const profile = await this.profileRepo.findOne({
|
||||||
where: { keycloak: id },
|
where: { keycloak: id },
|
||||||
relations: ["roleKeycloaks"],
|
relations: ["roleKeycloaks"],
|
||||||
|
|
@ -791,8 +814,8 @@ export class KeycloakController extends Controller {
|
||||||
|
|
||||||
if (
|
if (
|
||||||
req.user.sub === id &&
|
req.user.sub === id &&
|
||||||
req.user.role.some(x => x === 'ADMIN') &&
|
req.user.role.some((x) => x === "ADMIN") &&
|
||||||
!req.user.role.some(x => x === 'SUPER_ADMIN')
|
!req.user.role.some((x) => x === "SUPER_ADMIN")
|
||||||
) {
|
) {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่มีสิทธิ์เข้าถึงข้อมูลนี้");
|
throw new HttpError(HttpStatus.NOT_FOUND, "ไม่มีสิทธิ์เข้าถึงข้อมูลนี้");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -298,6 +298,7 @@ export async function updateName(
|
||||||
userId: string,
|
userId: string,
|
||||||
firstName: string,
|
firstName: string,
|
||||||
lastName: string,
|
lastName: string,
|
||||||
|
prefix: string,
|
||||||
// opts: Record<string, any>,
|
// opts: Record<string, any>,
|
||||||
) {
|
) {
|
||||||
// const { password, ...rest } = opts;
|
// const { password, ...rest } = opts;
|
||||||
|
|
@ -315,6 +316,9 @@ export async function updateName(
|
||||||
// ...rest,
|
// ...rest,
|
||||||
firstName,
|
firstName,
|
||||||
lastName,
|
lastName,
|
||||||
|
attributes: {
|
||||||
|
prefix,
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
}).catch((e) => console.log("Keycloak Error: ", e));
|
}).catch((e) => console.log("Keycloak Error: ", e));
|
||||||
|
|
||||||
|
|
@ -971,3 +975,44 @@ export async function getAllUsersPaginated(
|
||||||
enabled: v.enabled === true || v.enabled === "true",
|
enabled: v.enabled === true || v.enabled === "true",
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create keycloak user by given username and password with roles
|
||||||
|
*
|
||||||
|
* Client must have permission to manage realm's user
|
||||||
|
*
|
||||||
|
* @returns user uuid or true if success, false otherwise.
|
||||||
|
*/
|
||||||
|
export async function createUserHaveProfile(
|
||||||
|
username: string,
|
||||||
|
password: string,
|
||||||
|
profileId: string,
|
||||||
|
prefix: string,
|
||||||
|
opts?: Record<string, any>,
|
||||||
|
token?: string,
|
||||||
|
) {
|
||||||
|
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users`, {
|
||||||
|
// prettier-ignore
|
||||||
|
headers: {
|
||||||
|
"authorization": `Bearer ${token || await getToken()}`,
|
||||||
|
"content-type": `application/json`,
|
||||||
|
},
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify({
|
||||||
|
enabled: true,
|
||||||
|
credentials: [{ type: "password", value: password, temporary: false }],
|
||||||
|
username,
|
||||||
|
...opts,
|
||||||
|
}),
|
||||||
|
}).catch((e) => console.log("Keycloak Error: ", e));
|
||||||
|
|
||||||
|
if (!res) return false;
|
||||||
|
if (!res.ok) {
|
||||||
|
// return Boolean(console.error("Keycloak Error Response: ", await res.json()));
|
||||||
|
return await res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
const path = res.headers.get("Location");
|
||||||
|
const id = path?.split("/").at(-1);
|
||||||
|
return id || true;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import { ProfileEmployee } from "../entities/ProfileEmployee";
|
||||||
// import { EmployeePosMaster } from "../entities/EmployeePosMaster";
|
// import { EmployeePosMaster } from "../entities/EmployeePosMaster";
|
||||||
// import { OrgRoot } from "../entities/OrgRoot";
|
// import { OrgRoot } from "../entities/OrgRoot";
|
||||||
import {
|
import {
|
||||||
createUser,
|
createUserHaveProfile,
|
||||||
getUser,
|
getUser,
|
||||||
getUserByUsername,
|
getUserByUsername,
|
||||||
updateUserAttributes,
|
updateUserAttributes,
|
||||||
|
|
@ -809,12 +809,18 @@ export class KeycloakAttributeService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new user in Keycloak
|
// Create new user in Keycloak
|
||||||
const createResult = await createUser(profile.citizenId, "P@ssw0rd", {
|
const createResult = await createUserHaveProfile(
|
||||||
firstName: profile.firstName || "",
|
profile.citizenId,
|
||||||
lastName: profile.lastName || "",
|
"P@ssw0rd",
|
||||||
email: profile.email || undefined,
|
profile.id,
|
||||||
enabled: true,
|
profile.prefix,
|
||||||
});
|
{
|
||||||
|
firstName: profile.firstName || "",
|
||||||
|
lastName: profile.lastName || "",
|
||||||
|
email: profile.email || undefined,
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
if (!createResult || typeof createResult !== "string") {
|
if (!createResult || typeof createResult !== "string") {
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue