add host and minglar admin APIs for registration, login, and retrieval of host details
This commit is contained in:
93
src/modules/minglaradmin/dto/minglar.dto.ts
Normal file
93
src/modules/minglaradmin/dto/minglar.dto.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
// src/modules/host/dto/host.dto.ts
|
||||
import { IsInt, IsOptional, IsString, IsBoolean, IsEmail } from 'class-validator';
|
||||
|
||||
export class CreateMinglarDto {
|
||||
@IsString()
|
||||
firstName: string;
|
||||
|
||||
@IsString()
|
||||
lastName: string;
|
||||
|
||||
@IsEmail()
|
||||
emailAddress: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
isdCode?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
mobileNumber?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
userPassword?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsInt()
|
||||
roleXid?: number;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
isActive?: boolean;
|
||||
}
|
||||
|
||||
export class UpdateMinglarDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
firstName?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
lastName?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEmail()
|
||||
emailAddress?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsBoolean()
|
||||
isActive?: boolean;
|
||||
}
|
||||
|
||||
export class GetMinglarLoginResponseDTO {
|
||||
id: number;
|
||||
firstName: string | null;
|
||||
lastName: string | null;
|
||||
emailAddress: string;
|
||||
mobileNumber: string | null;
|
||||
isActive: boolean;
|
||||
roleXid: number;
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
|
||||
constructor(user: any, accessToken: string, refreshToken: string) {
|
||||
this.id = user.id;
|
||||
this.firstName = user.firstName;
|
||||
this.lastName = user.lastName;
|
||||
this.emailAddress = user.emailAddress;
|
||||
this.mobileNumber = user.mobileNumber;
|
||||
this.isActive = user.isActive;
|
||||
this.roleXid = user.roleXid;
|
||||
this.accessToken = accessToken;
|
||||
this.refreshToken = refreshToken;
|
||||
}
|
||||
}
|
||||
|
||||
export class AddPaymentDetailsDTO {
|
||||
bankXid: number;
|
||||
bankBranchXid: number;
|
||||
accountNumber: number;
|
||||
accountHolderName: string;
|
||||
ifscCode: string;
|
||||
hostXid: number;
|
||||
|
||||
constructor(bankXid: number, bankBranchXid: number, accountNumber: number, accountHolderName: string, ifscCode: string, hostXid: number) {
|
||||
this.bankXid = bankXid;
|
||||
this.bankBranchXid = bankBranchXid;
|
||||
this.accountNumber = accountNumber;
|
||||
this.accountHolderName = accountHolderName;
|
||||
this.ifscCode = ifscCode;
|
||||
this.hostXid = hostXid;
|
||||
}
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
// import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
||||
// import { safeHandler } from '../../../common/utils/handlers/safeHandler';
|
||||
// import { PrismaService } from '../../../common/database/prisma.service';
|
||||
// import { HostService } from '../services/host.service';
|
||||
// import { TokenService } from '../services/token.service';
|
||||
// import { GetHostLoginResponseDTO } from '../dto/host.dto';
|
||||
// import ApiError from '../../../common/utils/helper/ApiError';
|
||||
// import * as bcrypt from 'bcryptjs';
|
||||
|
||||
// const prismaService = new PrismaService();
|
||||
// const hostService = new HostService(prismaService);
|
||||
// const tokenService = new TokenService();
|
||||
|
||||
// export const handler = safeHandler(async (
|
||||
// event: APIGatewayProxyEvent,
|
||||
// context?: Context
|
||||
// ): Promise<APIGatewayProxyResult> => {
|
||||
// // Parse request body
|
||||
// let body: { emailAddress?: string; userPassword?: string };
|
||||
|
||||
// try {
|
||||
// body = event.body ? JSON.parse(event.body) : {};
|
||||
// } catch (error) {
|
||||
// throw new ApiError(400, 'Invalid JSON in request body');
|
||||
// }
|
||||
|
||||
// const { emailAddress } = body;
|
||||
|
||||
// if (!emailAddress) {
|
||||
// throw new ApiError(400, 'Email is required');
|
||||
// }
|
||||
|
||||
// const loginForHost = await hostService.loginForHost(emailAddress);
|
||||
|
||||
// if (!loginForHost) {
|
||||
// throw new ApiError(400, 'Failed to login');
|
||||
// }
|
||||
|
||||
// if (!matchPassword) {
|
||||
// throw new ApiError(401, 'Invalid credentials');
|
||||
// }
|
||||
|
||||
// const generateTokenForHost = await tokenService.generateAuthToken(
|
||||
// loginForHost.id
|
||||
// );
|
||||
|
||||
// if (!generateTokenForHost) {
|
||||
// throw new ApiError(500, 'Failed to generate token');
|
||||
// }
|
||||
|
||||
// const loginForHostResponse = new GetHostLoginResponseDTO(
|
||||
// loginForHost,
|
||||
// generateTokenForHost.access.token,
|
||||
// generateTokenForHost.refresh.token
|
||||
// );
|
||||
|
||||
// return {
|
||||
// statusCode: 200,
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/json',
|
||||
// 'Access-Control-Allow-Origin': '*',
|
||||
// },
|
||||
// body: JSON.stringify({
|
||||
// success: true,
|
||||
// message: 'Login successful',
|
||||
// data: loginForHostResponse,
|
||||
// }),
|
||||
// };
|
||||
// });
|
||||
|
||||
75
src/modules/minglaradmin/handlers/loginForMinglar.ts
Normal file
75
src/modules/minglaradmin/handlers/loginForMinglar.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
||||
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
|
||||
import { PrismaService } from '../../../common/database/prisma.service';
|
||||
import { MinglarService } from '../services/minglar.service';
|
||||
import { TokenService } from "../services/token.service";
|
||||
import { GetMinglarLoginResponseDTO } from '../dto/minglar.dto';
|
||||
import ApiError from '../../../common/utils/helper/ApiError';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
|
||||
const prismaService = new PrismaService();
|
||||
const minglarSerivce = new MinglarService(prismaService);
|
||||
const tokenService = new TokenService();
|
||||
|
||||
export const handler = safeHandler(async (
|
||||
event: APIGatewayProxyEvent,
|
||||
context?: Context
|
||||
): Promise<APIGatewayProxyResult> => {
|
||||
// Parse request body
|
||||
let body: { emailAddress?: string; userPassword?: string };
|
||||
|
||||
try {
|
||||
body = event.body ? JSON.parse(event.body) : {};
|
||||
} catch (error) {
|
||||
throw new ApiError(400, 'Invalid JSON in request body');
|
||||
}
|
||||
|
||||
const { emailAddress ,userPassword} = body;
|
||||
|
||||
if (!emailAddress) {
|
||||
throw new ApiError(400, 'Email is required');
|
||||
}
|
||||
|
||||
const loginForMinglar = await minglarSerivce.loginForMinglar(emailAddress ,userPassword);
|
||||
|
||||
if (!loginForMinglar) {
|
||||
throw new ApiError(400, 'Failed to login');
|
||||
}
|
||||
|
||||
const matchPassword = await bcrypt.compare(
|
||||
userPassword,
|
||||
loginForMinglar.userPassword
|
||||
);
|
||||
|
||||
if (!matchPassword) {
|
||||
throw new ApiError(401, 'Invalid credentials');
|
||||
}
|
||||
|
||||
const generateTokenForHost = await tokenService.generateAuthToken(
|
||||
loginForMinglar.id
|
||||
);
|
||||
|
||||
if (!generateTokenForHost) {
|
||||
throw new ApiError(500, 'Failed to generate token');
|
||||
}
|
||||
|
||||
const loginForHostResponse = new GetMinglarLoginResponseDTO(
|
||||
loginForMinglar,
|
||||
generateTokenForHost.access.token,
|
||||
generateTokenForHost.refresh.token
|
||||
);
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
message: 'Login successful',
|
||||
data: loginForHostResponse,
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
75
src/modules/minglaradmin/handlers/registration.ts
Normal file
75
src/modules/minglaradmin/handlers/registration.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { MinglarService } from './../services/minglar.service';
|
||||
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context } from 'aws-lambda';
|
||||
import { safeHandler } from '../../../common/utils/handlers/safeHandler';
|
||||
import { PrismaService } from '../../../common/database/prisma.service';
|
||||
import ApiError from '../../../common/utils/helper/ApiError';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import { generateOtpHelper } from '../../../common/utils/helper/sendOtp';
|
||||
|
||||
const prismaService = new PrismaService();
|
||||
const minglarService = new MinglarService(prismaService);
|
||||
|
||||
export const handler = safeHandler(async (
|
||||
event: APIGatewayProxyEvent,
|
||||
context?: Context
|
||||
): Promise<APIGatewayProxyResult> => {
|
||||
// Parse request body
|
||||
let body: { email?: string };
|
||||
|
||||
try {
|
||||
body = event.body ? JSON.parse(event.body) : {};
|
||||
} catch (error) {
|
||||
throw new ApiError(400, 'Invalid JSON in request body');
|
||||
}
|
||||
|
||||
const { email } = body;
|
||||
|
||||
if (!email) {
|
||||
throw new ApiError(400, 'Email is required');
|
||||
}
|
||||
|
||||
const user = await prismaService.user.findUnique({
|
||||
where: { emailAddress: email },
|
||||
select: { emailAddress: true, id: true, userPassword: true },
|
||||
});
|
||||
|
||||
if (user && user.userPassword) {
|
||||
throw new ApiError(404, 'User is already registered. Please login.');
|
||||
}
|
||||
|
||||
let newUser;
|
||||
|
||||
if (user && !user.userPassword) {
|
||||
// ✅ User already exists but without password → reuse record
|
||||
newUser = user;
|
||||
} else {
|
||||
// ✅ No user found → create new one
|
||||
newUser = await minglarService.createHostUser(email);
|
||||
}
|
||||
|
||||
const otpResult = await generateOtpHelper(
|
||||
Number(newUser?.id),
|
||||
newUser?.emailAddress,
|
||||
'Register',
|
||||
6,
|
||||
5
|
||||
);
|
||||
|
||||
if (!otpResult || !otpResult.otp) {
|
||||
throw new ApiError(500, 'Failed to send OTP');
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
success: true,
|
||||
message: 'OTP sent successfully.',
|
||||
data: {},
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -4,6 +4,8 @@ import ApiError from '../../../common/utils/helper/ApiError';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import { z } from 'zod';
|
||||
import { hostCompanyDetailsSchema } from '../../../common/utils/validation/host/hostCompanyDetails.validation';
|
||||
import { CreateMinglarDto, UpdateMinglarDto } from '../dto/minglar.dto';
|
||||
import { User } from '@prisma/client';
|
||||
|
||||
type HostCompanyDetailsInput = z.infer<typeof hostCompanyDetailsSchema>;
|
||||
|
||||
@@ -47,6 +49,134 @@ export class MinglarService {
|
||||
return true;
|
||||
}
|
||||
|
||||
async createHost(data: CreateMinglarDto) {
|
||||
return this.prisma.user.create({ data });
|
||||
}
|
||||
|
||||
async getAllHosts() {
|
||||
return this.prisma.user.findMany({ where: { roleXid: 3 } });
|
||||
}
|
||||
|
||||
async getHostById(id: number) {
|
||||
const host = await this.prisma.user.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
HostHeader: {
|
||||
include: {
|
||||
currencies: true,
|
||||
cities: true,
|
||||
states: true,
|
||||
countries: true,
|
||||
HostBankDetails: true,
|
||||
HostDocuments: true,
|
||||
HostSuggestion: true,
|
||||
hostParent: true,
|
||||
HostTrack: true,
|
||||
Activities: true,
|
||||
},
|
||||
},
|
||||
UserOtp: true,
|
||||
Token: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!host || host.roleXid !== 4) {
|
||||
throw new ApiError(404, 'Host not found');
|
||||
}
|
||||
|
||||
return host;
|
||||
}
|
||||
|
||||
async updateHost(id: number, data: UpdateMinglarDto) {
|
||||
return this.prisma.user.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
async deleteHost(id: number) {
|
||||
return this.prisma.user.delete({ where: { id } });
|
||||
}
|
||||
|
||||
async getHostByEmail(email: string): Promise<User> {
|
||||
return this.prisma.user.findUnique({ where: { emailAddress: email } });
|
||||
}
|
||||
|
||||
async verifyHostOtp(email: string, otp: string): Promise<boolean> {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: { emailAddress: email },
|
||||
select: {
|
||||
id: true,
|
||||
emailAddress: true,
|
||||
UserOtp: {
|
||||
where: { isActive: true, isVerified: false },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new ApiError(404, 'User not found.');
|
||||
}
|
||||
|
||||
const userOtp = user.UserOtp[0];
|
||||
|
||||
if (!userOtp) {
|
||||
throw new ApiError(400, 'No OTP found.');
|
||||
}
|
||||
|
||||
if (new Date() > userOtp.expiresOn) {
|
||||
throw new ApiError(400, 'OTP has expired.');
|
||||
}
|
||||
|
||||
const isMatch = await bcrypt.compare(otp, userOtp.otpCode);
|
||||
|
||||
if (!isMatch) {
|
||||
throw new ApiError(400, 'Invalid OTP.');
|
||||
}
|
||||
|
||||
await this.prisma.userOtp.update({
|
||||
where: { id: userOtp.id },
|
||||
data: {
|
||||
isVerified: true,
|
||||
verifiedOn: new Date(),
|
||||
isActive: false,
|
||||
},
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
async loginForMinglar(emailAddress: string, userPassword: string) {
|
||||
const existingUser = await this.prisma.user.findUnique({
|
||||
where: { emailAddress: emailAddress },
|
||||
});
|
||||
|
||||
if (!existingUser) {
|
||||
throw new ApiError(404, 'User not found');
|
||||
}
|
||||
|
||||
if (existingUser.roleXid !== 4) {
|
||||
throw new ApiError(403, 'Access denied. Not a host user.');
|
||||
}
|
||||
|
||||
const matchPassword = await bcrypt.compare(userPassword, existingUser.userPassword);
|
||||
if (!matchPassword) {
|
||||
throw new ApiError(401, 'Invalid credentials');
|
||||
}
|
||||
|
||||
return existingUser;
|
||||
}
|
||||
|
||||
async createHostUser(email: string) {
|
||||
const newUser = await this.prisma.user.create({
|
||||
data: { emailAddress: email, roleXid: 4 },
|
||||
});
|
||||
return newUser;
|
||||
}
|
||||
|
||||
|
||||
async addCompanyDetails(
|
||||
companyData: HostCompanyDetailsInput,
|
||||
documents: HostDocumentInput[] // Documents with S3 URLs
|
||||
|
||||
159
src/modules/minglaradmin/services/token.service.ts
Normal file
159
src/modules/minglaradmin/services/token.service.ts
Normal file
@@ -0,0 +1,159 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import jwt, { JwtPayload } from "jsonwebtoken";
|
||||
import moment from "moment";
|
||||
import config from "../../../config/config";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export class TokenService {
|
||||
private generateToken(
|
||||
user_xid: number,
|
||||
expiresIn: Date,
|
||||
type: string,
|
||||
secret: string
|
||||
): { token: string; expires: Date } {
|
||||
const token = jwt.sign(
|
||||
{
|
||||
sub: user_xid,
|
||||
iat: moment().unix(),
|
||||
exp: moment(expiresIn).unix(),
|
||||
type,
|
||||
},
|
||||
secret
|
||||
);
|
||||
|
||||
return { token, expires: expiresIn };
|
||||
}
|
||||
|
||||
async generateAuthToken(
|
||||
user_xid: number,
|
||||
): Promise<{
|
||||
access: { token: string; expires: Date };
|
||||
refresh: { token: string; expires: Date };
|
||||
}> {
|
||||
const accessTokenExpires = moment()
|
||||
.add(config.jwt.accessExpirationMinutes, "minutes")
|
||||
.toDate();
|
||||
|
||||
const refreshTokenExpires = moment()
|
||||
.add(config.jwt.refreshExpirationDays, "days")
|
||||
.toDate();
|
||||
|
||||
const accessToken = this.generateToken(
|
||||
user_xid,
|
||||
accessTokenExpires,
|
||||
"access",
|
||||
config.jwt.secret
|
||||
);
|
||||
|
||||
const refreshToken = this.generateToken(
|
||||
user_xid,
|
||||
refreshTokenExpires,
|
||||
"refresh",
|
||||
config.jwt.secret
|
||||
);
|
||||
|
||||
await prisma.token.create({
|
||||
data: {
|
||||
token: refreshToken.token,
|
||||
expiringAt: refreshToken.expires,
|
||||
tokenType: "refresh",
|
||||
isBlackListed: false,
|
||||
|
||||
user: {
|
||||
connect: { id: user_xid },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
access: accessToken,
|
||||
refresh: refreshToken,
|
||||
};
|
||||
}
|
||||
|
||||
async generateAuthTokenAdmin(
|
||||
user_xid: number
|
||||
): Promise<{
|
||||
access: { token: string; expires: Date };
|
||||
refresh: { token: string; expires: Date };
|
||||
}> {
|
||||
const accessTokenExpires = moment()
|
||||
.add(config.jwt.accessExpirationMinutes, "minutes")
|
||||
.toDate();
|
||||
|
||||
const refreshTokenExpires = moment()
|
||||
.add(config.jwt.refreshExpirationDays, "days")
|
||||
.toDate();
|
||||
|
||||
const accessToken = this.generateToken(
|
||||
user_xid,
|
||||
accessTokenExpires,
|
||||
"access",
|
||||
config.jwt.secret
|
||||
);
|
||||
|
||||
const refreshToken = this.generateToken(
|
||||
user_xid,
|
||||
refreshTokenExpires,
|
||||
"refresh",
|
||||
config.jwt.secret
|
||||
);
|
||||
|
||||
await prisma.token.create({
|
||||
data: {
|
||||
token: refreshToken.token,
|
||||
expiringAt: refreshToken.expires,
|
||||
tokenType: "refresh",
|
||||
isBlackListed: false,
|
||||
user: {
|
||||
connect: { id: user_xid },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
access: accessToken,
|
||||
refresh: refreshToken,
|
||||
};
|
||||
}
|
||||
|
||||
async revokeToken(user_xid: number, deviceId: string): Promise<boolean> {
|
||||
const existingToken = await prisma.token.findFirst({
|
||||
where: {
|
||||
id: user_xid,
|
||||
deviceId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!existingToken) return false;
|
||||
|
||||
await prisma.token.delete({ where: { id: existingToken.id } });
|
||||
return true;
|
||||
}
|
||||
|
||||
async isTokenBlackListed(token: string): Promise<boolean> {
|
||||
const existing = await prisma.token.findUnique({
|
||||
where: { token },
|
||||
});
|
||||
return existing ? true : false;
|
||||
}
|
||||
|
||||
async verifyRefreshToken(
|
||||
token: string
|
||||
): Promise<string | JwtPayload | null> {
|
||||
try {
|
||||
return jwt.verify(token, config.jwt.secret);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async decodeToken(token: string): Promise<string | JwtPayload | null> {
|
||||
try {
|
||||
return jwt.decode(token);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user