org root add responsibility

This commit is contained in:
kittapath 2025-03-14 13:26:37 +07:00
parent 11c79abc31
commit 856315b7bc
8 changed files with 74 additions and 45 deletions

View file

@ -30,7 +30,7 @@ async function main() {
); );
app.use(express.json()); app.use(express.json());
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));
// app.use(logMiddleware); app.use(logMiddleware);
app.use("/", express.static("static")); app.use("/", express.static("static"));
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));

View file

@ -54,6 +54,7 @@ export class OrgChild1Controller {
} }
const getOrgChild1 = { const getOrgChild1 = {
orgChild1Id: orgChild1.id, orgChild1Id: orgChild1.id,
responsibility: orgChild1.responsibility,
orgRootName: orgChild1.orgRoot.orgRootName, orgRootName: orgChild1.orgRoot.orgRootName,
orgName: `${orgChild1.orgChild1Name}/${orgChild1.orgRoot.orgRootName}`, orgName: `${orgChild1.orgChild1Name}/${orgChild1.orgRoot.orgRootName}`,
orgChild1Name: orgChild1.orgChild1Name, orgChild1Name: orgChild1.orgChild1Name,
@ -323,7 +324,7 @@ export class OrgChild1Controller {
// if(requestBody.orgChild1ShortName == rootIdExits.orgRootShortName){ // if(requestBody.orgChild1ShortName == rootIdExits.orgRootShortName){
// throw new HttpError(HttpStatusCode.NOT_FOUND, "อักษรย่อนี้ซ้ำกับอักษรย่อหน่วยงาน"); // throw new HttpError(HttpStatusCode.NOT_FOUND, "อักษรย่อนี้ซ้ำกับอักษรย่อหน่วยงาน");
// } // }
const _null:any = null; const _null: any = null;
const before = structuredClone(child1); const before = structuredClone(child1);
child1.lastUpdateUserId = request.user.sub; child1.lastUpdateUserId = request.user.sub;
@ -332,14 +333,14 @@ export class OrgChild1Controller {
child1.orgRevisionId = String(rootIdExits?.orgRevisionId); child1.orgRevisionId = String(rootIdExits?.orgRevisionId);
child1.orgRootId = String(rootIdExits?.id); child1.orgRootId = String(rootIdExits?.id);
child1.responsibility = child1.responsibility || ""; child1.responsibility = child1.responsibility || "";
this.child1Repository.merge(child1,{ this.child1Repository.merge(child1, {
orgChild1Name: requestBody.orgChild1Name, orgChild1Name: requestBody.orgChild1Name,
orgChild1ShortName: requestBody.orgChild1ShortName, orgChild1ShortName: requestBody.orgChild1ShortName,
orgChild1Code: requestBody.orgChild1Code, orgChild1Code: requestBody.orgChild1Code,
DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null,
DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null,
SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null,
JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null,
isOfficer: requestBody.isOfficer, isOfficer: requestBody.isOfficer,
isInformation: requestBody.isInformation, isInformation: requestBody.isInformation,
orgChild1PhoneEx: requestBody.orgChild1PhoneEx, orgChild1PhoneEx: requestBody.orgChild1PhoneEx,

View file

@ -67,6 +67,7 @@ export class OrgChild2Controller extends Controller {
} }
const getOrgChild2 = { const getOrgChild2 = {
orgChild2Id: orgChild2.id, orgChild2Id: orgChild2.id,
responsibility: orgChild2.responsibility,
orgRootName: orgChild2.orgRoot.orgRootName, orgRootName: orgChild2.orgRoot.orgRootName,
orgName: `${orgChild2.orgChild2Name}/${orgChild2.orgChild1.orgChild1Name}/${orgChild2.orgRoot.orgRootName}`, orgName: `${orgChild2.orgChild2Name}/${orgChild2.orgChild1.orgChild1Name}/${orgChild2.orgRoot.orgRootName}`,
orgChild2Name: orgChild2.orgChild2Name, orgChild2Name: orgChild2.orgChild2Name,
@ -230,7 +231,7 @@ export class OrgChild2Controller extends Controller {
if (!child2) { if (!child2) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
} }
const _null:any = null; const _null: any = null;
const before = structuredClone(child2); const before = structuredClone(child2);
child2.lastUpdateUserId = request.user.sub; child2.lastUpdateUserId = request.user.sub;
child2.lastUpdateFullName = request.user.name; child2.lastUpdateFullName = request.user.name;
@ -245,9 +246,9 @@ export class OrgChild2Controller extends Controller {
orgChild2Rank: requestBody.orgChild2Rank != null ? requestBody.orgChild2Rank : _null, orgChild2Rank: requestBody.orgChild2Rank != null ? requestBody.orgChild2Rank : _null,
orgChild2RankSub: requestBody.orgChild2RankSub, orgChild2RankSub: requestBody.orgChild2RankSub,
DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null,
DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null,
SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null,
JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null,
orgChild2PhoneEx: requestBody.orgChild2PhoneEx, orgChild2PhoneEx: requestBody.orgChild2PhoneEx,
orgChild2PhoneIn: requestBody.orgChild2PhoneIn, orgChild2PhoneIn: requestBody.orgChild2PhoneIn,
orgChild2Fax: requestBody.orgChild2Fax, orgChild2Fax: requestBody.orgChild2Fax,

View file

@ -51,6 +51,7 @@ export class OrgChild3Controller {
} }
const getOrgChild3 = { const getOrgChild3 = {
orgChild3Id: orgChild3.id, orgChild3Id: orgChild3.id,
responsibility: orgChild3.responsibility,
orgRootName: orgChild3.orgRoot.orgRootName, orgRootName: orgChild3.orgRoot.orgRootName,
orgName: `${orgChild3.orgChild3Name}/${orgChild3.orgChild2.orgChild2Name}/${orgChild3.orgChild1.orgChild1Name}/${orgChild3.orgRoot.orgRootName}`, orgName: `${orgChild3.orgChild3Name}/${orgChild3.orgChild2.orgChild2Name}/${orgChild3.orgChild1.orgChild1Name}/${orgChild3.orgRoot.orgRootName}`,
orgChild3Name: orgChild3.orgChild3Name, orgChild3Name: orgChild3.orgChild3Name,
@ -184,7 +185,7 @@ export class OrgChild3Controller {
if (!child3) { if (!child3) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
} }
const _null:any = null; const _null: any = null;
const before = structuredClone(child3); const before = structuredClone(child3);
child3.lastUpdateUserId = request.user.sub; child3.lastUpdateUserId = request.user.sub;
child3.lastUpdateFullName = request.user.name; child3.lastUpdateFullName = request.user.name;
@ -200,9 +201,9 @@ export class OrgChild3Controller {
orgChild3Rank: requestBody.orgChild3Rank != null ? requestBody.orgChild3Rank : _null, orgChild3Rank: requestBody.orgChild3Rank != null ? requestBody.orgChild3Rank : _null,
orgChild3RankSub: requestBody.orgChild3RankSub, orgChild3RankSub: requestBody.orgChild3RankSub,
DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null,
DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null,
SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null,
JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null,
orgChild3PhoneEx: requestBody.orgChild3PhoneEx, orgChild3PhoneEx: requestBody.orgChild3PhoneEx,
orgChild3PhoneIn: requestBody.orgChild3PhoneIn, orgChild3PhoneIn: requestBody.orgChild3PhoneIn,
orgChild3Fax: requestBody.orgChild3Fax, orgChild3Fax: requestBody.orgChild3Fax,

View file

@ -64,6 +64,7 @@ export class OrgChild4Controller extends Controller {
} }
const getOrgChild4 = { const getOrgChild4 = {
orgChild4Id: orgChild4.id, orgChild4Id: orgChild4.id,
responsibility: orgChild4.responsibility,
orgRootName: orgChild4.orgRoot.orgRootName, orgRootName: orgChild4.orgRoot.orgRootName,
orgName: `${orgChild4.orgChild4Name}/${orgChild4.orgChild3.orgChild3Name}/${orgChild4.orgChild2.orgChild2Name}/${orgChild4.orgChild1.orgChild1Name}/${orgChild4.orgRoot.orgRootName}`, orgName: `${orgChild4.orgChild4Name}/${orgChild4.orgChild3.orgChild3Name}/${orgChild4.orgChild2.orgChild2Name}/${orgChild4.orgChild1.orgChild1Name}/${orgChild4.orgRoot.orgRootName}`,
orgChild4Name: orgChild4.orgChild4Name, orgChild4Name: orgChild4.orgChild4Name,
@ -230,7 +231,7 @@ export class OrgChild4Controller extends Controller {
if (!child4) { if (!child4) {
throw new HttpError(HttpStatusCode.NOT_FOUND, "not found."); throw new HttpError(HttpStatusCode.NOT_FOUND, "not found.");
} }
const _null:any = null; const _null: any = null;
const before = structuredClone(child4); const before = structuredClone(child4);
child4.lastUpdateUserId = request.user.sub; child4.lastUpdateUserId = request.user.sub;
child4.lastUpdateFullName = request.user.name; child4.lastUpdateFullName = request.user.name;
@ -247,9 +248,9 @@ export class OrgChild4Controller extends Controller {
orgChild4Rank: requestBody.orgChild4Rank != null ? requestBody.orgChild4Rank : _null, orgChild4Rank: requestBody.orgChild4Rank != null ? requestBody.orgChild4Rank : _null,
orgChild4RankSub: requestBody.orgChild4RankSub, orgChild4RankSub: requestBody.orgChild4RankSub,
DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null, DEPARTMENT_CODE: requestBody.DEPARTMENT_CODE != null ? requestBody.DEPARTMENT_CODE : _null,
DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null, DIVISION_CODE: requestBody.DIVISION_CODE != null ? requestBody.DIVISION_CODE : _null,
SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null, SECTION_CODE: requestBody.SECTION_CODE != null ? requestBody.SECTION_CODE : _null,
JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null, JOB_CODE: requestBody.JOB_CODE != null ? requestBody.JOB_CODE : _null,
orgChild4PhoneEx: requestBody.orgChild4PhoneEx, orgChild4PhoneEx: requestBody.orgChild4PhoneEx,
orgChild4PhoneIn: requestBody.orgChild4PhoneIn, orgChild4PhoneIn: requestBody.orgChild4PhoneIn,
orgChild4Fax: requestBody.orgChild4Fax, orgChild4Fax: requestBody.orgChild4Fax,

View file

@ -64,6 +64,7 @@ export class OrgRootController extends Controller {
} }
const getOrgRoot = { const getOrgRoot = {
orgRootId: orgRoot.id, orgRootId: orgRoot.id,
responsibility: orgRoot.responsibility,
orgRootName: orgRoot.orgRootName, orgRootName: orgRoot.orgRootName,
orgName: "-", orgName: "-",
orgRootShortName: orgRoot.orgRootShortName, orgRootShortName: orgRoot.orgRootShortName,

View file

@ -39,7 +39,7 @@ import { Profile } from "../entities/Profile";
import { ProfileEmployee } from "../entities/ProfileEmployee"; import { ProfileEmployee } from "../entities/ProfileEmployee";
import { RequestWithUser } from "../middlewares/user"; import { RequestWithUser } from "../middlewares/user";
import HttpSuccess from "../interfaces/http-success"; import HttpSuccess from "../interfaces/http-success";
import { Brackets, In, IsNull } from "typeorm"; import { Brackets, In, IsNull, Not } from "typeorm";
import HttpError from "../interfaces/http-error"; import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status"; import HttpStatus from "../interfaces/http-status";
import { RoleKeycloak } from "../entities/RoleKeycloak"; import { RoleKeycloak } from "../entities/RoleKeycloak";
@ -830,7 +830,7 @@ export class KeycloakController extends Controller {
return result; return result;
} }
@Post("user/asdasdasd") @Post("user/create")
@Security("bearerAuth", ["system", "admin"]) @Security("bearerAuth", ["system", "admin"])
async createUserImport( async createUserImport(
@Request() request: { user: { sub: string; preferred_username: string } }, @Request() request: { user: { sub: string; preferred_username: string } },
@ -872,7 +872,7 @@ export class KeycloakController extends Controller {
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
const result = await addUserRoles( const result = await addUserRoles(
userId, userId,
list.filter((v) => v.id == "8a1a0dc9-304c-4e5b-a90a-65f841048212"), list.filter((v) => v.id == "f8619dc2-dc0d-4aab-957f-66bdf905e9d0"),
); );
if (!result) { if (!result) {
@ -882,13 +882,32 @@ export class KeycloakController extends Controller {
_item.keycloak = userId; _item.keycloak = userId;
} }
const roleKeycloak = await this.roleKeycloakRepo.find({ const roleKeycloak = await this.roleKeycloakRepo.find({
where: { id: "8a1a0dc9-304c-4e5b-a90a-65f841048212" }, where: { id: "f8619dc2-dc0d-4aab-957f-66bdf905e9d0" },
}); });
if (_item) { if (_item) {
_item.roleKeycloaks = Array.from(new Set([..._item.roleKeycloaks, ...roleKeycloak])); _item.roleKeycloaks = Array.from(new Set([..._item.roleKeycloaks, ...roleKeycloak]));
this.profileRepo.save(_item); this.profileRepo.save(_item);
} }
} }
return "555+"; return "";
}
@Post("user/change-password-all")
async changeUserPasswordAll(
@Request() request: { user: { sub: string; preferred_username: string } },
) {
const profiles = await this.profileRepo.find({
where: {
keycloak: Not(IsNull()),
},
});
for await (const _item of profiles) {
const result = await changeUserPassword(_item.keycloak, "P@ssw0rd");
if (!result) {
continue;
}
}
return;
} }
} }

View file

@ -4,8 +4,8 @@ const KC_URL = process.env.KC_URL;
const KC_REALMS = process.env.KC_REALMS; const KC_REALMS = process.env.KC_REALMS;
const KC_CLIENT_ID = process.env.KC_SERVICE_ACCOUNT_CLIENT_ID; const KC_CLIENT_ID = process.env.KC_SERVICE_ACCOUNT_CLIENT_ID;
const KC_SECRET = process.env.KC_SERVICE_ACCOUNT_SECRET; const KC_SECRET = process.env.KC_SERVICE_ACCOUNT_SECRET;
const AUTH_ACCOUNT_SECRET = process.env.AUTH_ACCOUNT_SECRET const AUTH_ACCOUNT_SECRET = process.env.AUTH_ACCOUNT_SECRET;
const API_KEY = process.env.API_KEY const API_KEY = process.env.API_KEY;
let token: string | null = null; let token: string | null = null;
let decoded: DecodedJwt | null = null; let decoded: DecodedJwt | null = null;
@ -742,7 +742,6 @@ export async function removeUserGroup(userId: string, groupId: string) {
// Function to change user password // Function to change user password
export async function changeUserPassword(userId: string, newPassword: string) { export async function changeUserPassword(userId: string, newPassword: string) {
try { try {
console.log(await getToken());
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/reset-password`, { const res = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/reset-password`, {
// prettier-ignore // prettier-ignore
headers: { headers: {
@ -788,27 +787,33 @@ export async function resetPassword(username: string) {
// const tokenData = await tokenResponse.json(); // const tokenData = await tokenResponse.json();
// const adminToken = tokenData.access_token; // const adminToken = tokenData.access_token;
const users = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users?email=${encodeURIComponent(username)}`, { const users = await fetch(
headers: { `${KC_URL}/admin/realms/${KC_REALMS}/users?email=${encodeURIComponent(username)}`,
"authorization": `Bearer ${await getToken()}`, {
headers: {
authorization: `Bearer ${await getToken()}`,
// "authorization": `Bearer ${adminToken}`, // "authorization": `Bearer ${adminToken}`,
"content-type": `application/json`, "content-type": `application/json`,
}, },
}); },
if(!users.ok) { );
if (!users.ok) {
return false; return false;
} }
const usersData = await users.json(); const usersData = await users.json();
const userId = usersData[0].id; const userId = usersData[0].id;
const resetResponse = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/execute-actions-email`, { const resetResponse = await fetch(
method: "PUT", `${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/execute-actions-email`,
headers: { {
"Authorization": `Bearer ${await getToken()}`, method: "PUT",
// "Authorization": `Bearer ${adminToken}`, headers: {
"Content-Type": "application/json" Authorization: `Bearer ${await getToken()}`,
// "Authorization": `Bearer ${adminToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(["UPDATE_PASSWORD"]),
}, },
body: JSON.stringify(["UPDATE_PASSWORD"]) );
});
if (!resetResponse.ok) { if (!resetResponse.ok) {
return false; return false;
} }