Implementação da chamada para obtenção do Resource Current User

This commit is contained in:
Paulo Porto
2026-04-30 17:24:19 -03:00
parent ca839345bc
commit 655d60b5ff
3 changed files with 157 additions and 23 deletions

View File

@@ -7,6 +7,8 @@ const COMCIP_CLIENT_ID_PROBE_URL =
`${COMCIP_ORIGIN}/ic/builder/rt/oalset_semc/live;profile=PROD/services/auth/1.1/proxy/oalsetSeaaSOKECustomRestAPI/uri/https/gxpap.oracle.com/oalcrm/service/set/seaas/crm/countries`; `${COMCIP_ORIGIN}/ic/builder/rt/oalset_semc/live;profile=PROD/services/auth/1.1/proxy/oalsetSeaaSOKECustomRestAPI/uri/https/gxpap.oracle.com/oalcrm/service/set/seaas/crm/countries`;
const COMCIP_TAB_URL_PATTERN = const COMCIP_TAB_URL_PATTERN =
`${COMCIP_ORIGIN}/ic/builder/rt/oalset_semc/live*`; `${COMCIP_ORIGIN}/ic/builder/rt/oalset_semc/live*`;
const COMCIP_TIME_ENTRY_TAB_URL_PATTERN =
`${COMCIP_ORIGIN}/ic/builder/rt/oalset_timeentrymobile/live*`;
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => { chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if ( if (
@@ -36,18 +38,35 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
}); });
async function requestComcipFromPage({ url, method = "POST", payload, headers = {} }) { async function requestComcipFromPage({ url, method = "POST", payload, headers = {} }) {
const target = await getComcipTab(); const route = getComcipRoute(url);
const activeTab = await captureActiveTab();
const target = await getComcipTab(route, activeTab);
try { try {
await restoreActiveTab(activeTab);
await waitForComcipTabReady(target.tab.id); await waitForComcipTabReady(target.tab.id);
await waitForComcipSessionReady(target.tab.id, headers); await waitForComcipSessionReady(target.tab.id, headers, route);
let response = await executeComcipFetch(target.tab.id, url, method, payload, headers); let response = await executeComcipFetch(
target.tab.id,
url,
method,
payload,
headers,
route
);
if (isAuthorizationFailure(response)) { if (isAuthorizationFailure(response)) {
await delay(1500); await delay(1500);
await waitForComcipSessionReady(target.tab.id, headers); await waitForComcipSessionReady(target.tab.id, headers, route);
response = await executeComcipFetch(target.tab.id, url, method, payload, headers); response = await executeComcipFetch(
target.tab.id,
url,
method,
payload,
headers,
route
);
} }
return response; return response;
@@ -55,11 +74,31 @@ async function requestComcipFromPage({ url, method = "POST", payload, headers =
if (target.created && target.tab.id) { if (target.created && target.tab.id) {
await chrome.tabs.remove(target.tab.id).catch(() => {}); await chrome.tabs.remove(target.tab.id).catch(() => {});
} }
await restoreActiveTab(activeTab);
} }
} }
async function getComcipTab() { function getComcipRoute(requestUrl) {
const tabs = await chrome.tabs.query({ url: COMCIP_TAB_URL_PATTERN }); const normalizedUrl = String(requestUrl || "");
if (normalizedUrl.includes("/ic/builder/rt/oalset_timeentrymobile/live")) {
return {
appUrl: normalizedUrl,
clientIdProbeUrl: normalizedUrl,
tabPattern: COMCIP_TIME_ENTRY_TAB_URL_PATTERN,
};
}
return {
appUrl: COMCIP_APP_URL,
clientIdProbeUrl: COMCIP_CLIENT_ID_PROBE_URL,
tabPattern: COMCIP_TAB_URL_PATTERN,
};
}
async function getComcipTab(route, activeTab) {
const tabs = await chrome.tabs.query({ url: route.tabPattern });
const existing = tabs.find((tab) => tab.id && !tab.discarded); const existing = tabs.find((tab) => tab.id && !tab.discarded);
if (existing) { if (existing) {
@@ -70,9 +109,11 @@ async function getComcipTab() {
} }
const created = await chrome.tabs.create({ const created = await chrome.tabs.create({
url: COMCIP_APP_URL, url: route.appUrl,
active: false, active: false,
windowId: activeTab?.windowId,
}); });
await restoreActiveTab(activeTab);
return { return {
tab: created, tab: created,
@@ -80,6 +121,31 @@ async function getComcipTab() {
}; };
} }
async function captureActiveTab() {
const [activeTab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});
if (!activeTab?.id) {
return null;
}
return {
id: activeTab.id,
windowId: activeTab.windowId,
};
}
async function restoreActiveTab(activeTab) {
if (!activeTab?.id) {
return;
}
await chrome.windows.update(activeTab.windowId, { focused: true }).catch(() => {});
await chrome.tabs.update(activeTab.id, { active: true }).catch(() => {});
}
async function waitForComcipTabReady(tabId) { async function waitForComcipTabReady(tabId) {
const startedAt = Date.now(); const startedAt = Date.now();
@@ -98,12 +164,12 @@ async function waitForComcipTabReady(tabId) {
throw new Error("Timed out while loading the COMCIP origin page."); throw new Error("Timed out while loading the COMCIP origin page.");
} }
async function waitForComcipSessionReady(tabId, headers = {}) { async function waitForComcipSessionReady(tabId, headers = {}, route) {
const startedAt = Date.now(); const startedAt = Date.now();
let lastStatus = ""; let lastStatus = "";
while (Date.now() - startedAt < 60000) { while (Date.now() - startedAt < 60000) {
const probe = await executeComcipProbe(tabId, headers).catch((error) => ({ const probe = await executeComcipProbe(tabId, headers, route).catch((error) => ({
ok: false, ok: false,
status: 0, status: 0,
error: error instanceof Error ? error.message : String(error), error: error instanceof Error ? error.message : String(error),
@@ -122,11 +188,11 @@ async function waitForComcipSessionReady(tabId, headers = {}) {
throw new Error(`Timed out while waiting for COMCIP authenticated session (${lastStatus}).`); throw new Error(`Timed out while waiting for COMCIP authenticated session (${lastStatus}).`);
} }
async function executeComcipProbe(tabId, headers = {}) { async function executeComcipProbe(tabId, headers = {}, route) {
const [injectionResult] = await chrome.scripting.executeScript({ const [injectionResult] = await chrome.scripting.executeScript({
target: { tabId }, target: { tabId },
world: "MAIN", world: "MAIN",
args: [COMCIP_CLIENT_ID_PROBE_URL, headers || {}], args: [route?.clientIdProbeUrl || COMCIP_CLIENT_ID_PROBE_URL, headers || {}],
func: async (probeUrl, sourceHeaders) => { func: async (probeUrl, sourceHeaders) => {
const APP_VERSION = "version_1754044416761"; const APP_VERSION = "version_1754044416761";
const response = await fetch(probeUrl, { const response = await fetch(probeUrl, {
@@ -160,15 +226,25 @@ async function executeComcipProbe(tabId, headers = {}) {
return injectionResult.result; return injectionResult.result;
} }
async function executeComcipFetch(tabId, requestUrl, method, payload, headers) { async function executeComcipFetch(tabId, requestUrl, method, payload, headers, route) {
const [injectionResult] = await chrome.scripting.executeScript({ const [injectionResult] = await chrome.scripting.executeScript({
target: { tabId }, target: { tabId },
world: "MAIN", world: "MAIN",
args: [requestUrl, method || "POST", payload || null, headers || {}], args: [
func: async (requestUrl, requestMethod, requestPayload, sourceHeaders) => { requestUrl,
method || "POST",
payload || null,
headers || {},
route?.clientIdProbeUrl || COMCIP_CLIENT_ID_PROBE_URL,
],
func: async (
requestUrl,
requestMethod,
requestPayload,
sourceHeaders,
clientIdProbeUrl
) => {
const APP_VERSION = "version_1754044416761"; const APP_VERSION = "version_1754044416761";
const CLIENT_ID_PROBE_URL =
`${window.location.origin}/ic/builder/rt/oalset_semc/live;profile=PROD/services/auth/1.1/proxy/oalsetSeaaSOKECustomRestAPI/uri/https/gxpap.oracle.com/oalcrm/service/set/seaas/crm/countries`;
function parseResponseText(text) { function parseResponseText(text) {
if (!text) { if (!text) {
@@ -206,7 +282,7 @@ async function executeComcipFetch(tabId, requestUrl, method, payload, headers) {
async function resolveAppBuilderClientId() { async function resolveAppBuilderClientId() {
try { try {
const response = await fetch(CLIENT_ID_PROBE_URL, { const response = await fetch(clientIdProbeUrl, {
method: "GET", method: "GET",
credentials: "include", credentials: "include",
cache: "no-store", cache: "no-store",

View File

@@ -76,6 +76,8 @@
"https://comcipapic-oalprod.integration.ocp.oraclecloud.com/ic/builder/rt/oalset_semc/live;profile=PROD/services/auth/1.1/proxy/oalsetCRMRestAPIElastic/uri/https/eeho.fa.us2.oraclecloud.com/crmRestApi/searchResources/latest/custom-actions/queries", "https://comcipapic-oalprod.integration.ocp.oraclecloud.com/ic/builder/rt/oalset_semc/live;profile=PROD/services/auth/1.1/proxy/oalsetCRMRestAPIElastic/uri/https/eeho.fa.us2.oraclecloud.com/crmRestApi/searchResources/latest/custom-actions/queries",
timeEntriesSummaryUrl: timeEntriesSummaryUrl:
"https://comcipapic-oalprod.integration.ocp.oraclecloud.com/ic/builder/rt/oalset_semc/live;profile=PROD/services/auth/1.1/proxy/oalsetSeaaSOKECustomRestAPI/uri/https/gxpap.oracle.com/oalcrm/service/set/seaas/mgmt/timeEntries/summary", "https://comcipapic-oalprod.integration.ocp.oraclecloud.com/ic/builder/rt/oalset_semc/live;profile=PROD/services/auth/1.1/proxy/oalsetSeaaSOKECustomRestAPI/uri/https/gxpap.oracle.com/oalcrm/service/set/seaas/mgmt/timeEntries/summary",
resourceCurrentUserUrl:
"https://comcipapic-oalprod.integration.ocp.oraclecloud.com/ic/builder/rt/oalset_timeentrymobile/live;profile=PROD/services/auth/1.1/proxy/oke_seaas/uri/https/gxpap.oracle.com/oalcrm/service/set/seaas/crm/resourceUsers",
appVersion: "version_1754044416761", appVersion: "version_1754044416761",
}; };
@@ -2097,6 +2099,12 @@
const user = await fetchCurrentUser(); const user = await fetchCurrentUser();
updateLoadingProgress("Loading current resource user", 8);
const resourceCurrentUser = await fetchResourceCurrentUser(user.userEmail).catch(
() => null
);
updateLoadingProgress("Loading customer summary pages", 12); updateLoadingProgress("Loading customer summary pages", 12);
const customers = await fetchAllCustomers(user.userEmail); const customers = await fetchAllCustomers(user.userEmail);
@@ -2208,13 +2216,16 @@
updateLoadingProgress("Loading pending delivery service requests", 90); updateLoadingProgress("Loading pending delivery service requests", 90);
const pendingServiceRequests = await fetchPendingServiceRequests(); const pendingServiceRequests = await fetchPendingServiceRequests().catch(
() => []
);
updateLoadingProgress("Preparing dashboard cache", 96); updateLoadingProgress("Preparing dashboard cache", 96);
const finalCustomers = Array.from(customersMap.values()); const finalCustomers = Array.from(customersMap.values());
const exportPayload = createExportPayload( const exportPayload = createExportPayload(
user, user,
finalCustomers, finalCustomers,
pendingServiceRequests pendingServiceRequests,
resourceCurrentUser
); );
const dataset = createDatasetSnapshot(exportPayload); const dataset = createDatasetSnapshot(exportPayload);
@@ -2686,6 +2697,39 @@
}; };
} }
async function fetchResourceCurrentUser(userEmail) {
const normalizedUserEmail = cleanString(userEmail);
if (!normalizedUserEmail) {
throw new Error("Unable to request resource current user without userEmail.");
}
const url = `${COMCIP_REQUEST.resourceCurrentUserUrl}?emailAddress=${encodeURIComponent(
normalizedUserEmail
)}`;
const response = await sendComcipGetMessage(url);
if (!response?.ok) {
const payloadMessage =
typeof response?.payload === "string"
? cleanString(response.payload)
: firstNonEmptyString([
response?.payload?.message,
response?.payload?.error,
response?.payload?.title,
response?.payload?.detail,
]);
throw new Error(
payloadMessage ||
response?.error ||
`COMCIP resource user request failed (${response?.status || 0}).`
);
}
return response.payload;
}
async function fetchAllCustomers(userEmail) { async function fetchAllCustomers(userEmail) {
const limit = 49; const limit = 49;
@@ -3142,10 +3186,16 @@
}; };
} }
function createExportPayload(user, customers, pendingServiceRequests = []) { function createExportPayload(
user,
customers,
pendingServiceRequests = [],
resourceCurrentUser = null
) {
return { return {
generatedAt: new Date().toISOString(), generatedAt: new Date().toISOString(),
user, user,
resourceCurrentUser,
customers, customers,
pendingServiceRequests, pendingServiceRequests,
}; };
@@ -3156,6 +3206,7 @@
return { return {
user: normalizedPayload.user, user: normalizedPayload.user,
resourceCurrentUser: normalizedPayload.resourceCurrentUser,
customers: normalizedPayload.customers, customers: normalizedPayload.customers,
exportPayload: normalizedPayload, exportPayload: normalizedPayload,
summary: buildSummary(normalizedPayload), summary: buildSummary(normalizedPayload),
@@ -3262,6 +3313,7 @@
]) || "", ]) || "",
administrator: Boolean(exportPayload?.user?.administrator), administrator: Boolean(exportPayload?.user?.administrator),
}; };
const resourceCurrentUser = exportPayload?.resourceCurrentUser ?? null;
const normalizedCustomers = extractArray(exportPayload?.customers, ["customers"]) const normalizedCustomers = extractArray(exportPayload?.customers, ["customers"])
.map((customer) => ({ .map((customer) => ({
customerId: cleanString(customer?.customerId || customer?.id), customerId: cleanString(customer?.customerId || customer?.id),
@@ -3284,6 +3336,7 @@
generatedAt: generatedAt:
firstNonEmptyString([exportPayload?.generatedAt]) || new Date().toISOString(), firstNonEmptyString([exportPayload?.generatedAt]) || new Date().toISOString(),
user: normalizedUser, user: normalizedUser,
resourceCurrentUser,
customers: normalizedCustomers, customers: normalizedCustomers,
pendingServiceRequests, pendingServiceRequests,
}; };
@@ -3916,7 +3969,8 @@
const exportPayload = createExportPayload( const exportPayload = createExportPayload(
appState.dataset.user, appState.dataset.user,
updatedCustomers, updatedCustomers,
appState.dataset.exportPayload?.pendingServiceRequests || [] appState.dataset.exportPayload?.pendingServiceRequests || [],
appState.dataset.exportPayload?.resourceCurrentUser || null
); );
const nextDataset = createDatasetSnapshot(exportPayload); const nextDataset = createDatasetSnapshot(exportPayload);
const previousDetailModal = appState.detailModal const previousDetailModal = appState.detailModal

View File

@@ -1,7 +1,7 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "Arch Panel Injector", "name": "Arch Panel Injector",
"version": "1.0.3", "version": "1.0.7",
"description": "Insere o botao Arch panel no header do Oracle Workload Workbench.", "description": "Insere o botao Arch panel no header do Oracle Workload Workbench.",
"permissions": [ "permissions": [
"cookies", "cookies",
@@ -40,3 +40,7 @@