diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index d939742..5b27c8d 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -8,6 +8,7 @@ import { RegisterGuard } from 'src/guards/register.guard'; import { RolesGuard } from 'src/guards/roles.guard'; import { Roles } from 'src/decorators/roles.decorator'; import { JwtAuthGuard } from 'src/guards/jwt-auth.guard'; +import { extractSubdomain } from 'src/utils/helper'; @ApiTags('Auth') @Controller() @@ -74,7 +75,7 @@ export class AuthController { @Post('2/register') async registerb(@Body() body: AuthLoginDTO) { - return this.authService.registerUser(body, 1); + return this.authService.registerUser(body, 2); } @UseGuards(RegisterGuard) @@ -189,9 +190,16 @@ export class AuthController { @Roles('sa') @Post('1/sendmail') @HttpCode(200) - async sendMaila(@Body() body: SendMailDTO) { + async sendMaila(@Body() body: SendMailDTO, @Req() req: Request) { // let token = await this.jwtService.generateToken({ email: mail }) - return this.authService.sendMail(body); + let appDomain; + if (req.headers.origin) { + appDomain = extractSubdomain(req.headers.origin) + } + else { + appDomain = "" + } + return this.authService.sendMail(body, appDomain); } @@ -199,9 +207,16 @@ export class AuthController { @Roles('psa') @Post('2/sendmail') @HttpCode(200) - async sendMailb(@Body() body: SendMailDTO) { + async sendMailb(@Body() body: SendMailDTO, @Req() req: Request) { // let token = await this.jwtService.generateToken({ email: mail }) - return this.authService.sendMail(body); + let appDomain; + if (req.headers.origin) { + appDomain = extractSubdomain(req.headers.origin) + } + else { + appDomain = "" + } + return this.authService.sendMail(body, appDomain); } // @Get('checkRoles') diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index e9b5aa8..db5abdd 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -842,7 +842,7 @@ export class AuthService { } } - async getMailTemplate(body: SendMailDTO, token: string, source: string): Promise { + async getMailTemplate(body: SendMailDTO, token: string, source: string, appDomain:string): Promise { switch (body.P_MAIL_TYPE) { case MailTypeDTO.REGISTER_CLIENT: @@ -852,7 +852,7 @@ export class AuthService { templateName: 'a', variables: { to: body.P_TO, - url: `https://client.alphaomegainfosys.com/register` + url: `https://${appDomain}.alphaomegainfosys.com/register` } } case MailTypeDTO.FORGOT_PASSWORD: @@ -862,9 +862,9 @@ export class AuthService { templateName: 'b', variables: { to: body.P_TO, - url: `${source === 'ua' ? `https://policy.alphaomegainfosys.com/forgot-password/${token}` - : source === 'sa' ? `https://sp.alphaomegainfosys.com/forgot-password/${token}` - : source === 'ca' ? `https://client.alphaomegainfosys.com/forgot-password/${token}` : ''}` + url: `${source === 'ua' ? `https://${appDomain}.alphaomegainfosys.com/forgot-password/${token}` + : source === 'sa' ? `https://${appDomain}.alphaomegainfosys.com/forgot-password/${token}` + : source === 'ca' ? `https://${appDomain}.alphaomegainfosys.com/forgot-password/${token}` : ''}` } } default: @@ -872,7 +872,7 @@ export class AuthService { } } - async sendMail(body: SendMailDTO) { + async sendMail(body: SendMailDTO, appDomain: string) { let token: string; let ROLE: string = ""; let uuid: string | undefined = undefined; @@ -897,7 +897,7 @@ export class AuthService { token = await this.generateToken({ P_TO: body.P_TO, uuid: uuid }) - const mailTemplate: MailTemplateDTO | null = await this.getMailTemplate(body, token, ROLE); + const mailTemplate: MailTemplateDTO | null = await this.getMailTemplate(body, token, ROLE, appDomain); if (!mailTemplate) { throw new BadRequestException(); diff --git a/src/oracle/user-maintenance/user-maintenance.controller.ts b/src/oracle/user-maintenance/user-maintenance.controller.ts index dfc187b..8d7f8af 100644 --- a/src/oracle/user-maintenance/user-maintenance.controller.ts +++ b/src/oracle/user-maintenance/user-maintenance.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Get, Param, Patch, Post, Query, UseGuards } from '@nestjs/common'; +import { Body, Controller, Get, Param, Patch, Post, Query, Req, UseGuards } from '@nestjs/common'; import { UserMaintenanceService } from './user-maintenance.service'; import { ApiTags } from '@nestjs/swagger'; @@ -10,6 +10,7 @@ import { import { Roles } from 'src/decorators/roles.decorator'; import { JwtAuthGuard } from 'src/guards/jwt-auth.guard'; import { RolesGuard } from 'src/guards/roles.guard'; +import { Request } from 'express'; @ApiTags('User Maintenance - Oracle') @UseGuards(JwtAuthGuard, RolesGuard) @@ -74,8 +75,8 @@ export class UserMaintenanceController { @Post('CreateClientLogins') @Roles('sa') - async CreateClientLogins(@Body() body: CreateClientLoginsDTO) { - return await this.userMaintenanceService.CreateClientLogins(body); + async CreateClientLogins(@Body() body: CreateClientLoginsDTO, @Req() req: Request) { + return await this.userMaintenanceService.CreateClientLogins(body, req); } // validate email diff --git a/src/oracle/user-maintenance/user-maintenance.service.ts b/src/oracle/user-maintenance/user-maintenance.service.ts index 4b76a1b..30f6c09 100644 --- a/src/oracle/user-maintenance/user-maintenance.service.ts +++ b/src/oracle/user-maintenance/user-maintenance.service.ts @@ -2,7 +2,7 @@ import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common'; import { OracleDBService } from 'src/db/db.service'; import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import * as oracledb from 'oracledb'; -import { closeOracleDbConnection, fetchCursor, handleError } from 'src/utils/helper'; +import { closeOracleDbConnection, extractSubdomain, fetchCursor, handleError } from 'src/utils/helper'; import { SPID_DTO, @@ -12,6 +12,7 @@ import { import { BadRequestException } from 'src/exceptions/badRequest.exception'; import { AuthService } from 'src/auth/auth.service'; import { MailTypeDTO } from 'src/auth/auth.dto'; +import { Request } from 'express'; interface RoleDetail { [key: string]: any; } @@ -356,7 +357,7 @@ export class UserMaintenanceService { } - async CreateClientLogins(body: CreateClientLoginsDTO) { + async CreateClientLogins(body: CreateClientLoginsDTO, req: Request) { let connection; try { connection = await this.oracleDBService.getConnection(); @@ -392,7 +393,15 @@ export class UserMaintenanceService { throw new BadRequestException(fres[0].ERRORMESG) } - const mailRes = await this.authService.sendMail({ P_TO: body.P_USERID, P_MAIL_TYPE: MailTypeDTO.REGISTER_CLIENT }) + let appDomain; + if (req.headers.origin) { + appDomain = extractSubdomain(req.headers.origin) + } + else { + appDomain = "" + } + + const mailRes = await this.authService.sendMail({ P_TO: body.P_USERID, P_MAIL_TYPE: MailTypeDTO.REGISTER_CLIENT }, appDomain) if (mailRes.statusCode !== 200) { throw new InternalServerException(); diff --git a/src/pg/user-maintenance/user-maintenance.controller.ts b/src/pg/user-maintenance/user-maintenance.controller.ts index a0e1ba6..522f2e0 100644 --- a/src/pg/user-maintenance/user-maintenance.controller.ts +++ b/src/pg/user-maintenance/user-maintenance.controller.ts @@ -1,10 +1,11 @@ -import { Body, Controller, Get, Param, Patch, Post, UseGuards } from '@nestjs/common'; +import { Body, Controller, Get, Param, Patch, Post, Req, UseGuards } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { Roles } from 'src/decorators/roles.decorator'; import { JwtAuthGuard } from 'src/guards/jwt-auth.guard'; import { RolesGuard } from 'src/guards/roles.guard'; import { UserMaintenanceService } from './user-maintenance.service'; import { CreateClientLoginsDTO, CreateSPLoginsDTO, CreateUSCIBLoginsDTO, EMAIL_DTO, GetCarnetDetailsbyCarnetStatusDTO, SPID_CLIENTID_DTO, SPID_DTO, SPID_EMAIL_DTO, USERID_DTO } from 'src/dto/property.dto'; +import { Request } from 'express'; @ApiTags('User Maintenance - PG') @UseGuards(JwtAuthGuard, RolesGuard) @@ -54,8 +55,8 @@ export class UserMaintenanceController { @Post('CreateSPLogins') @Roles('psa') - async CreateSPLogins(@Body() body: CreateSPLoginsDTO) { - return await this.userMaintenanceService.CreateSPLogins(body); + async CreateSPLogins(@Body() body: CreateSPLoginsDTO, @Req() req: Request) { + return await this.userMaintenanceService.CreateSPLogins(body,req); } // CLIENT LOGINS @@ -74,8 +75,8 @@ export class UserMaintenanceController { @Post('CreateClientLogins') @Roles('psa') - async CreateClientLogins(@Body() body: CreateClientLoginsDTO) { - return await this.userMaintenanceService.CreateClientLogins(body); + async CreateClientLogins(@Body() body: CreateClientLoginsDTO, @Req() req: Request) { + return await this.userMaintenanceService.CreateClientLogins(body, req); } // validate email diff --git a/src/pg/user-maintenance/user-maintenance.service.ts b/src/pg/user-maintenance/user-maintenance.service.ts index a0c683d..c7faee5 100644 --- a/src/pg/user-maintenance/user-maintenance.service.ts +++ b/src/pg/user-maintenance/user-maintenance.service.ts @@ -1,12 +1,13 @@ import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common'; import { CreateClientLoginsDTO, CreateSPLoginsDTO, CreateUSCIBLoginsDTO, EMAIL_DTO, GetCarnetDetailsbyCarnetStatusDTO, SPID_CLIENTID_DTO, SPID_DTO, SPID_EMAIL_DTO, USERID_DTO } from 'src/dto/property.dto'; -import { checkPgUserDefinedErrors, handlePgError, normalizeKeysToUpperCase, releasePgClient, ResponseStatus, toUpperCaseKeys } from 'src/utils/helper'; +import { checkPgUserDefinedErrors, extractSubdomain, handlePgError, normalizeKeysToUpperCase, releasePgClient, ResponseStatus, toUpperCaseKeys } from 'src/utils/helper'; import { PoolClient } from 'pg'; import { PgDBService } from 'src/db/db.service'; import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import { AuthService } from 'src/auth/auth.service'; import { MailTypeDTO } from 'src/auth/auth.dto'; +import { Request } from 'express'; @Injectable() export class UserMaintenanceService { @@ -244,7 +245,7 @@ export class UserMaintenanceService { } - async CreateSPLogins(body: CreateSPLoginsDTO) { + async CreateSPLogins(body: CreateSPLoginsDTO, req: Request) { let client: PoolClient | null = null; @@ -278,7 +279,15 @@ export class UserMaintenanceService { checkPgUserDefinedErrors(fetchResult); - const mailRes = await this.authService.sendMail({ P_TO: body.P_USERID, P_MAIL_TYPE: MailTypeDTO.REGISTER_SP }) + let appDomain; + if (req.headers.origin) { + appDomain = extractSubdomain(req.headers.origin) + } + else { + appDomain = "" + } + + const mailRes = await this.authService.sendMail({ P_TO: body.P_USERID, P_MAIL_TYPE: MailTypeDTO.REGISTER_SP }, appDomain) if (mailRes.statusCode !== 200) { throw new InternalServerException(); @@ -369,7 +378,7 @@ export class UserMaintenanceService { } } - async CreateClientLogins(body: CreateClientLoginsDTO) { + async CreateClientLogins(body: CreateClientLoginsDTO, req: Request) { let client: PoolClient | null = null; @@ -401,7 +410,15 @@ export class UserMaintenanceService { checkPgUserDefinedErrors(fetchResult); - const mailRes = await this.authService.sendMail({ P_TO: body.P_USERID, P_MAIL_TYPE: MailTypeDTO.REGISTER_CLIENT }) + let appDomain; + if (req.headers.origin) { + appDomain = extractSubdomain(req.headers.origin) + } + else { + appDomain = "" + } + + const mailRes = await this.authService.sendMail({ P_TO: body.P_USERID, P_MAIL_TYPE: MailTypeDTO.REGISTER_CLIENT }, appDomain) if (mailRes.statusCode !== 200) { throw new InternalServerException(); diff --git a/src/utils/helper.ts b/src/utils/helper.ts index ae328fa..52ce69d 100644 --- a/src/utils/helper.ts +++ b/src/utils/helper.ts @@ -40,6 +40,32 @@ export enum ResponseStatus { } +export function extractSubdomain(domain: string): string | null { + try { + const url = new URL(domain); + const hostname = url.hostname; // e.g., "der.asd.com", "localhost", "121.1.21.123" + + // Return the full hostname if it's an IP address or 'localhost' + if (/^\d{1,3}(\.\d{1,3}){3}$/.test(hostname) || hostname === 'localhost') { + return hostname; + } + + const parts = hostname.split('.'); + + if (parts.length < 3) { + // Not enough parts to have a subdomain (e.g., "asd.com") + return null; + } + + // Return the subdomain (leftmost part) + return parts[0]; + } catch (e) { + console.error('Invalid domain:', domain, e); + return null; + } +} + + export function normalizeKeysToUpperCase(obj) { return Object.keys(obj).reduce((acc, key) => { acc[key.toUpperCase()] = obj[key];