Implemented the customer service requests view
This commit is contained in:
60
src/services/spa/customer-service-requests.js
Normal file
60
src/services/spa/customer-service-requests.js
Normal file
@@ -0,0 +1,60 @@
|
||||
function createSpaCustomerServiceRequests({ ACCOUNT_SUMMARY_URL, fetchJson, findAccountPlanId, normalizeMessageAuthorization }) {
|
||||
const endpoint = "https://spa.oracle.com/oalcrm/web/api/provider-proxy/g2m-custsr-provider/service/oscSr/";
|
||||
const filter = encodeURIComponent(JSON.stringify({ hierarchy: "single", status: "open" }));
|
||||
|
||||
async function requestCustomerServiceRequest(message) {
|
||||
const debugRequests = [];
|
||||
try {
|
||||
const partyNumber = String(message.partyNumber || "").trim();
|
||||
const summaryAuth = normalizeMessageAuthorization(message.authorizations?.accountSummary, "account-ti-authorization");
|
||||
const srAuth = normalizeMessageAuthorization(message.authorizations?.customerServiceRequests, "");
|
||||
if (!partyNumber) throw new Error("Customer Party Number is required.");
|
||||
if (!summaryAuth.value) throw new Error("ACCP-TS authorization is unavailable.");
|
||||
if (!srAuth.value) throw new Error("SPA-ORANGE uiaasHeader.value is unavailable.");
|
||||
|
||||
const summary = await fetchJson(ACCOUNT_SUMMARY_URL, {
|
||||
method: "POST", credentials: "include",
|
||||
headers: { Accept: "application/json", "Content-Type": "application/json", [summaryAuth.name]: summaryAuth.value },
|
||||
body: JSON.stringify([{ key: "regId", value: partyNumber }])
|
||||
}, "requestAccountSummary", debugRequests);
|
||||
const accountSummaryId = findAccountPlanId(summary);
|
||||
if ((typeof accountSummaryId !== "string" && typeof accountSummaryId !== "number") || !String(accountSummaryId).trim()) {
|
||||
throw new Error("Account id was not found in requestAccountSummary.");
|
||||
}
|
||||
|
||||
const items = [];
|
||||
let offset = 0;
|
||||
// This endpoint uses totalResults/count rather than hasMore.
|
||||
for (let page = 0; page < 1000; page += 1) {
|
||||
const url = `${endpoint}${encodeURIComponent(String(accountSummaryId))}?customerSRFilter=${filter}&orderBy=creationDate&orderByMode=desc&limit=40&offset=${offset}`;
|
||||
const result = await fetchJson(url, {
|
||||
method: "GET", credentials: "include",
|
||||
// SPA-ORANGE's label may be lead-auth; this provider requires custsr-auth.
|
||||
headers: { Accept: "application/json", "custsr-auth": srAuth.value }
|
||||
}, "requestCustomerServiceRequest", debugRequests);
|
||||
if (!result || !Array.isArray(result.objects)
|
||||
|| result.objects.some(item => !item || typeof item !== "object" || Array.isArray(item))
|
||||
|| !Number.isSafeInteger(result.totalResults) || result.totalResults < 0
|
||||
|| !Number.isSafeInteger(result.count) || result.count !== result.objects.length) {
|
||||
throw new Error("requestCustomerServiceRequest returned an invalid collection.");
|
||||
}
|
||||
if (result.offset !== offset) throw new Error("requestCustomerServiceRequest returned an unexpected offset.");
|
||||
items.push(...result.objects);
|
||||
offset += result.count;
|
||||
if (offset >= result.totalResults) return { ok: true, items, debugRequests };
|
||||
if (!result.count) throw new Error("requestCustomerServiceRequest pagination made no progress.");
|
||||
}
|
||||
throw new Error("requestCustomerServiceRequest pagination limit reached.");
|
||||
} catch (error) {
|
||||
error.debugRequests = debugRequests;
|
||||
throw error;
|
||||
} finally {
|
||||
for (const entry of debugRequests) {
|
||||
for (const key of Object.keys(entry.requestHeaders || {})) {
|
||||
if (!/^(accept|content-type)$/i.test(key)) entry.requestHeaders[key] = "(present)";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return { requestCustomerServiceRequest };
|
||||
}
|
||||
Reference in New Issue
Block a user