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,