From 5e6149e704c5bdadd674adc31c4690e493c55367 Mon Sep 17 00:00:00 2001 From: Chika Date: Fri, 4 Dec 2020 13:59:53 +0300 Subject: [PATCH] refactor: split actions into files --- .../Calculation/lib/buildElement.js | 2 +- .../Calculation/lib/elements/titles.ts | 3 - .../Calculation/lib/elements/types.ts | 10 +- .../stores/CalculationStore/Effects/action.ts | 352 +----------------- .../Effects/actions/calculate.js | 108 ++++++ .../Effects/actions/createKP.js | 91 +++++ .../Effects/actions/createLead.js | 139 +++++++ src/core/types/Calculation/Store/elements.ts | 2 +- 8 files changed, 353 insertions(+), 354 deletions(-) create mode 100644 src/client/stores/CalculationStore/Effects/actions/calculate.js create mode 100644 src/client/stores/CalculationStore/Effects/actions/createKP.js create mode 100644 src/client/stores/CalculationStore/Effects/actions/createLead.js diff --git a/src/client/Containers/Calculation/lib/buildElement.js b/src/client/Containers/Calculation/lib/buildElement.js index c453193..985100c 100644 --- a/src/client/Containers/Calculation/lib/buildElement.js +++ b/src/client/Containers/Calculation/lib/buildElement.js @@ -26,7 +26,7 @@ export function buildElement(elementName) { ...tables[elementName].props, }); } - case ElementType.Button: { + case ElementType.Action: { return withButton(Component)({ name: elementName, actionName: elementsActions[elementName], diff --git a/src/client/Containers/Calculation/lib/elements/titles.ts b/src/client/Containers/Calculation/lib/elements/titles.ts index 9bfd641..af7b63b 100644 --- a/src/client/Containers/Calculation/lib/elements/titles.ts +++ b/src/client/Containers/Calculation/lib/elements/titles.ts @@ -1,9 +1,6 @@ import { TElements } from 'core/types/Calculation/Store/elements'; import { StoreTables } from 'core/types/Calculation/Store/tables'; - - - const elementsTitles: TElements = { selectLead: 'Интерес', selectOpportunity: 'Лизинговая сделка', diff --git a/src/client/Containers/Calculation/lib/elements/types.ts b/src/client/Containers/Calculation/lib/elements/types.ts index 548315f..5e25c34 100644 --- a/src/client/Containers/Calculation/lib/elements/types.ts +++ b/src/client/Containers/Calculation/lib/elements/types.ts @@ -3,12 +3,12 @@ import { TElements, ElementType } from 'core/types/Calculation/Store/elements'; const elementsTypes: TElements = { labelLeaseObjectRisk: ElementType.Computed, - btnInsCalculation: ElementType.Button, - btnFranschise: ElementType.Button, - btnDriversApplication: ElementType.Button, + btnInsCalculation: ElementType.Action, + btnFranschise: ElementType.Action, + btnDriversApplication: ElementType.Action, tbxInsKaskoPriceLeasePeriod: ElementType.Computed, - btnCreateKP: ElementType.Button, - btnCalculate: ElementType.Button, + btnCreateKP: ElementType.Action, + btnCalculate: ElementType.Action, labelIrrInfo: ElementType.Computed, }; diff --git a/src/client/stores/CalculationStore/Effects/action.ts b/src/client/stores/CalculationStore/Effects/action.ts index bbdc5de..99c16ad 100644 --- a/src/client/stores/CalculationStore/Effects/action.ts +++ b/src/client/stores/CalculationStore/Effects/action.ts @@ -1,347 +1,11 @@ -// @ts-nocheck -import { gql } from '@apollo/client'; -import { message } from 'antd'; -import { openNotification } from 'client/Elements/Notification'; -import CalculationService from 'client/services/CalculationService'; -import CrmService from 'client/services/CrmService'; -import CalculationStore from 'client/stores/CalculationStore'; -import { getUser } from 'client/tools/user'; -import { TAction } from 'core/types/Calculation/Store/effect'; -import { resultsValues } from 'core/types/Calculation/Store/values'; -import { Status } from 'core/types/statuses'; -import getAdditionalData from './lib/additionalData'; -import checkValidation from './lib/checkValidation'; -import customValues from './lib/customValues'; -import prepareCalculationData from './lib/prepareData'; -import results from './lib/results'; -import validate from './lib/validate'; +import calculate from './actions/calculate'; +import createKP from './actions/createKP'; +import createLead from './actions/createLead'; -const cleanResults = () => { - for (let resultValue of resultsValues) { - CalculationStore.setValue(resultValue, ''); - } -}; - -const BUTTONS_TIMEOUT = 500; - -function blockButtons() { - CalculationStore.setStatus('btnCreateKP', Status.Loading); - CalculationStore.setStatus('btnCalculate', Status.Loading); -} - -function unlockButtons() { - setTimeout(() => { - CalculationStore.setStatus('btnCreateKP', Status.Default); - }, BUTTONS_TIMEOUT); - setTimeout(() => { - CalculationStore.setStatus('btnCalculate', Status.Default); - }, BUTTONS_TIMEOUT); -} - -const actions: TAction = { - createLead: () => { - // TODO: block button for time - // TODO: collect errors - const { - contactClient, - contact, - newClient, - account, - commentLead, - phoneNumber, - } = CalculationStore.values; - if (!contactClient && !contact) { - CalculationStore.setValidation('selectContactClient', false); - CalculationStore.setValidation('tbxContact', false); - openNotification({ - type: 'error', - title: 'Ошибка во время создания интереса', - description: 'Не указан контакт клиента', - })(); - } - if (!newClient && !account) { - CalculationStore.setValidation('tbxNewClient', false); - CalculationStore.setValidation('selectAccount', false); - openNotification({ - type: 'error', - title: 'Ошибка во время создания интереса', - description: 'Не указан клиент', - })(); - } - if (!commentLead) { - CalculationStore.setValidation('tbxCommentLead', false); - openNotification({ - type: 'error', - title: 'Ошибка во время создания интереса', - description: 'Не указан комментарий к интересу', - })(); - } - if (contact && !phoneNumber) { - CalculationStore.setValidation('tbxPhoneNumber', false); - openNotification({ - type: 'error', - title: 'Ошибка во время создания интереса', - description: 'Не указан телефон контактного лица', - })(); - } - - const { channel } = CalculationStore.values; - if (!channel) { - CalculationStore.setValidation('selectChannel', false); - openNotification({ - type: 'error', - title: 'Ошибка во время создания интереса', - description: 'Не указан канал привлечения', - })(); - } else { - const { - supplier, - agent, - finDepartment, - broker, - } = CalculationStore.values; - switch (channel) { - case 100000000: { - CalculationStore.setValidation('selectFinDepartment', true); - CalculationStore.setValidation('selectBroker', true); - if (!supplier) { - CalculationStore.setValidation('selectSupplier', false); - openNotification({ - type: 'error', - title: 'Ошибка во время создания интереса', - description: 'Не указан поставщик', - })(); - } - if (!agent) { - CalculationStore.setValidation('selectAgent', false); - openNotification({ - type: 'error', - title: 'Ошибка во время создания интереса', - description: 'Не указан агент', - })(); - } - break; - } - case 100000001: { - CalculationStore.setValidation('selectAgent', true); - CalculationStore.setValidation('selectBroker', true); - if (!supplier) { - CalculationStore.setValidation('selectSupplier', false); - openNotification({ - type: 'error', - title: 'Ошибка во время создания интереса', - description: 'Не указан поставщик', - })(); - } - if (!finDepartment) { - CalculationStore.setValidation('selectFinDepartment', false); - openNotification({ - type: 'error', - title: 'Ошибка во время создания интереса', - description: 'Не указан финотдел', - })(); - } - break; - } - case 100000002: { - CalculationStore.setValidation('selectFinDepartment', true); - CalculationStore.setValidation('selectSupplier', true); - CalculationStore.setValidation('selectBroker', true); - if (!agent) { - CalculationStore.setValidation('selectAgent', false); - openNotification({ - type: 'error', - title: 'Ошибка во время создания интереса', - description: 'Не указан агент', - })(); - } - break; - } - case 100000003: { - CalculationStore.setValidation('selectFinDepartment', true); - CalculationStore.setValidation('selectSupplier', true); - CalculationStore.setValidation('selectAgent', true); - if (!broker) { - CalculationStore.setValidation('selectBroker', false); - openNotification({ - type: 'error', - title: 'Ошибка во время создания интереса', - description: 'Не указан брокер', - })(); - } - break; - } - case 100000004: - default: { - CalculationStore.setValidation('selectFinDepartment', true); - CalculationStore.setValidation('selectSupplier', true); - CalculationStore.setValidation('selectAgent', true); - CalculationStore.setValidation('selectBroker', true); - break; - } - } - } - }, - - createKP: async () => { - const { values, tables } = CalculationStore; - const calculationRes = await actions.calculate(); - - const insurances = tables.tableInsurance.rows.map(insuranceRow => { - const resObj = {}; - Object.keys(insuranceRow).map(prop => { - resObj[prop] = insuranceRow[prop].value; - }); - return resObj; - }); - - console.log('insurances', insurances); - const { - columns, - postValues, - preparedValues, - preparedPayments, - } = calculationRes; - - const domainName = (await getUser()).FullName || ''; - // if (process.env.NODE_ENV === 'development') { - console.log('domainName', domainName); - console.log('values', values); - console.log( - 'insKaskoPriceLeasePeriod', - CalculationStore.insKaskoPriceLeasePeriod(), - ); - // } - - CrmService.createKp({ - domainName, - calculation: { - insurances, - preparedValues, - preparedPayments, - columns, - postValues, - calculationValues: { - ...values, - insKaskoPriceLeasePeriod: customValues.insKaskoPriceLeasePeriod.call( - CalculationStore, - ), - }, - }, - }) - .then(({ quoteid }) => { - CrmService.crmgqlquery({ - query: gql` - query($quoteid: Uuid!) { - quote(quoteId: $quoteid) { - offerprintform - evo_quotename - } - } - `, - variables: { - quoteid, - }, - }).then(({ entities: { quote } }) => { - console.log('quote', quote); - if (!Array.isArray(quote)) { - message.success({ - content: `КП ${quote?.evo_quotename || ''} создано!`, - duration: 5, - style: { - marginTop: '7vh', - }, - }); - - if (quote?.offerprintform) { - Object.assign(document.createElement('a'), { - target: '_blank', - href: quote.offerprintform, - }).click(); - } - } - }); - }) - .catch(err => { - throw err; - }); - }, - - calculate: async () => { - validate(); - const { invalidElements, invalidTables } = checkValidation(); - if (invalidElements.length > 0 || invalidTables.length > 0) { - if (invalidElements.length > 0) - openNotification({ - type: 'error', - title: 'Ошибка во время расчета графика', - description: - 'Некорректно заполнены поля: ' + invalidElements.join(', '), - })(); - - if (invalidTables.length > 0) - openNotification({ - type: 'error', - title: 'Ошибка во время расчета графика', - description: - 'Некорректно заполнены таблицы: ' + invalidTables.join(', '), - })(); - return; - } - - blockButtons(); - - var preparedData = prepareCalculationData({ - calculationStore: CalculationStore, - }); - console.log('preparedData', preparedData); - - var additionalData = getAdditionalData({ - calculationStore: CalculationStore, - }); - console.log('additionalData', additionalData); - - CalculationStore.cleanTable('tableResults'); - cleanResults(); - - return CalculationService.calculate( - Object.assign(preparedData, { additionalData }), - ) - .then(res => { - console.log('calculation res', res); - - message.success({ - content: 'Расчет завершен успешно!', - duration: 5, - style: { - marginTop: '7vh', - }, - }); - - results.showResultsTable(preparedData, res); - results.showResults(preparedData, res); - - unlockButtons(); - - return { ...res, ...preparedData }; - }) - .catch(err => { - openNotification({ - type: 'error', - title: 'Ошибка во время расчета!', - description: - err.response && - err.response.data && - ((err.response.data.errors && - err.response.data.errors.join(', ')) || - err.response.data), - })(); - - unlockButtons(); - - throw err; - }); +export default { + actions: { + calculate, + createKP, + createLead, }, }; - -export default { actions }; diff --git a/src/client/stores/CalculationStore/Effects/actions/calculate.js b/src/client/stores/CalculationStore/Effects/actions/calculate.js new file mode 100644 index 0000000..0c9314b --- /dev/null +++ b/src/client/stores/CalculationStore/Effects/actions/calculate.js @@ -0,0 +1,108 @@ +import { message } from 'antd'; +import { openNotification } from 'client/Elements/Notification'; +import CalculationService from 'client/services/CalculationService'; +import { resultsValues } from 'core/types/Calculation/Store/values'; +import { Status } from 'core/types/statuses'; +import CalculationStore from '../..'; +import getAdditionalData from '../lib/additionalData'; +import checkValidation from '../lib/checkValidation'; +import prepareCalculationData from '../lib/prepareData'; +import results from '../lib/results'; +import validate from '../lib/validate'; + +const cleanResults = () => { + for (let resultValue of resultsValues) { + CalculationStore.setValue(resultValue, ''); + } +}; + +const BUTTONS_TIMEOUT = 500; + +function blockButtons() { + CalculationStore.setStatus('btnCreateKP', Status.Loading); + CalculationStore.setStatus('btnCalculate', Status.Loading); +} + +function unlockButtons() { + setTimeout(() => { + CalculationStore.setStatus('btnCreateKP', Status.Default); + }, BUTTONS_TIMEOUT); + setTimeout(() => { + CalculationStore.setStatus('btnCalculate', Status.Default); + }, BUTTONS_TIMEOUT); +} + +export default () => { + validate(); + const { invalidElements, invalidTables } = checkValidation(); + if (invalidElements.length > 0 || invalidTables.length > 0) { + if (invalidElements.length > 0) + openNotification({ + type: 'error', + title: 'Ошибка во время расчета графика', + description: + 'Некорректно заполнены поля: ' + invalidElements.join(', '), + })(); + + if (invalidTables.length > 0) + openNotification({ + type: 'error', + title: 'Ошибка во время расчета графика', + description: + 'Некорректно заполнены таблицы: ' + invalidTables.join(', '), + })(); + return; + } + + blockButtons(); + + var preparedData = prepareCalculationData({ + calculationStore: CalculationStore, + }); + console.log('preparedData', preparedData); + + var additionalData = getAdditionalData({ + calculationStore: CalculationStore, + }); + console.log('additionalData', additionalData); + + CalculationStore.cleanTable('tableResults'); + cleanResults(); + + return CalculationService.calculate( + Object.assign(preparedData, { additionalData }), + ) + .then(res => { + console.log('calculation res', res); + + message.success({ + content: 'Расчет завершен успешно!', + duration: 5, + style: { + marginTop: '7vh', + }, + }); + + results.showResultsTable(preparedData, res); + results.showResults(preparedData, res); + + unlockButtons(); + + return { ...res, ...preparedData }; + }) + .catch(err => { + openNotification({ + type: 'error', + title: 'Ошибка во время расчета!', + description: + err.response && + err.response.data && + ((err.response.data.errors && err.response.data.errors.join(', ')) || + err.response.data), + })(); + + unlockButtons(); + + throw err; + }); +}; diff --git a/src/client/stores/CalculationStore/Effects/actions/createKP.js b/src/client/stores/CalculationStore/Effects/actions/createKP.js new file mode 100644 index 0000000..117fad5 --- /dev/null +++ b/src/client/stores/CalculationStore/Effects/actions/createKP.js @@ -0,0 +1,91 @@ +import { gql } from '@apollo/client'; +import { message } from 'antd'; +import CrmService from 'client/services/CrmService'; +import { getUser } from 'client/tools/user'; +import CalculationStore from '../..'; +import customValues from '../lib/customValues'; +import calculate from './calculate'; + +export default async () => { + const { values, tables } = CalculationStore; + const calculationRes = await calculate(); + + const insurances = tables.tableInsurance.rows.map(insuranceRow => { + const resObj = {}; + Object.keys(insuranceRow).forEach(prop => { + resObj[prop] = insuranceRow[prop].value; + }); + return resObj; + }); + + console.log('insurances', insurances); + const { + columns, + postValues, + preparedValues, + preparedPayments, + } = calculationRes; + + const domainName = (await getUser()).FullName || ''; + // if (process.env.NODE_ENV === 'development') { + console.log('domainName', domainName); + console.log('values', values); + console.log( + 'insKaskoPriceLeasePeriod', + CalculationStore.insKaskoPriceLeasePeriod(), + ); + // } + + CrmService.createKp({ + domainName, + calculation: { + insurances, + preparedValues, + preparedPayments, + columns, + postValues, + calculationValues: { + ...values, + insKaskoPriceLeasePeriod: customValues.insKaskoPriceLeasePeriod.call( + CalculationStore, + ), + }, + }, + }) + .then(({ quoteid }) => { + CrmService.crmgqlquery({ + query: gql` + query($quoteid: Uuid!) { + quote(quoteId: $quoteid) { + offerprintform + evo_quotename + } + } + `, + variables: { + quoteid, + }, + }).then(({ entities: { quote } }) => { + console.log('quote', quote); + if (!Array.isArray(quote)) { + message.success({ + content: `КП ${quote?.evo_quotename || ''} создано!`, + duration: 5, + style: { + marginTop: '7vh', + }, + }); + + if (quote?.offerprintform) { + Object.assign(document.createElement('a'), { + target: '_blank', + href: quote.offerprintform, + }).click(); + } + } + }); + }) + .catch(err => { + throw err; + }); +}; diff --git a/src/client/stores/CalculationStore/Effects/actions/createLead.js b/src/client/stores/CalculationStore/Effects/actions/createLead.js new file mode 100644 index 0000000..a59055b --- /dev/null +++ b/src/client/stores/CalculationStore/Effects/actions/createLead.js @@ -0,0 +1,139 @@ +import { openNotification } from 'client/Elements/Notification'; +import CalculationStore from '../..'; + +export default () => { + const { + contactClient, + contact, + newClient, + account, + commentLead, + phoneNumber, + } = CalculationStore.values; + if (!contactClient && !contact) { + CalculationStore.setValidation('selectContactClient', false); + CalculationStore.setValidation('tbxContact', false); + openNotification({ + type: 'error', + title: 'Ошибка во время создания интереса', + description: 'Не указан контакт клиента', + })(); + } + if (!newClient && !account) { + CalculationStore.setValidation('tbxNewClient', false); + CalculationStore.setValidation('selectAccount', false); + openNotification({ + type: 'error', + title: 'Ошибка во время создания интереса', + description: 'Не указан клиент', + })(); + } + if (!commentLead) { + CalculationStore.setValidation('tbxCommentLead', false); + openNotification({ + type: 'error', + title: 'Ошибка во время создания интереса', + description: 'Не указан комментарий к интересу', + })(); + } + if (contact && !phoneNumber) { + CalculationStore.setValidation('tbxPhoneNumber', false); + openNotification({ + type: 'error', + title: 'Ошибка во время создания интереса', + description: 'Не указан телефон контактного лица', + })(); + } + + const { channel } = CalculationStore.values; + if (!channel) { + CalculationStore.setValidation('selectChannel', false); + openNotification({ + type: 'error', + title: 'Ошибка во время создания интереса', + description: 'Не указан канал привлечения', + })(); + } else { + const { supplier, agent, finDepartment, broker } = CalculationStore.values; + switch (channel) { + case 100000000: { + CalculationStore.setValidation('selectFinDepartment', true); + CalculationStore.setValidation('selectBroker', true); + if (!supplier) { + CalculationStore.setValidation('selectSupplier', false); + openNotification({ + type: 'error', + title: 'Ошибка во время создания интереса', + description: 'Не указан поставщик', + })(); + } + if (!agent) { + CalculationStore.setValidation('selectAgent', false); + openNotification({ + type: 'error', + title: 'Ошибка во время создания интереса', + description: 'Не указан агент', + })(); + } + break; + } + case 100000001: { + CalculationStore.setValidation('selectAgent', true); + CalculationStore.setValidation('selectBroker', true); + if (!supplier) { + CalculationStore.setValidation('selectSupplier', false); + openNotification({ + type: 'error', + title: 'Ошибка во время создания интереса', + description: 'Не указан поставщик', + })(); + } + if (!finDepartment) { + CalculationStore.setValidation('selectFinDepartment', false); + openNotification({ + type: 'error', + title: 'Ошибка во время создания интереса', + description: 'Не указан финотдел', + })(); + } + break; + } + case 100000002: { + CalculationStore.setValidation('selectFinDepartment', true); + CalculationStore.setValidation('selectSupplier', true); + CalculationStore.setValidation('selectBroker', true); + if (!agent) { + CalculationStore.setValidation('selectAgent', false); + openNotification({ + type: 'error', + title: 'Ошибка во время создания интереса', + description: 'Не указан агент', + })(); + } + break; + } + case 100000003: { + CalculationStore.setValidation('selectFinDepartment', true); + CalculationStore.setValidation('selectSupplier', true); + CalculationStore.setValidation('selectAgent', true); + if (!broker) { + CalculationStore.setValidation('selectBroker', false); + openNotification({ + type: 'error', + title: 'Ошибка во время создания интереса', + description: 'Не указан брокер', + })(); + } + break; + } + case 100000004: + default: { + CalculationStore.setValidation('selectFinDepartment', true); + CalculationStore.setValidation('selectSupplier', true); + CalculationStore.setValidation('selectAgent', true); + CalculationStore.setValidation('selectBroker', true); + break; + } + } + } +}; diff --git a/src/core/types/Calculation/Store/elements.ts b/src/core/types/Calculation/Store/elements.ts index 2ba1a91..1b297ad 100644 --- a/src/core/types/Calculation/Store/elements.ts +++ b/src/core/types/Calculation/Store/elements.ts @@ -155,7 +155,7 @@ export enum ElementType { Default, Computed, Table, - Button, + Action, } export type TElements = {