From 56f4d3b85b3bd82bbb5e8930181a64f904021ae6 Mon Sep 17 00:00:00 2001 From: Bright Date: Wed, 23 Apr 2025 17:15:34 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B9=80=E0=B8=9E=E0=B8=B4=E0=B9=88=E0=B8=A1?= =?UTF-8?q?=E0=B8=9F=E0=B8=B4=E0=B8=A5=E0=B8=94=E0=B9=8C=20=E0=B8=AA?= =?UTF-8?q?=E0=B8=96=E0=B8=B2=E0=B8=99=E0=B8=B0=E0=B8=AD=E0=B8=B5=E0=B9=80?= =?UTF-8?q?=E0=B8=A1=E0=B8=A5=20(=E0=B8=A5=E0=B8=B9=E0=B8=81=E0=B8=88?= =?UTF-8?q?=E0=B9=89=E0=B8=B2=E0=B8=87)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/ProfileEmployeeController.ts | 128 ++++++++++++++++++ .../ProfileEmployeeTempController.ts | 128 ++++++++++++++++++ src/entities/ProfileEmployee.ts | 8 ++ 3 files changed, 264 insertions(+) diff --git a/src/controllers/ProfileEmployeeController.ts b/src/controllers/ProfileEmployeeController.ts index 24828cf1..39a3b485 100644 --- a/src/controllers/ProfileEmployeeController.ts +++ b/src/controllers/ProfileEmployeeController.ts @@ -5458,4 +5458,132 @@ export class ProfileEmployeeController extends Controller { } return new HttpSuccess(_profile); } + + async sendVerifyEmail( + @Request() req: RequestWithUser, + @Body() + body: { + profileId: string; + email: string; + subject: string; + }, + ) { + const jwt = require("jsonwebtoken"); + const token = jwt.sign( + { email_id: body.email, profileId: body.profileId }, + process.env.AUTH_ACCOUNT_SECRET, + { expiresIn: "15m" }, + ); + const link = process.env.VITE_URL_USER + "/verifyemail?upn=" + token; + + await new CallAPI() + .PostData(req, "/placement/noti/send-mail", { + subject: body.subject, + body: link, + Email: body.email, + }) + .catch((error) => { + console.error("Error calling API:", error); + }); + return new HttpSuccess(); + } + + /** + * API แก้ไขเบอร์โทรศัพท์ ลูกจ้างประจำ + * + * @summary แก้ไขเบอร์โทรศัพท์ ลูกจ้างประจำ (USER) + * + */ + @Put("updatePhoneNumber/user") + async updatePhoneNumber( + @Request() request: RequestWithUser, + @Body() + body: { + phone: string; + }, + ) { + const profile = await this.profileRepo.findOne({ + relations: { + posLevel: true, + posType: true, + }, + where: { keycloak: request.user.sub }, + }); + + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileEmployeeHistory(); + + Object.assign(profile, body); + Object.assign(history, { ...profile, id: undefined }); + + profile.lastUpdateUserId = request.user.sub; + profile.lastUpdateFullName = request.user.name; + profile.lastUpdatedAt = new Date(); + history.lastUpdateUserId = request.user.sub; + history.lastUpdateFullName = request.user.name; + history.createdUserId = request.user.sub; + history.createdFullName = request.user.name; + history.createdAt = new Date(); + history.lastUpdatedAt = new Date(); + + await Promise.all([ + this.profileRepo.save(profile, { data: request }), + this.profileHistoryRepo.save(history, { data: request }), + ]); + return new HttpSuccess(); + } + + /** + * API แก้ไขอีเมล ลูกจ้างประจำ + * + * @summary แก้ไขอีเมล ลูกจ้างประจำ (USER) + * + */ + @Put("updateEmail/user") + async updateEmail( + @Request() request: RequestWithUser, + @Body() + body: { + email: string; + }, + ) { + const profile = await this.profileRepo.findOne({ + relations: { + posLevel: true, + posType: true, + }, + where: { keycloak: request.user.sub }, + }); + + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileEmployeeHistory(); + + Object.assign(profile, body); + Object.assign(history, { ...profile, id: undefined }); + + profile.statusEmail = "NOT_VERIFIED"; + profile.lastUpdateUserId = request.user.sub; + profile.lastUpdateFullName = request.user.name; + profile.lastUpdatedAt = new Date(); + history.lastUpdateUserId = request.user.sub; + history.lastUpdateFullName = request.user.name; + history.createdUserId = request.user.sub; + history.createdFullName = request.user.name; + history.createdAt = new Date(); + history.lastUpdatedAt = new Date(); + + await Promise.all([ + this.profileRepo.save(profile, { data: request }), + this.profileHistoryRepo.save(history, { data: request }), + ]); + const verifyemailBody = { + profileId: profile.id, + email: body.email, + subject: "ยืนยันอีเมล", + }; + this.sendVerifyEmail(request, verifyemailBody); + return new HttpSuccess(); + } } diff --git a/src/controllers/ProfileEmployeeTempController.ts b/src/controllers/ProfileEmployeeTempController.ts index e450733c..a3e7734b 100644 --- a/src/controllers/ProfileEmployeeTempController.ts +++ b/src/controllers/ProfileEmployeeTempController.ts @@ -4023,4 +4023,132 @@ export class ProfileEmployeeTempController extends Controller { } return new HttpSuccess(_profile); } + + async sendVerifyEmail( + @Request() req: RequestWithUser, + @Body() + body: { + profileId: string; + email: string; + subject: string; + }, + ) { + const jwt = require("jsonwebtoken"); + const token = jwt.sign( + { email_id: body.email, profileId: body.profileId }, + process.env.AUTH_ACCOUNT_SECRET, + { expiresIn: "15m" }, + ); + const link = process.env.VITE_URL_USER + "/verifyemail?upn=" + token; + + await new CallAPI() + .PostData(req, "/placement/noti/send-mail", { + subject: body.subject, + body: link, + Email: body.email, + }) + .catch((error) => { + console.error("Error calling API:", error); + }); + return new HttpSuccess(); + } + + /** + * API แก้ไขเบอร์โทรศัพท์ ลูกจ้างชั่วคราว + * + * @summary แก้ไขเบอร์โทรศัพท์ ลูกจ้างชั่วคราว (USER) + * + */ + @Put("updatePhoneNumber/user") + async updatePhoneNumber( + @Request() request: RequestWithUser, + @Body() + body: { + phone: string; + }, + ) { + const profile = await this.profileRepo.findOne({ + relations: { + posLevel: true, + posType: true, + }, + where: { keycloak: request.user.sub }, + }); + + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileEmployeeHistory(); + + Object.assign(profile, body); + Object.assign(history, { ...profile, id: undefined }); + + profile.lastUpdateUserId = request.user.sub; + profile.lastUpdateFullName = request.user.name; + profile.lastUpdatedAt = new Date(); + history.lastUpdateUserId = request.user.sub; + history.lastUpdateFullName = request.user.name; + history.createdUserId = request.user.sub; + history.createdFullName = request.user.name; + history.createdAt = new Date(); + history.lastUpdatedAt = new Date(); + + await Promise.all([ + this.profileRepo.save(profile, { data: request }), + this.profileHistoryRepo.save(history, { data: request }), + ]); + return new HttpSuccess(); + } + + /** + * API แก้ไขอีเมล ลูกจ้างชั่วคราว + * + * @summary แก้ไขอีเมล ลูกจ้างชั่วคราว (USER) + * + */ + @Put("updateEmail/user") + async updateEmail( + @Request() request: RequestWithUser, + @Body() + body: { + email: string; + }, + ) { + const profile = await this.profileRepo.findOne({ + relations: { + posLevel: true, + posType: true, + }, + where: { keycloak: request.user.sub }, + }); + + if (!profile) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล"); + + const history = new ProfileEmployeeHistory(); + + Object.assign(profile, body); + Object.assign(history, { ...profile, id: undefined }); + + profile.statusEmail = "NOT_VERIFIED"; + profile.lastUpdateUserId = request.user.sub; + profile.lastUpdateFullName = request.user.name; + profile.lastUpdatedAt = new Date(); + history.lastUpdateUserId = request.user.sub; + history.lastUpdateFullName = request.user.name; + history.createdUserId = request.user.sub; + history.createdFullName = request.user.name; + history.createdAt = new Date(); + history.lastUpdatedAt = new Date(); + + await Promise.all([ + this.profileRepo.save(profile, { data: request }), + this.profileHistoryRepo.save(history, { data: request }), + ]); + const verifyemailBody = { + profileId: profile.id, + email: body.email, + subject: "ยืนยันอีเมล", + }; + this.sendVerifyEmail(request, verifyemailBody); + return new HttpSuccess(); + } } diff --git a/src/entities/ProfileEmployee.ts b/src/entities/ProfileEmployee.ts index d7ba9d5b..56a113dc 100644 --- a/src/entities/ProfileEmployee.ts +++ b/src/entities/ProfileEmployee.ts @@ -178,6 +178,14 @@ export class ProfileEmployee extends EntityBase { }) email: string; + @Column({ + nullable: true, + comment: "สถานะอีเมล", //VERIFIED = ยืนยัน, NOT_VERIFIED = ไม่ได้ยืนยัน + length: 20, + default: null, + }) + statusEmail: string; + @Column({ nullable: true, length: 20,