Skip to content

Commit

Permalink
Merge pull request #546 from webitel/refactor/api-teams-moduls
Browse files Browse the repository at this point in the history
Refactor/api teams moduls [WTEL-3785, WTEL-3783]
  • Loading branch information
dlohvinov authored Nov 7, 2023
2 parents fda27ce + d4bb5e3 commit 6a3d50b
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,66 +1,114 @@
import { AgentServiceApiFactory } from 'webitel-sdk';
import {
SdkGetterApiConsumer,
SdkListGetterApiConsumer,
SdkPatcherApiConsumer,
} 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,
snakeToCamel,
starToSearch,
} from '@webitel/ui-sdk/src/api/transformers';
import instance from '../../../../../../../app/api/instance';
import configuration from '../../../../../../../app/api/openAPIConfig';

const agentService = new AgentServiceApiFactory(configuration, '', instance);

const defaultListObject = {
name: '',
status: '',
supervisor: {},
skills: [],
};

const getTeamAgents = (getList) => function({
parentId,
page = 1,
size = 10,
search,
sort,
}) {
// parent id == team id
if (!parentId) return;
const getTeamAgentsList = async (params) => {
const fields = ['id', 'name', 'status', 'supervisor', 'skills'];
const params = [
page,
size,

const defaultObject = {
name: '',
status: '',
supervisor: {},
skills: [],
};

const {
parentId,
page = 1,
size = 10,
search,
sort,
fields,
undefined,
undefined,
undefined,
parentId,
];
// eslint-disable-next-line consistent-return
return getList(params);
} = applyTransform(params, [
merge(getDefaultGetParams()),
starToSearch('search'),
]);

try {
const response = await agentService.searchAgent(
page,
size,
search,
sort,
fields,
undefined,
undefined,
undefined,
parentId,
);
const { items, next } = applyTransform(response.data, [
snakeToCamel(),
merge(getDefaultGetListResponse()),
]);
return {
items: applyTransform(items, [
mergeEach(defaultObject),
]),
next,
};
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

const agentGetterResponseHandler = (agent) => ({ agent });
const getTeamAgent = async ({ itemId: id }) => {
const responseHandler = (agent) => ({ agent });

try {
const response = await agentService.readAgent(id);
return applyTransform(response.data, [
snakeToCamel(),
responseHandler,
]);
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

const listGetter = new SdkListGetterApiConsumer(agentService.searchAgent, { defaultListObject })
.setGetListMethod(getTeamAgents);
const itemGetter = new SdkGetterApiConsumer(agentService.readAgent, {
itemResponseHandler: agentGetterResponseHandler,
});
const itemPatcher = new SdkPatcherApiConsumer(agentService.patchAgent);
const patchAgent = async ({ id, changes }) => {
const item = applyTransform(changes, [
camelToSnake(),
]);
try {
const response = await agentService.patchAgent(id, item);
return applyTransform(response.data, [
snakeToCamel(),
]);
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

const getTeamAgentsList = (params) => listGetter.getList(params);
const getTeamAgent = (params) => itemGetter.getItem(params);
const addTeamAgent = ({ parentId, itemInstance }) => {
const { id } = itemInstance.agent;
const changes = { team: { id: parentId } };
return itemPatcher.patchItem({ id, changes });
return patchAgent({ id, changes });
};


const deleteTeamAgent = ({ id }) => {
const changes = { team: { id: null } };
return itemPatcher.patchItem({ id, changes });
return patchAgent({ id, changes });
};

const updateTeamAgent = async ({ parentId, itemId, itemInstance }) => {
try {
await addTeamAgent({ parentId, itemInstance });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,71 +1,150 @@
import { AgentServiceApiFactory } from 'webitel-sdk';
import {
SdkGetterApiConsumer,
SdkListGetterApiConsumer,
SdkPatcherApiConsumer,
} from 'webitel-sdk/esm2015/api-consumers';
import instance from '../../../../../../../app/api/old/instance';
import { AgentServiceApiFactory } from 'webitel-sdk';import {
getDefaultGetListResponse,
getDefaultGetParams,
} from '@webitel/ui-sdk/src/api/defaults';
import applyTransform, {
camelToSnake,
merge,
notify,
snakeToCamel,
starToSearch,
} from '@webitel/ui-sdk/src/api/transformers';
import instance from '../../../../../../../app/api/instance';
import configuration from '../../../../../../../app/api/openAPIConfig';

const teamSupervisorService = new AgentServiceApiFactory(configuration, '', instance);

const _getTeamSupervisorsList = (getList) => function({
page,
size,
search,
parentId,
sort,
}) {
// parent id == team id
if (!parentId) return;
const getTeamSupervisorsList = async (params) => {
const isSupervisor = true;
const fields = ['id', 'name'];
const params = [
page, size, search, sort, fields, undefined,
undefined, undefined, parentId, undefined, undefined, isSupervisor,
];
// eslint-disable-next-line consistent-return
return getList(params);

const {
page,
size,
search,
parentId,
sort,
} = applyTransform(params, [
merge(getDefaultGetParams()),
starToSearch('search'),
]);

try {
const response = await teamSupervisorService.searchAgent(
page,
size,
search,
sort,
fields,
undefined,
undefined,
undefined,
parentId,
undefined,
undefined,
isSupervisor,
);
const { items, next } = applyTransform(response.data, [
snakeToCamel(),
merge(getDefaultGetListResponse()),
]);
return {
items,
next,
};
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

const _getTeamSupervisorSubordinatesList = (getList) => function({
page,
size,
search,
supervisorId,
teamId,
}) {
const fields = ['id', 'user'];
const params = [
page, size, search, undefined, fields, undefined,
undefined, supervisorId, teamId, undefined, undefined,
];
return getList(params);
const getTeamSupervisor = async ({ itemId: id }) => {
const responseHandler = (agent) => ({ agent });

try {
const response = await teamSupervisorService.readAgent(id);
return applyTransform(response.data, [
snakeToCamel(),
responseHandler,
]);
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

const patchAgent = async ({ id, changes }) => {
const item = applyTransform(changes, [
camelToSnake(),
]);
try {
const response = await teamSupervisorService.patchAgent(id, item);
return applyTransform(response.data, [
snakeToCamel(),
]);
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

const subordinateGetterResponseHandler = (agent) => ({ agent });
const getTeamSupervisorSubordinatesList = async (params) => {
const fields = ['id', 'user'];

const listGetter = new SdkListGetterApiConsumer(teamSupervisorService.searchAgent)
.setGetListMethod(_getTeamSupervisorsList);
const itemGetter = new SdkGetterApiConsumer(teamSupervisorService.readAgent, {
itemResponseHandler: subordinateGetterResponseHandler,
});
const itemPatcher = new SdkPatcherApiConsumer(teamSupervisorService.patchAgent);
const {
page,
size,
search,
supervisorId,
teamId,
} = applyTransform(params, [
merge(getDefaultGetParams()),
starToSearch('search'),
]);

const subordinatesListGetter = new SdkListGetterApiConsumer(teamSupervisorService.searchAgent)
.setGetListMethod(_getTeamSupervisorSubordinatesList);
try {
const response = await teamSupervisorService.searchAgent(
page,
size,
search,
undefined,
fields,
undefined,
undefined,
supervisorId,
teamId,
undefined,
undefined,
);
const { items, next } = applyTransform(response.data, [
snakeToCamel(),
merge(getDefaultGetListResponse()),
]);
return {
items,
next,
};
} catch (err) {
throw applyTransform(err, [
notify,
]);
}
};

const getTeamSupervisorsList = (params) => listGetter.getList(params);
const getTeamSupervisor = (params) => itemGetter.getItem(params);
const addTeamSupervisor = ({ parentId, itemInstance }) => {
const { id } = itemInstance.agent;
const changes = { team: { id: parentId } };
return itemPatcher.patchItem({ id, changes });
return patchAgent({ id, changes });
};

const deleteTeamSupervisor = ({ id }) => {
const changes = { team: { id: null } };
return itemPatcher.patchItem({ id, changes });
return patchAgent({ id, changes });
};

const updateTeamSupervisor = async ({ parentId, itemId, itemInstance }) => {
try {
await addTeamSupervisor({ parentId, itemInstance });
Expand All @@ -75,7 +154,6 @@ const updateTeamSupervisor = async ({ parentId, itemId, itemInstance }) => {
}
};

const getTeamSupervisorSubordinatesList = (params) => subordinatesListGetter.getList(params);

const TeamSupervisorsAPI = {
getList: getTeamSupervisorsList,
Expand Down

0 comments on commit 6a3d50b

Please sign in to comment.