UNPKG

@atlaskit/profilecard

Version:

A React component to display a card with user information.

432 lines (430 loc) 21.1 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _platformFeatureFlags = require("@atlaskit/platform-feature-flags"); var _analytics = require("../util/analytics"); var _performance = require("../util/performance"); var _CachingClient2 = _interopRequireDefault(require("./CachingClient")); var _errorUtils = require("./errorUtils"); var _graphqlUtils = require("./graphqlUtils"); 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) { (0, _defineProperty2.default)(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; } function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(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; })(); } 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 = exports.default = /*#__PURE__*/function (_CachingClient) { function RovoAgentCardClient(options) { var _this; (0, _classCallCheck2.default)(this, RovoAgentCardClient); _this = _callSuper(this, RovoAgentCardClient, [options]); _this.options = options; return _this; } (0, _inherits2.default)(RovoAgentCardClient, _CachingClient); return (0, _createClass2.default)(RovoAgentCardClient, [{ key: "basePath", value: function basePath() { return (0, _platformFeatureFlags.fg)('pt-deprecate-assistance-service') ? '/gateway/api/assist/rovo/v1/agents' : '/gateway/api/assist/agents/v1'; } }, { key: "getActivationId", value: function () { var _getActivationId = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(cloudId, product) { var _response$tenantConte, _response$tenantConte2; var response; return _regenerator.default.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.next = 2; return (0, _graphqlUtils.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 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(agentAri) { var _response$agentStudio; var response; return _regenerator.default.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return (0, _graphqlUtils.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 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(identityAccountId, cloudId) { var _response$agentStudio2; var response; return _regenerator.default.wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return (0, _graphqlUtils.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 = (0, _performance.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({}, (0, _errorUtils.getErrorAttributes)(error)), {}, { errorType: 'RovoAgentProfileCardAggError', duration: (0, _performance.getPageTime)() - aggStartTime, gateway: true, firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.PACKAGE_META_DATA)); } return Promise.resolve(null); }); return Promise.all([restPromise, aggPromise]).then(function (_ref) { var _ref2 = (0, _slicedToArray2.default)(_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 = (0, _performance.getPageTime)(); if (analytics) { analytics('operational.rovoAgentProfilecard.triggered.request', _objectSpread({ firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.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: (0, _performance.getPageTime)() - startTime, gateway: true, firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.PACKAGE_META_DATA)); } resolve(data); }).catch(function (error) { if (analytics) { analytics('operational.rovoAgentProfilecard.failed.request', _objectSpread(_objectSpread({ duration: (0, _performance.getPageTime)() - startTime }, (0, _errorUtils.getErrorAttributes)(error)), {}, { gateway: true, firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.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 = (0, _performance.getPageTime)(); var product = _this4.options.productIdentifier || 'rovo'; if (analytics) { analytics('operational.rovoAgentProfilecard.triggered.request', _objectSpread({ firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.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: (0, _performance.getPageTime)() - startTime, gateway: true, firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.PACKAGE_META_DATA)); } resolve(); }).catch(function (error) { if (analytics) { analytics('operational.rovoAgentProfilecard.failed.deleteAgent', _objectSpread(_objectSpread({ duration: (0, _performance.getPageTime)() - startTime }, (0, _errorUtils.getErrorAttributes)(error)), {}, { gateway: true, firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.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 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(resolve, reject) { var startTime, product, actionSubjectId, requestMethod, headers; return _regenerator.default.wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: startTime = (0, _performance.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((0, _performance.getPageTime)()) }, _analytics.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: (0, _performance.getPageTime)() - startTime, gateway: true, firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.PACKAGE_META_DATA)); } resolve(); }).catch(function (error) { if (analytics) { analytics("operational.rovoAgentProfilecard.failed.".concat(actionSubjectId), _objectSpread(_objectSpread({ duration: (0, _performance.getPageTime)() - startTime }, (0, _errorUtils.getErrorAttributes)(error)), {}, { gateway: true, firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.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 = (0, _performance.getPageTime)(); var product = _this6.options.productIdentifier || 'rovo'; if (fireAnalytics) { fireAnalytics('operational.rovoAgentProfilecard.triggered.request', _objectSpread({ firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.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: (0, _performance.getPageTime)() - startTime, gateway: true, firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.PACKAGE_META_DATA)); } resolve(data); }).catch(function (error) { if (fireAnalytics) { fireAnalytics('operational.rovoAgentProfilecard.failed.getAgentPermissions', _objectSpread(_objectSpread({ duration: (0, _performance.getPageTime)() - startTime }, (0, _errorUtils.getErrorAttributes)(error)), {}, { gateway: true, firedAt: Math.round((0, _performance.getPageTime)()) }, _analytics.PACKAGE_META_DATA)); } reject(error); }); }); } }]); }(_CachingClient2.default);