Refactor document name handling in host service to sanitize input and ensure valid connections for city, state, and country IDs during host creation and updates.
This commit is contained in:
@@ -32,6 +32,15 @@ import {
|
||||
import { getPresignedUrl } from '../../../common/middlewares/aws/getPreSignedUrl';
|
||||
import config from '../../../config/config';
|
||||
|
||||
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)
|
||||
@@ -629,6 +638,7 @@ export class HostService {
|
||||
where: { userXid: user_xid },
|
||||
include: { hostParent: true },
|
||||
});
|
||||
console.log(existingHostCompany, "-: Existing hai")
|
||||
|
||||
let existingParentCompany;
|
||||
|
||||
@@ -704,12 +714,14 @@ export class HostService {
|
||||
// -------------------------------------------------------
|
||||
if (!existingHostCompany) {
|
||||
if (!isDraft) {
|
||||
console.log("First time direct final submit.")
|
||||
const existingByPan = await tx.hostHeader.findFirst({
|
||||
where: { panNumber: companyData.panNumber },
|
||||
});
|
||||
if (existingByPan)
|
||||
throw new ApiError(400, 'Company already exists with this pan/bin number');
|
||||
}
|
||||
console.log("First Time Aaya hai")
|
||||
|
||||
const createdHost = await tx.hostHeader.create({
|
||||
data: {
|
||||
@@ -721,7 +733,7 @@ export class HostService {
|
||||
states: companyData.stateXid ? { connect: { id: companyData.stateXid } } : undefined,
|
||||
countries: companyData.countryXid ? { connect: { id: companyData.countryXid } } : undefined,
|
||||
pinCode: companyData.pinCode,
|
||||
logoPath: companyData.logoPath || existingHostCompany.logoPath,
|
||||
logoPath: companyData.logoPath || null,
|
||||
isSubsidairy: companyData.isSubsidairy,
|
||||
registrationNumber: companyData.registrationNumber,
|
||||
panNumber: companyData.panNumber,
|
||||
@@ -751,7 +763,7 @@ export class HostService {
|
||||
const docsData = documents.map((doc) => ({
|
||||
hostXid: createdHost.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
documentName: sanitizeDocumentName(doc.documentName),
|
||||
filePath: doc.filePath,
|
||||
}));
|
||||
await tx.hostDocuments.createMany({ data: docsData });
|
||||
@@ -759,23 +771,29 @@ export class HostService {
|
||||
|
||||
// parent create
|
||||
if (companyData.isSubsidairy && parentCompanyData) {
|
||||
console.log("Parent ke saath aaya hai first time.")
|
||||
const createdParent = await tx.hostParent.create({
|
||||
data: {
|
||||
host: { connect: { id: createdHost.id } },
|
||||
companyName: parentCompanyData.companyName,
|
||||
address1: parentCompanyData.address1 || null,
|
||||
address2: parentCompanyData.address2 || null,
|
||||
cities: parentCompanyData.cityXid
|
||||
? { connect: { id: parentCompanyData.cityXid } }
|
||||
// Safely handle city connection - only connect if valid ID exists
|
||||
cities: companyData.cityXid && !isNaN(Number(companyData.cityXid))
|
||||
? { connect: { id: Number(companyData.cityXid) } }
|
||||
: undefined, // Don't change if not provided
|
||||
|
||||
// Same for state
|
||||
states: companyData.stateXid && !isNaN(Number(companyData.stateXid))
|
||||
? { connect: { id: Number(companyData.stateXid) } }
|
||||
: undefined,
|
||||
states: parentCompanyData.stateXid
|
||||
? { connect: { id: parentCompanyData.stateXid } }
|
||||
: undefined,
|
||||
countries: parentCompanyData.countryXid
|
||||
? { connect: { id: parentCompanyData.countryXid } }
|
||||
|
||||
// Same for country
|
||||
countries: companyData.countryXid && !isNaN(Number(companyData.countryXid))
|
||||
? { connect: { id: Number(companyData.countryXid) } }
|
||||
: undefined,
|
||||
pinCode: parentCompanyData.pinCode || null,
|
||||
logoPath: parentCompanyData.logoPath || existingParentCompany.logoPath,
|
||||
logoPath: parentCompanyData.logoPath || null,
|
||||
registrationNumber: parentCompanyData.registrationNumber || null,
|
||||
panNumber: parentCompanyData.panNumber || null,
|
||||
gstNumber: parentCompanyData.gstNumber || null,
|
||||
@@ -798,7 +816,7 @@ export class HostService {
|
||||
const parentDocsData = parentDocuments.map((doc) => ({
|
||||
hostParentXid: createdParent.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
documentName: sanitizeDocumentName(doc.documentName),
|
||||
filePath: doc.filePath,
|
||||
}));
|
||||
await tx.hostParenetDocuments.createMany({ data: parentDocsData });
|
||||
@@ -827,9 +845,20 @@ export class HostService {
|
||||
companyName: companyData.companyName,
|
||||
address1: companyData.address1,
|
||||
address2: companyData.address2,
|
||||
cities: companyData.cityXid ? { connect: { id: companyData.cityXid } } : undefined,
|
||||
states: companyData.stateXid ? { connect: { id: companyData.stateXid } } : undefined,
|
||||
countries: companyData.countryXid ? { connect: { id: companyData.countryXid } } : undefined,
|
||||
// Safely handle city connection - only connect if valid ID exists
|
||||
cities: companyData.cityXid && !isNaN(Number(companyData.cityXid))
|
||||
? { connect: { id: Number(companyData.cityXid) } }
|
||||
: undefined, // Don't change if not provided
|
||||
|
||||
// Same for state
|
||||
states: companyData.stateXid && !isNaN(Number(companyData.stateXid))
|
||||
? { connect: { id: Number(companyData.stateXid) } }
|
||||
: undefined,
|
||||
|
||||
// Same for country
|
||||
countries: companyData.countryXid && !isNaN(Number(companyData.countryXid))
|
||||
? { connect: { id: Number(companyData.countryXid) } }
|
||||
: undefined,
|
||||
pinCode: companyData.pinCode,
|
||||
logoPath: companyData.logoPath || existingHostCompany.logoPath,
|
||||
isSubsidairy: companyData.isSubsidairy,
|
||||
@@ -859,6 +888,7 @@ export class HostService {
|
||||
// documents UPSERT
|
||||
if (documents?.length) {
|
||||
for (const doc of documents) {
|
||||
if (!doc.filePath) continue;
|
||||
const existingDoc = await tx.hostDocuments.findFirst({
|
||||
where: {
|
||||
hostXid: updatedHost.id,
|
||||
@@ -871,7 +901,7 @@ export class HostService {
|
||||
where: { id: existingDoc.id },
|
||||
data: {
|
||||
filePath: doc.filePath,
|
||||
documentName: doc.documentName || existingDoc.documentName,
|
||||
documentName: sanitizeDocumentName(doc.documentName) || existingDoc.documentName,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
@@ -879,7 +909,7 @@ export class HostService {
|
||||
data: {
|
||||
hostXid: updatedHost.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
documentName: sanitizeDocumentName(doc.documentName),
|
||||
filePath: doc.filePath,
|
||||
},
|
||||
});
|
||||
@@ -891,8 +921,9 @@ export class HostService {
|
||||
if (companyData.isSubsidairy) {
|
||||
const parentRecords = existingHostCompany.hostParent;
|
||||
const parentRecord = Array.isArray(parentRecords) ? parentRecords[0] : parentRecords;
|
||||
|
||||
console.log("Yaha aaya update in the apretn me")
|
||||
if (!parentRecord) {
|
||||
console.log("Parent record nahi mila to create kar raha hai.")
|
||||
const createdParent = await tx.hostParent.create({
|
||||
data: {
|
||||
host: { connect: { id: updatedHost.id } },
|
||||
@@ -933,7 +964,7 @@ export class HostService {
|
||||
data: {
|
||||
hostParentXid: createdParent.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
documentName: sanitizeDocumentName(doc.documentName),
|
||||
filePath: doc.filePath,
|
||||
},
|
||||
});
|
||||
@@ -988,7 +1019,7 @@ export class HostService {
|
||||
where: { id: existingParentDoc.id },
|
||||
data: {
|
||||
filePath: doc.filePath,
|
||||
documentName: doc.documentName || existingParentDoc.documentName,
|
||||
documentName: sanitizeDocumentName(doc.documentName) || existingParentDoc.documentName,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
@@ -996,7 +1027,7 @@ export class HostService {
|
||||
data: {
|
||||
hostParentXid: parentRecord.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
documentName: sanitizeDocumentName(doc.documentName),
|
||||
filePath: doc.filePath,
|
||||
},
|
||||
});
|
||||
@@ -1005,6 +1036,7 @@ export class HostService {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log("Last ke else block me aaya hai")
|
||||
const previousParent = existingHostCompany.hostParent;
|
||||
let prevParentId = null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user