Implemented the functionality to add a WL Action
This commit is contained in:
83
src/services/spa/action-options.js
Normal file
83
src/services/spa/action-options.js
Normal file
@@ -0,0 +1,83 @@
|
||||
function createSpaActionOptions({ fetchJson }) {
|
||||
const baseUrl = "https://spa.oracle.com/oalcrm/web/api/provider-proxy/wwb-provider/service/workbench-proxy/wwb/mgmt/workloads";
|
||||
const lookupKeys = ["WWB_ACTION_ROLE", "WWB_ACTION_NAME", "WWB_ACTION_TEAM", "WWB_ACTION_STATUS", "WWB_ACTION_COMPLEXITY"];
|
||||
async function request(message, name, path, normalize) {
|
||||
const debugRequests = [];
|
||||
try {
|
||||
const token = message.authorization?.value;
|
||||
if (typeof token !== "string" || !token.trim()) throw new Error("WWB-TS uiaasHeader.value is unavailable.");
|
||||
const result = await fetchJson(`${baseUrl}/${path()}`, {
|
||||
method: "GET", credentials: "include",
|
||||
headers: { Accept: "application/json", "Wwb-Provider-Authorization": token.trim() }
|
||||
}, name, debugRequests);
|
||||
if (debugRequests.at(-1)?.httpStatus !== 200) throw new Error(`${name} requires HTTP 200.`);
|
||||
return { ok: true, ...normalize(result), debugRequests };
|
||||
} catch (error) {
|
||||
const entry = debugRequests.at(-1);
|
||||
if (entry) { entry.status = "failed"; entry.error = error.message; }
|
||||
error.debugRequests = debugRequests;
|
||||
throw error;
|
||||
} finally {
|
||||
for (const entry of debugRequests) {
|
||||
if (entry.requestHeaders?.["Wwb-Provider-Authorization"]) entry.requestHeaders["Wwb-Provider-Authorization"] = "(present)";
|
||||
}
|
||||
}
|
||||
}
|
||||
function requestActionTeamMembers(message) {
|
||||
return request(message, "requestActionTeamMembers", () => {
|
||||
const id = String(message.workloadId ?? "").trim();
|
||||
if (!/^\d+$/.test(id)) throw new Error("A numeric RevnId is required.");
|
||||
return `${encodeURIComponent(id)}/teamMembers?includeLeads=true`;
|
||||
}, result => {
|
||||
if (!Array.isArray(result) || result.some(item => typeof item?.email !== "string" || !item.email.trim())) {
|
||||
throw new Error("requestActionTeamMembers returned an invalid collection.");
|
||||
}
|
||||
return { members: result.map(item => ({ email: item.email })) };
|
||||
});
|
||||
}
|
||||
function requestActionFindLookup(message) {
|
||||
return request(message, "requestActionFindLookup", () => "fndLookup", result => {
|
||||
const lookups = {};
|
||||
for (const key of lookupKeys) {
|
||||
if (!Array.isArray(result?.[key]) || result[key].some(item => typeof item?.code !== "string" || !item.code.trim()
|
||||
|| typeof item?.name !== "string" || !item.name.trim())) {
|
||||
throw new Error(`requestActionFindLookup returned an invalid ${key} collection.`);
|
||||
}
|
||||
lookups[key] = result[key].map(({ code, name }) => ({ code, name }));
|
||||
}
|
||||
return { lookups };
|
||||
});
|
||||
}
|
||||
async function requestMgmtAction(message) {
|
||||
const debugRequests = [];
|
||||
try {
|
||||
const token = message.authorization?.value;
|
||||
const payload = message.payload;
|
||||
const action = payload?.action;
|
||||
if (!token || typeof token !== "string") throw new Error("WWB-TS uiaasHeader.value is unavailable.");
|
||||
if (payload?.parentType !== "WORKLOAD" || !/^\d+$/.test(String(payload.parentId))) throw new Error("A numeric RevnId is required.");
|
||||
for (const key of ["ownerEmail", "role", "name", "team", "status", "complexity", "createdBy", "updatedBy"]) {
|
||||
if (typeof action?.[key] !== "string" || !action[key].trim()) throw new Error(`Missing ${key}.`);
|
||||
}
|
||||
for (const key of ["startDate", "endDate"]) {
|
||||
if (typeof action[key] !== "string" || !/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/.test(action[key]) || !Number.isFinite(Date.parse(action[key]))) throw new Error(`Invalid ${key}.`);
|
||||
}
|
||||
if (action.endDate < action.startDate) throw new Error("End date must not precede start date.");
|
||||
if (!Array.isArray(action.notes) || action.notes.some(note => typeof note !== "string")) throw new Error("Invalid notes.");
|
||||
const result = await fetchJson("https://spa.oracle.com/oalcrm/web/api/provider-proxy/wwb-provider/service/workbench-proxy/wwb/mgmt/actions", {
|
||||
method: "POST", credentials: "include",
|
||||
headers: { Accept: "application/json", "Content-Type": "application/json", "Wwb-Provider-Authorization": token.trim() },
|
||||
body: JSON.stringify(payload)
|
||||
}, "requestMgmtAction", debugRequests, { allowEmpty: true });
|
||||
return { ok: true, result, debugRequests };
|
||||
} catch (error) {
|
||||
error.debugRequests = debugRequests;
|
||||
throw error;
|
||||
} finally {
|
||||
for (const entry of debugRequests) {
|
||||
if (entry.requestHeaders?.["Wwb-Provider-Authorization"]) entry.requestHeaders["Wwb-Provider-Authorization"] = "(present)";
|
||||
}
|
||||
}
|
||||
}
|
||||
return { requestActionTeamMembers, requestActionFindLookup, requestMgmtAction };
|
||||
}
|
||||
Reference in New Issue
Block a user