-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring/queue members api [WTEL-3619] #481
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
42d41b0
refactoring: queue-members api [WTEL-3669]
liza-pohranichna 77db9d6
refactoring: queue members api [WTEL-3619]
lizacoma 0dd1913
refactoring: queue members api [WTEL-3619]
lizacoma f29aadc
refactoring: queue members api [WTEL-3619]
lizacoma da85cfc
refactor: queue mambers api
lizacoma 968e47f
refactor: queue mambers api
lizacoma 4eda9f0
Merge branch 'master' into refactoring/queue-members-api
liza-pohranichna 04d2f49
refactor: removed handleUnauthorized
liza-pohranichna 1d9837b
Merge branch 'master' into refactoring/queue-members-api
lizacoma ad489ca
refactor: refactor after review [WTEL-3619]
lizacoma 0e68462
Merge branch 'refactoring/queue-members-api' of https://github.com/we…
lizacoma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
import eventBus from '@webitel/ui-sdk/src/scripts/eventBus'; | ||
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'; | ||
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 { MemberServiceApiFactory } from 'webitel-sdk'; | ||
import instance from '../../../../../../../app/api/instance'; | ||
import configuration from '../../../../../../../app/api/openAPIConfig'; | ||
import sanitizer from '../../../../../../../app/api/old/utils/sanitizer'; | ||
|
||
const memberService = new MemberServiceApiFactory(configuration, '', instance); | ||
|
||
|
@@ -30,23 +28,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 +38,213 @@ 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 listResponseHandler = (response) => { | ||
const items = response.items.map((item) => ({ | ||
...item, | ||
communications: mapDefaultCommunications(item), | ||
})); | ||
return { items, next: response.next }; | ||
}; | ||
|
||
const itemResponseHandler = (response) => { | ||
let variables = []; | ||
if (response.variables) { | ||
variables = Object.keys(response.variables).map((key) => ({ | ||
key, | ||
value: response.variables[key], | ||
})); | ||
} | ||
const communications = mapDefaultCommunications(response); | ||
return { ...response, variables, communications }; | ||
const mapDefaultCommunications = (item) => { | ||
return item.communications ? item.communications | ||
.map((comm) => ({ ...defaultSingleObjectCommunication, ...comm })) : []; | ||
}; | ||
|
||
const preRequestHandler = (item) => { | ||
item.communications | ||
.forEach((item) => sanitizer(item, communicationsFieldsToSend)); | ||
.forEach((item) => sanitize(item, communicationsFieldsToSend)); | ||
const variables = item.variables.reduce((variables, variable) => ({ | ||
...variables, | ||
[variable.key]: variable.value, | ||
}), {}); | ||
return { ...item, variables }; | ||
}; | ||
|
||
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 getMembersList = async (params) => { | ||
const defaultObject = { | ||
createdAt: 'unknown', | ||
priority: 0, | ||
} | ||
const listHandler = (items) => { | ||
return items.map((item) => ({ | ||
...item, | ||
communications: mapDefaultCommunications(item), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. зійде для сільської місцевості, але якщо хочеш по-крутому, то було би круто на коммунікації накласти трансформер, замість виклику метода) |
||
})); | ||
}; | ||
|
||
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 getMember = async ({ parentId, itemId: id }) => { | ||
const defaultObject = { | ||
createdAt: 'unknown', | ||
priority: '0', | ||
name: 'member', | ||
expireAt: 0, | ||
bucket: {}, | ||
timezone: {}, | ||
agent: {}, | ||
communications: [], | ||
variables: [], | ||
}; | ||
|
||
const responseHandler = (response) => { | ||
let variables = []; | ||
if (response.variables) { | ||
variables = Object.keys(response.variables).map((key) => ({ | ||
key, | ||
value: response.variables[key], | ||
})); | ||
} | ||
const communications = mapDefaultCommunications(response); | ||
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 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 +253,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, | ||
]); | ||
} | ||
}; | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
мутує початковий об'єкт
треба зробити копію спершу