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:
paritosh18
2025-12-02 13:40:41 +05:30
parent c3f0a1d82a
commit 9a777eb3f9

View File

@@ -411,7 +411,7 @@ export class HostService {
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({
@@ -423,6 +423,7 @@ export class HostService {
let hostStatusDisplay;
let minglarStatusInternal;
let minglarStatusDisplay;
if (existingHostCompany) {
hostStatusInternal = existingHostCompany.hostStatusInternal;
hostStatusDisplay = existingHostCompany.hostStatusDisplay;
@@ -430,29 +431,32 @@ export class HostService {
minglarStatusDisplay = existingHostCompany.adminStatusDisplay;
}
// CASE 1: Host was asked to update AND is submitting final (NOT draft)
if (existingHostCompany.hostStatusInternal === HOST_STATUS_INTERNAL.HOST_TO_UPDATE && !isDraft) {
// 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;
}
// CASE 2: Host was asked to update BUT is saving as draft
else if (existingHostCompany.hostStatusInternal === HOST_STATUS_INTERNAL.HOST_TO_UPDATE && isDraft) {
// DO NOT CHANGE ANY STATUS — KEEP ORIGINAL
// 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 logic (new submission or regular update)
// CASE 3: Normal create or update
else {
hostStatusInternal = isDraft
? HOST_STATUS_INTERNAL.DRAFT
: HOST_STATUS_INTERNAL.HOST_SUBMITTED;
@@ -470,13 +474,12 @@ export class HostService {
: MINGLAR_STATUS_DISPLAY.NEW;
}
// Determine status based on isDraft flag
const stepper = isDraft ? STEPPER.NOT_SUBMITTED : STEPPER.UNDER_REVIEW;
// CREATE
// -------------------------------------------------------
// CREATE FLOW
// -------------------------------------------------------
if (!existingHostCompany) {
// Optionally check unique registration number (only for final submission)
if (!isDraft) {
const existingByPan = await tx.hostHeader.findFirst({
where: { panNumber: companyData.panNumber },
@@ -484,6 +487,7 @@ export class HostService {
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 } },
@@ -499,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,
@@ -508,15 +514,15 @@ 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,
},
});
// Create host documents (initial insert)
// host documents
if (documents?.length) {
const docsData = documents.map((doc) => ({
hostXid: createdHost.id,
@@ -527,7 +533,7 @@ export class HostService {
await tx.hostDocuments.createMany({ data: docsData });
}
// Parent company and its docs (if present)
// parent create
if (companyData.isSubsidairy && parentCompanyData) {
const createdParent = await tx.hostParent.create({
data: {
@@ -549,7 +555,9 @@ export class HostService {
registrationNumber: parentCompanyData.registrationNumber || null,
panNumber: parentCompanyData.panNumber || null,
gstNumber: parentCompanyData.gstNumber || null,
formationDate: parentCompanyData.formationDate ? new Date(parentCompanyData.formationDate as any) : null,
formationDate: parentCompanyData.formationDate
? new Date(parentCompanyData.formationDate as any)
: null,
companyTypes: parentCompanyData.companyTypeXid
? { connect: { id: parentCompanyData.companyTypeXid } }
: undefined,
@@ -561,6 +569,7 @@ export class HostService {
},
});
// parent docs
if (parentDocuments?.length) {
const parentDocsData = parentDocuments.map((doc) => ({
hostParentXid: createdParent.id,
@@ -572,10 +581,22 @@ export class HostService {
}
}
// ⭐ 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 existing
// -------------------------------------------------------
// UPDATE FLOW
// -------------------------------------------------------
const updatedHost = await tx.hostHeader.update({
where: { id: existingHostCompany.id },
data: {
@@ -591,7 +612,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,15 +623,15 @@ 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)
// documents UPSERT
if (documents?.length) {
for (const doc of documents) {
const existingDoc = await tx.hostDocuments.findFirst({
@@ -619,7 +642,6 @@ export class HostService {
});
if (existingDoc) {
// update only filePath (and name if required)
await tx.hostDocuments.update({
where: { id: existingDoc.id },
data: {
@@ -640,13 +662,12 @@ export class HostService {
}
}
// Parent company create/update and replace parent docs by documentTypeXid
// parent logic untouched
if (companyData.isSubsidairy) {
let parentRecord = (existingHostCompany as any).hostParent;
if (Array.isArray(parentRecord)) parentRecord = parentRecord[0];
const parentRecords = existingHostCompany.hostParent;
const parentRecord = Array.isArray(parentRecords) ? parentRecords[0] : parentRecords;
if (!parentRecord) {
// create
const createdParent = await tx.hostParent.create({
data: {
host: { connect: { id: updatedHost.id } },
@@ -667,7 +688,9 @@ export class HostService {
registrationNumber: parentCompanyData.registrationNumber || null,
panNumber: parentCompanyData.panNumber || null,
gstNumber: parentCompanyData.gstNumber || null,
formationDate: parentCompanyData.formationDate ? new Date(parentCompanyData.formationDate as any) : null,
formationDate: parentCompanyData.formationDate
? new Date(parentCompanyData.formationDate as any)
: null,
companyTypes: parentCompanyData.companyTypeXid
? { connect: { id: parentCompanyData.companyTypeXid } }
: undefined,
@@ -692,7 +715,6 @@ export class HostService {
}
}
} else {
// update existing parent
await tx.hostParent.update({
where: { id: parentRecord.id },
data: {
@@ -713,7 +735,9 @@ export class HostService {
registrationNumber: parentCompanyData.registrationNumber || null,
panNumber: parentCompanyData.panNumber || null,
gstNumber: parentCompanyData.gstNumber || null,
formationDate: parentCompanyData.formationDate ? new Date(parentCompanyData.formationDate as any) : null,
formationDate: parentCompanyData.formationDate
? new Date(parentCompanyData.formationDate as any)
: null,
companyTypes: parentCompanyData.companyTypeXid
? { connect: { id: parentCompanyData.companyTypeXid } }
: undefined,
@@ -725,7 +749,6 @@ export class HostService {
},
});
// replace / upsert parent docs by documentTypeXid (no deletes)
if (parentDocuments?.length) {
for (const doc of parentDocuments) {
const existingParentDoc = await tx.hostParenetDocuments.findFirst({
@@ -757,13 +780,14 @@ export class HostService {
}
}
} else {
// If previously had a parent and now isSubsidairy=false -> optionally delete parent and its docs
const previousParent = (existingHostCompany as any).hostParent;
const previousParent = existingHostCompany.hostParent;
let prevParentId = null;
if (Array.isArray(previousParent) && previousParent.length)
if (Array.isArray(previousParent) && previousParent.length) {
prevParentId = previousParent[0].id;
else if (previousParent && previousParent.id)
} else if (previousParent && typeof previousParent === 'object' && 'id' in previousParent) {
prevParentId = previousParent.id;
}
if (prevParentId) {
await tx.hostParenetDocuments.deleteMany({
@@ -773,33 +797,33 @@ export class HostService {
}
}
// Create a host track entry
const hostDetails = await tx.hostHeader.findFirst({
where: { userXid: user_xid },
});
// ⭐ FIX — USE updatedHost instead of re-querying hostHeader
await tx.hostTrack.create({
data: {
hostXid: hostDetails!.id,
hostXid: updatedHost.id,
updatedByRole: ROLE_NAME.HOST,
updatedByXid: user_xid,
trackStatus: hostDetails!.hostStatusInternal,
trackStatus: updatedHost.hostStatusInternal,
},
});
// suggestion update unchanged
if (!isDraft) {
await tx.hostSuggestion.updateMany({
where: { hostXid: hostDetails.id, isActive: true, isreviewed: false },
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({