mail domain

This commit is contained in:
Kallesh B S 2025-09-24 16:56:25 +05:30
parent 7ea099d611
commit 9a19d9660e
7 changed files with 97 additions and 28 deletions

View File

@ -8,6 +8,7 @@ import { RegisterGuard } from 'src/guards/register.guard';
import { RolesGuard } from 'src/guards/roles.guard'; import { RolesGuard } from 'src/guards/roles.guard';
import { Roles } from 'src/decorators/roles.decorator'; import { Roles } from 'src/decorators/roles.decorator';
import { JwtAuthGuard } from 'src/guards/jwt-auth.guard'; import { JwtAuthGuard } from 'src/guards/jwt-auth.guard';
import { extractSubdomain } from 'src/utils/helper';
@ApiTags('Auth') @ApiTags('Auth')
@Controller() @Controller()
@ -74,7 +75,7 @@ export class AuthController {
@Post('2/register') @Post('2/register')
async registerb(@Body() body: AuthLoginDTO) { async registerb(@Body() body: AuthLoginDTO) {
return this.authService.registerUser(body, 1); return this.authService.registerUser(body, 2);
} }
@UseGuards(RegisterGuard) @UseGuards(RegisterGuard)
@ -189,9 +190,16 @@ export class AuthController {
@Roles('sa') @Roles('sa')
@Post('1/sendmail') @Post('1/sendmail')
@HttpCode(200) @HttpCode(200)
async sendMaila(@Body() body: SendMailDTO) { async sendMaila(@Body() body: SendMailDTO, @Req() req: Request) {
// let token = await this.jwtService.generateToken({ email: mail }) // 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') @Roles('psa')
@Post('2/sendmail') @Post('2/sendmail')
@HttpCode(200) @HttpCode(200)
async sendMailb(@Body() body: SendMailDTO) { async sendMailb(@Body() body: SendMailDTO, @Req() req: Request) {
// let token = await this.jwtService.generateToken({ email: mail }) // 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') // @Get('checkRoles')

View File

@ -842,7 +842,7 @@ export class AuthService {
} }
} }
async getMailTemplate(body: SendMailDTO, token: string, source: string): Promise<MailTemplateDTO | null> { async getMailTemplate(body: SendMailDTO, token: string, source: string, appDomain:string): Promise<MailTemplateDTO | null> {
switch (body.P_MAIL_TYPE) { switch (body.P_MAIL_TYPE) {
case MailTypeDTO.REGISTER_CLIENT: case MailTypeDTO.REGISTER_CLIENT:
@ -852,7 +852,7 @@ export class AuthService {
templateName: 'a', templateName: 'a',
variables: { variables: {
to: body.P_TO, to: body.P_TO,
url: `https://client.alphaomegainfosys.com/register` url: `https://${appDomain}.alphaomegainfosys.com/register`
} }
} }
case MailTypeDTO.FORGOT_PASSWORD: case MailTypeDTO.FORGOT_PASSWORD:
@ -862,9 +862,9 @@ export class AuthService {
templateName: 'b', templateName: 'b',
variables: { variables: {
to: body.P_TO, to: body.P_TO,
url: `${source === 'ua' ? `https://policy.alphaomegainfosys.com/forgot-password/${token}` url: `${source === 'ua' ? `https://${appDomain}.alphaomegainfosys.com/forgot-password/${token}`
: source === 'sa' ? `https://sp.alphaomegainfosys.com/forgot-password/${token}` : source === 'sa' ? `https://${appDomain}.alphaomegainfosys.com/forgot-password/${token}`
: source === 'ca' ? `https://client.alphaomegainfosys.com/forgot-password/${token}` : ''}` : source === 'ca' ? `https://${appDomain}.alphaomegainfosys.com/forgot-password/${token}` : ''}`
} }
} }
default: default:
@ -872,7 +872,7 @@ export class AuthService {
} }
} }
async sendMail(body: SendMailDTO) { async sendMail(body: SendMailDTO, appDomain: string) {
let token: string; let token: string;
let ROLE: string = ""; let ROLE: string = "";
let uuid: string | undefined = undefined; let uuid: string | undefined = undefined;
@ -897,7 +897,7 @@ export class AuthService {
token = await this.generateToken({ P_TO: body.P_TO, uuid: uuid }) 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) { if (!mailTemplate) {
throw new BadRequestException(); throw new BadRequestException();

View File

@ -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 { UserMaintenanceService } from './user-maintenance.service';
import { ApiTags } from '@nestjs/swagger'; import { ApiTags } from '@nestjs/swagger';
@ -10,6 +10,7 @@ import {
import { Roles } from 'src/decorators/roles.decorator'; import { Roles } from 'src/decorators/roles.decorator';
import { JwtAuthGuard } from 'src/guards/jwt-auth.guard'; import { JwtAuthGuard } from 'src/guards/jwt-auth.guard';
import { RolesGuard } from 'src/guards/roles.guard'; import { RolesGuard } from 'src/guards/roles.guard';
import { Request } from 'express';
@ApiTags('User Maintenance - Oracle') @ApiTags('User Maintenance - Oracle')
@UseGuards(JwtAuthGuard, RolesGuard) @UseGuards(JwtAuthGuard, RolesGuard)
@ -74,8 +75,8 @@ export class UserMaintenanceController {
@Post('CreateClientLogins') @Post('CreateClientLogins')
@Roles('sa') @Roles('sa')
async CreateClientLogins(@Body() body: CreateClientLoginsDTO) { async CreateClientLogins(@Body() body: CreateClientLoginsDTO, @Req() req: Request) {
return await this.userMaintenanceService.CreateClientLogins(body); return await this.userMaintenanceService.CreateClientLogins(body, req);
} }
// validate email // validate email

View File

@ -2,7 +2,7 @@ import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
import { OracleDBService } from 'src/db/db.service'; import { OracleDBService } from 'src/db/db.service';
import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import { InternalServerException } from 'src/exceptions/internalServerError.exception';
import * as oracledb from 'oracledb'; import * as oracledb from 'oracledb';
import { closeOracleDbConnection, fetchCursor, handleError } from 'src/utils/helper'; import { closeOracleDbConnection, extractSubdomain, fetchCursor, handleError } from 'src/utils/helper';
import { import {
SPID_DTO, SPID_DTO,
@ -12,6 +12,7 @@ import {
import { BadRequestException } from 'src/exceptions/badRequest.exception'; import { BadRequestException } from 'src/exceptions/badRequest.exception';
import { AuthService } from 'src/auth/auth.service'; import { AuthService } from 'src/auth/auth.service';
import { MailTypeDTO } from 'src/auth/auth.dto'; import { MailTypeDTO } from 'src/auth/auth.dto';
import { Request } from 'express';
interface RoleDetail { [key: string]: any; } interface RoleDetail { [key: string]: any; }
@ -356,7 +357,7 @@ export class UserMaintenanceService {
} }
async CreateClientLogins(body: CreateClientLoginsDTO) { async CreateClientLogins(body: CreateClientLoginsDTO, req: Request) {
let connection; let connection;
try { try {
connection = await this.oracleDBService.getConnection(); connection = await this.oracleDBService.getConnection();
@ -392,7 +393,15 @@ export class UserMaintenanceService {
throw new BadRequestException(fres[0].ERRORMESG) 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) { if (mailRes.statusCode !== 200) {
throw new InternalServerException(); throw new InternalServerException();

View File

@ -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 { ApiTags } from '@nestjs/swagger';
import { Roles } from 'src/decorators/roles.decorator'; import { Roles } from 'src/decorators/roles.decorator';
import { JwtAuthGuard } from 'src/guards/jwt-auth.guard'; import { JwtAuthGuard } from 'src/guards/jwt-auth.guard';
import { RolesGuard } from 'src/guards/roles.guard'; import { RolesGuard } from 'src/guards/roles.guard';
import { UserMaintenanceService } from './user-maintenance.service'; 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 { 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') @ApiTags('User Maintenance - PG')
@UseGuards(JwtAuthGuard, RolesGuard) @UseGuards(JwtAuthGuard, RolesGuard)
@ -54,8 +55,8 @@ export class UserMaintenanceController {
@Post('CreateSPLogins') @Post('CreateSPLogins')
@Roles('psa') @Roles('psa')
async CreateSPLogins(@Body() body: CreateSPLoginsDTO) { async CreateSPLogins(@Body() body: CreateSPLoginsDTO, @Req() req: Request) {
return await this.userMaintenanceService.CreateSPLogins(body); return await this.userMaintenanceService.CreateSPLogins(body,req);
} }
// CLIENT LOGINS // CLIENT LOGINS
@ -74,8 +75,8 @@ export class UserMaintenanceController {
@Post('CreateClientLogins') @Post('CreateClientLogins')
@Roles('psa') @Roles('psa')
async CreateClientLogins(@Body() body: CreateClientLoginsDTO) { async CreateClientLogins(@Body() body: CreateClientLoginsDTO, @Req() req: Request) {
return await this.userMaintenanceService.CreateClientLogins(body); return await this.userMaintenanceService.CreateClientLogins(body, req);
} }
// validate email // validate email

View File

@ -1,12 +1,13 @@
import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common'; 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 { 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 { PoolClient } from 'pg';
import { PgDBService } from 'src/db/db.service'; import { PgDBService } from 'src/db/db.service';
import { InternalServerException } from 'src/exceptions/internalServerError.exception'; import { InternalServerException } from 'src/exceptions/internalServerError.exception';
import { AuthService } from 'src/auth/auth.service'; import { AuthService } from 'src/auth/auth.service';
import { MailTypeDTO } from 'src/auth/auth.dto'; import { MailTypeDTO } from 'src/auth/auth.dto';
import { Request } from 'express';
@Injectable() @Injectable()
export class UserMaintenanceService { 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; let client: PoolClient | null = null;
@ -278,7 +279,15 @@ export class UserMaintenanceService {
checkPgUserDefinedErrors(fetchResult); 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) { if (mailRes.statusCode !== 200) {
throw new InternalServerException(); 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; let client: PoolClient | null = null;
@ -401,7 +410,15 @@ export class UserMaintenanceService {
checkPgUserDefinedErrors(fetchResult); 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) { if (mailRes.statusCode !== 200) {
throw new InternalServerException(); throw new InternalServerException();

View File

@ -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) { export function normalizeKeysToUpperCase(obj) {
return Object.keys(obj).reduce((acc, key) => { return Object.keys(obj).reduce((acc, key) => {
acc[key.toUpperCase()] = obj[key]; acc[key.toUpperCase()] = obj[key];