Remove repeated Plant, Financial and Workforce shortcut cards. Add site-scoped Plant and Financial sidebar attention status with automatic refresh. Include accumulated delivery processing, purchase order recovery, supplier reconciliation, plant data and user management changes and audit records.
39 lines
1.7 KiB
JavaScript
39 lines
1.7 KiB
JavaScript
const claimItem = $('Atomic Claim').item.json;
|
|
const purchaseOrderId = claimItem.id;
|
|
const attempts = Number(claimItem.attempts || 0);
|
|
const input = $input.item.json || {};
|
|
const error = input.error || input;
|
|
const lastError = String(typeof error === 'string' ? error : error.message || JSON.stringify(error)).slice(0, 2000);
|
|
const backoffMinutes = [1, 5, 15, 60][Math.min(Math.max(attempts - 1, 0), 3)];
|
|
const nextRetry = new Date(Date.now() + backoffMinutes * 60000).toISOString();
|
|
const pbToken = $('Auth PB Admin').item.json.token;
|
|
const url = 'https://pb.mega.infra.apphub.co.za/api/collections/purchase_orders/records/' + purchaseOrderId;
|
|
|
|
let currentBody;
|
|
try {
|
|
const response = await this.helpers.httpRequest({
|
|
method: 'GET', url, headers: { Authorization: pbToken }, returnResponse: true,
|
|
});
|
|
currentBody = response.body || response;
|
|
} catch (error) {
|
|
if (Number(error.statusCode || error.status) === 404) {
|
|
return { json: { escalated: false, purchaseOrderId, skipped: true, reason: 'record_removed' } };
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
if (currentBody.processing_status !== 'processing' ||
|
|
Number(currentBody.attempts) !== attempts ||
|
|
Date.parse(currentBody.last_attempt_at) !== Date.parse(claimItem.last_attempt_at)) {
|
|
return { json: { escalated: false, purchaseOrderId, skipped: true, reason: 'claim_no_longer_current' } };
|
|
}
|
|
|
|
await this.helpers.httpRequest({
|
|
method: 'PATCH', url,
|
|
headers: { Authorization: pbToken, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
processing_status: 'failed', last_error: lastError, next_retry_at: nextRetry,
|
|
}),
|
|
returnResponse: true,
|
|
});
|
|
return { json: { escalated: true, purchaseOrderId, status: 'failed', next_retry_at: nextRetry } };
|