From cb819088a0bdae6bbf245d3eeb9f2db32ff1c71f Mon Sep 17 00:00:00 2001 From: Mayank Mishra Date: Wed, 25 Mar 2026 16:29:13 +0530 Subject: [PATCH] fixed the comments string issue --- .../Activity_Hub/OnBoarding/submitPQAnswer.ts | 53 +++++++++++++------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/src/modules/host/handlers/Activity_Hub/OnBoarding/submitPQAnswer.ts b/src/modules/host/handlers/Activity_Hub/OnBoarding/submitPQAnswer.ts index 33350e1..948c19e 100644 --- a/src/modules/host/handlers/Activity_Hub/OnBoarding/submitPQAnswer.ts +++ b/src/modules/host/handlers/Activity_Hub/OnBoarding/submitPQAnswer.ts @@ -13,6 +13,40 @@ const hostService = new HostService(prismaClient); const s3 = new AWS.S3({ region: config.aws.region }); +function parseMultipartFieldValue(val: string) { + if (val === '' || val === 'null' || val === 'undefined') return null; + + const cleaned = val.trim(); + const looksLikeJson = + (cleaned.startsWith('{') && cleaned.endsWith('}')) || + (cleaned.startsWith('[') && cleaned.endsWith(']')) || + (cleaned.startsWith('"') && cleaned.endsWith('"')); + + if (!looksLikeJson) return val; + + try { + return JSON.parse(cleaned); + } catch { + return val; + } +} + +function normalizeComments(comments: unknown): string | null { + if (comments === null || comments === undefined || comments === '') return null; + + const value = String(comments).trim(); + if (!value) return null; + + if ( + (value.startsWith('"') && value.endsWith('"')) || + (value.startsWith("'") && value.endsWith("'")) + ) { + return value.slice(1, -1); + } + + return value; +} + // Function to extract S3 key from URL function getS3KeyFromUrl(url: string): string { const bucketBaseUrl = `https://${config.aws.bucketName}.s3.${config.aws.region}.amazonaws.com/`; @@ -122,22 +156,7 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise< bb.on("field", (fieldname, val) => { console.log(`FIELD RAW: ${fieldname} =`, val); - if (val === '' || val === 'null' || val === 'undefined') fields[fieldname] = null; - else { - try { - const cleaned = val.trim(); - - // If it starts and ends with quotes, remove them - const withoutQuotes = - (cleaned.startsWith('"') && cleaned.endsWith('"')) - ? cleaned.slice(1, -1) - : cleaned; - - fields[fieldname] = JSON.parse(withoutQuotes); - } catch { - fields[fieldname] = val; - } - } + fields[fieldname] = parseMultipartFieldValue(val); }); bb.on("close", () => resolve()); @@ -154,7 +173,7 @@ export const handler = safeHandler(async (event: APIGatewayProxyEvent): Promise< const activityXid = Number(fields.activityXid); const pqqQuestionXid = Number(fields.pqqQuestionXid); const pqqAnswerXid = Number(fields.pqqAnswerXid); - const comments = fields.comments || null; + const comments = normalizeComments(fields.comments); if (!activityXid || isNaN(activityXid)) throw new ApiError(400, "Please provide a valid activity"); if (!pqqQuestionXid || isNaN(pqqQuestionXid)) throw new ApiError(400, "Please select a valid question");