diff --git a/src/pg/carnet-application/carnet-application.service.ts b/src/pg/carnet-application/carnet-application.service.ts index 7a4c0dc..a260d9b 100644 --- a/src/pg/carnet-application/carnet-application.service.ts +++ b/src/pg/carnet-application/carnet-application.service.ts @@ -365,33 +365,34 @@ export class CarnetApplicationService { await client.query('BEGIN'); - function escapePostgresString(str: string): string { + function escapePostgresCompositeField(str: string): string { if (str == null) return ''; - return `"${str.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`; - } - - function escapePostgresValue(val: any): string { - if (val == null || val === '') return ''; // NULL or empty becomes empty string - if (typeof val === 'number') return String(val); // pass numbers as-is - return escapePostgresString(String(val)); // quote and escape strings + return str.replace(/\\/g, '\\\\').replace(/"/g, '""'); // PostgreSQL-compliant } const contactLiterals = body.P_COUNTRYTABLE.map(c => { const fields = [ - escapePostgresValue(c.P_VISISTTRANSITIND), // integer - escapePostgresValue(c.COUNTRYCODE), // varchar - escapePostgresValue(c.NOOFTIMESENTLEAVE), // numeric - ]; + c.P_VISISTTRANSITIND ?? '', + c.COUNTRYCODE ?? '', + c.NOOFTIMESENTLEAVE ?? '', + ].map(field => escapePostgresCompositeField(String(field))).join(','); - return `(${fields.join(',')})`; // composite literal without outer double quotes + return `"(${fields})"`; // ✅ wrap entire composite in double quotes }); const pgArrayLiteral = `{${contactLiterals.join(',')}}`; const callProcQuery = ` - CALL "CARNETSYS".carnetapplication_pkg_addcountries($1, $2, $3::"CARNETSYS".carnetcountrytable[], $4, $5) - `; - await client.query(callProcQuery, [body.P_HEADERID, body.P_USSETS, pgArrayLiteral, body.P_USERID, cursorName]); + CALL "CARNETSYS".carnetapplication_pkg_addcountries($1, $2, $3::"CARNETSYS".carnetcountrytable[], $4, $5) +`; + + await client.query(callProcQuery, [ + body.P_HEADERID, + body.P_USSETS, + pgArrayLiteral, + body.P_USERID, + cursorName + ]); // 🚫 DON'T quote the cursor name in FETCH const fetchResult = await client.query(`FETCH ALL FROM ${cursorName}`);