UNPKG

@atlaskit/profilecard

Version:

A React component to display a card with user information.

426 lines (425 loc) 19.7 kB
import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _inherits from "@babel/runtime/helpers/inherits"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import _regeneratorRuntime from "@babel/runtime/regenerator"; function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); } function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } 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'; var buildActivationIdQuery = function buildActivationIdQuery(cloudId, product) { return { query: "\n\t\tquery RovoAgentProfileCard_ActivationQuery($cloudId: ID!, $product: String!) {\n\t\t\ttenantContexts(cloudIds: [$cloudId]) {\n\t\t\t\tactivationIdByProduct(product: $product) {\n\t\t\t\t\tactive\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t", variables: { cloudId: cloudId, product: product } }; }; var buildRovoAgentQueryByAri = function buildRovoAgentQueryByAri(agentAri) { return { query: "\n\t query RovoAgentProfileCard_AgentQueryByAri($agentAri: ID!) {\n\t\t\tagentStudio_agentById(id: $agentAri) @optIn(to: \"AgentStudio\") {\n\t\t\t __typename\n\t\t\t\t... on AgentStudioAssistant {\n\t\t\t\t\tauthoringTeam {\n\t\t\t\t\t\tdisplayName\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t... on QueryError {\n\t\t\t\t\tmessage\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t", variables: { agentAri: agentAri } }; }; var buildRovoAgentQueryByAccountId = function buildRovoAgentQueryByAccountId(identityAccountId, cloudId) { return { query: "\n\t\tquery RovoAgentProfileCard_AgentQueryByAccountId($identityAccountId: ID!, $cloudId: ID!) {\n\t\t\tagentStudio_agentByIdentityAccountId(identityAccountId: $identityAccountId, cloudId: $cloudId) @optIn(to: \"AgentStudio\") {\n\t\t\t __typename\n\t\t\t\t... on AgentStudioAssistant {\n\t\t\t\t\tauthoringTeam {\n\t\t\t\t\t\tdisplayName\n\t\t\t\t\t\tprofileUrl\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t... on QueryError {\n\t\t\t\t\tmessage\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t", variables: { identityAccountId: identityAccountId, cloudId: cloudId } }; }; var createHeaders = function createHeaders(product, cloudId, isBodyJson) { var headers = new Headers({ 'X-Product': product, 'X-Experience-Id': 'profile-card', 'X-Cloudid': cloudId || '' }); if (isBodyJson) { headers.set('Content-Type', 'application/json'); } return headers; }; var RovoAgentCardClient = /*#__PURE__*/function (_CachingClient) { function RovoAgentCardClient(options) { var _this; _classCallCheck(this, RovoAgentCardClient); _this = _callSuper(this, RovoAgentCardClient, [options]); _this.options = options; return _this; } _inherits(RovoAgentCardClient, _CachingClient); return _createClass(RovoAgentCardClient, [{ key: "basePath", value: function basePath() { return fg('pt-deprecate-assistance-service') ? '/gateway/api/assist/rovo/v1/agents' : '/gateway/api/assist/agents/v1'; } }, { key: "getActivationId", value: function () { var _getActivationId = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(cloudId, product) { var _response$tenantConte, _response$tenantConte2; var response; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.next = 2; return AGGQuery('/gateway/api/graphql', buildActivationIdQuery(cloudId, product)); case 2: response = _context.sent; return _context.abrupt("return", (_response$tenantConte = response === null || response === void 0 || (_response$tenantConte2 = response.tenantContexts) === null || _response$tenantConte2 === void 0 || (_response$tenantConte2 = _response$tenantConte2[0]) === null || _response$tenantConte2 === void 0 || (_response$tenantConte2 = _response$tenantConte2.activationIdByProduct) === null || _response$tenantConte2 === void 0 ? void 0 : _response$tenantConte2.active) !== null && _response$tenantConte !== void 0 ? _response$tenantConte : null); case 4: case "end": return _context.stop(); } }, _callee); })); function getActivationId(_x, _x2) { return _getActivationId.apply(this, arguments); } return getActivationId; }() }, { key: "getAgentByARIAgg", value: function () { var _getAgentByARIAgg = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(agentAri) { var _response$agentStudio; var response; return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return AGGQuery('/gateway/api/graphql', buildRovoAgentQueryByAri(agentAri)); case 2: response = _context2.sent; if (!(((_response$agentStudio = response.agentStudio_agentById) === null || _response$agentStudio === void 0 ? void 0 : _response$agentStudio.__typename) === 'QueryError')) { _context2.next = 5; break; } throw new Error("ProfileCard agentStudio_agentById returning QueryError: ".concat(response.agentStudio_agentById.message)); case 5: return _context2.abrupt("return", response === null || response === void 0 ? void 0 : response.agentStudio_agentById); case 6: case "end": return _context2.stop(); } }, _callee2); })); function getAgentByARIAgg(_x3) { return _getAgentByARIAgg.apply(this, arguments); } return getAgentByARIAgg; }() }, { key: "getAgentByAccountIdAgg", value: function () { var _getAgentByAccountIdAgg = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(identityAccountId, cloudId) { var _response$agentStudio2; var response; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return AGGQuery('/gateway/api/graphql', buildRovoAgentQueryByAccountId(identityAccountId, cloudId)); case 2: response = _context3.sent; if (!(((_response$agentStudio2 = response.agentStudio_agentByIdentityAccountId) === null || _response$agentStudio2 === void 0 ? void 0 : _response$agentStudio2.__typename) === 'QueryError')) { _context3.next = 5; break; } throw new Error("ProfileCard agentStudio_agentByIdentityAccountId returning QueryError: ".concat(response.agentStudio_agentByIdentityAccountId.message)); case 5: return _context3.abrupt("return", response === null || response === void 0 ? void 0 : response.agentStudio_agentByIdentityAccountId); case 6: case "end": return _context3.stop(); } }, _callee3); })); function getAgentByAccountIdAgg(_x4, _x5) { return _getAgentByAccountIdAgg.apply(this, arguments); } return getAgentByAccountIdAgg; }() }, { key: "makeRequest", value: function makeRequest(id, analytics) { var _this2 = this; var product = this.options.productIdentifier || 'rovo'; var headers = createHeaders(product, this.options.cloudId); var restPromise; if (id.type === 'identity') { restPromise = fetch(new Request("".concat(this.basePath(), "/accountid/").concat(id.value), { method: 'GET', credentials: 'include', mode: 'cors', headers: headers })).then(function (response) { return response.json(); }); } else { restPromise = fetch(new Request("".concat(this.basePath(), "/").concat(id.value), { method: 'GET', credentials: 'include', mode: 'cors', headers: headers })).then(function (response) { return response.json(); }); } var aggStartTime = getPageTime(); var aggPromise = this.getActivationId(this.options.cloudId || '', this.options.productIdentifier || 'rovo').then(function (activationId) { if (!activationId) { throw new Error('ProfileCard Activation ID not found'); } if (id.type === 'identity') { return _this2.getAgentByAccountIdAgg(id.value, _this2.options.cloudId || ''); } else { var agentAri = "ari:cloud:rovo::agent/activation/".concat(activationId, "/").concat(id.value); return _this2.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(function (error) { if (analytics) { analytics('operational.rovoAgentProfilecard.failed.request', _objectSpread(_objectSpread({}, 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(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), restData = _ref2[0], aggData = _ref2[1]; return { restData: restData, aggData: 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 */ }, { key: "getProfile", value: function getProfile(id, analytics) { var _this3 = this; if (!id.value) { return Promise.reject(new Error('Id is missing')); } if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } var cache = this.getCachedProfile(id.value); if (cache) { return Promise.resolve(cache); } return new Promise(function (resolve, reject) { var startTime = getPageTime(); if (analytics) { analytics('operational.rovoAgentProfilecard.triggered.request', _objectSpread({ firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } _this3.makeRequest(id, analytics).then(function (data) { if (_this3.cache) { _this3.setCachedProfile(id.value, data); } if (analytics) { analytics('operational.rovoAgentProfilecard.succeeded.request', _objectSpread({ duration: getPageTime() - startTime, gateway: true, firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } resolve(data); }).catch(function (error) { if (analytics) { analytics('operational.rovoAgentProfilecard.failed.request', _objectSpread(_objectSpread({ duration: getPageTime() - startTime }, getErrorAttributes(error)), {}, { gateway: true, firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } reject(error); }); }); } }, { key: "deleteAgent", value: function deleteAgent(agentId, analytics) { var _this4 = this; if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } return new Promise(function (resolve, reject) { var startTime = getPageTime(); var product = _this4.options.productIdentifier || 'rovo'; if (analytics) { analytics('operational.rovoAgentProfilecard.triggered.request', _objectSpread({ firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } var headers = createHeaders(product, _this4.options.cloudId); fetch(new Request("".concat(_this4.basePath(), "/").concat(agentId), { method: 'DELETE', credentials: 'include', mode: 'cors', headers: headers })).then(function () { if (analytics) { analytics('operational.rovoAgentProfilecard.succeeded.deleteAgent', _objectSpread({ duration: getPageTime() - startTime, gateway: true, firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } resolve(); }).catch(function (error) { if (analytics) { analytics('operational.rovoAgentProfilecard.failed.deleteAgent', _objectSpread(_objectSpread({ duration: getPageTime() - startTime }, getErrorAttributes(error)), {}, { gateway: true, firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } reject(error); }); }); } }, { key: "setFavouriteAgent", value: function setFavouriteAgent(agentId, isFavourite, analytics) { var _this5 = this; if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } return new Promise( /*#__PURE__*/function () { var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(resolve, reject) { var startTime, product, actionSubjectId, requestMethod, headers; return _regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: startTime = getPageTime(); product = _this5.options.productIdentifier || 'rovo'; actionSubjectId = isFavourite ? 'favourite' : 'unfavourite'; requestMethod = isFavourite ? 'POST' : 'DELETE'; if (analytics) { analytics("operational.rovoAgentProfilecard.triggered.".concat(actionSubjectId), _objectSpread({ firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } headers = createHeaders(product, _this5.options.cloudId); _context4.next = 8; return fetch(new Request("".concat(_this5.basePath(), "/").concat(agentId, "/favourite"), { method: requestMethod, credentials: 'include', mode: 'cors', headers: headers })).then(function () { if (analytics) { analytics("operational.rovoAgentProfilecard.succeeded.".concat(actionSubjectId), _objectSpread({ duration: getPageTime() - startTime, gateway: true, firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } resolve(); }).catch(function (error) { if (analytics) { analytics("operational.rovoAgentProfilecard.failed.".concat(actionSubjectId), _objectSpread(_objectSpread({ duration: getPageTime() - startTime }, getErrorAttributes(error)), {}, { gateway: true, firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } reject(error); }); case 8: case "end": return _context4.stop(); } }, _callee4); })); return function (_x6, _x7) { return _ref3.apply(this, arguments); }; }()); } }, { key: "getPermissions", value: function getPermissions(id, fireAnalytics) { var _this6 = this; if (!this.options.cloudId) { return Promise.reject(new Error('cloudId is missing')); } return new Promise(function (resolve, reject) { var startTime = getPageTime(); var product = _this6.options.productIdentifier || 'rovo'; if (fireAnalytics) { fireAnalytics('operational.rovoAgentProfilecard.triggered.request', _objectSpread({ firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } var headers = createHeaders(product, _this6.options.cloudId, true); fetch(new Request("/gateway/api/assist/api/rovo/v2/permissions/agents/".concat(id), { method: 'POST', credentials: 'include', mode: 'cors', headers: headers, body: JSON.stringify({ permission_ids: ['AGENT_CREATE', 'AGENT_UPDATE', 'AGENT_DELETE', 'AGENT_DEACTIVATE', 'AGENT_READ'] }) })).then(function (response) { return response.json(); }).then(function (data) { if (fireAnalytics) { fireAnalytics('operational.rovoAgentProfilecard.succeeded.getAgentPermissions', _objectSpread({ duration: getPageTime() - startTime, gateway: true, firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } resolve(data); }).catch(function (error) { if (fireAnalytics) { fireAnalytics('operational.rovoAgentProfilecard.failed.getAgentPermissions', _objectSpread(_objectSpread({ duration: getPageTime() - startTime }, getErrorAttributes(error)), {}, { gateway: true, firedAt: Math.round(getPageTime()) }, PACKAGE_META_DATA)); } reject(error); }); }); } }]); }(CachingClient); export { RovoAgentCardClient as default };