Merge branch 'develop' of github.com:Frappet/hrms-api-org into develop

This commit is contained in:
kittapath 2025-02-21 19:11:15 +07:00
commit 54554d140e
3 changed files with 52 additions and 4 deletions

View file

@ -7050,7 +7050,7 @@ export class OrganizationController extends Controller {
orgRootId: orgRoot.id,
orgRootDnaId: orgRoot.ancestorDNA,
orgLevel: 1,
orgName: `${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`,
orgName: `${orgChild1.orgChild1Name} ${orgRoot.orgRootName}`,
orgTreeName: orgChild1.orgChild1Name,
orgTreeShortName: orgChild1.orgChild1ShortName,
orgTreeCode: orgChild1.orgChild1Code,
@ -7178,7 +7178,7 @@ export class OrganizationController extends Controller {
orgRootId: orgChild1.id,
orgRootDnaId: orgChild1.ancestorDNA,
orgLevel: 2,
orgName: `${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`,
orgName: `${orgChild2.orgChild2Name} ${orgChild1.orgChild1Name} ${orgRoot.orgRootName}`,
orgTreeName: orgChild2.orgChild2Name,
orgTreeShortName: orgChild2.orgChild2ShortName,
orgTreeCode: orgChild2.orgChild2Code,
@ -7316,7 +7316,7 @@ export class OrganizationController extends Controller {
orgRootId: orgChild2.id,
orgRootDnaId: orgChild2.ancestorDNA,
orgLevel: 3,
orgName: `${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`,
orgName: `${orgChild3.orgChild3Name} ${orgChild2.orgChild2Name} ${orgChild1.orgChild1Name} ${orgRoot.orgRootName}`,
orgTreeName: orgChild3.orgChild3Name,
orgTreeShortName: orgChild3.orgChild3ShortName,
orgTreeCode: orgChild3.orgChild3Code,
@ -7461,7 +7461,7 @@ export class OrganizationController extends Controller {
orgRootId: orgChild3.id,
orgRootDnaId: orgChild3.ancestorDNA,
orgLevel: 4,
orgName: `${orgChild4.orgChild4Name}/${orgChild3.orgChild3Name}/${orgChild2.orgChild2Name}/${orgChild1.orgChild1Name}/${orgRoot.orgRootName}`,
orgName: `${orgChild4.orgChild4Name} ${orgChild3.orgChild3Name} ${orgChild2.orgChild2Name} ${orgChild1.orgChild1Name} ${orgRoot.orgRootName}`,
orgTreeName: orgChild4.orgChild4Name,
orgTreeShortName: orgChild4.orgChild4ShortName,
orgTreeCode: orgChild4.orgChild4Code,

View file

@ -32,6 +32,7 @@ import {
enableStatus,
getUserByUsername,
changeUserPassword,
resetPassword
} from "../keycloak";
import { AppDataSource } from "../database/data-source";
import { Profile } from "../entities/Profile";
@ -828,4 +829,19 @@ export class KeycloakController extends Controller {
}
return result;
}
@Post("user/reset-password")
async forgetPassword(
@Request() request: { user: { sub: string; preferred_username: string } },
@Body()
body: {
username: string;
},
) {
const result = await resetPassword(body.username);
if (!result) {
throw new Error("Failed. Cannot change password.");
}
return result;
}
}

View file

@ -761,3 +761,35 @@ export async function changeUserPassword(userId: string, newPassword: string) {
return false;
}
}
// Function to reset password
export async function resetPassword(username: string) {
try {
const users = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users?email=${encodeURIComponent(username)}`, {
headers: {
"authorization": `Bearer ${await getToken()}`,
"content-type": `application/json`,
},
});
if(!users.ok) {
return false;
}
const usersData = await users.json();
const userId = usersData[0].id;
const resetResponse = await fetch(`${KC_URL}/admin/realms/${KC_REALMS}/users/${userId}/execute-actions-email`, {
method: "PUT",
headers: {
"Authorization": `Bearer ${await getToken()}`,
"Content-Type": "application/json"
},
body: JSON.stringify(["UPDATE_PASSWORD"])
});
if (!resetResponse.ok) {
return false;
}
return { message: "Password reset email sent" };
} catch (error) {
console.error("Error triggering password reset:", error);
return false;
}
}