From 657d885e32ee88f46b0a009c3a28305fe6a742ab Mon Sep 17 00:00:00 2001 From: AdisakKanthawilang Date: Thu, 21 Nov 2024 11:38:24 +0700 Subject: [PATCH] sort and verify email --- src/controllers/PositionController.ts | 6 ++- src/controllers/ProfileController.ts | 66 ++++++++++++++------------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/controllers/PositionController.ts b/src/controllers/PositionController.ts index b87ee041..3363fb74 100644 --- a/src/controllers/PositionController.ts +++ b/src/controllers/PositionController.ts @@ -1482,7 +1482,8 @@ export class PositionController extends Controller { }, relations: ["posLevel", "posType", "posExecutive"], order: { - createdAt: "ASC", + posType:{posTypeRank: "ASC"}, + posLevel:{posLevelRank: "ASC"} }, }); @@ -1908,7 +1909,8 @@ export class PositionController extends Controller { }, relations: ["posLevel", "posType", "posExecutive"], order: { - createdAt: "ASC", + posType:{posTypeRank: "ASC"}, + posLevel:{posLevelRank: "ASC"} }, }); diff --git a/src/controllers/ProfileController.ts b/src/controllers/ProfileController.ts index 06635fe0..8a970d22 100644 --- a/src/controllers/ProfileController.ts +++ b/src/controllers/ProfileController.ts @@ -70,6 +70,8 @@ import { ProfileFamilyCoupleHistory } from "../entities/ProfileFamilyCoupleHisto import { viewDirector } from "../entities/view/viewDirector"; import { viewDirectorActing } from "../entities/view/viewDirectorActing"; import CallAPI from "../interfaces/call-api"; +import { log } from "console"; +import { Subject } from "typeorm/persistence/Subject.js"; @Route("api/v1/org/profile") @Tags("Profile") @Security("bearerAuth") @@ -1873,13 +1875,6 @@ export class ProfileController extends Controller { return new HttpSuccess(profile.id); } - /** - * API ส่งลิงค์ยืนยัน Email - * - * @summary ส่งลิงค์ยืนยัน Email - * - */ - @Post("send-verify-email") async sendVerifyEmail( @Request() req:RequestWithUser, @Body() @@ -1889,29 +1884,22 @@ export class ProfileController extends Controller { subject: string; } ) { - - const profile = await this.profileRepo.findOne({ - where:{ - id: body.profileId, - email: body.email - } - }); - const jwt = require('jsonwebtoken'); - const token = jwt.sign({email_id: body.email}, "Stack", {expiresIn: '24h'}); - console.log("[token]",token); - const link = process.env.API_URL + "/" + token; + const token = jwt.sign({email_id: body.email, profileId: body.profileId}, process.env.AUTH_ACCOUNT_SECRET, {expiresIn: '5m'}); + // console.log("[token]",token); + const link = process.env.URL + "/verifyemail/" + token; + // console.log("[link]",link); - const detail = null; await new CallAPI() - .PostData(req, "/placement/notisend-mail", { + .PostData(req, "/placement/noti/send-mail", { subject: body.subject, - body: detail, + body: link, Email: body.email, }) .catch((error) => { console.error("Error calling API:", error); - }); + }); + return new HttpSuccess(); } /** @@ -1920,19 +1908,29 @@ export class ProfileController extends Controller { * @summary ยืนยัน Email * */ - @Post("gen-link-verify-email") + @Post("verify-email") async genLinkVerifyEmail( - @Request() req:RequestWithUser, @Body() body:{ - email: string; + token: string; }, ) { - // const profile = await this.profileRepo.findOne({ - // where:{ - // id: body.profileId, - // email: body.email - // } - // }); + 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) } @@ -7982,6 +7980,12 @@ export class ProfileController extends Controller { 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(); }