UNPKG

@atlaskit/profilecard

Version:

A React component to display a card with user information.

343 lines (342 loc) 12.6 kB
import { fg } from '@atlaskit/platform-feature-flags'; import { PACKAGE_META_DATA } from '../util/analytics'; import { getPageTime } from '../util/performance'; import CachingClient from './CachingClient'; import { getErrorAttributes } from './errorUtils'; import { AGGQuery } from './graphqlUtils'; const buildActivationIdQuery = (cloudId, product) => ({ query: ` query RovoAgentProfileCard_ActivationQuery($cloudId: ID!, $product: String!) { tenantContexts(cloudIds: [$cloudId]) { activationIdByProduct(product: $product) { active } } } `, variables: { cloudId, product } }); const buildRovoAgentQueryByAri = agentAri => ({ query: ` query RovoAgentProfileCard_AgentQueryByAri($agentAri: ID!) { agentStudio_agentById(id: $agentAri) @optIn(to: "AgentStudio") { __typename ... on AgentStudioAssistant { authoringTeam { displayName } } ... on QueryError { message } } } `, variables: { agentAri } }); const buildRovoAgentQueryByAccountId = (identityAccountId, cloudId) => ({ query: ` query RovoAgentProfileCard_AgentQueryByAccountId($identityAccountId: ID!, $cloudId: ID!) { agentStudio_agentByIdentityAccountId(identityAccountId: $identityAccountId, cloudId: $cloudId) @optIn(to: "AgentStudio") { __typename ... on AgentStudioAssistant { authoringTeam { displayName profileUrl } } ... on QueryError { message } } } `, variables: { identityAccountId, cloudId } }); const createHeaders = (product, cloudId, isBodyJson) => { const headers = new Headers({ 'X-Product': product, 'X-Experience-Id': 'profile-card', 'X-Cloudid': cloudId || '' }); if (isBodyJson) { headers.set('Content-Type', 'application/json'); } return headers; }; export default class RovoAgentCardClient extends CachingClient { constructor(options) { super(options); this.options = options; } basePath() { return fg('pt-deprecate-assistance-service') ? '/gateway/api/assist/rovo/v1/agents' : '/gateway/api/assist/agents/v1'; } async getActivationId(cloudId, product) { var _response$tenantConte, _response$tenantConte2, _response$tenantConte3, _response$tenantConte4; const response = await AGGQuery('/gateway/api/graphql', buildActivationIdQuery(cloudId, product)); return (_response$tenantConte = response === null || response === void 0 ? void 0 : (_response$tenantConte2 = response.tenantContexts) === null || _response$tenantConte2 === void 0 ? void 0 : (_response$tenantConte3 = _response$tenantConte2[0]) === null || _response$tenantConte3 === void 0 ? void 0 : (_response$tenantConte4 = _response$tenantConte3.activationIdByProduct) === null || _response$tenantConte4 === void 0 ? void 0 : _response$tenantConte4.active) !== null && _response$tenantConte !== void 0 ? _response$tenantConte : null; } async getAgentByARIAgg(agentAri) { var _response$agentStudio; const response = await AGGQuery('/gateway/api/graphql', buildRovoAgentQueryByAri(agentAri)); if (((_response$agentStudio = response.agentStudio_agentById) === null || _response$agentStudio === void 0 ? void 0 : _response$agentStudio.__typename) === 'QueryError') { throw new Error(`ProfileCard agentStudio_agentById returning QueryError: ${response.agentStudio_agentById.message}`); } return response === null || response === void 0 ? void 0 : response.agentStudio_agentById; } async getAgentByAccountIdAgg(identityAccountId, cloudId) { var _response$agentStudio2; const response = await AGGQuery('/gateway/api/graphql', buildRovoAgentQueryByAccountId(identityAccountId, cloudId)); if (((_response$agentStudio2 = response.agentStudio_agentByIdentityAccountId) === null || _response$agentStudio2 === void 0 ? void 0 : _response$agentStudio2.__typename) === 'QueryError') { throw new Error(`ProfileCard agentStudio_agentByIdentityAccountId returning QueryError: ${response.agentStudio_agentByIdentityAccountId.message}`); } return response === null || response === void 0 ? void 0 : response.agentStudio_agentByIdentityAccountId; } makeRequest(id, analytics) { const product = this.options.productIdentifier || 'rovo'; const headers = createHeaders(product, this.options.cloudId); let restPromise; if (id.type === 'identity') { restPromise = fetch(new Request(`${this.basePath()}/accountid/${id.value}`, { method: 'GET', credentials: 'include', mode: 'cors', headers })).then(response => response.json()); } else { restPromise = fetch(new Request(`${this.basePath()}/${id.value}`, { method: 'GET', credentials: 'include', mode: 'cors', headers })).then(response => response.json()); } const aggStartTime = getPageTime(); const aggPromise = this.getActivationId(this.options.cloudId || '', this.options.productIdentifier || 'rovo').then(activationId => { if (!activationId) { throw new Error('ProfileCard Activation ID not found'); } if (id.type === 'identity') { return this.getAgentByAccountIdAgg(id.value, this.options.cloudId || ''); } else { const agentAri = `ari:cloud:rovo::agent/activation/${activationId}/${id.value}`; return this.getAgentByARIAgg(agentAri); } }) // We are not going to break the flow if the AGG endpoint fails for now // @TODO once all the data moved to AGG, we can remove this catch .catch(error => { if (analytics) { analytics('operational.rovoAgentProfilecard.failed.request', { ...getErrorAttributes(error), errorType: 'RovoAgentProfileCardAggError', duration: getPageTime() - aggStartTime, gateway: true, firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } return Promise.resolve(null); }); return Promise.all([restPromise, aggPromise]).then(([restData, aggData]) => ({ restData, aggData })); } /** * This function will call both REST and AGG endpoints to get the agent profile * There are some data that is only available in the AGG endpoint, so we need to call both * For any new fields, please only add them to the AGG endpoint * * @TODO migrate everything to AGG endpoint */ getProfile(id, analytics) { if (!id.value) { return Promise.reject(new Error('Id is missing')); } if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } const cache = this.getCachedProfile(id.value); if (cache) { return Promise.resolve(cache); } return new Promise((resolve, reject) => { const startTime = getPageTime(); if (analytics) { analytics('operational.rovoAgentProfilecard.triggered.request', { firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } this.makeRequest(id, analytics).then(data => { if (this.cache) { this.setCachedProfile(id.value, data); } if (analytics) { analytics('operational.rovoAgentProfilecard.succeeded.request', { duration: getPageTime() - startTime, gateway: true, firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } resolve(data); }).catch(error => { if (analytics) { analytics('operational.rovoAgentProfilecard.failed.request', { duration: getPageTime() - startTime, ...getErrorAttributes(error), gateway: true, firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } reject(error); }); }); } deleteAgent(agentId, analytics) { if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } return new Promise((resolve, reject) => { const startTime = getPageTime(); const product = this.options.productIdentifier || 'rovo'; if (analytics) { analytics('operational.rovoAgentProfilecard.triggered.request', { firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } const headers = createHeaders(product, this.options.cloudId); fetch(new Request(`${this.basePath()}/${agentId}`, { method: 'DELETE', credentials: 'include', mode: 'cors', headers })).then(() => { if (analytics) { analytics('operational.rovoAgentProfilecard.succeeded.deleteAgent', { duration: getPageTime() - startTime, gateway: true, firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } resolve(); }).catch(error => { if (analytics) { analytics('operational.rovoAgentProfilecard.failed.deleteAgent', { duration: getPageTime() - startTime, ...getErrorAttributes(error), gateway: true, firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } reject(error); }); }); } setFavouriteAgent(agentId, isFavourite, analytics) { if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } return new Promise(async (resolve, reject) => { const startTime = getPageTime(); const product = this.options.productIdentifier || 'rovo'; const actionSubjectId = isFavourite ? 'favourite' : 'unfavourite'; const requestMethod = isFavourite ? 'POST' : 'DELETE'; if (analytics) { analytics(`operational.rovoAgentProfilecard.triggered.${actionSubjectId}`, { firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } const headers = createHeaders(product, this.options.cloudId); await fetch(new Request(`${this.basePath()}/${agentId}/favourite`, { method: requestMethod, credentials: 'include', mode: 'cors', headers })).then(() => { if (analytics) { analytics(`operational.rovoAgentProfilecard.succeeded.${actionSubjectId}`, { duration: getPageTime() - startTime, gateway: true, firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } resolve(); }).catch(error => { if (analytics) { analytics(`operational.rovoAgentProfilecard.failed.${actionSubjectId}`, { duration: getPageTime() - startTime, ...getErrorAttributes(error), gateway: true, firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } reject(error); }); }); } getPermissions(id, fireAnalytics) { if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } return new Promise((resolve, reject) => { const startTime = getPageTime(); const product = this.options.productIdentifier || 'rovo'; if (fireAnalytics) { fireAnalytics('operational.rovoAgentProfilecard.triggered.request', { firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } const headers = createHeaders(product, this.options.cloudId, true); fetch(new Request(`/gateway/api/assist/api/rovo/v2/permissions/agents/${id}`, { method: 'POST', credentials: 'include', mode: 'cors', headers, body: JSON.stringify({ permission_ids: ['AGENT_CREATE', 'AGENT_UPDATE', 'AGENT_DELETE', 'AGENT_DEACTIVATE', 'AGENT_READ'] }) })).then(response => response.json()).then(data => { if (fireAnalytics) { fireAnalytics('operational.rovoAgentProfilecard.succeeded.getAgentPermissions', { duration: getPageTime() - startTime, gateway: true, firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } resolve(data); }).catch(error => { if (fireAnalytics) { fireAnalytics('operational.rovoAgentProfilecard.failed.getAgentPermissions', { duration: getPageTime() - startTime, ...getErrorAttributes(error), gateway: true, firedAt: Math.round(getPageTime()), ...PACKAGE_META_DATA }); } reject(error); }); }); } }