submit company details api s3 uploading issue resolved and required docs removed

This commit is contained in:
2025-11-27 20:51:04 +05:30
parent 8ccbdc8f32
commit 4f3adff517
3 changed files with 10 additions and 83 deletions

View File

@@ -1,12 +1,12 @@
import { z } from "zod";
// REQUIRED_DOC_TYPES (use your mapping)
export const REQUIRED_DOC_TYPES = {
GST: 1,
PAN: 2,
REGISTRATION: 3,
AADHAAR: 4,
};
// export const REQUIRED_DOC_TYPES = {
// GST: 1,
// PAN: 2,
// REGISTRATION: 3,
// AADHAAR: 4,
// };
export const parentCompanySchema = z.object({
companyName: z.string().min(1, "Parent company name is required"),
@@ -14,7 +14,6 @@ export const parentCompanySchema = z.object({
address2: z.string().optional(),
// Parent companies DO NOT need this
hostRefNumber: z.string().optional(),
cityXid: z.number().min(1, "City is required"),
stateXid: z.number().min(1, "State is required"),

View File

@@ -11,7 +11,7 @@ import {
hostCompanyDetailsSchema,
hostDocumentsSchema,
parentCompanySchema,
REQUIRED_DOC_TYPES
// REQUIRED_DOC_TYPES
} from '../../../../../common/utils/validation/host/hostCompanyDetails.validation';
import { HostService } from '../../../services/host.service';
import { sendEmailToAM, sendEmailToMinglarAdmin } from '../../../services/sendHostResubmitEmailToAM.service';
@@ -158,25 +158,6 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise<
where: { userXid: userInfo.id },
});
if (!existingHost) {
// Create empty hostHeader FIRST before uploading files
const createdHost = await prisma.hostHeader.create({
data: {
userXid: Number(userInfo.id),
companyName: "", // temporary placeholder
hostStatusInternal: HOST_STATUS_INTERNAL.DRAFT, // or whatever default
}
});
hostId = createdHost.id;
existingHost = createdHost;
console.log("🔥 HostHeader created first. Host ID:", hostId);
} else {
hostId = existingHost.id;
console.log(" Using existing Host ID:", hostId);
}
// Define uploadToS3 function (same as before)
async function uploadToS3(buffer: Buffer, mimeType: string, originalName: string, folderType: 'logo' | 'documents' | 'parent_company', documentTypeXid?: number, fieldName?: string) {
let s3Key: string;
@@ -193,15 +174,15 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise<
if (folderType === 'logo') {
const sanitizedFileName = sanitizeFileName(originalName);
s3Key = `Documents/Host/${hostId}/logo/${sanitizedFileName}`;
s3Key = `Documents/Host/${userInfo.id}/logo/${sanitizedFileName}`;
} else if (folderType === 'documents' && documentTypeXid && fieldName) {
const fileName = `${documentTypeXid}_${fieldName}.${fileExtension}`;
const sanitizedFileName = sanitizeFileName(fileName);
s3Key = `Documents/Host/${hostId}/documents/${sanitizedFileName}`;
s3Key = `Documents/Host/${userInfo.id}/documents/${sanitizedFileName}`;
} else if (folderType === 'parent_company' && documentTypeXid && fieldName) {
const fileName = `${documentTypeXid}_${fieldName}.${fileExtension}`;
const sanitizedFileName = sanitizeFileName(fileName);
s3Key = `Documents/Host/${hostId}/parent_company/${sanitizedFileName}`;
s3Key = `Documents/Host/${userInfo.id}/parent_company/${sanitizedFileName}`;
} else {
throw new ApiError(400, 'Invalid folder type or missing documentTypeXid/fieldName');
}
@@ -268,16 +249,6 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise<
const hostDocs = documentMetadata.filter((d) => d.owner === 'host');
const parentDocs = documentMetadata.filter((d) => d.owner === 'parent');
// 10) If NOT draft, validate required documents
if (!isDraft) {
const hostUploadedTypes = hostDocs.map((d) => d.documentTypeXid);
const requiredHostTypes = Object.values(REQUIRED_DOC_TYPES);
const missingHostDocs = requiredHostTypes.filter((typeId) => !hostUploadedTypes.includes(typeId));
if (missingHostDocs.length > 0) {
throw new ApiError(400, `Missing mandatory documents for host: ${missingHostDocs.join(', ')}`);
}
}
// 11) If isSubsidairy === true and parentCompany provided -> validate parent company & docs
let parsedParentCompany: any = null;
if (parsedCompany.isSubsidairy) {
@@ -291,16 +262,6 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise<
throw new ApiError(400, `Parent company validation failed: ${message}`);
}
parsedParentCompany = parsedCompany.parentCompany;
// If NOT draft, validate required parent documents
if (!isDraft) {
const parentUploadedTypes = parentDocs.map((d) => d.documentTypeXid);
const requiredParentTypes = Object.values(REQUIRED_DOC_TYPES);
const missingParentDocs = requiredParentTypes.filter((typeId) => !parentUploadedTypes.includes(typeId));
if (missingParentDocs.length > 0) {
throw new ApiError(400, `Missing mandatory documents for parent company: ${missingParentDocs.join(', ')}`);
}
}
}
// 12) Upload files to S3 (same for both draft and final submission)