2025-11-10 15:05:01 +05:30
|
|
|
// src/modules/host/services/host.service.ts
|
|
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
|
import { PrismaService } from '../../../common/database/prisma.service';
|
2025-11-12 19:59:54 +05:30
|
|
|
import { AddPaymentDetailsDTO, CreateHostDto, UpdateHostDto } from '../dto/host.dto';
|
2025-11-12 16:03:57 +05:30
|
|
|
import * as bcrypt from 'bcryptjs';
|
|
|
|
|
import ApiError from '../../../common/utils/helper/ApiError';
|
|
|
|
|
import { User } from '@prisma/client';
|
2025-11-10 15:05:01 +05:30
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class HostService {
|
2025-11-12 16:03:57 +05:30
|
|
|
constructor(private prisma: PrismaService) { }
|
2025-11-10 15:05:01 +05:30
|
|
|
|
|
|
|
|
async createHost(data: CreateHostDto) {
|
|
|
|
|
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 } });
|
2025-11-12 16:03:57 +05:30
|
|
|
if (!host || host.roleXid !== 4) {
|
|
|
|
|
throw new ApiError(404, 'Host not found');
|
|
|
|
|
}
|
2025-11-10 15:05:01 +05:30
|
|
|
return host;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async updateHost(id: number, data: UpdateHostDto) {
|
|
|
|
|
return this.prisma.user.update({
|
|
|
|
|
where: { id },
|
|
|
|
|
data,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deleteHost(id: number) {
|
|
|
|
|
return this.prisma.user.delete({ where: { id } });
|
|
|
|
|
}
|
2025-11-12 16:03:57 +05:30
|
|
|
|
|
|
|
|
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 loginForHost(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 createPassword(user_xid: number, password: string): Promise<boolean> {
|
|
|
|
|
// Find user by id
|
|
|
|
|
const user = await this.prisma.user.findUnique({
|
|
|
|
|
where: { id: user_xid },
|
|
|
|
|
select: { id: true, emailAddress: true, userPassword: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new ApiError(404, 'User not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if password already exists
|
|
|
|
|
if (user.userPassword) {
|
|
|
|
|
throw new ApiError(400, 'Password already exists. Use update password instead.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hash the password
|
|
|
|
|
const saltRounds = parseInt(process.env.SALT_ROUNDS || '10', 10);
|
|
|
|
|
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
|
|
|
|
|
|
|
|
|
// Update user with hashed password
|
|
|
|
|
await this.prisma.user.update({
|
|
|
|
|
where: { id: user.id },
|
|
|
|
|
data: { userPassword: hashedPassword },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-11-12 19:59:54 +05:30
|
|
|
|
|
|
|
|
async addPaymentDetails(id: number, data: AddPaymentDetailsDTO): Promise<AddPaymentDetailsDTO> {
|
|
|
|
|
const existingUser = await this.prisma.user.findUnique({
|
|
|
|
|
where: { id },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!existingUser) {
|
|
|
|
|
throw new ApiError(404, 'User not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const addedPaymentDetails = await this.prisma.hostBankDetails.create({
|
|
|
|
|
data,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!addedPaymentDetails) {
|
|
|
|
|
throw new ApiError(400, 'Failed to add payment details');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return addedPaymentDetails;
|
|
|
|
|
}
|
2025-11-10 15:05:01 +05:30
|
|
|
}
|