diff --git a/src/modules/contact-center/modules/queues/modules/members/api/queueMembers.js b/src/modules/contact-center/modules/queues/modules/members/api/queueMembers.js index ce02fba60..0cbe4531c 100644 --- a/src/modules/contact-center/modules/queues/modules/members/api/queueMembers.js +++ b/src/modules/contact-center/modules/queues/modules/members/api/queueMembers.js @@ -1,18 +1,18 @@ -import eventBus from '@webitel/ui-sdk/src/scripts/eventBus'; +import { + getDefaultGetListResponse, + getDefaultGetParams, +} from '@webitel/ui-sdk/src/api/defaults'; +import applyTransform, { + camelToSnake, + merge, mergeEach, notify, sanitize, snakeToCamel, + starToSearch, +} from '@webitel/ui-sdk/src/api/transformers'; import deepCopy from 'deep-copy'; import { MemberServiceApiFactory } from 'webitel-sdk'; -import { - SdkCreatorApiConsumer, - SdkDeleterApiConsumer, - SdkGetterApiConsumer, - SdkListGetterApiConsumer, - SdkPatcherApiConsumer, - SdkUpdaterApiConsumer, -} from 'webitel-sdk/esm2015/api-consumers'; -import instance from '../../../../../../../app/api/old/instance'; -import sanitizer from '../../../../../../../app/api/old/utils/sanitizer'; +import instance from '../../../../../../../app/api/instance'; import configuration from '../../../../../../../app/api/openAPIConfig'; + const memberService = new MemberServiceApiFactory(configuration, '', instance); const fieldsToSend = [ @@ -30,23 +30,6 @@ const communicationsFieldsToSend = [ 'dtmf', ]; -const defaultListObject = { - createdAt: 'unknown', - priority: 0, -}; - -const defaultSingleObject = { - createdAt: 'unknown', - priority: '0', - name: 'member', - expireAt: 0, - bucket: {}, - timezone: {}, - agent: {}, - communications: [], - variables: [], -}; - const defaultSingleObjectCommunication = { destination: '', display: '', @@ -57,99 +40,220 @@ const defaultSingleObjectCommunication = { dtmf: '', }; -const mapDefaultCommunications = (item) => ( - item.communications ? item.communications - .map((comm) => ({ ...defaultSingleObjectCommunication, ...comm })) : [] -); - -const _getMembersList = (getList) => function({ - page, - size, - search, - sort, - fields, - id, - parentId, - from, - to, - bucket, - priorityFrom, - priorityTo, - priority, - cause, - agent, - }) { - const params = [ - parentId, page, size, search, sort, fields, id, bucket, - undefined, from, to, undefined, undefined, cause, - priorityFrom || priority?.from, priorityTo || priority?.to, - undefined, undefined, undefined, agent, - ]; - - return getList(params); +const mapDefaultCommunications = (item) => { + const copy = deepCopy(item); + return copy.communications ? copy.communications + .map((comm) => ({ ...defaultSingleObjectCommunication, ...comm })) : []; }; -const listResponseHandler = (response) => { - const items = response.items.map((item) => ({ - ...item, - communications: mapDefaultCommunications(item), - })); - return { items, next: response.next }; +const preRequestHandler = (item) => { + const copy = deepCopy(item); + copy.communications + .forEach((copy) => sanitize(copy, communicationsFieldsToSend)); + const variables = copy.variables.reduce((variables, variable) => ({ + ...variables, + [variable.key]: variable.value, + }), {}); + return { ...copy, variables }; }; -const itemResponseHandler = (response) => { - let variables = []; - if (response.variables) { - variables = Object.keys(response.variables).map((key) => ({ - key, - value: response.variables[key], +const getMembersList = async (params) => { + const defaultObject = { + createdAt: 'unknown', + priority: 0, + } + + const listHandler = (items) => { + const copy = deepCopy(items); + return copy.map((item) => ({ + ...item, + communications: applyTransform(item, [ + mapDefaultCommunications, + ]), })); + }; + + const { + page, + size, + search, + sort, + fields, + id, + parentId, + from, + to, + bucket, + priorityFrom, + priorityTo, + priority, + cause, + agent, + } = applyTransform(params, [ + merge(getDefaultGetParams()), + starToSearch('search'), + ]); + + try { + const response = await memberService.searchMemberInQueue( + parentId, + page, + size, + search, + sort, + fields, + id, + bucket, + undefined, + from, + to, + undefined, + undefined, + cause, + priorityFrom || priority?.from, + priorityTo || priority?.to, + undefined, + undefined, + undefined, + agent, + ); + const { items, next } = applyTransform(response.data, [ + snakeToCamel(), + merge(getDefaultGetListResponse()), + ]); + return { + items: applyTransform(items, [ + mergeEach(defaultObject), + listHandler, + ]), + next, + }; + } catch (err) { + throw applyTransform(err, [ + notify, + ]); } - const communications = mapDefaultCommunications(response); - return { ...response, variables, communications }; }; -const preRequestHandler = (item) => { - item.communications - .forEach((item) => sanitizer(item, communicationsFieldsToSend)); - const variables = item.variables.reduce((variables, variable) => ({ - ...variables, - [variable.key]: variable.value, - }), {}); - return { ...item, variables }; +const getMember = async ({ parentId, itemId: id }) => { + const defaultObject = { + createdAt: 'unknown', + priority: '0', + name: 'member', + expireAt: 0, + bucket: {}, + timezone: {}, + agent: {}, + communications: [], + variables: [], + }; + + const responseHandler = (response) => { + const copy = deepCopy(response); + let variables = []; + if (copy.variables) { + variables = Object.keys(copy.variables).map((key) => ({ + key, + value: copy.variables[key], + })); + } + const communications = mapDefaultCommunications(copy); + return { ...response, variables, communications }; + }; + + try { + const response = await memberService.readMember(parentId, id); + return applyTransform(response.data, [ + snakeToCamel(), + merge(defaultObject), + responseHandler, + ]); + } catch (err) { + throw applyTransform(err, [ + notify, + ]); + } }; -const listGetter = new SdkListGetterApiConsumer( - memberService.searchMemberInQueue, - { defaultListObject, listResponseHandler }, -) -.setGetListMethod(_getMembersList); -const itemGetter = new SdkGetterApiConsumer( - memberService.readMember, - { defaultSingleObject, itemResponseHandler }, -); -const itemCreator = new SdkCreatorApiConsumer( - memberService.createMember, - { fieldsToSend, preRequestHandler }, -); -const itemUpdater = new SdkUpdaterApiConsumer( - memberService.updateMember, - { fieldsToSend, preRequestHandler }, -); -const itemDeleter = new SdkDeleterApiConsumer(memberService.deleteMember); - -const resetMembersApiConsumer = new SdkPatcherApiConsumer(memberService.resetMembers); - -const getMembersList = (params) => listGetter.getList(params); -const getMember = (params) => itemGetter.getNestedItem(params); -const addMember = (params) => itemCreator.createNestedItem(params); -const updateMember = (params) => itemUpdater.updateNestedItem(params); -const deleteMember = (params) => itemDeleter.deleteNestedItem(params); - -const resetMembers = ({ parentId }) => resetMembersApiConsumer -.patchItem({ id: parentId, changes: {} }); - -export const deleteMembersBulk = async (queueId, { +const addMember = async ({ parentId, itemInstance }) => { + const item = applyTransform(itemInstance, [ + preRequestHandler, + sanitize(fieldsToSend), + camelToSnake(), + ]); + try { + const response = await memberService.createMember(parentId, item); + return applyTransform(response.data, [ + snakeToCamel(), + ]); + } catch (err) { + throw applyTransform(err, [ + notify, + ]); + } +}; + +const updateMember = async ({ itemInstance, itemId: id, parentId }) => { + const body = applyTransform(itemInstance, [ + preRequestHandler, + sanitize(fieldsToSend), + camelToSnake(), + ]); + try { + const response = await memberService.updateMember(parentId, id, body); + return applyTransform(response.data, [ + snakeToCamel(), + ]); + } catch (err) { + throw applyTransform(err, [ + notify, + ]); + } +}; + +const deleteMember = async ({ parentId, id }) => { + try { + const response = await memberService.deleteMember(parentId, id); + return applyTransform(response.data, []); + } catch (err) { + throw applyTransform(err, [ + notify, + ]); + } +}; + +const resetMembers = async ({ parentId }) => { + try { + const response = await memberService.resetMembers(parentId, {}); + return applyTransform(response.data, [ + snakeToCamel(), + ]); + } catch (err) { + throw applyTransform(err, [ + notify, + ]); + } +}; + +const addMembersBulk = async (parentId, fileName, items) => { + const body = { parentId, fileName, items }; + try { + const response = await memberService.createMemberBulk(parentId, body); + return applyTransform(response.data, [ + snakeToCamel(), + notify(({ callback }) => callback({ + type: 'info', + text: 'Successfully added', + })), + ]); + } catch (err) { + throw applyTransform(err, [ + notify, + ]); + } +}; + +export const deleteMembersBulk = async (parentId, { search, id, from, @@ -158,31 +262,21 @@ export const deleteMembersBulk = async (queueId, { priority, cause, }) => { + const body = { + id, + q: search, + createdAt: (from || to) ? { from, to } : undefined, + priority, + stopCause: cause, + bucketId: bucket, + }; try { - await memberService.deleteMembers(queueId, { - id, - q: search, - createdAt: (from || to) ? { from, to } : undefined, - priority, - stopCause: cause, - bucketId: bucket, - }); - } catch (err) { - throw err; - } -}; - -const addMembersBulk = async (queueId, fileName, items) => { - const itemsCopy = deepCopy(items); - const body = { queueId, fileName, items: itemsCopy }; - try { - await memberService.createMemberBulk(queueId, body); - eventBus.$emit('notification', { - type: 'info', - text: 'Successfully added', - }); + const response = await memberService.deleteMembers(parentId, body); + return applyTransform(response.data, []); } catch (err) { - throw err; + throw applyTransform(err, [ + notify, + ]); } };