Refactor addOrUpdateCompanyDetails method in HostService to streamline host status handling and improve document management logic for both new and existing companies.
This commit is contained in:
@@ -405,180 +405,92 @@ export class HostService {
|
||||
}
|
||||
|
||||
async addOrUpdateCompanyDetails(
|
||||
user_xid: number,
|
||||
companyData: HostCompanyDetailsInput,
|
||||
documents: HostDocumentInput[],
|
||||
parentCompanyData?: any | null,
|
||||
parentDocuments?: HostDocumentInput[],
|
||||
isDraft: boolean = false,
|
||||
) {
|
||||
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 },
|
||||
});
|
||||
user_xid: number,
|
||||
companyData: HostCompanyDetailsInput,
|
||||
documents: HostDocumentInput[],
|
||||
parentCompanyData?: any | null,
|
||||
parentDocuments?: HostDocumentInput[],
|
||||
isDraft: boolean = false,
|
||||
) {
|
||||
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 },
|
||||
});
|
||||
|
||||
let hostStatusInternal;
|
||||
let hostStatusDisplay;
|
||||
let minglarStatusInternal;
|
||||
let minglarStatusDisplay;
|
||||
if (existingHostCompany) {
|
||||
hostStatusInternal = existingHostCompany.hostStatusInternal;
|
||||
hostStatusDisplay = existingHostCompany.hostStatusDisplay;
|
||||
minglarStatusInternal = existingHostCompany.adminStatusInternal;
|
||||
minglarStatusDisplay = existingHostCompany.adminStatusDisplay;
|
||||
}
|
||||
let hostStatusInternal;
|
||||
let hostStatusDisplay;
|
||||
let minglarStatusInternal;
|
||||
let minglarStatusDisplay;
|
||||
|
||||
// CASE 1: Host was asked to update AND is submitting final (NOT draft)
|
||||
if (existingHostCompany.hostStatusInternal === HOST_STATUS_INTERNAL.HOST_TO_UPDATE && !isDraft) {
|
||||
if (existingHostCompany) {
|
||||
hostStatusInternal = existingHostCompany.hostStatusInternal;
|
||||
hostStatusDisplay = existingHostCompany.hostStatusDisplay;
|
||||
minglarStatusInternal = existingHostCompany.adminStatusInternal;
|
||||
minglarStatusDisplay = existingHostCompany.adminStatusDisplay;
|
||||
}
|
||||
|
||||
hostStatusInternal = HOST_STATUS_INTERNAL.HOST_SUBMITTED;
|
||||
hostStatusDisplay = HOST_STATUS_DISPLAY.UNDER_REVIEW;
|
||||
// CASE 1: Host was asked to update AND is submitting final
|
||||
if (
|
||||
existingHostCompany &&
|
||||
existingHostCompany.hostStatusInternal === 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.TO_REVIEW;
|
||||
minglarStatusInternal = MINGLAR_STATUS_INTERNAL.AM_TO_REVIEW;
|
||||
minglarStatusDisplay = MINGLAR_STATUS_DISPLAY.TO_REVIEW;
|
||||
}
|
||||
// CASE 2: Host was asked to update BUT saving draft
|
||||
else if (
|
||||
existingHostCompany &&
|
||||
existingHostCompany.hostStatusInternal === 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;
|
||||
|
||||
}
|
||||
// CASE 2: Host was asked to update BUT is saving as draft
|
||||
else if (existingHostCompany.hostStatusInternal === HOST_STATUS_INTERNAL.HOST_TO_UPDATE && isDraft) {
|
||||
hostStatusDisplay = isDraft
|
||||
? HOST_STATUS_DISPLAY.DRAFT
|
||||
: HOST_STATUS_DISPLAY.UNDER_REVIEW;
|
||||
|
||||
// DO NOT CHANGE ANY STATUS — KEEP ORIGINAL
|
||||
hostStatusInternal = existingHostCompany.hostStatusInternal;
|
||||
hostStatusDisplay = existingHostCompany.hostStatusDisplay;
|
||||
minglarStatusInternal = existingHostCompany.adminStatusInternal;
|
||||
minglarStatusDisplay = existingHostCompany.adminStatusDisplay;
|
||||
minglarStatusInternal = isDraft
|
||||
? MINGLAR_STATUS_INTERNAL.DRAFT
|
||||
: MINGLAR_STATUS_INTERNAL.ADMIN_TO_REVIEW;
|
||||
|
||||
}
|
||||
// CASE 3: Normal logic (new submission or regular update)
|
||||
else {
|
||||
minglarStatusDisplay = isDraft
|
||||
? MINGLAR_STATUS_DISPLAY.DRAFT
|
||||
: MINGLAR_STATUS_DISPLAY.NEW;
|
||||
}
|
||||
|
||||
hostStatusInternal = isDraft
|
||||
? HOST_STATUS_INTERNAL.DRAFT
|
||||
: HOST_STATUS_INTERNAL.HOST_SUBMITTED;
|
||||
const stepper = isDraft ? STEPPER.NOT_SUBMITTED : STEPPER.UNDER_REVIEW;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Determine status based on isDraft flag
|
||||
|
||||
const stepper = isDraft ? STEPPER.NOT_SUBMITTED : STEPPER.UNDER_REVIEW;
|
||||
|
||||
// CREATE
|
||||
if (!existingHostCompany) {
|
||||
// Optionally check unique registration number (only for final submission)
|
||||
if (!isDraft) {
|
||||
const existingByPan = await tx.hostHeader.findFirst({
|
||||
where: { panNumber: companyData.panNumber },
|
||||
});
|
||||
if (existingByPan)
|
||||
throw new ApiError(400, 'Company already exists with this pan/bin number');
|
||||
}
|
||||
const createdHost = await tx.hostHeader.create({
|
||||
data: {
|
||||
user: { connect: { id: user_xid } },
|
||||
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,
|
||||
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,
|
||||
websiteUrl: companyData.websiteUrl || null,
|
||||
instagramUrl: companyData.instagramUrl || null,
|
||||
facebookUrl: companyData.facebookUrl || null,
|
||||
linkedinUrl: companyData.linkedinUrl || null,
|
||||
twitterUrl: companyData.twitterUrl || null,
|
||||
stepper: stepper,
|
||||
hostStatusInternal: hostStatusInternal,
|
||||
hostStatusDisplay: hostStatusDisplay,
|
||||
adminStatusInternal: minglarStatusInternal,
|
||||
adminStatusDisplay: minglarStatusDisplay,
|
||||
},
|
||||
// -------------------------------------------------------
|
||||
// CREATE FLOW
|
||||
// -------------------------------------------------------
|
||||
if (!existingHostCompany) {
|
||||
if (!isDraft) {
|
||||
const existingByPan = await tx.hostHeader.findFirst({
|
||||
where: { panNumber: companyData.panNumber },
|
||||
});
|
||||
|
||||
// Create host documents (initial insert)
|
||||
if (documents?.length) {
|
||||
const docsData = documents.map((doc) => ({
|
||||
hostXid: createdHost.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
filePath: doc.filePath,
|
||||
}));
|
||||
await tx.hostDocuments.createMany({ data: docsData });
|
||||
}
|
||||
|
||||
// Parent company and its docs (if present)
|
||||
if (companyData.isSubsidairy && parentCompanyData) {
|
||||
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 } }
|
||||
: undefined,
|
||||
states: parentCompanyData.stateXid
|
||||
? { connect: { id: parentCompanyData.stateXid } }
|
||||
: undefined,
|
||||
countries: parentCompanyData.countryXid
|
||||
? { connect: { id: 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,
|
||||
},
|
||||
});
|
||||
|
||||
if (parentDocuments?.length) {
|
||||
const parentDocsData = parentDocuments.map((doc) => ({
|
||||
hostParentXid: createdParent.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
filePath: doc.filePath,
|
||||
}));
|
||||
await tx.hostParenetDocuments.createMany({ data: parentDocsData });
|
||||
}
|
||||
}
|
||||
|
||||
return createdHost;
|
||||
if (existingByPan)
|
||||
throw new ApiError(400, 'Company already exists with this pan/bin number');
|
||||
}
|
||||
|
||||
// UPDATE existing
|
||||
const updatedHost = await tx.hostHeader.update({
|
||||
where: { id: existingHostCompany.id },
|
||||
const createdHost = await tx.hostHeader.create({
|
||||
data: {
|
||||
user: { connect: { id: user_xid } },
|
||||
companyName: companyData.companyName,
|
||||
address1: companyData.address1,
|
||||
address2: companyData.address2,
|
||||
@@ -591,7 +503,9 @@ export class HostService {
|
||||
registrationNumber: companyData.registrationNumber,
|
||||
panNumber: companyData.panNumber,
|
||||
gstNumber: companyData.gstNumber || null,
|
||||
formationDate: companyData.formationDate ? new Date(companyData.formationDate as any) : null,
|
||||
formationDate: companyData.formationDate
|
||||
? new Date(companyData.formationDate as any)
|
||||
: null,
|
||||
companyTypes: companyData.companyTypeXid
|
||||
? { connect: { id: companyData.companyTypeXid } }
|
||||
: undefined,
|
||||
@@ -600,37 +514,199 @@ export class HostService {
|
||||
facebookUrl: companyData.facebookUrl || null,
|
||||
linkedinUrl: companyData.linkedinUrl || null,
|
||||
twitterUrl: companyData.twitterUrl || null,
|
||||
stepper: stepper,
|
||||
hostStatusInternal: hostStatusInternal,
|
||||
hostStatusDisplay: hostStatusDisplay,
|
||||
stepper,
|
||||
hostStatusInternal,
|
||||
hostStatusDisplay,
|
||||
adminStatusInternal: minglarStatusInternal,
|
||||
adminStatusDisplay: minglarStatusDisplay,
|
||||
},
|
||||
});
|
||||
|
||||
// REPLACE/UPSERT host documents by documentTypeXid (Option A)
|
||||
// host documents
|
||||
if (documents?.length) {
|
||||
for (const doc of documents) {
|
||||
const existingDoc = await tx.hostDocuments.findFirst({
|
||||
where: {
|
||||
hostXid: updatedHost.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
const docsData = documents.map((doc) => ({
|
||||
hostXid: createdHost.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
filePath: doc.filePath,
|
||||
}));
|
||||
await tx.hostDocuments.createMany({ data: docsData });
|
||||
}
|
||||
|
||||
// parent create
|
||||
if (companyData.isSubsidairy && parentCompanyData) {
|
||||
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 } }
|
||||
: undefined,
|
||||
states: parentCompanyData.stateXid
|
||||
? { connect: { id: parentCompanyData.stateXid } }
|
||||
: undefined,
|
||||
countries: parentCompanyData.countryXid
|
||||
? { connect: { id: 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: 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,
|
||||
},
|
||||
});
|
||||
|
||||
return createdHost;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// UPDATE FLOW
|
||||
// -------------------------------------------------------
|
||||
const updatedHost = await tx.hostHeader.update({
|
||||
where: { id: existingHostCompany.id },
|
||||
data: {
|
||||
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,
|
||||
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,
|
||||
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,
|
||||
},
|
||||
});
|
||||
|
||||
// documents UPSERT
|
||||
if (documents?.length) {
|
||||
for (const doc of documents) {
|
||||
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: doc.documentName || existingDoc.documentName,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await tx.hostDocuments.create({
|
||||
data: {
|
||||
hostXid: updatedHost.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
filePath: doc.filePath,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (existingDoc) {
|
||||
// update only filePath (and name if required)
|
||||
await tx.hostDocuments.update({
|
||||
where: { id: existingDoc.id },
|
||||
// parent logic untouched
|
||||
if (companyData.isSubsidairy) {
|
||||
const parentRecords = existingHostCompany.hostParent;
|
||||
const parentRecord = Array.isArray(parentRecords) ? parentRecords[0] : parentRecords;
|
||||
|
||||
if (!parentRecord) {
|
||||
const createdParent = await tx.hostParent.create({
|
||||
data: {
|
||||
host: { connect: { id: updatedHost.id } },
|
||||
companyName: parentCompanyData.companyName,
|
||||
address1: parentCompanyData.address1 || null,
|
||||
address2: parentCompanyData.address2 || null,
|
||||
cities: parentCompanyData.cityXid
|
||||
? { connect: { id: parentCompanyData.cityXid } }
|
||||
: undefined,
|
||||
states: parentCompanyData.stateXid
|
||||
? { connect: { id: parentCompanyData.stateXid } }
|
||||
: undefined,
|
||||
countries: parentCompanyData.countryXid
|
||||
? { connect: { id: 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,
|
||||
},
|
||||
});
|
||||
|
||||
if (parentDocuments?.length) {
|
||||
for (const doc of parentDocuments) {
|
||||
await tx.hostParenetDocuments.create({
|
||||
data: {
|
||||
filePath: doc.filePath,
|
||||
documentName: doc.documentName || existingDoc.documentName,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await tx.hostDocuments.create({
|
||||
data: {
|
||||
hostXid: updatedHost.id,
|
||||
hostParentXid: createdParent.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
filePath: doc.filePath,
|
||||
@@ -638,52 +714,62 @@ export class HostService {
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
await tx.hostParent.update({
|
||||
where: { id: parentRecord.id },
|
||||
data: {
|
||||
companyName: parentCompanyData.companyName,
|
||||
address1: parentCompanyData.address1 || null,
|
||||
address2: parentCompanyData.address2 || null,
|
||||
cities: parentCompanyData.cityXid
|
||||
? { connect: { id: parentCompanyData.cityXid } }
|
||||
: undefined,
|
||||
states: parentCompanyData.stateXid
|
||||
? { connect: { id: parentCompanyData.stateXid } }
|
||||
: undefined,
|
||||
countries: parentCompanyData.countryXid
|
||||
? { connect: { id: 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 company create/update and replace parent docs by documentTypeXid
|
||||
if (companyData.isSubsidairy) {
|
||||
let parentRecord = (existingHostCompany as any).hostParent;
|
||||
if (Array.isArray(parentRecord)) parentRecord = parentRecord[0];
|
||||
if (parentDocuments?.length) {
|
||||
for (const doc of parentDocuments) {
|
||||
const existingParentDoc = await tx.hostParenetDocuments.findFirst({
|
||||
where: {
|
||||
hostParentXid: parentRecord.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
},
|
||||
});
|
||||
|
||||
if (!parentRecord) {
|
||||
// create
|
||||
const createdParent = await tx.hostParent.create({
|
||||
data: {
|
||||
host: { connect: { id: updatedHost.id } },
|
||||
companyName: parentCompanyData.companyName,
|
||||
address1: parentCompanyData.address1 || null,
|
||||
address2: parentCompanyData.address2 || null,
|
||||
cities: parentCompanyData.cityXid
|
||||
? { connect: { id: parentCompanyData.cityXid } }
|
||||
: undefined,
|
||||
states: parentCompanyData.stateXid
|
||||
? { connect: { id: parentCompanyData.stateXid } }
|
||||
: undefined,
|
||||
countries: parentCompanyData.countryXid
|
||||
? { connect: { id: 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,
|
||||
},
|
||||
});
|
||||
|
||||
if (parentDocuments?.length) {
|
||||
for (const doc of parentDocuments) {
|
||||
if (existingParentDoc) {
|
||||
await tx.hostParenetDocuments.update({
|
||||
where: { id: existingParentDoc.id },
|
||||
data: {
|
||||
filePath: doc.filePath,
|
||||
documentName: doc.documentName || existingParentDoc.documentName,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await tx.hostParenetDocuments.create({
|
||||
data: {
|
||||
hostParentXid: createdParent.id,
|
||||
hostParentXid: parentRecord.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
filePath: doc.filePath,
|
||||
@@ -691,115 +777,53 @@ export class HostService {
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// update existing parent
|
||||
await tx.hostParent.update({
|
||||
where: { id: parentRecord.id },
|
||||
data: {
|
||||
companyName: parentCompanyData.companyName,
|
||||
address1: parentCompanyData.address1 || null,
|
||||
address2: parentCompanyData.address2 || null,
|
||||
cities: parentCompanyData.cityXid
|
||||
? { connect: { id: parentCompanyData.cityXid } }
|
||||
: undefined,
|
||||
states: parentCompanyData.stateXid
|
||||
? { connect: { id: parentCompanyData.stateXid } }
|
||||
: undefined,
|
||||
countries: parentCompanyData.countryXid
|
||||
? { connect: { id: 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,
|
||||
},
|
||||
});
|
||||
|
||||
// replace / upsert parent docs by documentTypeXid (no deletes)
|
||||
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: doc.documentName || existingParentDoc.documentName,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
await tx.hostParenetDocuments.create({
|
||||
data: {
|
||||
hostParentXid: parentRecord.id,
|
||||
documentTypeXid: doc.documentTypeXid,
|
||||
documentName: doc.documentName,
|
||||
filePath: doc.filePath,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// If previously had a parent and now isSubsidairy=false -> optionally delete parent and its docs
|
||||
const previousParent = (existingHostCompany as any).hostParent;
|
||||
let prevParentId = null;
|
||||
if (Array.isArray(previousParent) && previousParent.length)
|
||||
prevParentId = previousParent[0].id;
|
||||
else if (previousParent && previousParent.id)
|
||||
prevParentId = previousParent.id;
|
||||
|
||||
if (prevParentId) {
|
||||
await tx.hostParenetDocuments.deleteMany({
|
||||
where: { hostParentXid: prevParentId },
|
||||
});
|
||||
await tx.hostParent.delete({ where: { id: prevParentId } });
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const previousParent = existingHostCompany.hostParent;
|
||||
let prevParentId = null;
|
||||
|
||||
// Create a host track entry
|
||||
const hostDetails = await tx.hostHeader.findFirst({
|
||||
where: { userXid: user_xid },
|
||||
});
|
||||
if (Array.isArray(previousParent) && previousParent.length) {
|
||||
prevParentId = previousParent[0].id;
|
||||
} else if (previousParent && typeof previousParent === 'object' && 'id' in previousParent) {
|
||||
prevParentId = previousParent.id;
|
||||
}
|
||||
|
||||
await tx.hostTrack.create({
|
||||
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: {
|
||||
hostXid: hostDetails!.id,
|
||||
updatedByRole: ROLE_NAME.HOST,
|
||||
updatedByXid: user_xid,
|
||||
trackStatus: hostDetails!.hostStatusInternal,
|
||||
isreviewed: true,
|
||||
reviewedByXid: user_xid,
|
||||
reviewOn: new Date(),
|
||||
},
|
||||
});
|
||||
if (!isDraft) {
|
||||
await tx.hostSuggestion.updateMany({
|
||||
where: { hostXid: hostDetails.id, isActive: true, isreviewed: false },
|
||||
data: {
|
||||
isreviewed: true,
|
||||
reviewedByXid: user_xid,
|
||||
reviewOn: new Date(),
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return updatedHost;
|
||||
});
|
||||
}
|
||||
return updatedHost;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
async getSuggestionDetails(user_xid: number) {
|
||||
const hostDetails = await this.prisma.hostHeader.findFirst({
|
||||
|
||||
Reference in New Issue
Block a user