Files
MinglarBackendNestJS/src/modules/host/services/host.service.ts

4396 lines
133 KiB
TypeScript
Raw Normal View History

2025-11-10 15:05:01 +05:30
// src/modules/host/services/host.service.ts
import { Injectable } from '@nestjs/common';
import { PrismaClient, User } from '@prisma/client';
import AWS from 'aws-sdk';
2025-11-12 16:03:57 +05:30
import * as bcrypt from 'bcryptjs';
import dayjs from 'dayjs';
import { PDFDocument, StandardFonts } from 'pdf-lib';
import { z } from 'zod';
import { getPresignedUrl } from '../../../common/middlewares/aws/getPreSignedUrl';
import { AGREEMENT_TEMPLATE } from '../../../common/utils/constants/agreementTemplate';
import {
RESTRICTION_NAME,
ROLE,
ROLE_NAME,
USER_STATUS,
} from '../../../common/utils/constants/common.constant';
import {
ACTIVITY_AM_DISPLAY_STATUS,
ACTIVITY_AM_INTERNAL_STATUS,
2025-12-22 13:30:55 +05:30
ACTIVITY_DISPLAY_STATUS,
ACTIVITY_INTERNAL_STATUS,
HOST_STATUS_DISPLAY,
HOST_STATUS_INTERNAL,
STEPPER,
} from '../../../common/utils/constants/host.constant';
import {
2025-12-03 12:44:07 +05:30
ACTIVITY_TRACK_STATUS,
ACTIVITY_TRACK_TYPE,
MINGLAR_STATUS_DISPLAY,
MINGLAR_STATUS_INTERNAL,
} from '../../../common/utils/constants/minglar.constant';
import ApiError from '../../../common/utils/helper/ApiError';
import { hostCompanyDetailsSchema } from '../../../common/utils/validation/host/hostCompanyDetails.validation';
import config from '../../../config/config';
import { CreateActivityInput } from '../dto/createActivity.schema';
import {
AddPaymentDetailsDTO,
CreateHostDto,
UpdateHostDto,
} from '../dto/host.dto';
function sanitizeDocumentName(name?: string) {
if (!name) return null;
return name
.replace(/[^a-zA-Z0-9 _-]/g, '') // remove / .
.substring(0, 100);
}
type HostCompanyDetailsInput = z.infer<typeof hostCompanyDetailsSchema>;
// Document input after S3 upload (with S3 URL as filePath)
interface HostDocumentInput {
documentTypeXid: number;
documentName: string;
filePath: string; // S3 URL
}
export async function generateActivityRefNumber(
tx: any,
hostXid: number,
activityTypeXid: number,
hostRefNumber: string
) {
// 1⃣ Get ActivityType with Interest
const activityType = await tx.activityTypes.findUnique({
where: { id: activityTypeXid },
include: {
interests: true, // relation is named "interests" in schema
},
});
if (!activityType || !activityType.interests) {
throw new Error("Invalid activity type or interest not found");
}
// Use the Interest's ID from the relation
const interestId = activityType.interests.id;
const interestCode = activityType.interests.interestCode;
// 2⃣ Check if this host already has activities under this interest
const existingActivityForInterest = await tx.activities.findFirst({
where: {
hostXid,
activityType: {
is: {
interestXid: interestId,
},
},
},
select: {
activityRefNumber: true,
},
});
let interestSequence: number;
if (existingActivityForInterest?.activityRefNumber) {
// Extract existing interest sequence from ref number
const match =
existingActivityForInterest.activityRefNumber.match(
new RegExp(`E-${interestCode}(\\d{3})-`)
);
interestSequence = match ? parseInt(match[1], 10) : 1;
} else {
// Count distinct interests already used by this host
const distinctInterests = await tx.activities.findMany({
where: { hostXid },
select: {
activityType: {
select: {
interestXid: true,
},
},
},
});
const uniqueInterestIds = new Set(
distinctInterests
.map((a: any) => a.activityType?.interestXid)
.filter((id: number | null | undefined): id is number => id != null)
);
interestSequence = uniqueInterestIds.size + 1;
}
// 3⃣ Count activities for same host + same interest + same activityType
const activityTypeCount = await tx.activities.count({
where: {
hostXid,
activityTypeXid,
},
});
const nextActivityTypeSequence = activityTypeCount + 1;
return `${hostRefNumber}-E-${interestCode}${String(interestSequence).padStart(
3,
"0"
)}-${String(nextActivityTypeSequence).padStart(2, "0")}`;
}
2025-11-10 15:05:01 +05:30
function round2(value: number) {
2025-12-02 20:09:42 +05:30
return Math.round(value);
}
2025-12-21 17:28:08 +05:30
function computeBasePriceAndTaxes(
sellPrice: number,
taxes: Array<{ id: number; taxPer: number }>,
) {
if (!taxes?.length) {
return {
basePrice: round2(sellPrice),
2025-12-22 13:30:55 +05:30
taxDetails: [] as Array<{
taxXid: number;
taxPer: number;
taxAmount: number;
}>,
2025-12-21 17:28:08 +05:30
};
}
2025-12-22 13:30:55 +05:30
const totalTaxPer = taxes.reduce(
(sum, t) => sum + (Number(t.taxPer) || 0),
0,
);
2025-12-21 17:28:08 +05:30
const denominator = 1 + totalTaxPer / 100;
2025-12-22 13:30:55 +05:30
const basePrice =
denominator > 0 ? round2(sellPrice / denominator) : round2(sellPrice);
2025-12-21 17:28:08 +05:30
const taxDetails = taxes.map((t) => ({
taxXid: t.id,
taxPer: t.taxPer,
taxAmount: round2(basePrice * (t.taxPer / 100)),
}));
return { basePrice, taxDetails };
}
2026-03-06 15:49:25 +05:30
const normalize = (v?: string | null, maxLength: number = 50) =>
v ? v.trim().toLowerCase().substring(0, maxLength) : null;
async function renderAgreementPdf(vars: {
effectiveDate: string;
companyName: string;
companyType?: string | null;
fullAddress: string;
durationText: string;
expiryDate: string;
commissionText: string;
acceptDate: string;
}) {
const pdfDoc = await PDFDocument.create();
const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
const fontBold = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
const fontSize = 10;
const titleSize = 14;
const margin = 50;
const { width, height } = { width: 595, height: 842 }; // A4
const contentWidth = width - 2 * margin;
let page = pdfDoc.addPage([width, height]);
let cursorY = height - margin;
const addNewPage = () => {
page = pdfDoc.addPage([width, height]);
cursorY = height - margin;
};
const drawLine = (text: string, isBold = false, size = fontSize) => {
const currentFont = isBold ? fontBold : font;
const words = text.split(' ');
let line = '';
for (const word of words) {
const testLine = line + word + ' ';
const testLineWidth = currentFont.widthOfTextAtSize(testLine.trim(), size);
if (testLineWidth > contentWidth && line !== '') {
if (cursorY < margin + 20) addNewPage();
page.drawText(line.trim(), { x: margin, y: cursorY, size, font: currentFont });
cursorY -= size * 1.5;
line = word + ' ';
} else {
line = testLine;
}
}
if (line !== '') {
if (cursorY < margin + 20) addNewPage();
page.drawText(line.trim(), { x: margin, y: cursorY, size, font: currentFont });
cursorY -= size * 1.5;
}
};
let template = AGREEMENT_TEMPLATE;
template = template.replace(/\[EFFECTIVE_DATE\]/g, vars.effectiveDate);
template = template.replace(/\[HOST_LEGAL_NAME\]/g, vars.companyName);
template = template.replace(/\[COMPANY_TYPE\]/g, vars.companyType || 'Entity');
template = template.replace(/\[FULL_ADDRESS\]/g, vars.fullAddress);
template = template.replace(/\[DURATION_TEXT\]/g, vars.durationText);
template = template.replace(/\[EXPIRY_DATE\]/g, vars.expiryDate);
template = template.replace(/\[COMMISSION_TEXT\]/g, vars.commissionText);
template = template.replace(/\[ACCEPT_DATE\]/g, vars.acceptDate);
const lines = template.split('\n');
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed) {
cursorY -= fontSize; // Paragraph spacing
continue;
}
// Heuristic for titles/headers
const isTitle = trimmed === trimmed.toUpperCase() && trimmed.length > 5;
const isSubHeader = /^\d+\.?\s/.test(trimmed) || /^[A-Z]\)\s/.test(trimmed);
drawLine(trimmed, isTitle || isSubHeader, isTitle ? titleSize : fontSize);
}
const pdfBytes = await pdfDoc.save();
return Buffer.from(pdfBytes);
}
function buildDurationText(host: any) {
if (!host.durationNumber || !host.durationFrequency) return 'N/A';
return `${host.durationNumber} ${host.durationFrequency}`;
}
function buildExpiryDate(host: any) {
if (!host.agreementStartDate || !host.durationNumber || !host.durationFrequency) return 'N/A';
const start = dayjs(host.agreementStartDate);
const duration = host.durationNumber;
// Handle frequency units for dayjs (months/years)
const unit = host.durationFrequency.toLowerCase().includes('month') ? 'month' : 'year';
return start.add(duration, unit as any).format('DD-MMM-YY');
}
function buildCommissionText(host: any) {
if (host.isCommisionBase) {
return `${host.commisionPer || 0}%`;
} else {
return `INR ${host.amountPerBooking || 0} per booking`;
}
}
function buildFullAddress(host: any) {
const parts = [
host.address1,
host.address2,
host.cities?.cityName,
host.states?.stateName,
host.countries?.countryName,
host.pinCode,
].filter(Boolean);
return parts.join(', ');
}
// generateAgreementPdfBuffer and generateAgreementDocxBuffer removed
// End of agreement rendering logic
const findOrCreateCountry = async (
tx: any,
countryName?: string | null,
) => {
if (!countryName) return null;
const name = normalize(countryName);
let country = await tx.countries.findFirst({
where: {
countryName: { equals: name, mode: 'insensitive' },
isActive: true,
},
});
if (!country) {
country = await tx.countries.create({
data: {
countryName: countryName.trim(),
countryCode: countryName
.substring(0, 2)
.toUpperCase(), // fallback
countryFlag: '',
},
});
}
return country.id;
};
const findOrCreateState = async (
tx: any,
stateName?: string | null,
countryXid?: number | null,
) => {
if (!stateName || !countryXid) return null;
2026-03-06 15:49:25 +05:30
const trimmedStateName = stateName.trim().substring(0, 50);
const state = await tx.states.findFirst({
where: {
2026-03-06 15:49:25 +05:30
stateName: { equals: trimmedStateName, mode: 'insensitive' },
countryXid,
isActive: true,
},
});
if (state) return state.id;
const created = await tx.states.create({
data: {
2026-03-06 15:49:25 +05:30
stateName: trimmedStateName,
countryXid,
},
});
return created.id;
};
const findOrCreateCity = async (
tx: any,
cityName?: string | null,
stateXid?: number | null,
) => {
if (!cityName || !stateXid) return null;
2026-03-06 15:49:25 +05:30
const trimmedCityName = cityName.trim().substring(0, 50);
const city = await tx.cities.findFirst({
where: {
2026-03-06 15:49:25 +05:30
cityName: { equals: trimmedCityName, mode: 'insensitive' },
stateXid,
isActive: true,
},
});
if (city) return city.id;
const created = await tx.cities.create({
data: {
2026-03-06 15:49:25 +05:30
cityName: trimmedCityName,
stateXid,
},
});
return created.id;
};
const bucket = config.aws.bucketName;
const s3 = new AWS.S3({
region: config.aws.region,
});
type UpdateHostProfileInput = {
firstName?: string;
lastName?: string | null;
isdCode?: string;
mobileNumber?: string;
dateOfBirth?: Date;
address?: {
address1?: string;
address2?: string;
countryXid?: number;
stateXid?: number;
cityXid?: number;
pinCode?: string;
};
};
2025-11-10 15:05:01 +05:30
@Injectable()
export class HostService {
2026-01-09 18:10:38 +05:30
constructor(private prisma: PrismaClient) { }
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 } });
}
2026-01-09 18:10:38 +05:30
2026-01-05 15:46:47 +05:30
async getActivityDetailsById(activityXid: number) {
return this.prisma.activities.findFirst({ where: { id: activityXid } });
}
2025-11-10 15:05:01 +05:30
2025-11-14 14:08:47 +05:30
async getHostIdByUserXid(user_xid: number) {
const host = await this.prisma.hostHeader.findFirst({
where: { userXid: user_xid },
select: { id: true, stepper: true },
2025-11-14 14:08:47 +05:30
});
const user = await this.prisma.user.findUnique({
where: { id: user_xid, isActive: true },
select: { id: true, emailAddress: true, userRefNumber: true },
2025-12-22 13:30:55 +05:30
});
return { host, user };
2025-11-14 14:08:47 +05:30
}
2025-11-10 15:05:01 +05:30
async getHostById(id: number) {
2025-11-14 15:04:01 +05:30
const host = await this.prisma.hostHeader.findFirst({
where: { userXid: id },
include: {
hostParent: {
select: {
id: true,
logoPath: true,
companyName: true,
address1: true,
address2: true,
cities: {
select: {
id: true,
cityName: true
}
},
states: {
select: {
id: true,
stateName: true
}
},
countries: {
select: {
id: true,
countryName: true
}
},
pinCode: true,
registrationNumber: true,
panNumber: true,
gstNumber: true,
formationDate: true,
companyTypes: {
select: {
id: true,
companyTypeName: true
}
},
websiteUrl: true,
instagramUrl: true,
facebookUrl: true,
linkedinUrl: true,
twitterUrl: true,
HostParenetDocuments: {
select: {
id: true,
filePath: true,
documentName: true,
documentTypeXid: true,
2025-12-22 13:30:55 +05:30
documentType: true,
},
},
},
},
2025-11-14 15:04:01 +05:30
HostBankDetails: true,
2025-11-24 23:19:18 +05:30
HostDocuments: {
include: {
documentType: true,
},
},
accountManager: {
select: {
id: true,
firstName: true,
lastName: true,
emailAddress: true,
mobileNumber: true,
profileImage: true,
userRefNumber: true,
2025-12-22 13:30:55 +05:30
},
},
user: {
select: {
id: true,
emailAddress: true,
dateOfBirth: true,
firstName: true,
lastName: true,
mobileNumber: true,
profileImage: true,
userStatus: true,
userRefNumber: true,
userAddressDetails: {
where: { isActive: true },
select: {
id: true,
address1: true,
address2: true,
locationAddress: true,
locationLat: true,
locationLong: true,
2026-03-05 11:25:33 +05:30
pinCode: true,
cityXid: true,
cities: {
select: {
id: true,
cityName: true,
}
},
stateXid: true,
states: {
select: {
id: true,
stateName: true,
}
},
countryXid: true,
country: {
select: {
id: true,
countryName: true
}
}
}
}
2025-12-22 13:30:55 +05:30
},
},
companyTypes: {
select: {
id: true,
companyTypeName: true,
},
},
HostSuggestion: {
where: {
isActive: true,
isreviewed: false,
},
select: {
id: true,
hostXid: true,
title: true,
comments: true,
isparent: true,
2025-12-22 13:30:55 +05:30
},
},
2025-11-24 23:19:18 +05:30
countries: true,
currencies: true,
states: true,
cities: true,
},
});
2025-11-14 15:04:01 +05:30
if (!host) {
// If host record doesn't exist yet, return stepper 1 (NOT_SUBMITTED)
// so callers (like the stepper endpoint) can show initial step.
return { stepper: STEPPER.NOT_SUBMITTED } as any;
2025-11-12 16:03:57 +05:30
}
2025-11-21 14:53:53 +05:30
if (host.HostDocuments?.length) {
for (const doc of host.HostDocuments) {
if (doc.filePath) {
const filePath = doc.filePath;
// If full URL is saved, extract only key
const key = filePath.startsWith('http')
? filePath.split('.com/')[1]
2025-11-21 14:53:53 +05:30
: filePath;
(doc as any).presignedUrl = await getPresignedUrl(bucket, key);
}
}
}
2025-12-02 18:08:39 +05:30
if (host.user?.profileImage) {
2025-12-22 13:30:55 +05:30
const key = host.user.profileImage.startsWith('http')
? host.user.profileImage.split('.com/')[1]
: host.user.profileImage;
host.user.profileImage = await getPresignedUrl(bucket, key);
}
2025-11-21 14:53:53 +05:30
2025-12-02 18:08:39 +05:30
if (host?.logoPath) {
const key = host.logoPath.startsWith('http')
? host.logoPath.split('.com/')[1]
: host.logoPath;
host.logoPath = await getPresignedUrl(bucket, key);
}
2025-12-02 18:08:39 +05:30
if (host.accountManager?.profileImage) {
const key = host.accountManager.profileImage.startsWith('http')
? host.accountManager.profileImage.split('.com/')[1]
: host.accountManager.profileImage;
host.accountManager.profileImage = await getPresignedUrl(bucket, key);
}
if (host.hostParent?.length) {
const parent = host.hostParent[0]; // since you allow only 1 parent
// Parent company logo
if (parent.logoPath) {
2025-12-22 13:30:55 +05:30
const key = parent.logoPath.startsWith('http')
? parent.logoPath.split('.com/')[1]
: parent.logoPath;
parent.logoPath = await getPresignedUrl(bucket, key);
}
// Parent documents
if (parent.HostParenetDocuments?.length) {
for (const doc of parent.HostParenetDocuments) {
if (doc.filePath) {
2025-12-22 13:30:55 +05:30
const key = doc.filePath.startsWith('http')
? doc.filePath.split('.com/')[1]
: doc.filePath;
(doc as any).presignedUrl = await getPresignedUrl(bucket, key);
}
}
}
}
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
/**
* Update the logged-in Host's personal profile details.
* Email is intentionally NOT editable here.
*/
async updateHostProfileDetails(userId: number, input: UpdateHostProfileInput) {
return this.prisma.$transaction(async (tx) => {
const user = await tx.user.findUnique({
where: { id: userId, isActive: true },
select: { id: true, roleXid: true },
});
if (!user) throw new ApiError(404, 'User not found');
if (user.roleXid !== ROLE.HOST) throw new ApiError(403, 'Access denied.');
// 1) Update `User` (whitelist only)
const userUpdateData: any = {};
if (input.firstName !== undefined) userUpdateData.firstName = input.firstName || null;
if (input.lastName !== undefined) userUpdateData.lastName = input.lastName;
if (input.isdCode !== undefined) userUpdateData.isdCode = input.isdCode || null;
if (input.mobileNumber !== undefined) userUpdateData.mobileNumber = input.mobileNumber || null;
if (input.dateOfBirth !== undefined) userUpdateData.dateOfBirth = input.dateOfBirth;
if (Object.keys(userUpdateData).length > 0) {
await tx.user.update({
where: { id: userId },
data: {
...userUpdateData,
isProfileUpdated: true,
},
});
}
// 2) Update/Create `UserAddressDetails` (if any address field sent)
const addressData = input.address || {};
const hasAnyAddressField = Object.values(addressData).some((v) => v !== undefined);
if (hasAnyAddressField) {
const existingAddress = await tx.userAddressDetails.findFirst({
where: { userXid: userId, isActive: true },
select: { id: true },
});
const addressUpdateData: any = {};
if (addressData.address1 !== undefined) addressUpdateData.address1 = addressData.address1;
if (addressData.address2 !== undefined) addressUpdateData.address2 = addressData.address2;
if (addressData.countryXid !== undefined) addressUpdateData.countryXid = addressData.countryXid;
if (addressData.stateXid !== undefined) addressUpdateData.stateXid = addressData.stateXid;
if (addressData.cityXid !== undefined) addressUpdateData.cityXid = addressData.cityXid;
if (addressData.pinCode !== undefined) addressUpdateData.pinCode = addressData.pinCode;
if (existingAddress) {
await tx.userAddressDetails.update({
where: { id: existingAddress.id },
data: addressUpdateData,
});
} else {
const required = ['address1', 'countryXid', 'stateXid', 'cityXid', 'pinCode'] as const;
const missing = required.filter((k) => addressData[k] === undefined);
if (missing.length) {
throw new ApiError(400, `Missing required address fields: ${missing.join(', ')}`);
}
await tx.userAddressDetails.create({
data: {
userXid: userId,
...addressUpdateData,
},
});
}
}
// 3) Return updated profile snapshot (including read-only email)
const updated = await tx.user.findUnique({
where: { id: userId },
select: {
id: true,
firstName: true,
lastName: true,
emailAddress: true,
isdCode: true,
mobileNumber: true,
dateOfBirth: true,
profileImage: true,
isProfileUpdated: true,
userAddressDetails: {
where: { isActive: true },
take: 1,
select: {
id: true,
address1: true,
address2: true,
countryXid: true,
stateXid: true,
cityXid: true,
pinCode: true,
},
},
},
});
return {
user: updated,
address: updated?.userAddressDetails?.[0] ?? null,
};
});
}
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 trimmedOtp = (otp || '').toString().trim();
const user = await this.prisma.user.findFirst({
where: { emailAddress: email, isActive: true },
2025-11-12 16:03:57 +05:30
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(trimmedOtp, userOtp.otpCode);
2025-11-12 16:03:57 +05:30
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, isActive: true },
select: {
id: true,
roleXid: true,
firstName: true,
lastName: true,
emailAddress: true,
mobileNumber: true,
userPassword: true,
2025-12-22 13:30:55 +05:30
userStatus: true,
},
2025-11-12 16:03:57 +05:30
});
if (!existingUser) {
throw new ApiError(404, 'User not found');
}
if (existingUser.userStatus == USER_STATUS.REJECTED) {
2025-12-22 13:30:55 +05:30
throw new ApiError(
403,
'You are not allowed to login. Please contact minglar admin.',
);
}
2025-11-12 16:03:57 +05:30
if (existingUser.roleXid !== 4) {
throw new ApiError(403, 'Access denied. Not a host user.');
}
const matchPassword = await bcrypt.compare(
userPassword,
existingUser.userPassword,
);
2025-11-12 16:03:57 +05:30
if (!matchPassword) {
throw new ApiError(401, 'Invalid credentials');
}
return existingUser;
}
async createMinglarUser(email: string) {
2025-11-12 16:03:57 +05:30
const newUser = await this.prisma.user.create({
data: { emailAddress: email, roleXid: ROLE.HOST },
2025-11-12 16:03:57 +05:30
});
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.',
);
2025-11-12 16:03:57 +05:30
}
// 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,
isEmailVerfied: true,
userStatus: USER_STATUS.ACTIVE,
},
2025-11-12 16:03:57 +05:30
});
return true;
}
2025-11-12 19:59:54 +05:30
async getBankBranchById(bankBranchXid: number) {
return await this.prisma.bankBranches.findUnique({
where: { id: bankBranchXid },
select: {
id: true,
ifscCode: true,
bankXid: true,
},
});
}
2025-11-21 13:31:41 +05:30
async addPaymentDetails(data: AddPaymentDetailsDTO) {
return await this.prisma.$transaction(async (tx) => {
const existingAccount = await tx.hostBankDetails.findFirst({
where: {
accountNumber: data.accountNumber,
isActive: true,
},
});
if (existingAccount) {
throw new ApiError(
400,
2025-12-22 13:30:55 +05:30
'Host account with this account number already exists.',
);
}
2025-11-21 13:31:41 +05:30
const addedPaymentDetails = await tx.hostBankDetails.create({
data,
});
if (!addedPaymentDetails) {
throw new ApiError(400, 'Failed to add payment details');
}
2025-11-14 14:08:47 +05:30
2025-11-21 13:31:41 +05:30
await tx.hostHeader.update({
where: { id: data.hostXid },
data: {
stepper: STEPPER.BANK_DETAILS_UPDATED,
2025-12-22 13:30:55 +05:30
currencyXid: data.currencyXid,
},
});
});
2025-11-21 13:31:41 +05:30
}
2025-11-14 14:08:47 +05:30
2025-12-22 13:30:55 +05:30
async getAllHostActivity(
search?: string,
user_xid?: number,
paginationOptions?: { page: number; limit: number; skip: number },
) {
const hostDetails = await this.prisma.hostHeader.findFirst({
2025-12-22 13:30:55 +05:30
where: { userXid: user_xid, isActive: true },
});
2025-12-03 13:18:28 +05:30
const whereClause: any = {
isActive: true,
hostXid: hostDetails.id,
};
2025-12-03 13:18:28 +05:30
if (!hostDetails) {
return {
data: [],
total: 0,
page: paginationOptions?.page || 1,
2025-12-22 13:30:55 +05:30
limit: paginationOptions?.limit || 10,
2025-12-03 13:18:28 +05:30
};
}
// 🔍 SEARCH (fixed)
if (search?.trim()) {
const term = search.trim();
whereClause.OR = [
{ activityRefNumber: { contains: term, mode: 'insensitive' } },
{ activityTitle: { contains: term, mode: 'insensitive' } },
{
activityType: {
2025-12-22 13:30:55 +05:30
activityTypeName: { contains: term, mode: 'insensitive' },
},
2025-12-03 13:18:28 +05:30
},
];
}
const [hostAllActivities, totalCount] = await Promise.all([
this.prisma.activities.findMany({
where: whereClause,
2025-12-03 13:18:28 +05:30
select: {
id: true,
activityRefNumber: true,
activityTitle: true,
totalScore: true,
activityInternalStatus: true,
activityDisplayStatus: true,
amInternalStatus: true,
amDisplayStatus: true,
createdAt: true,
checkInAddress: true,
frequency: {
select: {
id: true,
2025-12-22 13:30:55 +05:30
frequencyName: true,
},
2025-12-03 13:18:28 +05:30
},
ActivityAmDetails: {
select: {
accountManager: {
select: {
id: true,
firstName: true,
lastName: true,
profileImage: true,
emailAddress: true,
roleXid: true,
},
},
},
},
2025-12-03 13:18:28 +05:30
activityType: {
select: {
id: true,
activityTypeName: true,
interests: {
select: {
id: true,
2025-12-22 13:30:55 +05:30
interestName: true,
},
},
energyLevel: {
select: {
id: true,
energyLevelName: true,
energyIcon: true,
2026-01-05 14:47:24 +05:30
energyColor: true,
},
},
2025-12-22 13:30:55 +05:30
},
2025-12-03 13:18:28 +05:30
},
},
skip: paginationOptions?.skip || 0,
take: paginationOptions?.limit || 10,
orderBy: { id: 'desc' },
}),
this.prisma.activities.count({ where: whereClause }),
]);
for (const activity of hostAllActivities) {
/** 2⃣ Process AM Profile Image */
const am = activity.ActivityAmDetails?.[0]?.accountManager;
if (am?.profileImage) {
2025-12-22 13:30:55 +05:30
const key = am.profileImage.startsWith('http')
? am.profileImage.split('.com/')[1]
: am.profileImage;
const presignedUrl = await getPresignedUrl(bucket, key);
activity.ActivityAmDetails[0].accountManager = {
...am,
profileImage: presignedUrl,
};
}
}
2025-12-22 13:30:55 +05:30
const {
paginationService,
} = require('@/common/utils/pagination/pagination.service');
return paginationService.createPaginatedResponse(
hostAllActivities,
totalCount,
2025-12-22 13:30:55 +05:30
paginationOptions || { page: 1, limit: 10, skip: 0 },
);
2025-11-21 13:31:41 +05:30
}
2025-11-14 14:08:47 +05:30
2025-11-21 13:31:41 +05:30
async acceptMinglarAgreement(user_xid: number) {
const host = await this.prisma.hostHeader.findFirst({
where: { userXid: user_xid, isActive: true },
include: {
cities: true,
states: true,
countries: true,
companyTypes: true,
},
});
if (!host) {
throw new ApiError(404, 'Host not found for this user');
}
const effectiveDate = host.agreementStartDate
? dayjs(host.agreementStartDate).format('DD-MMM-YY')
: dayjs().format('DD-MMM-YY');
const durationText = buildDurationText(host);
const expiryDate = buildExpiryDate(host);
const commissionText = buildCommissionText(host);
const fullAddress = buildFullAddress(host);
const acceptDate = dayjs().format('DD-MMM-YYYY');
const agreementVars = {
effectiveDate,
companyName: host.companyName,
companyType: host.companyTypes?.companyTypeName,
fullAddress,
durationText,
expiryDate,
commissionText,
acceptDate,
};
let pdfUrl: string | null = null;
try {
const pdfBuffer = await renderAgreementPdf(agreementVars);
const existingCount = await this.prisma.hostAgreement.count({
where: { hostXid: host.id, isActive: true },
});
const nextVersionNumber = `AG${existingCount + 1}`;
const baseKey = `Documents/Host/${host.id}/agreements/${nextVersionNumber}`;
const pdfKey = `${baseKey}.pdf`;
await s3
.upload({
Bucket: config.aws.bucketName,
Key: pdfKey,
Body: pdfBuffer,
ContentType: 'application/pdf',
ACL: 'private',
})
.promise();
pdfUrl = `https://${config.aws.bucketName}.s3.${config.aws.region}.amazonaws.com/${pdfKey}`;
} catch (error) {
console.error('Error generating or uploading PDF:', error);
// Continue without PDF - will return dynamic fields instead
}
try {
const existingCount = await this.prisma.hostAgreement.count({
where: { hostXid: host.id, isActive: true },
});
const nextVersionNumber = `AG${existingCount + 1}`;
await this.prisma.$transaction(async (tx) => {
// Optional: mark previous agreements inactive
await tx.hostAgreement.updateMany({
where: { hostXid: host.id, isActive: true },
data: { isActive: false },
});
await tx.hostAgreement.create({
data: {
hostXid: host.id,
filePath: pdfUrl,
versionNumber: nextVersionNumber,
isActive: true,
},
});
await tx.hostHeader.update({
where: { id: host.id },
data: {
stepper: STEPPER.AGREEMENT_ACCEPTED,
isApproved: true,
agreementAccepted: true,
agreementStartDate: host.agreementStartDate || new Date(),
},
});
});
} catch (error) {
console.error('Error creating host agreement record:', error);
// Continue without creating agreement record - will return dynamic fields instead
}
// Return dynamic fields and PDF URL
return {
filePath: pdfUrl,
dynamicFields: agreementVars,
};
2025-11-12 19:59:54 +05:30
}
2026-03-02 13:01:53 +05:30
/**
* Get the latest (active) agreement for a specific host by hostXid.
*/
async getLatestHostAgreement(hostXid: number) {
if (!hostXid || Number.isNaN(hostXid)) {
throw new ApiError(400, 'Valid hostXid is required');
}
const hostHeader = await this.prisma.hostHeader.findFirst({
where: { id: hostXid, isActive: true },
select: {
id: true,
isCommisionBase: true,
commisionPer: true,
durationNumber: true,
durationFrequency: true,
amountPerBooking: true,
agreementStartDate: true,
payoutDurationNum: true,
payoutDurationFrequency: true,
registrationNumber: true,
companyName: true,
companyTypes: {
select: {
id: true,
companyTypeName: true
}
}
}
});
2026-03-02 13:01:53 +05:30
const agreement = await this.prisma.hostAgreement.findFirst({
where: { hostXid, isActive: true },
orderBy: { createdAt: 'desc' },
select: {
id: true,
hostXid: true,
filePath: true,
versionNumber: true,
createdAt: true,
updatedAt: true,
},
});
// ❌ If both missing
if (!agreement && !hostHeader) {
2026-03-02 13:01:53 +05:30
throw new ApiError(404, 'No active agreement found for this host');
}
let presignedUrl = "";
2026-03-02 13:01:53 +05:30
if (agreement?.filePath) {
const key = agreement.filePath.startsWith('http')
? agreement.filePath.split('.com/')[1]
: agreement.filePath;
2026-03-02 13:01:53 +05:30
const bucket = config.aws.bucketName;
presignedUrl = await getPresignedUrl(bucket, key);
}
2026-03-02 13:01:53 +05:30
return {
hostHeader: hostHeader || null,
agreement: agreement
? {
...agreement,
presignedUrl
}
: null
2026-03-02 13:01:53 +05:30
};
}
async getPQQQuestionDetail(question_xid: number, activity_xid: number) {
const detailsOfQuestion = await this.prisma.activityPQQheader.findFirst({
where: {
activityXid: activity_xid,
pqqQuestionXid: question_xid,
isActive: true,
},
select: {
pqqQuestionXid: true,
pqqAnswerXid: true,
ActivityPQQSupportings: {
select: {
id: true,
activityPqqHeaderXid: true,
mediaFileName: true,
2025-12-22 13:30:55 +05:30
mediaType: true,
},
},
ActivityPQQSuggestions: {
where: { isActive: true, isReviewed: false },
select: {
id: true,
title: true,
comments: true,
2025-12-22 13:30:55 +05:30
},
},
},
});
if (detailsOfQuestion.ActivityPQQSupportings?.length) {
for (const doc of detailsOfQuestion.ActivityPQQSupportings) {
if (doc.mediaFileName) {
const filePath = doc.mediaFileName;
// If full URL is saved, extract only key
const key = filePath.startsWith('http')
? filePath.split('.com/')[1]
: filePath;
(doc as any).presignedUrl = await getPresignedUrl(bucket, key);
}
}
}
return detailsOfQuestion;
}
async getLatestQuestionDetailsPQQ(activity_xid: number) {
return await this.prisma.activityPQQheader.findFirst({
where: {
activityXid: activity_xid,
isActive: true,
pqqAnswerXid: {
2025-12-22 13:30:55 +05:30
not: null,
},
},
2025-11-24 19:19:02 +05:30
select: {
pqqQuestionXid: true,
pqqAnswerXid: true,
pqqQuestions: {
select: {
pqqSubCategoryXid: true,
pqqSubCategories: {
select: {
categoryXid: true,
},
},
},
},
2025-11-24 19:19:02 +05:30
},
orderBy: { id: 'desc' },
});
}
async getParentDocumentsByHostId(userId: number) {
const host = await this.prisma.hostHeader.findFirst({
where: { userXid: userId },
select: { id: true },
});
if (!host) return [];
const parents = await this.prisma.hostParent.findMany({
where: { hostXid: host.id },
include: { HostParenetDocuments: true },
});
2025-12-22 13:30:55 +05:30
return parents.flatMap((p) => p.HostParenetDocuments);
}
async deleteExistingParentRecords(userId: number) {
const host = await this.prisma.hostHeader.findFirst({
where: { userXid: userId },
select: { id: true },
});
if (!host) return;
const parents = await this.prisma.hostParent.findMany({
where: { hostXid: host.id },
select: { id: true },
});
if (!parents.length) return;
2025-12-22 13:30:55 +05:30
const parentIds = parents.map((p) => p.id);
// 1⃣ Delete documents first
await this.prisma.hostParenetDocuments.deleteMany({
where: { hostParentXid: { in: parentIds } },
});
// 2⃣ Then delete parent records
await this.prisma.hostParent.deleteMany({
where: { id: { in: parentIds } },
});
}
2025-11-17 19:05:26 +05:30
async addOrUpdateCompanyDetails(
user_xid: number,
companyData: HostCompanyDetailsInput,
documents: HostDocumentInput[],
parentCompanyData?: any | null,
parentDocuments?: HostDocumentInput[],
isDraft: boolean = false,
options?: {
deleteCompanyLogo?: boolean;
deleteParentCompanyLogo?: boolean;
},
) {
return await this.prisma.$transaction(async (tx) => {
// Check if host already has a company
const existingHostCompany = await tx.hostHeader.findFirst({
where: { userXid: user_xid },
include: { hostParent: true },
});
2025-12-22 13:30:55 +05:30
console.log(existingHostCompany, '-: Existing hai');
let existingParentCompany;
if (existingHostCompany) {
existingParentCompany = await tx.hostParent.findFirst({
where: { hostXid: existingHostCompany.id },
select: {
id: true,
2025-12-22 13:30:55 +05:30
logoPath: true,
},
});
}
let hostStatusInternal;
let hostStatusDisplay;
let minglarStatusInternal;
let minglarStatusDisplay;
if (existingHostCompany) {
hostStatusInternal = existingHostCompany.hostStatusInternal;
hostStatusDisplay = existingHostCompany.hostStatusDisplay;
minglarStatusInternal = existingHostCompany.adminStatusInternal;
minglarStatusDisplay = existingHostCompany.adminStatusDisplay;
}
// CASE 1: Host was asked to update AND is submitting final
if (
existingHostCompany &&
2025-12-22 13:30:55 +05:30
existingHostCompany.hostStatusInternal ===
2026-01-09 18:10:38 +05:30
HOST_STATUS_INTERNAL.HOST_TO_UPDATE &&
!isDraft
) {
hostStatusInternal = HOST_STATUS_INTERNAL.HOST_SUBMITTED;
hostStatusDisplay = HOST_STATUS_DISPLAY.UNDER_REVIEW;
minglarStatusInternal = MINGLAR_STATUS_INTERNAL.AM_TO_REVIEW;
minglarStatusDisplay = MINGLAR_STATUS_DISPLAY.RE_SUBMITTED;
}
// CASE 2: Admin has rejected but host can resubmit
else if (
existingHostCompany &&
existingHostCompany.hostStatusInternal ===
HOST_STATUS_INTERNAL.REJECTED &&
!isDraft
) {
hostStatusInternal = HOST_STATUS_INTERNAL.HOST_SUBMITTED;
hostStatusDisplay = HOST_STATUS_DISPLAY.UNDER_REVIEW;
minglarStatusInternal = MINGLAR_STATUS_INTERNAL.ADMIN_TO_REVIEW;
minglarStatusDisplay = MINGLAR_STATUS_DISPLAY.RE_SUBMITTED;
}
// CASE 2: Host was asked to update BUT saving draft
else if (
existingHostCompany &&
2025-12-22 13:30:55 +05:30
existingHostCompany.hostStatusInternal ===
2026-01-09 18:10:38 +05:30
HOST_STATUS_INTERNAL.HOST_TO_UPDATE &&
isDraft
) {
// keep original
hostStatusInternal = existingHostCompany.hostStatusInternal;
hostStatusDisplay = existingHostCompany.hostStatusDisplay;
minglarStatusInternal = existingHostCompany.adminStatusInternal;
minglarStatusDisplay = existingHostCompany.adminStatusDisplay;
}
// CASE 3: Normal create or update
else {
hostStatusInternal = isDraft
? HOST_STATUS_INTERNAL.DRAFT
: HOST_STATUS_INTERNAL.HOST_SUBMITTED;
hostStatusDisplay = isDraft
? HOST_STATUS_DISPLAY.DRAFT
: HOST_STATUS_DISPLAY.UNDER_REVIEW;
minglarStatusInternal = isDraft
? MINGLAR_STATUS_INTERNAL.DRAFT
: MINGLAR_STATUS_INTERNAL.ADMIN_TO_REVIEW;
minglarStatusDisplay = isDraft
? MINGLAR_STATUS_DISPLAY.DRAFT
: MINGLAR_STATUS_DISPLAY.NEW;
}
const stepper = isDraft ? STEPPER.NOT_SUBMITTED : STEPPER.UNDER_REVIEW;
// -------------------------------------------------------
// CREATE FLOW
// -------------------------------------------------------
if (!existingHostCompany) {
if (!isDraft) {
2025-12-22 13:30:55 +05:30
console.log('First time direct final submit.');
const existingByPan = await tx.hostHeader.findFirst({
where: { panNumber: companyData.panNumber },
});
if (existingByPan)
2025-12-22 13:30:55 +05:30
throw new ApiError(
400,
'Company already exists with this pan/bin number',
);
}
2025-12-22 13:30:55 +05:30
console.log('First Time Aaya hai');
const createdHost = await tx.hostHeader.create({
data: {
user: { connect: { id: user_xid } },
companyName: companyData.companyName,
address1: companyData.address1,
address2: companyData.address2,
2025-12-22 13:30:55 +05:30
cities: companyData.cityXid
? { connect: { id: companyData.cityXid } }
: undefined,
states: companyData.stateXid
? { connect: { id: companyData.stateXid } }
: undefined,
countries: companyData.countryXid
? { connect: { id: companyData.countryXid } }
: undefined,
pinCode: companyData.pinCode,
logoPath: companyData.logoPath || null,
isSubsidairy: companyData.isSubsidairy,
registrationNumber: companyData.registrationNumber,
panNumber: companyData.panNumber,
gstNumber: companyData.gstNumber || null,
formationDate: companyData.formationDate
? new Date(companyData.formationDate as any)
: null,
companyTypes: companyData.companyTypeXid
? { connect: { id: companyData.companyTypeXid } }
: undefined,
referencedBy: companyData.referencedBy || null,
websiteUrl: companyData.websiteUrl || null,
instagramUrl: companyData.instagramUrl || null,
facebookUrl: companyData.facebookUrl || null,
linkedinUrl: companyData.linkedinUrl || null,
twitterUrl: companyData.twitterUrl || null,
stepper,
hostStatusInternal,
hostStatusDisplay,
adminStatusInternal: minglarStatusInternal,
adminStatusDisplay: minglarStatusDisplay,
},
});
// host documents
if (documents?.length) {
const docsData = documents.map((doc) => ({
hostXid: createdHost.id,
documentTypeXid: doc.documentTypeXid,
documentName: sanitizeDocumentName(doc.documentName),
filePath: doc.filePath,
}));
await tx.hostDocuments.createMany({ data: docsData });
}
// parent create
if (companyData.isSubsidairy && parentCompanyData) {
2025-12-22 13:30:55 +05:30
console.log('Parent ke saath aaya hai first time.');
const createdParent = await tx.hostParent.create({
data: {
host: { connect: { id: createdHost.id } },
companyName: parentCompanyData.companyName || null,
address1: parentCompanyData.address1 || null,
address2: parentCompanyData.address2 || null,
// Safely handle city connection - only connect if valid ID exists
2025-12-22 13:30:55 +05:30
cities:
parentCompanyData?.cityXid &&
2026-01-09 18:10:38 +05:30
!isNaN(Number(parentCompanyData.cityXid))
2025-12-22 13:30:55 +05:30
? { connect: { id: Number(parentCompanyData.cityXid) } }
: undefined,
states:
parentCompanyData?.stateXid &&
2026-01-09 18:10:38 +05:30
!isNaN(Number(parentCompanyData.stateXid))
2025-12-22 13:30:55 +05:30
? { connect: { id: Number(parentCompanyData.stateXid) } }
: undefined,
countries:
parentCompanyData?.countryXid &&
2026-01-09 18:10:38 +05:30
!isNaN(Number(parentCompanyData.countryXid))
2025-12-22 13:30:55 +05:30
? { connect: { id: Number(parentCompanyData.countryXid) } }
: undefined,
pinCode: parentCompanyData.pinCode || null,
logoPath: parentCompanyData.logoPath || null,
registrationNumber: parentCompanyData.registrationNumber || null,
panNumber: parentCompanyData.panNumber || null,
gstNumber: parentCompanyData.gstNumber || null,
formationDate: parentCompanyData.formationDate
? new Date(parentCompanyData.formationDate as any)
: null,
companyTypes: parentCompanyData.companyTypeXid
? { connect: { id: parentCompanyData.companyTypeXid } }
: undefined,
websiteUrl: parentCompanyData.websiteUrl || null,
instagramUrl: parentCompanyData.instagramUrl || null,
facebookUrl: parentCompanyData.facebookUrl || null,
linkedinUrl: parentCompanyData.linkedinUrl || null,
twitterUrl: parentCompanyData.twitterUrl || null,
},
});
// parent docs
if (parentDocuments?.length) {
const parentDocsData = parentDocuments.map((doc) => ({
hostParentXid: createdParent.id,
documentTypeXid: doc.documentTypeXid,
documentName: sanitizeDocumentName(doc.documentName),
filePath: doc.filePath,
}));
await tx.hostParenetDocuments.createMany({ data: parentDocsData });
}
}
// ⭐ FIX — TRACK USING createdHost (no null risk)
await tx.hostTrack.create({
data: {
hostXid: createdHost.id,
updatedByRole: ROLE_NAME.HOST,
updatedByXid: user_xid,
trackStatus: createdHost.hostStatusInternal,
},
2025-11-17 19:05:26 +05:30
});
return createdHost;
}
2025-11-14 14:08:47 +05:30
// -------------------------------------------------------
// UPDATE FLOW
// -------------------------------------------------------
const updatedHost = await tx.hostHeader.update({
where: { id: existingHostCompany.id },
data: {
companyName: companyData.companyName,
address1: companyData.address1,
address2: companyData.address2,
// Safely handle city connection - only connect if valid ID exists
2025-12-22 13:30:55 +05:30
cities:
companyData.cityXid && !isNaN(Number(companyData.cityXid))
? { connect: { id: Number(companyData.cityXid) } }
: undefined, // Don't change if not provided
// Same for state
2025-12-22 13:30:55 +05:30
states:
companyData.stateXid && !isNaN(Number(companyData.stateXid))
? { connect: { id: Number(companyData.stateXid) } }
: undefined,
// Same for country
2025-12-22 13:30:55 +05:30
countries:
companyData.countryXid && !isNaN(Number(companyData.countryXid))
? { connect: { id: Number(companyData.countryXid) } }
: undefined,
pinCode: companyData.pinCode,
logoPath: options?.deleteCompanyLogo
? companyData.logoPath ?? null
: companyData.logoPath || existingHostCompany.logoPath,
isSubsidairy: companyData.isSubsidairy,
registrationNumber: companyData.registrationNumber,
panNumber: companyData.panNumber,
2025-11-17 19:05:26 +05:30
gstNumber: companyData.gstNumber || null,
formationDate: companyData.formationDate
? new Date(companyData.formationDate as any)
: null,
companyTypes: companyData.companyTypeXid
? { connect: { id: companyData.companyTypeXid } }
: undefined,
referencedBy: companyData.referencedBy || null,
2025-11-17 19:05:26 +05:30
websiteUrl: companyData.websiteUrl || null,
instagramUrl: companyData.instagramUrl || null,
facebookUrl: companyData.facebookUrl || null,
linkedinUrl: companyData.linkedinUrl || null,
twitterUrl: companyData.twitterUrl || null,
stepper,
hostStatusInternal,
hostStatusDisplay,
adminStatusInternal: minglarStatusInternal,
adminStatusDisplay: minglarStatusDisplay,
},
});
2025-11-14 14:08:47 +05:30
// // documents UPSERT
// if (documents?.length) {
// for (const doc of documents) {
// if (!doc.filePath) continue;
// const existingDoc = await tx.hostDocuments.findFirst({
// where: {
// hostXid: updatedHost.id,
// documentTypeXid: doc.documentTypeXid,
// },
// });
// if (existingDoc) {
// await tx.hostDocuments.update({
// where: { id: existingDoc.id },
// data: {
// filePath: doc.filePath,
// documentName:
// sanitizeDocumentName(doc.documentName) ||
// existingDoc.documentName,
// },
// });
// } else {
// await tx.hostDocuments.create({
// data: {
// hostXid: updatedHost.id,
// documentTypeXid: doc.documentTypeXid,
// documentName: sanitizeDocumentName(doc.documentName),
// filePath: doc.filePath,
// },
// });
// }
// }
// }
// documents handling (FINAL FIX)
2026-01-09 18:10:38 +05:30
if (documents?.length) {
for (const doc of documents) {
if (!doc.filePath) continue;
// 🔹 CUSTOM DOCUMENTS → ALWAYS CREATE
if (doc.documentTypeXid === 9) {
await tx.hostDocuments.create({
data: {
hostXid: updatedHost.id,
documentTypeXid: doc.documentTypeXid,
documentName: sanitizeDocumentName(doc.documentName),
filePath: doc.filePath,
},
});
continue;
}
2026-01-09 18:10:38 +05:30
// 🔹 NORMAL DOCUMENTS → UPSERT (ONE PER TYPE)
const existingDoc = await tx.hostDocuments.findFirst({
where: {
hostXid: updatedHost.id,
documentTypeXid: doc.documentTypeXid,
},
});
2026-01-09 18:10:38 +05:30
if (existingDoc) {
await tx.hostDocuments.update({
where: { id: existingDoc.id },
data: {
filePath: doc.filePath,
documentName:
sanitizeDocumentName(doc.documentName) ||
existingDoc.documentName,
},
});
} else {
await tx.hostDocuments.create({
data: {
hostXid: updatedHost.id,
documentTypeXid: doc.documentTypeXid,
documentName: sanitizeDocumentName(doc.documentName),
filePath: doc.filePath,
},
});
}
}
}
2025-11-17 19:05:26 +05:30
// parent logic untouched
if (companyData.isSubsidairy) {
const parentRecords = existingHostCompany.hostParent;
2025-12-22 13:30:55 +05:30
const parentRecord = Array.isArray(parentRecords)
? parentRecords[0]
: parentRecords;
console.log('Yaha aaya update in the apretn me');
if (!parentRecord) {
2025-12-22 13:30:55 +05:30
console.log('Parent record nahi mila to create kar raha hai.');
const createdParent = await tx.hostParent.create({
data: {
host: { connect: { id: updatedHost.id } },
companyName: parentCompanyData.companyName || null,
address1: parentCompanyData.address1 || null,
address2: parentCompanyData.address2 || null,
2025-12-22 13:30:55 +05:30
cities:
parentCompanyData?.cityXid &&
2026-01-09 18:10:38 +05:30
!isNaN(Number(parentCompanyData.cityXid))
2025-12-22 13:30:55 +05:30
? { connect: { id: Number(parentCompanyData.cityXid) } }
: undefined,
states:
parentCompanyData?.stateXid &&
2026-01-09 18:10:38 +05:30
!isNaN(Number(parentCompanyData.stateXid))
2025-12-22 13:30:55 +05:30
? { connect: { id: Number(parentCompanyData.stateXid) } }
: undefined,
countries:
parentCompanyData?.countryXid &&
2026-01-09 18:10:38 +05:30
!isNaN(Number(parentCompanyData.countryXid))
2025-12-22 13:30:55 +05:30
? { connect: { id: Number(parentCompanyData.countryXid) } }
: undefined,
pinCode: parentCompanyData.pinCode || null,
logoPath: options?.deleteParentCompanyLogo
? parentCompanyData?.logoPath ?? null
: parentCompanyData?.logoPath ||
existingParentCompany?.logoPath ||
null,
registrationNumber: parentCompanyData.registrationNumber || null,
panNumber: parentCompanyData.panNumber || null,
gstNumber: parentCompanyData.gstNumber || null,
formationDate: parentCompanyData.formationDate
? new Date(parentCompanyData.formationDate as any)
: null,
companyTypes: parentCompanyData.companyTypeXid
? { connect: { id: parentCompanyData.companyTypeXid } }
: undefined,
websiteUrl: parentCompanyData.websiteUrl || null,
instagramUrl: parentCompanyData.instagramUrl || null,
facebookUrl: parentCompanyData.facebookUrl || null,
linkedinUrl: parentCompanyData.linkedinUrl || null,
twitterUrl: parentCompanyData.twitterUrl || null,
},
});
if (parentDocuments?.length) {
for (const doc of parentDocuments) {
await tx.hostParenetDocuments.create({
data: {
hostParentXid: createdParent.id,
documentTypeXid: doc.documentTypeXid,
documentName: sanitizeDocumentName(doc.documentName),
filePath: doc.filePath,
},
});
}
}
} else {
await tx.hostParent.update({
where: { id: parentRecord.id },
data: {
companyName: parentCompanyData.companyName || null,
address1: parentCompanyData.address1 || null,
address2: parentCompanyData.address2 || null,
2025-12-22 13:30:55 +05:30
cities:
parentCompanyData?.cityXid &&
2026-01-09 18:10:38 +05:30
!isNaN(Number(parentCompanyData.cityXid))
2025-12-22 13:30:55 +05:30
? { connect: { id: Number(parentCompanyData.cityXid) } }
: undefined,
states:
parentCompanyData?.stateXid &&
2026-01-09 18:10:38 +05:30
!isNaN(Number(parentCompanyData.stateXid))
2025-12-22 13:30:55 +05:30
? { connect: { id: Number(parentCompanyData.stateXid) } }
: undefined,
countries:
parentCompanyData?.countryXid &&
2026-01-09 18:10:38 +05:30
!isNaN(Number(parentCompanyData.countryXid))
2025-12-22 13:30:55 +05:30
? { connect: { id: Number(parentCompanyData.countryXid) } }
: undefined,
pinCode: parentCompanyData.pinCode || null,
logoPath: options?.deleteParentCompanyLogo
? parentCompanyData?.logoPath ?? null
: parentCompanyData?.logoPath ||
existingParentCompany?.logoPath ||
null,
registrationNumber: parentCompanyData.registrationNumber || null,
panNumber: parentCompanyData.panNumber || null,
gstNumber: parentCompanyData.gstNumber || null,
formationDate: parentCompanyData.formationDate
? new Date(parentCompanyData.formationDate as any)
: null,
companyTypes: parentCompanyData.companyTypeXid
? { connect: { id: parentCompanyData.companyTypeXid } }
: undefined,
websiteUrl: parentCompanyData.websiteUrl || null,
instagramUrl: parentCompanyData.instagramUrl || null,
facebookUrl: parentCompanyData.facebookUrl || null,
linkedinUrl: parentCompanyData.linkedinUrl || null,
twitterUrl: parentCompanyData.twitterUrl || null,
},
});
2025-12-29 17:07:48 +05:30
// if (parentDocuments?.length) {
// for (const doc of parentDocuments) {
// const existingParentDoc = await tx.hostParenetDocuments.findFirst(
// {
// where: {
// hostParentXid: parentRecord.id,
// documentTypeXid: doc.documentTypeXid,
// },
// },
// );
// if (existingParentDoc) {
// await tx.hostParenetDocuments.update({
// where: { id: existingParentDoc.id },
// data: {
// filePath: doc.filePath,
// documentName:
// sanitizeDocumentName(doc.documentName) ||
// existingParentDoc.documentName,
// },
// });
// } else {
// await tx.hostParenetDocuments.create({
// data: {
// hostParentXid: parentRecord.id,
// documentTypeXid: doc.documentTypeXid,
// documentName: sanitizeDocumentName(doc.documentName),
// filePath: doc.filePath,
// },
// });
// }
// }
// }
if (parentDocuments?.length) {
2025-12-29 17:07:48 +05:30
const parentDocsData = parentDocuments
.filter((doc) => doc.filePath)
.map((doc) => ({
hostParentXid: parentRecord.id,
documentTypeXid: doc.documentTypeXid,
documentName: sanitizeDocumentName(doc.documentName),
filePath: doc.filePath,
}));
if (parentDocsData.length) {
await tx.hostParenetDocuments.createMany({
data: parentDocsData,
});
}
2025-11-17 19:05:26 +05:30
}
}
2025-12-30 14:22:18 +05:30
} else {
2025-12-22 13:30:55 +05:30
console.log('Last ke else block me aaya hai');
const previousParent = existingHostCompany.hostParent;
let prevParentId = null;
2025-11-14 14:08:47 +05:30
if (Array.isArray(previousParent) && previousParent.length) {
prevParentId = previousParent[0].id;
2025-12-22 13:30:55 +05:30
} else if (
previousParent &&
typeof previousParent === 'object' &&
'id' in previousParent
) {
prevParentId = previousParent.id;
}
if (prevParentId) {
await tx.hostParenetDocuments.deleteMany({
where: { hostParentXid: prevParentId },
});
await tx.hostParent.delete({ where: { id: prevParentId } });
}
}
// ⭐ FIX — USE updatedHost instead of re-querying hostHeader
await tx.hostTrack.create({
data: {
hostXid: updatedHost.id,
updatedByRole: ROLE_NAME.HOST,
updatedByXid: user_xid,
trackStatus: updatedHost.hostStatusInternal,
},
});
// suggestion update unchanged
if (!isDraft) {
await tx.hostSuggestion.updateMany({
where: { hostXid: updatedHost.id, isActive: true, isreviewed: false },
data: {
isreviewed: true,
reviewedByXid: user_xid,
reviewOn: new Date(),
},
});
}
return updatedHost;
});
}
async getSuggestionDetails(user_xid: number) {
const hostDetails = await this.prisma.hostHeader.findFirst({
where: { userXid: user_xid, isActive: true },
include: {
user: {
select: {
id: true,
emailAddress: true,
firstName: true,
2025-12-22 13:30:55 +05:30
userRefNumber: true,
},
},
accountManager: {
select: {
id: true,
emailAddress: true,
firstName: true,
},
},
},
});
if (!hostDetails) {
return { hostSuggestionDetails: [], hostDetails: null };
}
const hostSuggestionDetails = await this.prisma.hostSuggestion.findMany({
where: { hostXid: hostDetails.id, isActive: true, isreviewed: false },
});
if (hostSuggestionDetails) {
await this.prisma.hostSuggestion.updateMany({
where: { hostXid: hostDetails.id, isActive: true, isreviewed: false },
data: {
isreviewed: true,
reviewedByXid: hostDetails.id,
reviewOn: new Date(),
},
});
}
return { hostSuggestionDetails, hostDetails };
}
// async createOrUpdateHeader(
// activityXid: number,
// pqqQuestionXid: number,
// pqqAnswerXid: number,
// comments: string | null
// ) {
// // find existing header
// const existing = await this.prisma.activityPQQheader.findFirst({
// where: { activityXid, pqqQuestionXid, deletedAt: null }
// });
// if (!existing) {
// return await this.prisma.activityPQQheader.create({
// data: {
// activityXid,
// pqqQuestionXid,
// pqqAnswerXid,
// comments
// }
// });
// }
// // mark old supportings deleted
// await this.prisma.activityPQQSupportings.updateMany({
// where: { activityPqqHeaderXid: existing.id },
// data: {
// isActive: false,
// deletedAt: new Date()
// }
// });
// // update header
// return await this.prisma.activityPQQheader.update({
// where: { id: existing.id },
// data: {
// pqqAnswerXid,
// comments
// }
// });
// }
// async addSupportingFile(
// headerId: number,
// mimeType: string,
// fileUrl: string
// ) {
// return await this.prisma.activityPQQSupportings.create({
// data: {
// activityPqqHeaderXid: headerId,
// mediaType: mimeType,
// mediaFileName: fileUrl
// }
// });
// }
2025-11-24 23:19:18 +05:30
async calculatePqqScoreForUser(activityXid: number) {
return await this.prisma.$transaction(async (tx) => {
// 1. Get all headers for this activity (user's answers)
const answers = await this.prisma.activityPQQheader.findMany({
where: { activityXid, isActive: true },
include: {
pqqQuestions: {
include: {
pqqSubCategories: {
include: {
category: true,
},
},
},
},
pqqAnswers: true,
2025-11-24 23:19:18 +05:30
},
});
2025-11-24 23:19:18 +05:30
if (!answers.length) {
return {
overallPercentage: 0,
categoryWise: {},
};
}
2025-11-24 23:19:18 +05:30
// Prepare accumulators
let totalUserPoints = 0;
let totalMaxPoints = 0;
// For category-wise scoring
const categories: Record<
number,
{
categoryId: number;
categoryName: string;
userPoints: number;
maxPoints: number;
}
> = {};
2025-11-24 23:19:18 +05:30
for (const item of answers) {
const question = item.pqqQuestions;
const answer = item.pqqAnswers; // may be null if no answer selected
2025-11-24 23:19:18 +05:30
// skip if question missing
if (!question) continue;
const maxPoints = Number(question.maxPoints || 0);
const userPoints = Number(answer?.answerPoints || 0);
2025-11-24 23:19:18 +05:30
totalUserPoints += userPoints;
totalMaxPoints += maxPoints;
2025-11-24 23:19:18 +05:30
// Category (guard nested relations)
const category = question.pqqSubCategories?.category;
if (!category) continue;
const categoryId = category.id;
if (!categories[categoryId]) {
categories[categoryId] = {
categoryId,
categoryName: category.categoryName,
userPoints: 0,
maxPoints: 0,
};
}
categories[categoryId].userPoints += userPoints;
categories[categoryId].maxPoints += maxPoints;
2025-11-24 23:19:18 +05:30
}
// Overall percent
const overallPercentage =
2025-12-22 13:30:55 +05:30
totalMaxPoints > 0
? round2((totalUserPoints / totalMaxPoints) * 100)
: 0;
2025-11-24 23:19:18 +05:30
// ---------- 🔥 ONLY FIRST 2 CATEGORIES ----------
const categoryArray = Object.values(categories);
// Sort by categoryId (or change to displayOrder if needed)
categoryArray.sort((a, b) => a.categoryId - b.categoryId);
// Take only first 2 categories
const topTwo = categoryArray.slice(0, 2);
const categoryWise: Record<string, number> = {};
for (const c of topTwo) {
categoryWise[c.categoryName] =
c.maxPoints > 0 ? round2((c.userPoints / c.maxPoints) * 100) : 0;
}
2025-11-24 23:19:18 +05:30
await this.prisma.activities.update({
where: {
2025-12-22 13:30:55 +05:30
id: activityXid,
},
data: {
totalScore: round2(overallPercentage),
sustainabilityScore: round2(categoryWise.Sustainability),
safetyScore: round2(categoryWise.Safety),
2025-12-22 13:30:55 +05:30
},
});
2025-11-24 23:19:18 +05:30
// Return final score object
return {
overallPercentage,
categoryWise,
};
});
2025-11-24 23:19:18 +05:30
}
async createHeader(
2025-11-19 16:55:54 +05:30
activityXid: number,
pqqQuestionXid: number,
pqqAnswerXid: number,
comments?: string | null,
2025-11-19 16:55:54 +05:30
) {
return await this.prisma.activityPQQheader.create({
data: {
activityXid,
pqqQuestionXid,
pqqAnswerXid,
comments: comments || null, // Handle null comments
},
2025-11-19 16:55:54 +05:30
});
}
2025-11-19 16:55:54 +05:30
2025-12-22 13:30:55 +05:30
async findHeaderByCompositeKey(activityXid: number, pqqQuestionXid: number) {
return await this.prisma.activityPQQheader.findFirst({
where: {
activityXid,
pqqQuestionXid,
},
2025-11-19 16:55:54 +05:30
});
}
2025-11-19 16:55:54 +05:30
2025-12-22 13:30:55 +05:30
async updateHeader(
headerId: number,
pqqAnswerXid: number,
comments?: string | null,
) {
2025-11-19 16:55:54 +05:30
return await this.prisma.activityPQQheader.update({
where: {
id: headerId,
},
2025-11-19 16:55:54 +05:30
data: {
comments: comments || null, // Handle null comments
pqqAnswerXid: pqqAnswerXid,
updatedAt: new Date(),
},
2025-11-19 16:55:54 +05:30
});
}
async addSupportingFile(headerId: number, mimeType: string, fileUrl: string) {
2025-11-19 16:55:54 +05:30
return await this.prisma.activityPQQSupportings.create({
data: {
activityPqqHeaderXid: headerId,
mediaType: mimeType,
mediaFileName: fileUrl,
},
2025-11-19 16:55:54 +05:30
});
}
async getSupportingFilesByHeaderId(headerId: number) {
return await this.prisma.activityPQQSupportings.findMany({
where: {
activityPqqHeaderXid: headerId,
},
orderBy: {
id: 'asc', // Maintain consistent order
},
});
}
2025-12-03 12:44:07 +05:30
async submitpqqforreview(activity_xid: number, user_xid: number) {
return await this.prisma.$transaction(async (tx) => {
const activity = await this.prisma.activities.findFirst({
where: { id: activity_xid, isActive: true },
select: {
id: true,
activityTitle: true,
activityRefNumber: true,
activityDisplayStatus: true,
activityInternalStatus: true,
amInternalStatus: true,
2025-12-22 13:30:55 +05:30
amDisplayStatus: true,
},
});
2025-12-03 12:44:07 +05:30
if (!activity) {
2025-12-22 13:30:55 +05:30
throw new ApiError(404, 'Activity not found');
}
2025-12-22 13:30:55 +05:30
if (
activity.activityInternalStatus == ACTIVITY_INTERNAL_STATUS.PQ_TO_UPDATE
) {
2025-12-03 12:44:07 +05:30
return await this.prisma.$transaction(async (tx) => {
await this.prisma.activities.update({
where: { id: activity_xid },
data: {
activityInternalStatus: ACTIVITY_INTERNAL_STATUS.PQ_SUBMITTED,
activityDisplayStatus: ACTIVITY_DISPLAY_STATUS.PQ_IN_REVIEW,
amInternalStatus: ACTIVITY_AM_INTERNAL_STATUS.PQ_TO_REVIEW,
2025-12-22 13:30:55 +05:30
amDisplayStatus: ACTIVITY_AM_DISPLAY_STATUS.REVISED,
},
});
2025-12-03 12:44:07 +05:30
await tx.activityTrack.create({
data: {
activityXid: activity_xid,
trackType: ACTIVITY_TRACK_TYPE.PQ,
trackStatus: ACTIVITY_TRACK_STATUS.PQ_SUBMITTED,
updatedByXid: user_xid,
updatedByRole: ROLE_NAME.HOST,
2025-12-22 13:30:55 +05:30
updatedOn: new Date(),
},
});
});
2025-12-03 12:44:07 +05:30
} else {
return await this.prisma.$transaction(async (tx) => {
await this.prisma.activities.update({
where: { id: activity_xid },
data: {
activityInternalStatus: ACTIVITY_INTERNAL_STATUS.PQ_SUBMITTED,
activityDisplayStatus: ACTIVITY_DISPLAY_STATUS.PQ_IN_REVIEW,
amInternalStatus: ACTIVITY_AM_INTERNAL_STATUS.PQ_TO_REVIEW,
2025-12-22 13:30:55 +05:30
amDisplayStatus: ACTIVITY_AM_DISPLAY_STATUS.NEW,
},
});
2025-12-03 12:44:07 +05:30
await tx.activityTrack.create({
data: {
activityXid: activity_xid,
trackType: ACTIVITY_TRACK_TYPE.PQ,
trackStatus: ACTIVITY_TRACK_STATUS.PQ_SUBMITTED,
updatedByXid: user_xid,
updatedByRole: ROLE_NAME.HOST,
2025-12-22 13:30:55 +05:30
updatedOn: new Date(),
},
});
});
2025-12-03 12:44:07 +05:30
}
2025-12-22 13:30:55 +05:30
});
}
async updateSupportingFile(
supportingFileId: number,
mimeType: string,
fileUrl: string,
) {
return await this.prisma.activityPQQSupportings.update({
where: {
id: supportingFileId,
},
data: {
mediaType: mimeType,
mediaFileName: fileUrl,
updatedAt: new Date(),
},
});
}
async deleteSupportingFile(supportingFileId: number) {
return await this.prisma.activityPQQSupportings.delete({
where: {
id: supportingFileId,
},
});
}
2025-12-22 13:30:55 +05:30
async markPQQSuggestionReviewed(
user_xid: number,
activityPqqHeaderXid: number,
activityPQQSuggestionId: number,
) {
return await this.prisma.activityPQQSuggestions.update({
where: {
id: activityPQQSuggestionId,
activityPqqHeaderXid: activityPqqHeaderXid,
isActive: true,
2025-12-22 13:30:55 +05:30
isReviewed: false,
},
data: {
isReviewed: true,
reviewedByXid: user_xid,
2025-12-22 13:30:55 +05:30
reviewedOn: new Date(),
},
});
}
async getAllPQQQuesAndSubmittedAns(activity_xid: number) {
return await this.prisma.activityPQQheader.findMany({
where: { isActive: true, activityXid: activity_xid },
select: {
id: true,
activityXid: true,
pqqQuestionXid: true,
pqqAnswerXid: true,
comments: true,
activity: {
select: {
id: true,
activityType: {
select: {
id: true,
activityTypeName: true
}
}
}
},
pqqQuestions: {
select: {
questionName: true,
maxPoints: true,
displayOrder: true,
pqqSubCategories: {
select: {
id: true,
subCategoryName: true,
displayOrder: true,
category: {
select: {
id: true,
categoryName: true,
displayOrder: true,
2025-12-22 13:30:55 +05:30
},
},
},
},
},
},
ActivityPQQSuggestions: {
select: {
id: true,
title: true,
comments: true,
isReviewed: true,
reviewedBy: true,
reviewedOn: true,
2025-12-22 13:30:55 +05:30
},
},
pqqAnswers: {
select: {
id: true,
displayOrder: true,
answerName: true,
2025-12-22 13:30:55 +05:30
answerPoints: true,
},
},
ActivityPQQSupportings: {
select: {
id: true,
mediaFileName: true,
mediaType: true,
2025-12-22 13:30:55 +05:30
},
},
},
});
}
async getAllActivityTypesWithInterest(search?: string) {
const where: any = {
isActive: true,
deletedAt: null,
};
if (search && search.trim() !== '') {
const q = search.trim();
where.OR = [
{ activityTypeName: { contains: q, mode: 'insensitive' } },
{ interests: { interestName: { contains: q, mode: 'insensitive' } } },
];
}
return await this.prisma.activityTypes.findMany({
where,
select: {
id: true,
activityTypeName: true,
interestXid: true,
interests: {
select: {
id: true,
interestName: true,
displayOrder: true,
},
},
},
orderBy: { activityTypeName: 'asc' },
});
}
2026-02-13 11:34:55 +05:30
/************* ✨ Windsurf Command ⭐ *************/
/**
* Get all details of an activity and its venue.
*
* @param {number} activityXid - The id of the activity to fetch.
*
* @returns {Promise<object>} - The activity details with its venue.
*
* @example
* const activity = await getAllDetailsOfActivityAndVenue(1);
* console.log(activity);
*/
/******* 88cdc2a8-b07f-4da8-972a-4e00f5399a65 *******/
async getAllDetailsOfActivityAndVenue(activityXid: number) {
const activity = await this.prisma.activities.findFirst({
where: { id: activityXid, isActive: true },
select: {
id: true,
activityTitle: true,
activityDescription: true,
activityDisplayStatus: true,
activityInternalStatus: true,
activityRefNumber: true,
checkInAddress: true,
checkInLat: true,
checkInLong: true,
checkOutAddress: true,
checkOutLat: true,
checkOutLong: true,
pickUpDropAvailable: true,
pickUpDropIsChargeable: true,
inActivityAvailable: true,
activityDurationMins: true,
totalScore: true,
isCheckOutSame: true,
activityType: {
select: {
id: true,
activityTypeName: true,
2025-12-29 17:07:48 +05:30
interests: {
select: {
id: true,
interestName: true,
2025-12-30 14:22:18 +05:30
},
2025-12-29 17:07:48 +05:30
},
energyLevel: {
select: {
id: true,
energyLevelName: true,
energyIcon: true,
energyColor: true,
2025-12-30 14:22:18 +05:30
displayOrder: true,
},
},
2025-12-24 15:11:37 +05:30
},
},
ActivitiesMedia: {
where: {
2025-12-24 15:11:37 +05:30
isActive: true,
},
select: {
id: true,
mediaType: true,
mediaFileName: true,
isCoverImage: true
2025-12-24 15:11:37 +05:30
},
},
ActivityVenues: {
where: {
2025-12-24 15:11:37 +05:30
isActive: true,
},
select: {
id: true,
venueName: true,
2025-12-30 12:15:16 +05:30
venueLabel: true,
venueCapacity: true,
availableSeats: true,
isMinPeopleReqMandatory: true,
minPeopleRequired: true,
minReqfullfilledBeforeMins: true,
venueDescription: true,
ActivityVenueArtifacts: {
select: {
id: true,
mediaType: true,
2025-12-24 15:11:37 +05:30
mediaFileName: true,
},
},
2025-12-29 17:07:48 +05:30
ActivityPrices: {
select: {
id: true,
noOfSession: true,
isPackage: true,
sessionValidity: true,
sessionValidityFrequency: true,
2025-12-30 14:22:18 +05:30
sellPrice: true,
ActivityPriceTaxes: {
select: {
id: true,
taxXid: true,
taxPer: true,
taxes: {
select: {
id: true,
countryXid: true,
country: {
select: {
countryName: true,
countryCode: true,
2025-12-31 13:03:23 +05:30
countryFlag: true,
},
},
taxName: true,
taxPer: true,
2025-12-31 13:03:23 +05:30
},
},
},
},
2025-12-30 14:22:18 +05:30
},
orderBy: { createdAt: 'asc' },
2025-12-30 14:22:18 +05:30
},
2025-12-24 15:11:37 +05:30
},
},
ActivityPickUpDetails: {
where: {
isActive: true,
},
select: {
id: true,
isPickUp: true,
locationAddress: true,
locationLat: true,
locationLong: true,
transportTotalPrice: true,
transportBasePrice: true,
2025-12-24 15:11:37 +05:30
},
},
2025-12-30 13:39:36 +05:30
activityPickUpTransports: {
where: {
2025-12-30 14:22:18 +05:30
isActive: true,
2025-12-30 13:39:36 +05:30
},
select: {
transportModeXid: true,
transportMode: {
select: {
transportModeName: true,
2025-12-30 14:22:18 +05:30
transportModeIcon: true,
},
},
},
2025-12-30 13:39:36 +05:30
},
foodAvailable: true,
foodIsChargeable: true,
activityFoodTypes: {
where: { isActive: true },
select: {
id: true,
foodType: {
select: {
id: true,
2025-12-24 15:11:37 +05:30
foodTypeName: true,
},
},
2025-12-24 15:11:37 +05:30
},
},
ActivityFoodCost: {
where: {
2025-12-31 13:03:23 +05:30
isActive: true,
},
select: {
id: true,
2025-12-31 13:03:23 +05:30
totalAmount: true,
},
},
activityCuisines: {
where: {
2025-12-24 15:11:37 +05:30
isActive: true,
},
select: {
id: true,
foodCuisine: {
select: {
id: true,
2025-12-24 15:11:37 +05:30
cuisineName: true,
},
},
},
},
alcoholAvailable: true,
trainerAvailable: true,
trainerIsChargeable: true,
ActivityTrainers: {
where: {
2025-12-24 15:11:37 +05:30
isActive: true,
},
select: {
id: true,
2025-12-24 15:11:37 +05:30
totalAmount: true,
},
},
ActivityNavigationModes: {
where: {
2025-12-24 15:11:37 +05:30
isActive: true,
},
select: {
id: true,
navigationModeName: true,
isInActivityChargeable: true,
navigationModesTotalPrice: true,
2025-12-24 15:11:37 +05:30
},
},
equipmentAvailable: true,
equipmentIsChargeable: true,
ActivityEquipments: {
where: {
2025-12-24 15:11:37 +05:30
isActive: true,
},
select: {
id: true,
equipmentName: true,
isEquipmentChargeable: true,
equipmentTotalPrice: true,
2025-12-24 15:11:37 +05:30
},
},
ActivityOtherDetails: {
where: {
2025-12-24 15:11:37 +05:30
isActive: true,
},
select: {
id: true,
exclusiveNotes: true,
dosNotes: true,
dontsNotes: true,
tipsNotes: true,
termsAndCondition: true,
Cancellations: true,
SafetyInstruction: true
2025-12-24 15:11:37 +05:30
},
},
ActivityEligibility: {
where: {
2025-12-24 15:11:37 +05:30
isActive: true,
},
select: {
id: true,
isAgeRestriction: true,
// ageRestriction: {
// select: {
// id: true,
// ageRestrictionName: true,
// minAge: true,
// maxAge: true,
// },
// },
ageRestrictionName: true,
ageEntered: true,
minAge: true,
maxAge: true,
isWeightRestriction: true,
weightRestrictionName: true,
weightEntered: true,
weightIn: true,
minWeight: true,
maxWeight: true,
isHeightRestriction: true,
heightRestrictionName: true,
heightEntered: true,
heightIn: true,
minHeight: true,
2025-12-24 15:11:37 +05:30
maxHeight: true,
},
},
ActivityAllowedEntry: {
where: {
2025-12-24 15:11:37 +05:30
isActive: true,
},
select: {
id: true,
allowedEntryType: {
select: {
id: true,
2025-12-24 15:11:37 +05:30
allowedEntryTypeName: true,
},
},
},
},
frequency: {
where: {
isActive: true
},
select: {
id: true,
frequencyName: true
}
},
ActivityAmenities: {
where: {
2025-12-24 15:11:37 +05:30
isActive: true,
},
select: {
id: true,
amenities: {
select: {
id: true,
2025-12-24 15:11:37 +05:30
amenitiesName: true,
2026-02-13 11:34:55 +05:30
amenitiesIcon: true
2025-12-24 15:11:37 +05:30
},
},
},
},
cancellationAvailable: true,
2025-12-24 15:11:37 +05:30
cancellationAllowedBeforeMins: true,
// accountManager: {
// select: {
// id: true,
// firstName: true,
// lastName: true,
// emailAddress: true,
// mobileNumber: true,
// }
// },
host: {
select: {
id: true,
companyName: true,
stepper: true,
adminStatusDisplay: true,
adminStatusInternal: true,
user: {
select: {
id: true,
userRefNumber: true,
firstName: true,
2026-01-05 14:47:24 +05:30
lastName: true,
},
},
},
},
2025-12-24 15:11:37 +05:30
},
});
if (!activity) {
throw new ApiError(404, 'Activity not found');
}
if (Array.isArray(activity.ActivitiesMedia)) {
for (const media of activity.ActivitiesMedia) {
if (!media?.mediaFileName) continue;
const filePath = media.mediaFileName;
// ✅ Robust S3 key extraction
const key = filePath.startsWith('http')
? new URL(filePath).pathname.replace(/^\/+/, '')
: filePath;
(media as any).presignedUrl = await getPresignedUrl(bucket, key);
}
}
2026-02-13 11:34:55 +05:30
if (Array.isArray(activity.ActivityAmenities)) {
for (const item of activity.ActivityAmenities) {
const filePath = item?.amenities?.amenitiesIcon;
if (!filePath) continue;
// ✅ Robust S3 key extraction
const key = filePath.startsWith('http')
? new URL(filePath).pathname.replace(/^\/+/, '')
: filePath;
(item.amenities as any).presignedUrl = await getPresignedUrl(bucket, key);
}
}
if (Array.isArray(activity.ActivityVenues)) {
for (const venue of activity.ActivityVenues) {
if (!Array.isArray(venue.ActivityVenueArtifacts)) continue;
for (const artifact of venue.ActivityVenueArtifacts) {
if (!artifact?.mediaFileName) continue;
const filePath = artifact.mediaFileName;
// ✅ Robust S3 key extraction
const key = filePath.startsWith('http')
? new URL(filePath).pathname.replace(/^\/+/, '')
: filePath;
(artifact as any).preSignedURL = await getPresignedUrl(bucket, key);
}
}
}
return activity;
}
// async createActivity(
// userId: number,
// activityTypeXid: number,
// frequenciesXid?: number,
// ) {
// return await this.prisma.$transaction(async (tx) => {
// // Fetch host
// const host = await tx.hostHeader.findFirst({
// where: { userXid: userId, isActive: true },
// });
// if (!host) throw new ApiError(404, 'Host not found for the user');
// // Validate activityType
// const activityType = await tx.activityTypes.findUnique({
// where: { id: activityTypeXid },
// });
// if (!activityType) throw new ApiError(404, 'Activity type not found');
// // Validate frequency
// if (frequenciesXid) {
// const freq = await tx.frequencies.findUnique({
// where: { id: frequenciesXid },
// });
// if (!freq) throw new ApiError(404, 'Frequency not found');
// }
// // Generate reference number
// const referenceNumber = await generateActivityRefNumber(tx);
// // Create activity
// const created = await tx.activities.create({
// data: {
// hostXid: host.id,
// activityTypeXid,
// frequenciesXid: frequenciesXid || null,
// activityInternalStatus: ACTIVITY_INTERNAL_STATUS.DRAFT_PQ,
// activityDisplayStatus: ACTIVITY_DISPLAY_STATUS.DRAFT_PQ,
// amInternalStatus: ACTIVITY_AM_INTERNAL_STATUS.DRAFT_PQ,
// amDisplayStatus: ACTIVITY_AM_DISPLAY_STATUS.DRAFT_PQ,
// activityRefNumber: referenceNumber,
// },
// });
// return created;
// });
// }
async createActivityAndAllQuestionsEntry(
userId: number,
activityTypeXid: number,
2025-12-22 13:30:55 +05:30
frequenciesXid: number,
) {
return await this.prisma.$transaction(async (tx) => {
const hostUserDetail = await tx.user.findFirst({
2026-02-27 19:03:28 +05:30
where: { id: userId, isActive: true },
select: {
id: true,
userRefNumber: true,
}
})
2026-02-27 19:03:28 +05:30
if (!hostUserDetail) {
throw new ApiError(404, 'User not found');
}
const host = await tx.hostHeader.findFirst({
where: { userXid: userId, isActive: true },
});
if (!host) throw new ApiError(404, 'Host not found for the user');
const activityType = await tx.activityTypes.findUnique({
2026-02-27 19:03:28 +05:30
where: { id: activityTypeXid, isActive: true },
include: {
2026-02-27 19:03:28 +05:30
interests: true, // ✅ correct
energyLevel: true, // ✅ this is correct already
},
});
if (!activityType) throw new ApiError(404, 'Activity type not found');
if (frequenciesXid) {
const freq = await tx.frequencies.findUnique({
where: { id: frequenciesXid },
});
if (!freq) throw new ApiError(404, 'Frequency not found');
}
const referenceNumber = await generateActivityRefNumber(tx, host.id, activityTypeXid, hostUserDetail.userRefNumber);
const created = await tx.activities.create({
data: {
hostXid: host.id,
activityTypeXid,
frequenciesXid: frequenciesXid || null,
activityInternalStatus: ACTIVITY_INTERNAL_STATUS.DRAFT_PQ,
activityDisplayStatus: ACTIVITY_DISPLAY_STATUS.DRAFT_PQ,
amInternalStatus: ACTIVITY_AM_INTERNAL_STATUS.DRAFT_PQ,
amDisplayStatus: ACTIVITY_AM_DISPLAY_STATUS.DRAFT_PQ,
activityRefNumber: referenceNumber,
},
});
const questions = await tx.pQQCategories.findMany({
where: { isActive: true },
select: {
id: true,
categoryName: true,
displayOrder: true,
pqqsubCategories: {
where: { isActive: true },
select: {
id: true,
subCategoryName: true,
displayOrder: true,
questions: {
where: { isActive: true },
select: {
id: true,
questionName: true,
maxPoints: true,
displayOrder: true,
},
orderBy: { displayOrder: 'asc' },
},
},
orderBy: { displayOrder: 'asc' },
},
},
orderBy: { displayOrder: 'asc' },
});
// FLATTEN questions
const allQuestions: number[] = [];
for (const cat of questions) {
for (const sub of cat.pqqsubCategories) {
for (const q of sub.questions) {
allQuestions.push(q.id);
}
}
}
await tx.activityPQQheader.createMany({
data: allQuestions.map((id) => ({
activityXid: created.id,
pqqQuestionXid: id,
pqqAnswerXid: null,
})),
});
const pqqHeaderData = await tx.activityPQQheader.findMany({
where: {
activityXid: created.id,
isActive: true,
},
select: {
comments: true,
pqqAnswerXid: true,
pqqQuestions: {
select: {
id: true,
questionName: true,
maxPoints: true,
displayOrder: true,
pqqSubCategories: {
select: {
id: true,
subCategoryName: true,
displayOrder: true,
category: {
select: {
id: true,
categoryName: true,
2025-12-22 13:30:55 +05:30
displayOrder: true,
},
},
},
},
// 🔥 ALL ANSWER OPTIONS FOR THIS QUESTION
PQQAnswers: {
where: { isActive: true },
select: {
id: true,
answerName: true,
answerPoints: true,
2025-12-22 13:30:55 +05:30
displayOrder: true,
},
2025-12-22 13:30:55 +05:30
orderBy: { displayOrder: 'asc' },
},
},
},
ActivityPQQSuggestions: {
where: { isActive: true },
select: {
id: true,
title: true,
comments: true,
2025-12-22 13:30:55 +05:30
activityPqqHeaderXid: true,
},
},
ActivityPQQSupportings: {
where: { isActive: true },
select: {
id: true,
mediaType: true,
2025-12-22 13:30:55 +05:30
mediaFileName: true,
},
},
},
2025-12-22 13:30:55 +05:30
orderBy: { id: 'asc' },
});
// ---------------- GROUPING ------------------
const grouped: any = {};
for (const item of pqqHeaderData) {
const q = item.pqqQuestions;
const sub = q.pqqSubCategories;
const cat = sub.category;
// 1⃣ Category level
if (!grouped[cat.id]) {
grouped[cat.id] = {
id: cat.id,
categoryName: cat.categoryName,
displayOrder: cat.displayOrder,
2025-12-22 13:30:55 +05:30
pqqsubCategories: [],
};
}
const category = grouped[cat.id];
2025-12-22 13:30:55 +05:30
let subCat = category.pqqsubCategories.find(
(s: any) => s.id === sub.id,
);
if (!subCat) {
subCat = {
id: sub.id,
subCategoryName: sub.subCategoryName,
displayOrder: sub.displayOrder,
2025-12-22 13:30:55 +05:30
questions: [],
};
category.pqqsubCategories.push(subCat);
}
subCat.questions.push({
id: q.id,
questionName: q.questionName,
maxPoints: q.maxPoints,
comments: item.comments || null,
displayOrder: q.displayOrder,
allAnswerOptions: q.PQQAnswers || [],
suggestions: item.ActivityPQQSuggestions || [],
supportings: item.ActivityPQQSupportings || [],
});
}
2025-12-22 13:30:55 +05:30
const sortedCategories: any = Object.values(grouped).sort(
(a: any, b: any) => a.displayOrder - b.displayOrder,
);
for (const cat of sortedCategories) {
2025-12-22 13:30:55 +05:30
cat.pqqsubCategories.sort(
(a: any, b: any) => a.displayOrder - b.displayOrder,
);
for (const sub of cat.pqqsubCategories) {
2025-12-22 13:30:55 +05:30
sub.questions.sort(
(a: any, b: any) => a.displayOrder - b.displayOrder,
);
}
}
return {
activity_xid: created.id,
activityType: activityType,
2025-12-22 13:30:55 +05:30
sortedCategories,
};
});
}
/**
* Create a full activity with related records based on payload from the onboarding form.
* This method will create Activities + ActivityOtherDetails + ActivitiesMedia +
* ActivityVenues + ActivityPrices + ActivityFoodTypes + ActivityCuisine +
* ActivityPickUpTransport/Details + ActivityNavigationModes + ActivityEquipments +
2025-12-22 13:30:55 +05:30
* ActivityAmenities + ActivityEligibility
*/
2025-12-24 19:04:51 +05:30
async createOrUpdateActivity(
userId: number,
payload: CreateActivityInput,
isDraft: boolean,
) {
/* =====================================================
* HELPERS
* ===================================================== */
const toBool = (v: any) =>
v === true || v === 'true' || v === 1 || v === '1';
const toBoolOrNull = (v: any): boolean | null => {
if (v === null || v === undefined || v === '') return null;
return v === true || v === 'true' || v === 1 || v === '1';
};
const toNumber = (v: any) =>
v === undefined || v === null || v === '' ? undefined : Number(v);
const round2 = (v: number) => Math.round(v);
const computeBasePriceAndTaxes = (
sellPrice: number,
taxes: Array<{ id: number; taxPer: number }>,
) => {
if (!taxes.length) {
return { basePrice: round2(sellPrice), taxDetails: [] };
}
const totalTaxPer = taxes.reduce((s, t) => s + Number(t.taxPer || 0), 0);
const basePrice = round2(sellPrice / (1 + totalTaxPer / 100));
return {
basePrice,
taxDetails: taxes.map((t) => ({
taxXid: t.id,
taxPer: t.taxPer,
taxAmount: round2(basePrice * (t.taxPer / 100)),
})),
};
2025-12-21 17:28:08 +05:30
};
2025-12-24 17:07:11 +05:30
/* =====================================================
* DURATION CONVERSION
* ===================================================== */
const durationDays = Number(payload.durationDays ?? 0);
const durationHours = Number(payload.durationHours ?? 0);
const durationMins = Number(payload.durationMins ?? 0);
/* =====================================================
* BASIC GUARDS
* ===================================================== */
if (!payload.activityXid) {
throw new ApiError(400, 'activityXid is required');
2025-12-21 17:28:08 +05:30
}
2026-01-09 18:10:38 +05:30
payload.venues = Array.isArray(payload.venues)
? payload.venues
.filter(v => v && typeof v === 'object')
.map(v => ({
...v,
venueName: v.venueName ?? null,
venueLabel: v.venueLabel ?? null,
prices: Array.isArray(v.prices) ? v.prices : [],
media: Array.isArray(v.media) ? v.media : [],
}))
: [];
/* =====================================================
* HARD NORMALIZATION (SERVICE-LEVEL)
* ===================================================== */
payload.foodAvailable = toBoolOrNull(payload.foodAvailable);
payload.alcoholAvailable = toBoolOrNull(payload.alcoholAvailable);
payload.trainerAvailable = toBoolOrNull(payload.trainerAvailable);
payload.pickUpDropAvailable = toBoolOrNull(payload.pickUpDropAvailable);
payload.inActivityAvailable = toBoolOrNull(payload.inActivityAvailable);
payload.equipmentAvailable = toBoolOrNull(payload.equipmentAvailable);
payload.cancellationAvailable = toBoolOrNull(payload.cancellationAvailable);
payload.isInstantBooking = toBool(payload.isInstantBooking);
payload.isCheckOutSame = toBool(payload.isCheckOutSame);
payload.alcoholAvailable = toBoolOrNull(payload.alcoholAvailable);
payload.trainerTotalAmount = toNumber(payload.trainerTotalAmount);
2025-12-31 13:01:08 +05:30
payload.cancellationAllowedBeforeMins = toNumber(
2025-12-31 13:03:23 +05:30
payload.cancellationAllowedBeforeMins,
);
2025-12-31 13:01:08 +05:30
2025-12-31 13:03:23 +05:30
/* =====================================================
* CANCELLATION VALIDATION (NO CONVERSION)
* ===================================================== */
if (payload.cancellationAvailable) {
if (!isDraft) {
if (
typeof payload.cancellationAllowedBeforeMins !== 'number' ||
Number.isNaN(payload.cancellationAllowedBeforeMins) ||
payload.cancellationAllowedBeforeMins <= 0
) {
throw new ApiError(
400,
'cancellationAllowedBeforeMins must be a positive number (in minutes)',
);
}
2025-12-31 13:03:23 +05:30
}
} else {
delete payload.cancellationAllowedBeforeMins;
2025-12-31 13:01:08 +05:30
}
const trainerIsChargeable = toBool(payload.trainerIsChargeable);
const pickUpDropIsChargeable = toBool(payload.pickUpDropIsChargeable);
if (payload.trainerAvailable && trainerIsChargeable) {
if (!isDraft) {
if (
typeof payload.trainerTotalAmount !== 'number' ||
Number.isNaN(payload.trainerTotalAmount) ||
payload.trainerTotalAmount <= 0
) {
throw new ApiError(400, 'trainerTotalAmount must be > 0');
}
}
} else {
// If trainer cost is not chargeable, default the amount to 0
payload.trainerTotalAmount = 0;
2025-12-21 17:28:08 +05:30
}
if (payload.venues && !Array.isArray(payload.venues)) {
throw new ApiError(400, 'venues must be an array');
2025-12-21 17:28:08 +05:30
}
payload.venues?.forEach((v, idx) => {
v.isMinPeopleReqMandatory = toBool(v.isMinPeopleReqMandatory);
2025-12-21 17:28:08 +05:30
if (!isDraft) {
if (!v.venueName) {
throw new ApiError(400, `venues[${idx}] venueName required`);
}
if (v.isMinPeopleReqMandatory && !v.minPeopleRequired) {
throw new ApiError(
400,
`venues[${idx}] min people requirement missing`,
);
}
if (!Array.isArray(v.prices) || !v.prices.length) {
throw new ApiError(
400,
`venues[${idx}] must have at least one price`,
);
}
}
2025-12-21 17:28:08 +05:30
});
/* =====================================================
* ROOT TAX
* ===================================================== */
const taxIds = Array.isArray(payload.taxXids)
? payload.taxXids.map(Number)
: [];
const rootTaxes =
taxIds.length > 0
? await this.prisma.taxes.findMany({
2026-01-09 18:10:38 +05:30
where: { id: { in: taxIds }, isActive: true },
select: { id: true, taxPer: true },
})
: [];
2025-12-22 13:30:55 +05:30
if (taxIds.length !== rootTaxes.length) {
throw new ApiError(400, 'Invalid or inactive tax provided');
2025-12-22 13:30:55 +05:30
}
const eligibility = payload.eligibility;
if (eligibility?.isAgeRestriction) {
if (eligibility.ageRestrictionName == RESTRICTION_NAME.ABOVE) {
eligibility.minAge = toNumber(eligibility.ageEntered);
eligibility.maxAge = 150;
} else if (eligibility.ageRestrictionName == RESTRICTION_NAME.BELOW) {
eligibility.maxAge = toNumber(eligibility.ageEntered);
eligibility.minAge = 0;
}
}
if (eligibility?.isWeightRestriction) {
if (eligibility.weightRestrictionName == RESTRICTION_NAME.ABOVE) {
eligibility.minWeight = toNumber(eligibility.weightEntered);
eligibility.maxWeight = 400;
} else if (eligibility.weightRestrictionName == RESTRICTION_NAME.BELOW) {
eligibility.maxWeight = toNumber(eligibility.weightEntered);
eligibility.minWeight = 0;
}
}
if (eligibility?.isHeightRestriction) {
if (eligibility.heightRestrictionName == RESTRICTION_NAME.ABOVE) {
eligibility.minHeight = toNumber(eligibility.heightEntered);
eligibility.maxHeight = 250;
} else if (eligibility.heightRestrictionName == RESTRICTION_NAME.BELOW) {
eligibility.maxHeight = toNumber(eligibility.heightEntered);
eligibility.minHeight = 0;
}
}
/* =====================================================
* TRANSACTION
* ===================================================== */
return await this.prisma.$transaction(async (tx) => {
/* --------------------------------
* 1 HOST
* -------------------------------- */
const host = await tx.hostHeader.findFirst({
where: { userXid: userId, isActive: true },
2025-12-22 13:30:55 +05:30
});
if (!host) throw new ApiError(404, 'Host not found');
2025-12-22 13:30:55 +05:30
/* --------------------------------
* 2 ACTIVITY
* -------------------------------- */
const existingActivity = await tx.activities.findFirst({
where: {
id: Number(payload.activityXid),
hostXid: host.id,
isActive: true,
},
});
if (!existingActivity) {
throw new ApiError(404, 'Activity not found');
}
const normalizedActivityTitle =
typeof payload.activityTitle === 'string'
? payload.activityTitle.trim()
: '';
if (normalizedActivityTitle) {
payload.activityTitle = normalizedActivityTitle;
const duplicateActivity = await tx.activities.findFirst({
where: {
id: { not: existingActivity.id },
isActive: true,
activityTitle: {
equals: normalizedActivityTitle,
mode: 'insensitive',
},
},
select: { id: true },
});
if (duplicateActivity) {
throw new ApiError(
400,
'Same activity name already exists. Please choose a different name.',
);
}
}
/* --------------------------------
* 3 STATUS DECISION
* -------------------------------- */
let activityInternalStatus;
let activityDisplayStatus;
let amInternalStatus;
let amDisplayStatus;
const wasRejected =
existingActivity.activityInternalStatus ===
ACTIVITY_INTERNAL_STATUS.ACTIVITY_REJECTED;
if (wasRejected) {
if (isDraft) {
activityInternalStatus = existingActivity.activityInternalStatus;
activityDisplayStatus = existingActivity.activityDisplayStatus;
amInternalStatus = existingActivity.amInternalStatus;
amDisplayStatus = existingActivity.amDisplayStatus;
} else {
activityInternalStatus = ACTIVITY_INTERNAL_STATUS.ACTIVITY_SUBMITTED;
activityDisplayStatus = ACTIVITY_DISPLAY_STATUS.ACTIVITY_IN_REVIEW;
amInternalStatus = ACTIVITY_AM_INTERNAL_STATUS.ACTIVITY_TO_REVIEW;
amDisplayStatus = ACTIVITY_AM_DISPLAY_STATUS.ACTIVITY_REVISED;
}
} else {
if (isDraft) {
activityInternalStatus = ACTIVITY_INTERNAL_STATUS.ACTIVITY_DRAFT;
activityDisplayStatus = ACTIVITY_DISPLAY_STATUS.ACTIVITY_DRAFT;
amInternalStatus = existingActivity.amInternalStatus;
amDisplayStatus = existingActivity.amDisplayStatus;
} else {
activityInternalStatus = ACTIVITY_INTERNAL_STATUS.ACTIVITY_SUBMITTED;
activityDisplayStatus = ACTIVITY_DISPLAY_STATUS.ACTIVITY_IN_REVIEW;
amInternalStatus = ACTIVITY_AM_INTERNAL_STATUS.ACTIVITY_SUBMITED;
amDisplayStatus = ACTIVITY_AM_DISPLAY_STATUS.ACTIVITY_NEW;
}
}
2025-12-21 17:28:08 +05:30
/* --------------------------------
* 🌍 RESOLVE CHECK-IN LOCATION
* -------------------------------- */
const checkInCountryXid = await findOrCreateCountry(
tx,
payload.checkInCountryName,
);
const checkInStateXid = await findOrCreateState(
tx,
payload.checkInStateName,
checkInCountryXid,
);
const checkInCityXid = await findOrCreateCity(
tx,
payload.checkInCityName,
checkInStateXid,
);
/* --------------------------------
* 🌍 RESOLVE CHECK-OUT LOCATION
* -------------------------------- */
const checkOutCountryXid = await findOrCreateCountry(
tx,
payload.checkOutCountryName,
);
const checkOutStateXid = await findOrCreateState(
tx,
payload.checkOutStateName,
checkOutCountryXid,
);
const checkOutCityXid = await findOrCreateCity(
tx,
payload.checkOutCityName,
checkOutStateXid,
);
/* --------------------------------
* 4 UPDATE ACTIVITY CORE + FLAGS
* -------------------------------- */
const activity = await tx.activities.update({
where: { id: existingActivity.id },
2025-12-21 17:28:08 +05:30
data: {
activityTypeXid: payload.activityTypeXid ?? undefined,
frequenciesXid: payload.frequenciesXid ?? undefined,
activityTitle: payload.activityTitle ?? undefined,
activityDescription: payload.activityDescription ?? undefined,
checkInLat: payload.checkInLat ?? undefined,
checkInLong: payload.checkInLong ?? undefined,
checkInAddress: payload.checkInAddress ?? undefined,
isCheckOutSame: toBool(payload.isCheckOutSame),
checkOutLat: payload.checkOutLat ?? undefined,
checkOutLong: payload.checkOutLong ?? undefined,
checkOutAddress: payload.checkOutAddress ?? undefined,
2025-12-29 19:03:11 +05:30
// energyLevelXid: payload.energyLevelXid ?? undefined,
activityDurationMins: durationMins ?? undefined,
currencyXid: payload.currencyXid ?? undefined,
sustainabilityScore: payload.sustainabilityScore ?? undefined,
safetyScore: payload.safetyScore ?? undefined,
isInstantBooking: payload.isInstantBooking ?? undefined,
foodAvailable: payload.foodAvailable,
foodIsChargeable: toBool(payload.foodIsChargeable),
alcoholAvailable: payload.alcoholAvailable,
trainerAvailable: payload.trainerAvailable,
trainerIsChargeable: toBool(payload.trainerIsChargeable),
pickUpDropAvailable: payload.pickUpDropAvailable,
pickUpDropIsChargeable: toBool(payload.pickUpDropIsChargeable),
inActivityAvailable: payload.inActivityAvailable,
inActivityIsChargeable: toBool(payload.inActivityIsChargeable),
equipmentAvailable: payload.equipmentAvailable,
equipmentIsChargeable: toBool(payload.equipmentIsChargeable),
cancellationAvailable: payload.cancellationAvailable,
2025-12-31 13:03:23 +05:30
cancellationAllowedBeforeMins: payload.cancellationAvailable
? payload.cancellationAllowedBeforeMins
: null,
activityInternalStatus,
activityDisplayStatus,
amInternalStatus,
amDisplayStatus,
checkInCountryXid: checkInCountryXid ?? undefined,
checkInStateXid: checkInStateXid ?? undefined,
checkInCityXid: checkInCityXid ?? undefined,
checkOutCountryXid: checkOutCountryXid ?? undefined,
checkOutStateXid: checkOutStateXid ?? undefined,
checkOutCityXid: checkOutCityXid ?? undefined,
},
});
const activityXid = activity.id;
/* --------------------------------
* 5 CLEAN OLD ACTIVITY MEDIA
* -------------------------------- */
await tx.activitiesMedia.deleteMany({ where: { activityXid } });
/* --------------------------------
* 6 SAVE NEW ACTIVITY MEDIA
* -------------------------------- */
if (Array.isArray(payload.media) && payload.media.length) {
await tx.activitiesMedia.createMany({
data: payload.media.map((m, index) => ({
activityXid,
mediaType: m.mediaType ?? 'unknown',
2025-12-22 13:30:55 +05:30
mediaFileName: m.mediaFileName,
isCoverImage: m.isCoverImage ?? false,
displayOrder: index + 1,
2025-12-22 13:30:55 +05:30
})),
});
}
/* --------------------------------
* 7 CLEAN OLD VENUES & RELATED DATA
* -------------------------------- */
const oldVenueIds = (
await tx.activityVenues.findMany({
where: { activityXid },
select: { id: true },
})
).map((v) => v.id);
if (oldVenueIds.length) {
// Clean venue artifacts (media)
await tx.activityVenueArtifacts.deleteMany({
where: { activityVenueXid: { in: oldVenueIds } },
});
// Clean price taxes and prices
const priceIds = (
await tx.activityPrices.findMany({
where: { activityVenueXid: { in: oldVenueIds } },
select: { id: true },
})
).map((p) => p.id);
if (priceIds.length) {
await tx.activityPriceTaxes.deleteMany({
where: { activityPriceXid: { in: priceIds } },
});
await tx.activityPrices.deleteMany({
where: { id: { in: priceIds } },
});
}
// Clean venues
await tx.activityVenues.deleteMany({
where: { id: { in: oldVenueIds } },
});
}
2025-12-21 17:28:08 +05:30
/* --------------------------------
* 8 CREATE VENUES WITH MEDIA & PRICES (DRAFT SAFE)
* -------------------------------- */
for (const venue of payload.venues ?? []) {
const venueRow = await tx.activityVenues.create({
2025-12-21 17:28:08 +05:30
data: {
activityXid,
2026-01-09 18:10:38 +05:30
venueName: venue.venueName ?? null,
venueLabel: venue.venueLabel ?? null,
venueCapacity: toNumber(venue.venueCapacity) ?? 0,
availableSeats: toNumber(venue.availableSeats) ?? 0,
isMinPeopleReqMandatory: venue.isMinPeopleReqMandatory,
minPeopleRequired: toNumber(venue.minPeopleRequired) ?? null,
minReqfullfilledBeforeMins:
toNumber(venue.minReqfullfilledBeforeMins) ?? null,
venueDescription: venue.venueDescription ?? null,
2025-12-21 17:28:08 +05:30
},
});
// Create venue media/artifacts
if (Array.isArray(venue.media) && venue.media.length) {
await tx.activityVenueArtifacts.createMany({
data: venue.media.map((m) => ({
activityVenueXid: venueRow.id,
mediaType: m.mediaType ?? 'image',
mediaFileName: m.mediaFileName,
2025-12-21 17:28:08 +05:30
})),
});
}
// Create venue prices with taxes
for (const price of venue.prices ?? []) {
const sellPrice = Number(price.sellPrice);
// On submit enforce > 0, on draft just skip invalid
if (!isDraft) {
if (!sellPrice || sellPrice <= 0) {
throw new ApiError(
400,
'sellPrice must be > 0 for submitted activities',
);
}
}
if (!sellPrice || sellPrice <= 0) continue;
const { basePrice, taxDetails } = computeBasePriceAndTaxes(
sellPrice,
rootTaxes,
);
const priceRow = await tx.activityPrices.create({
data: {
activityVenueXid: venueRow.id,
noOfSession: price.noOfSession ?? 1,
isPackage: price.isPackage ?? false,
sessionValidity: price.sessionValidity ?? 0,
2025-12-24 15:11:37 +05:30
sessionValidityFrequency:
price.sessionValidityFrequency ?? 'Days',
basePrice,
sellPrice,
},
});
if (taxDetails.length) {
await tx.activityPriceTaxes.createMany({
data: taxDetails.map((t) => ({
activityPriceXid: priceRow.id,
taxXid: t.taxXid,
taxPer: t.taxPer,
taxAmount: t.taxAmount,
})),
});
}
}
}
2025-12-24 19:04:51 +05:30
/* 8.1️⃣ CLEAN & CREATE FOOD COST (if chargeable) */
const oldFoodCostIds = (
await tx.activityFoodCost.findMany({
where: { activityXid },
select: { id: true },
})
).map((f) => f.id);
if (oldFoodCostIds.length) {
await tx.activityFoodTaxes.deleteMany({
where: { activityFoodCostXid: { in: oldFoodCostIds } },
});
await tx.activityFoodCost.deleteMany({
where: { id: { in: oldFoodCostIds } },
});
}
if (payload.foodAvailable && payload.foodIsChargeable) {
const foodTotalAmount = toNumber(payload.foodTotalAmount) ?? 0;
if (!isDraft && foodTotalAmount <= 0) {
2025-12-24 19:04:51 +05:30
throw new ApiError(
400,
'foodTotalAmount must be > 0 when foodIsChargeable',
);
}
2025-12-24 19:04:51 +05:30
if (foodTotalAmount > 0) {
const { basePrice, taxDetails } = computeBasePriceAndTaxes(
foodTotalAmount,
rootTaxes,
);
2025-12-24 19:04:51 +05:30
const foodCost = await tx.activityFoodCost.create({
data: {
activityXid,
baseAmount: basePrice,
totalAmount: foodTotalAmount,
},
2025-12-24 19:04:51 +05:30
});
if (taxDetails.length) {
await tx.activityFoodTaxes.createMany({
data: taxDetails.map((t) => ({
activityFoodCostXid: foodCost.id,
taxXid: t.taxXid,
taxPer: t.taxPer,
taxAmount: t.taxAmount,
})),
});
}
2025-12-24 19:04:51 +05:30
}
}
// 🍲 FOOD TYPES
await tx.activityFoodTypes.deleteMany({ where: { activityXid } });
if (Array.isArray(payload.foodTypeIds) && payload.foodTypeIds.length) {
await tx.activityFoodTypes.createMany({
data: payload.foodTypeIds.map((foodTypeId) => ({
activityXid,
foodTypeXid: foodTypeId,
})),
});
}
// 🍛 CUISINES
await tx.activityCuisine.deleteMany({ where: { activityXid } });
if (Array.isArray(payload.cuisineIds) && payload.cuisineIds.length) {
await tx.activityCuisine.createMany({
data: payload.cuisineIds.map((cuisineId) => ({
activityXid,
foodCuisineXid: cuisineId,
})),
});
}
/* --------------------------------
* 9 CLEAN & CREATE EQUIPMENT WITH TAXES
* -------------------------------- */
const oldEquipmentIds = (
await tx.activityEquipments.findMany({
where: { activityXid },
select: { id: true },
})
).map((e) => e.id);
if (oldEquipmentIds.length) {
await tx.activityEquipmentTaxes.deleteMany({
where: { activityEquipmentXid: { in: oldEquipmentIds } },
});
await tx.activityEquipments.deleteMany({
where: { id: { in: oldEquipmentIds } },
});
}
if (Array.isArray(payload.equipments)) {
for (const eq of payload.equipments) {
const isChargeable = toBool(eq.isEquipmentChargeable);
const totalPrice = isChargeable
? toNumber(eq.equipmentTotalPrice) ?? 0
: 0;
// ❌ Validate only on submit
if (!isDraft && isChargeable && totalPrice <= 0) {
throw new ApiError(
400,
'equipmentTotalPrice must be > 0 when equipment is chargeable',
);
}
const { basePrice, taxDetails } =
isChargeable && totalPrice > 0
? computeBasePriceAndTaxes(totalPrice, rootTaxes)
: { basePrice: 0, taxDetails: [] };
// ✅ ALWAYS CREATE EQUIPMENT
const equipment = await tx.activityEquipments.create({
data: {
activityXid,
equipmentName: eq.equipmentName,
isEquipmentChargeable: isChargeable,
equipmentBasePrice: basePrice,
equipmentTotalPrice: totalPrice,
},
});
// 💰 Taxes ONLY if chargeable
if (isChargeable && taxDetails.length) {
await tx.activityEquipmentTaxes.createMany({
data: taxDetails.map((t) => ({
activityEquipmentXid: equipment.id,
taxXid: t.taxXid,
taxPer: t.taxPer,
taxAmount: t.taxAmount,
})),
});
}
}
}
/* --------------------------------
* 🔟 CLEAN & CREATE TRAINER WITH TAXES
* -------------------------------- */
const oldTrainerIds = (
await tx.activityTrainers.findMany({
where: { activityXid },
select: { id: true },
})
).map((t) => t.id);
if (oldTrainerIds.length) {
await tx.activityTrainerTaxes.deleteMany({
where: { activityTrainerXid: { in: oldTrainerIds } },
});
await tx.activityTrainers.deleteMany({
where: { id: { in: oldTrainerIds } },
});
}
2025-12-22 13:30:55 +05:30
if (payload.trainerAvailable) {
const isChargeable = trainerIsChargeable;
const totalAmount = isChargeable
? payload.trainerTotalAmount
: 0;
const { basePrice, taxDetails } = isChargeable && totalAmount > 0
? computeBasePriceAndTaxes(totalAmount, rootTaxes)
: { basePrice: 0, taxDetails: [] };
2025-12-21 17:28:08 +05:30
const trainer = await tx.activityTrainers.create({
2025-12-22 13:30:55 +05:30
data: {
activityXid,
baseAmount: basePrice,
totalAmount,
2025-12-22 13:30:55 +05:30
},
});
if (isChargeable && taxDetails.length) {
await tx.activityTrainerTaxes.createMany({
2025-12-22 13:30:55 +05:30
data: taxDetails.map((t) => ({
activityTrainerXid: trainer.id,
2025-12-22 13:30:55 +05:30
taxXid: t.taxXid,
taxPer: t.taxPer,
taxAmount: t.taxAmount,
})),
});
}
}
/* --------------------------------
* 11 CLEAN & CREATE PICKUP/DROP TRANSPORTS (INDEPENDENT ARRAY)
* -------------------------------- */
2025-12-30 14:22:18 +05:30
/* --------------------------------
* 11 CLEAN OLD PICKUP/DROP TRANSPORT DATA (INDEPENDENT FROM NAVIGATION MODES)
2025-12-30 14:22:18 +05:30
* -------------------------------- */
// Clean up old pickup transport modes (independent array)
await tx.activityPickUpTransport.deleteMany({
where: {
activityXid: Number(activityXid),
},
});
// Clean up old pickup details and their taxes (independent from transport modes)
2025-12-30 14:22:18 +05:30
const oldPickupDetailIds = (
await tx.activityPickUpDetails.findMany({
where: { activitiesXid: activityXid },
select: { id: true },
})
2025-12-30 14:22:18 +05:30
).map((p) => p.id);
2025-12-30 14:22:18 +05:30
if (oldPickupDetailIds.length) {
await tx.activityPickUpTransportTaxes.deleteMany({
where: {
activityPickUpDetailsXid: { in: oldPickupDetailIds },
},
});
2025-12-22 13:30:55 +05:30
2025-12-30 14:22:18 +05:30
await tx.activityPickUpDetails.deleteMany({
where: { id: { in: oldPickupDetailIds } },
});
}
2025-12-22 13:30:55 +05:30
/* --------------------------------
* 11 CREATE PICKUP TRANSPORTS (INDEPENDENT ARRAY - JUST TRANSPORT MODES)
* -------------------------------- */
2025-12-30 14:22:18 +05:30
if (Array.isArray(payload.pickupTransports)) {
for (const transport of payload.pickupTransports) {
// ✅ CREATE TRANSPORT MODE INDEPENDENTLY (NO RELATION TO PICKUP DETAILS)
await tx.activityPickUpTransport.create({
data: {
activityXid: activityXid,
transportModeXid: transport.transportModeXid,
},
});
}
}
2025-12-21 17:28:08 +05:30
/* --------------------------------
* 11 CREATE PICKUP DETAILS (INDEPENDENT ARRAY - SEPARATE FROM TRANSPORT MODES)
* -------------------------------- */
if (Array.isArray(payload.pickupDetails)) {
for (const detail of payload.pickupDetails) {
const isChargeable = pickUpDropIsChargeable;
2026-01-09 19:11:46 +05:30
// 🔒 HARD RULE: NOT chargeable → ALWAYS 0
const totalPrice = isChargeable
? toNumber(detail.transportTotalPrice) ?? 0
: 0;
2026-01-09 19:11:46 +05:30
// ❌ Validate ONLY when chargeable + submit
if (!isDraft && isChargeable && totalPrice <= 0) {
throw new ApiError(
400,
'Pick-up and drop-off price is required.',
);
}
const { basePrice, taxDetails } =
isChargeable && totalPrice > 0
? computeBasePriceAndTaxes(totalPrice, rootTaxes)
: { basePrice: 0, taxDetails: [] };
2026-01-09 19:11:46 +05:30
// ✅ ALWAYS CREATE PICKUP DETAIL
const pickupDetail = await tx.activityPickUpDetails.create({
data: {
activitiesXid: activityXid,
isPickUp: toBool(detail.isPickUp),
locationLat: toNumber(detail.locationLat),
locationLong: toNumber(detail.locationLong),
locationAddress: detail.locationAddress ?? null,
2026-01-09 19:11:46 +05:30
// ✅ Guaranteed consistency
transportBasePrice: basePrice,
transportTotalPrice: totalPrice,
},
});
2026-01-09 19:11:46 +05:30
// 💰 Taxes ONLY when chargeable
if (isChargeable && taxDetails.length) {
await tx.activityPickUpTransportTaxes.createMany({
data: taxDetails.map((t) => ({
activityPickUpDetailsXid: pickupDetail.id,
taxXid: t.taxXid,
taxPer: t.taxPer,
taxAmount: t.taxAmount,
})),
});
}
}
2025-12-22 13:30:55 +05:30
}
/* --------------------------------
* 12 CLEAN & CREATE NAVIGATION MODES WITH TAXES
* -------------------------------- */
const oldNavIds = (
await tx.activityNavigationModes.findMany({
where: { activityXid },
2025-12-22 13:30:55 +05:30
select: { id: true },
})
).map((n) => n.id);
2025-12-22 13:30:55 +05:30
if (oldNavIds.length) {
await tx.activityNavigationModesTaxes.deleteMany({
where: { activityNavigationModeXid: { in: oldNavIds } },
2025-12-22 13:30:55 +05:30
});
2025-12-31 14:42:30 +05:30
await tx.activityNavigationModes.deleteMany({
where: { id: { in: oldNavIds } },
2025-12-22 13:30:55 +05:30
});
}
2025-12-31 14:42:30 +05:30
/* --------------------------------
* 12 CREATE NAVIGATION MODES (PER MODE)
* -------------------------------- */
if (Array.isArray(payload.navigationModes)) {
for (const mode of payload.navigationModes) {
const isChargeable = toBool(mode.isChargeable);
const totalPrice = isChargeable
? (toNumber(mode.totalPrice) ?? 0)
: 0;
if (!isDraft && isChargeable && totalPrice <= 0) {
2025-12-31 14:42:30 +05:30
throw new ApiError(
400,
'totalPrice must be > 0 when navigation mode is chargeable',
);
}
let basePrice = 0;
let taxDetails: Array<{
taxXid: number;
taxPer: number;
taxAmount: number;
}> = [];
if (isChargeable) {
const result = computeBasePriceAndTaxes(totalPrice, rootTaxes);
basePrice = result.basePrice;
taxDetails = result.taxDetails;
}
2025-12-22 13:30:55 +05:30
2025-12-31 14:42:30 +05:30
/* 1⃣ CREATE NAVIGATION MODE ROW */
const navMode = await tx.activityNavigationModes.create({
data: {
activityXid,
navigationModeName: mode.navigationModeName,
2025-12-31 14:42:30 +05:30
isInActivityChargeable: isChargeable,
navigationModesBasePrice: basePrice,
navigationModesTotalPrice: totalPrice,
},
});
2025-12-22 13:30:55 +05:30
2025-12-31 14:42:30 +05:30
/* 2⃣ CREATE TAXES (ONLY IF CHARGEABLE) */
if (taxDetails.length) {
await tx.activityNavigationModesTaxes.createMany({
data: taxDetails.map((t) => ({
activityNavigationModeXid: navMode.id,
taxXid: t.taxXid,
taxPer: t.taxPer,
taxAmount: t.taxAmount,
})),
2025-12-22 13:30:55 +05:30
});
}
}
}
/* --------------------------------
* 13 CLEAN & CREATE AMENITIES
* -------------------------------- */
await tx.activityAmenities.deleteMany({ where: { activityXid } });
2025-12-22 13:30:55 +05:30
if (Array.isArray(payload.amenitiesIds) && payload.amenitiesIds.length) {
await tx.activityAmenities.createMany({
data: payload.amenitiesIds.map((amenityId) => ({
activityXid,
amenitiesXid: amenityId,
})),
});
}
/* --------------------------------
* 14 CLEAN & CREATE ELIGIBILITY
* -------------------------------- */
await tx.activityEligibility.deleteMany({ where: { activityXid } });
2025-12-22 13:30:55 +05:30
if (payload.eligibility) {
await tx.activityEligibility.create({
2025-12-22 13:30:55 +05:30
data: {
activityXid,
isAgeRestriction: toBool(payload.eligibility.isAgeRestriction),
2025-12-24 19:04:51 +05:30
ageRestrictionName: payload.eligibility.ageRestrictionName,
ageEntered: payload.eligibility.ageEntered,
ageIn: payload.eligibility.ageIn,
minAge: payload.eligibility.minAge,
maxAge: payload.eligibility.maxAge,
2025-12-24 15:11:37 +05:30
isWeightRestriction: toBool(
payload.eligibility.isWeightRestriction,
),
weightRestrictionName:
payload.eligibility.weightRestrictionName ?? null,
weightEntered: toNumber(payload.eligibility.weightEntered),
weightIn: payload.eligibility.weightIn ?? null,
minWeight: toNumber(payload.eligibility.minWeight),
maxWeight: toNumber(payload.eligibility.maxWeight),
2025-12-24 15:11:37 +05:30
isHeightRestriction: toBool(
payload.eligibility.isHeightRestriction,
),
heightRestrictionName:
payload.eligibility.heightRestrictionName ?? null,
heightEntered: toNumber(payload.eligibility.heightEntered),
heightIn: payload.eligibility.heightIn ?? null,
minHeight: toNumber(payload.eligibility.minHeight),
maxHeight: toNumber(payload.eligibility.maxHeight),
2025-12-22 13:30:55 +05:30
},
});
}
/* --------------------------------
* 15 CLEAN & CREATE OTHER DETAILS
* -------------------------------- */
await tx.activityOtherDetails.deleteMany({ where: { activityXid } });
2025-12-22 13:30:55 +05:30
if (payload.otherDetails) {
await tx.activityOtherDetails.create({
data: {
activityXid,
exclusiveNotes: payload.otherDetails.exclusiveNotes ?? null,
SafetyInstruction: (() => {
const s = payload.otherDetails.safetyInstruction ?? null;
if (s === undefined || s === null) return null;
if (typeof s !== 'string') {
throw new ApiError(400, 'safetyInstruction must be a string');
}
return s;
})(),
Cancellations: (() => {
const c = payload.otherDetails.cancellations ?? null;
if (c === undefined || c === null) return null;
if (typeof c !== 'string') {
throw new ApiError(400, 'cancellations must be a string');
}
return c;
})(),
dosNotes: payload.otherDetails.dosNotes ?? null,
dontsNotes: payload.otherDetails.dontsNotes ?? null,
tipsNotes: payload.otherDetails.tipsNotes ?? null,
termsAndCondition: payload.otherDetails.termsAndCondition ?? null,
},
});
}
2025-12-22 13:30:55 +05:30
/* --------------------------------
* 16 CLEAN & CREATE FOOD TYPES
* -------------------------------- */
await tx.activityFoodTypes.deleteMany({ where: { activityXid } });
2025-12-22 13:30:55 +05:30
if (Array.isArray(payload.foodTypeIds) && payload.foodTypeIds.length) {
await tx.activityFoodTypes.createMany({
data: payload.foodTypeIds.map((foodTypeId) => ({
activityXid,
foodTypeXid: foodTypeId,
})),
});
}
2025-12-22 13:30:55 +05:30
/* --------------------------------
* 17 CLEAN & CREATE CUISINES
* -------------------------------- */
await tx.activityCuisine.deleteMany({ where: { activityXid } });
2025-12-22 13:30:55 +05:30
if (Array.isArray(payload.cuisineIds) && payload.cuisineIds.length) {
await tx.activityCuisine.createMany({
data: payload.cuisineIds.map((cuisineId) => ({
activityXid,
foodCuisineXid: cuisineId,
})),
});
}
2025-12-24 15:11:37 +05:30
const allowedEntryIds = Array.isArray(payload.allowedEntryTypes)
? payload.allowedEntryTypes.map(Number)
: [];
if (allowedEntryIds.length) {
const validEntryTypes = await this.prisma.allowedEntryTypes.findMany({
where: { id: { in: allowedEntryIds }, isActive: true },
select: { id: true },
});
if (validEntryTypes.length !== allowedEntryIds.length)
throw new ApiError(
400,
'Invalid or inactive allowed entry type(s) provided',
);
}
/* --------------------------------
* CLEAN & CREATE ALLOWED ENTRY
* -------------------------------- */
await tx.activityAllowedEntry.deleteMany({ where: { activityXid } });
if (allowedEntryIds.length) {
await tx.activityAllowedEntry.createMany({
data: allowedEntryIds.map((entryId) => ({
activityXid,
allowedEntryTypeXid: entryId,
})),
});
}
2026-01-05 14:47:24 +05:30
/* --------------------------------
* MARK ACTIVITY SUGGESTIONS AS REVIEWED
* (REJECTED ENHANCE SUBMIT FLOW)
* -------------------------------- */
if (wasRejected && !isDraft) {
await tx.activitySuggestions.updateMany({
where: {
activityXid: activityXid,
isActive: true,
isReviewed: false,
},
data: {
isReviewed: true,
reviewedByXid: userId,
reviewedOn: new Date(),
},
});
}
/* --------------------------------
* 18 ACTIVITY TRACK
* -------------------------------- */
await tx.activityTrack.create({
2025-12-22 13:30:55 +05:30
data: {
activityXid,
trackType: 'ACTIVITY',
trackStatus: activityInternalStatus,
updatedByXid: userId,
updatedByRole: ROLE_NAME.HOST,
updatedOn: new Date(),
2025-12-22 13:30:55 +05:30
},
});
/* --------------------------------
* 19 RESPONSE
* -------------------------------- */
return {
2025-12-21 17:28:08 +05:30
activityXid,
activityRefNumber: activity.activityRefNumber,
2025-12-24 15:11:37 +05:30
status: isDraft
? ACTIVITY_INTERNAL_STATUS.ACTIVITY_DRAFT
: ACTIVITY_INTERNAL_STATUS.ACTIVITY_SUBMITTED,
};
});
}
2025-12-24 15:11:37 +05:30
async getAllPQUpdatedResponse(activityXid: number) {
const pqqHeaderData = await this.prisma.activityPQQheader.findMany({
where: {
activityXid: activityXid,
isActive: true,
},
select: {
id: true,
comments: true,
pqqAnswerXid: true,
pqqQuestions: {
select: {
id: true,
questionName: true,
maxPoints: true,
displayOrder: true,
pqqSubCategories: {
select: {
id: true,
subCategoryName: true,
displayOrder: true,
category: {
select: {
id: true,
categoryName: true,
2025-12-22 13:30:55 +05:30
displayOrder: true,
},
},
},
},
// 🔥 ALL ANSWER OPTIONS FOR THIS QUESTION
PQQAnswers: {
where: { isActive: true },
select: {
id: true,
answerName: true,
answerPoints: true,
2025-12-22 13:30:55 +05:30
displayOrder: true,
},
2025-12-22 13:30:55 +05:30
orderBy: { displayOrder: 'asc' },
},
},
},
ActivityPQQSuggestions: {
2026-01-09 18:10:38 +05:30
where: { isActive: true, isReviewed: false },
select: {
id: true,
title: true,
comments: true,
2025-12-22 13:30:55 +05:30
activityPqqHeaderXid: true,
},
},
ActivityPQQSupportings: {
where: { isActive: true },
select: {
id: true,
mediaType: true,
2025-12-22 13:30:55 +05:30
mediaFileName: true,
},
},
},
2025-12-22 13:30:55 +05:30
orderBy: { id: 'asc' },
});
// ---------- GROUPING START ----------
const grouped: any = {};
for (const item of pqqHeaderData) {
const q = item.pqqQuestions;
const sub = q.pqqSubCategories;
const cat = sub.category;
// 1⃣ Category level
// 1⃣ Category level
if (!grouped[cat.id]) {
grouped[cat.id] = {
id: cat.id,
categoryName: cat.categoryName,
displayOrder: cat.displayOrder,
2025-12-22 13:30:55 +05:30
activityPqqHeaderId: item.id, // ✅ Added to match AM response
pqqsubCategories: [],
};
} else if (!grouped[cat.id].activityPqqHeaderId) {
2025-12-22 13:30:55 +05:30
grouped[cat.id].activityPqqHeaderId = item.id; // Ensures it's set if missing
}
const category = grouped[cat.id];
// 2⃣ Subcategory level
let subCat = category.pqqsubCategories.find((s: any) => s.id === sub.id);
if (!subCat) {
subCat = {
id: sub.id,
subCategoryName: sub.subCategoryName,
displayOrder: sub.displayOrder,
2025-12-22 13:30:55 +05:30
questions: [],
};
category.pqqsubCategories.push(subCat);
}
// 3⃣ Questions level
subCat.questions.push({
id: q.id,
questionName: q.questionName,
maxPoints: q.maxPoints,
pqqAnswerXid: item.pqqAnswerXid,
comments: item.comments || null,
displayOrder: q.displayOrder,
2025-12-22 13:30:55 +05:30
allAnswerOptions: q.PQQAnswers || [], // 🔥 All answers
suggestions: item.ActivityPQQSuggestions,
supportings: item.ActivityPQQSupportings,
});
}
// ---------- SORTING ----------
2025-12-22 13:30:55 +05:30
const sortedCategories: any = Object.values(grouped).sort(
(a: any, b: any) => a.displayOrder - b.displayOrder,
);
for (const cat of sortedCategories) {
2025-12-22 13:30:55 +05:30
cat.pqqsubCategories.sort(
(a: any, b: any) => a.displayOrder - b.displayOrder,
);
for (const sub of cat.pqqsubCategories) {
sub.questions.sort((a: any, b: any) => a.displayOrder - b.displayOrder);
}
}
// ---------- PRESIGNED URL GENERATION ----------
for (const cat of sortedCategories) {
for (const sub of cat.pqqsubCategories) {
for (const q of sub.questions) {
if (q.supportings?.length) {
for (const doc of q.supportings) {
if (doc.mediaFileName) {
const filePath = doc.mediaFileName;
2025-12-22 13:30:55 +05:30
const key = filePath.startsWith('http')
? filePath.split('.com/')[1]
: filePath;
doc.presignedUrl = await getPresignedUrl(bucket, key);
}
}
}
}
}
}
// ---------- RETURN GROUPED STRUCTURE ----------
return sortedCategories;
}
2025-11-10 15:05:01 +05:30
}