diff --git a/src/controllers/OrganizationUnauthorizeController.ts b/src/controllers/OrganizationUnauthorizeController.ts index 4fdfbf92..caf55741 100644 --- a/src/controllers/OrganizationUnauthorizeController.ts +++ b/src/controllers/OrganizationUnauthorizeController.ts @@ -11,6 +11,7 @@ import { calculateRetireDate } from "../interfaces/utils"; import { EmployeePosMaster } from "../entities/EmployeePosMaster"; import { Profile } from "../entities/Profile"; import { ProfileEmployee } from "../entities/ProfileEmployee"; +import HttpStatus from "../interfaces/http-status"; @Route("api/v1/org/unauthorize") @Tags("OrganizationUnauthorize") @@ -1123,4 +1124,33 @@ export class OrganizationUnauthorizeController extends Controller { return new HttpSuccess(mapProfile); } + + /** + * API ยืนยัน Email + * + * @summary ยืนยัน Email + * + */ + @Post("verify-email") + async genLinkVerifyEmail(@Body() body: { token: string }) { + const jwt = require("jsonwebtoken"); + const secretKey = process.env.AUTH_ACCOUNT_SECRET || "defaultSecretKey"; + const decodedToken = jwt.verify(body.token, secretKey); + // console.log("[email]",decodedToken); + // console.log("[1]",decodedToken.email_id); + + const profile = await this.profileRepo.findOne({ + where: { + id: decodedToken.profileId, + email: decodedToken.email_id, + }, + }); + if (!profile) { + throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์"); + } + + Object.assign(profile, body); + profile.statusEmail = "VERIFIED"; + await this.profileRepo.save(profile); + } } diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 2c468dca..0752fb01 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -1887,7 +1887,7 @@ export class ProfileController extends Controller { const jwt = require('jsonwebtoken'); const token = jwt.sign({email_id: body.email, profileId: body.profileId}, process.env.AUTH_ACCOUNT_SECRET, {expiresIn: '15m'}); // console.log("[token]",token); - const link = process.env.URL + "/verifyemail/" + token; + const link = process.env.URL + "/verifyemail?upn=" + token; // console.log("[link]",link); await new CallAPI() @@ -1902,35 +1902,6 @@ export class ProfileController extends Controller { return new HttpSuccess(); } - /** - * API ยืนยัน Email - * - * @summary ยืนยัน Email - * - */ - @Post("verify-email") - async genLinkVerifyEmail(@Body() body: { token: string }) { - const jwt = require("jsonwebtoken"); - const secretKey = process.env.AUTH_ACCOUNT_SECRET || "defaultSecretKey"; - const decodedToken = jwt.verify(body.token, secretKey); - // console.log("[email]",decodedToken); - // console.log("[1]",decodedToken.email_id); - - const profile = await this.profileRepo.findOne({ - where: { - id: decodedToken.profileId, - email: decodedToken.email_id, - }, - }); - if (!profile) { - throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูลโปรไฟล์"); - } - - Object.assign(profile, body); - profile.statusEmail = "VERIFIED"; - await this.profileRepo.save(profile); - } - /** * API ออกคำสั่ง คำสั่งให้ข้าราชการที่มีผลการทดลองปฏิบัติหน้าที่ราชการไม่ต่ำกว่ามาตรฐานที่กำหนดรับราชการต่อไป * diff --git a/src/controllers/UserController.ts b/src/controllers/UserController.ts index dafa6c60..ab083d6a 100644 --- a/src/controllers/UserController.ts +++ b/src/controllers/UserController.ts @@ -80,9 +80,6 @@ export class KeycloakController extends Controller { return userDataWithRoles; } - // async getUser(@Path() id: string) { - // return await getUser(id); - // } @Post("user") @Security("bearerAuth", ["system", "admin"]) @@ -102,11 +99,10 @@ export class KeycloakController extends Controller { const userId = await createUser(body.username, body.password, { firstName: body.firstName, lastName: body.lastName, - email: body.email, + // email: body.email, }); if (typeof userId !== "string") { - // throw new Error("ไม่สามารถติดต่อกับระบบจัดการผู้ใช้งานได้"); throw new Error(userId.errorMessage); } @@ -138,14 +134,6 @@ export class KeycloakController extends Controller { updatedByUserId: request.user.sub, }; - // await elasticsearch.index({ - // index: DEFAULT_INDEX!, - // document: folderData, - // refresh: "wait_for", - // }); - - // io.getInstance()?.emit("FolderCreate", folderData); - const profile = await this.profileRepo.findOne({ where: { id: body.profileId, @@ -154,7 +142,9 @@ export class KeycloakController extends Controller { if (profile) { let _null: any = null; - profile.keycloak = userId; + if (typeof userId === "string") { + profile.keycloak = userId; + } profile.email = body.email == null ? _null : body.email; await this.profileRepo.save(profile); if (body.roles != null && body.roles.length > 0) { @@ -249,6 +239,18 @@ export class KeycloakController extends Controller { @Post("{userId}/role") async addRole(@Path() userId: string, @Body() body: { role: string[] }) { + + const list = await getRoles(); + if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); + const result = await addUserRoles( + userId, + list.filter((v) => body.role.includes(v.id)), + ); + + if (!result) { + throw new Error("Failed. Cannot set user's role."); + } + const roleKeycloak = await this.roleKeycloakRepo.find({ where: { id: In(body.role) }, }); @@ -261,26 +263,19 @@ export class KeycloakController extends Controller { where: { keycloak: userId }, relations: ["roleKeycloaks"], }); - if (!profileEmp) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); - profileEmp.roleKeycloaks = Array.from( - new Set([...profileEmp.roleKeycloaks, ...roleKeycloak]), - ); - this.profileEmpRepo.save(profileEmp); - } else { + // if (!profileEmp) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + if(profileEmp) { + profileEmp.roleKeycloaks = Array.from( + new Set([...profileEmp.roleKeycloaks, ...roleKeycloak]), + ); + this.profileEmpRepo.save(profileEmp); + } + } + else { profile.roleKeycloaks = Array.from(new Set([...profile.roleKeycloaks, ...roleKeycloak])); this.profileRepo.save(profile); } - const list = await getRoles(); - - if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server."); - - const result = await addUserRoles( - userId, - list.filter((v) => body.role.includes(v.id)), - ); - - if (!result) throw new Error("Failed. Cannot set user's role."); return new HttpSuccess(); }