From 50f93bbeae5edfb8749783bbfe8e8d1d3224a227 Mon Sep 17 00:00:00 2001 From: Mayank Mishra Date: Wed, 1 Apr 2026 15:10:10 +0530 Subject: [PATCH] added the delete logo path of the main and parent company --- .../onboarding/submitCompanyDetails.ts | 62 +++++++++++++++++++ src/modules/host/services/host.service.ts | 26 +++++--- 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/src/modules/host/handlers/Host_Admin/onboarding/submitCompanyDetails.ts b/src/modules/host/handlers/Host_Admin/onboarding/submitCompanyDetails.ts index c3b8493..d0c9550 100644 --- a/src/modules/host/handlers/Host_Admin/onboarding/submitCompanyDetails.ts +++ b/src/modules/host/handlers/Host_Admin/onboarding/submitCompanyDetails.ts @@ -142,6 +142,10 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise< const deletedFiles = normalizeJsonField(fields, "deletedFiles") || []; const parentDeletedFiles = normalizeJsonField(fields, "parentDeletedFiles") || []; + const deleteCompanyLogo = + fields.deleteCompanyLogo === 'true' || fields.deleteCompanyLogo === true; + const deleteParentCompanyLogo = + fields.deleteParentCompanyLogo === 'true' || fields.deleteParentCompanyLogo === true; /** 4) Extract and clean isDraft flag */ const isDraft = fields.isDraft === 'true' || fields.isDraft === true; @@ -379,6 +383,63 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise< }); } + /** DELETE EXISTING LOGO IF REQUESTED */ + if (deleteCompanyLogo) { + const existingHost = await prismaClient.hostHeader.findFirst({ + where: { userXid: userInfo.id }, + select: { logoPath: true }, + }); + + if (existingHost?.logoPath) { + try { + const s3Key = getS3KeyFromUrl(existingHost.logoPath); + await deleteFromS3(s3Key); + } catch (e) { + console.error('S3 delete failed for company logo:', existingHost.logoPath, e); + } + } + + parsedCompany.logoPath = null; + } + + /** DELETE EXISTING PARENT COMPANY LOGO IF REQUESTED */ + if (deleteParentCompanyLogo && parsedCompany.isSubsidairy) { + const existingHost = await prismaClient.hostHeader.findFirst({ + where: { userXid: userInfo.id }, + select: { + id: true, + hostParent: { + select: { + id: true, + logoPath: true, + }, + take: 1, + }, + }, + }); + + const existingParent = Array.isArray(existingHost?.hostParent) + ? existingHost.hostParent[0] + : existingHost?.hostParent; + + if (existingParent?.logoPath) { + try { + const s3Key = getS3KeyFromUrl(existingParent.logoPath); + await deleteFromS3(s3Key); + } catch (e) { + console.error('S3 delete failed for parent company logo:', existingParent.logoPath, e); + } + } + + if (parsedParentCompany) { + parsedParentCompany.logoPath = null; + } else { + parsedParentCompany = { + logoPath: null, + }; + } + } + /** UPLOAD LOGO (if provided) */ const logoFile = files.find( (f) => f.fieldName === 'companyLogo' || f.fieldName === 'companyLogoFile' @@ -449,6 +510,7 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise< parsedParentCompany, uploadedParentDocs, isDraft, + { deleteCompanyLogo, deleteParentCompanyLogo }, ); if (!createdOrUpdated) throw new ApiError(400, 'Failed to add/update company details.'); diff --git a/src/modules/host/services/host.service.ts b/src/modules/host/services/host.service.ts index 5e7fb71..f89008b 100644 --- a/src/modules/host/services/host.service.ts +++ b/src/modules/host/services/host.service.ts @@ -1391,6 +1391,10 @@ export class HostService { 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 @@ -1651,7 +1655,9 @@ export class HostService { ? { connect: { id: Number(companyData.countryXid) } } : undefined, pinCode: companyData.pinCode, - logoPath: companyData.logoPath || existingHostCompany.logoPath, + logoPath: options?.deleteCompanyLogo + ? companyData.logoPath ?? null + : companyData.logoPath || existingHostCompany.logoPath, isSubsidairy: companyData.isSubsidairy, registrationNumber: companyData.registrationNumber, panNumber: companyData.panNumber, @@ -1792,10 +1798,11 @@ export class HostService { ? { connect: { id: Number(parentCompanyData.countryXid) } } : undefined, pinCode: parentCompanyData.pinCode || null, - logoPath: - parentCompanyData?.logoPath || - existingParentCompany?.logoPath || - null, + logoPath: options?.deleteParentCompanyLogo + ? parentCompanyData?.logoPath ?? null + : parentCompanyData?.logoPath || + existingParentCompany?.logoPath || + null, registrationNumber: parentCompanyData.registrationNumber || null, panNumber: parentCompanyData.panNumber || null, gstNumber: parentCompanyData.gstNumber || null, @@ -1850,10 +1857,11 @@ export class HostService { ? { connect: { id: Number(parentCompanyData.countryXid) } } : undefined, pinCode: parentCompanyData.pinCode || null, - logoPath: - parentCompanyData?.logoPath || - existingParentCompany?.logoPath || - null, + logoPath: options?.deleteParentCompanyLogo + ? parentCompanyData?.logoPath ?? null + : parentCompanyData?.logoPath || + existingParentCompany?.logoPath || + null, registrationNumber: parentCompanyData.registrationNumber || null, panNumber: parentCompanyData.panNumber || null, gstNumber: parentCompanyData.gstNumber || null,