Skip to content

Commit 51692a8

Browse files
committed
Fix state code resolution at shipment time + fix INT.XP fallback
- Resolve full state names (e.g. California -> CA) in createShipment XML so existing orders with full names in DB are handled - Only fall back to INT.XP when CP_CONTRACT_ID is set (it requires a contract) - Include error details in 502 response
1 parent 360d3f9 commit 51692a8

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

resolution-frontend/src/lib/server/canada-post.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { env } from '$env/dynamic/private';
22
import xml2js from 'xml2js';
33
import { PDFDocument } from 'pdf-lib';
44
import { fetchChitChatsRates } from './chit-chats';
5+
import { resolveStateCode } from './countries';
56

67
export const INCHES_TO_CM = 2.54;
78
export const GRAMS_TO_KG = 0.001;
@@ -194,7 +195,7 @@ export function buildCreateShipmentXml(params: {
194195
<address-line-1>${escapeXml((order.addressLine1 || '').substring(0, 44))}</address-line-1>
195196
${(order.addressLine2 || order.addressLine1?.length > 44) ? `<address-line-2>${escapeXml((order.addressLine2 || order.addressLine1?.substring(44) || '').substring(0, 44))}</address-line-2>` : ''}
196197
<city>${escapeXml((order.city || '').substring(0, 40))}</city>
197-
<prov-state>${escapeXml((order.stateProvince || '').substring(0, 20))}</prov-state>
198+
<prov-state>${escapeXml(resolveStateCode(order.stateProvince || '').substring(0, 20))}</prov-state>
198199
<country-code>${escapeXml(order.country)}</country-code>
199200
<postal-zip-code>${(order.postalCode ?? '').replace(/\s/g, '').toUpperCase()}</postal-zip-code>
200201
</address-details>

resolution-frontend/src/routes/api/fulfillment/get-label/+server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ export const POST: RequestHandler = async ({ request, locals }) => {
231231
console.log(`Creating shipment: country=${order.country}, estimatedService=${order.estimatedServiceName}, serviceCode=${serviceCode}`);
232232

233233
// Try creating shipment, fall back to alternate international services if unavailable
234-
const fallbackCodes = serviceCode === 'INT.TP' ? ['INT.XP'] : serviceCode === 'INT.XP' ? ['INT.TP'] : [];
234+
// INT.XP requires a contract, so only use it as fallback when contract is available
235+
const fallbackCodes: string[] = [];
236+
if (serviceCode === 'INT.TP' && env.CP_CONTRACT_ID) fallbackCodes.push('INT.XP');
237+
if (serviceCode === 'INT.XP') fallbackCodes.push('INT.TP');
235238
let lastError: any;
236239
for (const code of [serviceCode, ...fallbackCodes]) {
237240
try {
@@ -247,7 +250,7 @@ export const POST: RequestHandler = async ({ request, locals }) => {
247250
}
248251
}
249252
if (lastError) {
250-
throw error(502, `Canada Post shipment creation failed`);
253+
throw error(502, `Canada Post shipment creation failed: ${lastError.message}`);
251254
}
252255
}
253256

0 commit comments

Comments
 (0)