diff --git a/.eslintrc.json b/.eslintrc.json index 428a986..f320aec 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -76,7 +76,8 @@ ], "import/no-unresolved": "warn", "implicit-arrow-linebreak": "warn", - "operator-linebreak": "warn" + "operator-linebreak": "warn", + "function-paren-newline": "warn" }, "overrides": [ // Only uses Testing Library lint rules in test files diff --git a/Elements/message.js b/Elements/message.js new file mode 100644 index 0000000..16cca37 --- /dev/null +++ b/Elements/message.js @@ -0,0 +1,2 @@ +/* eslint-disable no-restricted-exports */ +export { message as default } from 'antd'; diff --git a/config/schema/values.ts b/config/schema/values.ts new file mode 100644 index 0000000..691d28c --- /dev/null +++ b/config/schema/values.ts @@ -0,0 +1,144 @@ +import { z } from 'zod'; + +const ValuesSchema = z.object({ + lead: z.string().nullable(), + opportunity: z.string().nullable(), + quote: z.string().nullable(), + recalcWithRevision: z.boolean(), + product: z.string().nullable(), + clientRisk: z.string().nullable(), + clientType: z.string().nullable(), + leaseObjectPrice: z.number(), + supplierCurrency: z.string().nullable(), + supplierDiscountRub: z.number(), + supplierDiscountPerc: z.number(), + leasingPeriod: z.number(), + firstPaymentPerc: z.number(), + firstPaymentRub: z.number(), + lastPaymentPerc: z.number(), + lastPaymentRub: z.number(), + lastPaymentRule: z.number().nullable(), + importProgram: z.string().nullable(), + importProgramSum: z.number(), + addEquipmentPrice: z.number(), + redemptionPaymentSum: z.number(), + balanceHolder: z.number().nullable(), + graphType: z.number().nullable(), + parmentsDecreasePercent: z.number(), + seasonType: z.number().nullable(), + highSeasonStart: z.number().nullable(), + comissionPerc: z.number(), + comissionRub: z.number(), + saleBonus: z.number(), + IRR_Perc: z.number(), + leaseObjectType: z.string().nullable(), + deliveryTime: z.number().nullable(), + leaseObjectCount: z.number(), + withTrailer: z.boolean(), + leaseObjectUsed: z.boolean(), + maxMass: z.number(), + countSeats: z.number(), + maxSpeed: z.number(), + brand: z.string().nullable(), + model: z.string().nullable(), + configuration: z.string().nullable(), + leaseObjectYear: z.number(), + engineType: z.number().nullable(), + leaseObjectCategory: z.number().nullable(), + leaseObjectMotorPower: z.number(), + engineVolume: z.number(), + leaseObjectUseFor: z.number().nullable(), + dealer: z.string().nullable(), + dealerPerson: z.string().nullable(), + dealerRewardCondition: z.string().nullable(), + dealerRewardSumm: z.number(), + dealerBroker: z.string().nullable(), + dealerBrokerRewardCondition: z.string().nullable(), + dealerBrokerRewardSumm: z.number(), + indAgent: z.string().nullable(), + indAgentRewardCondition: z.string().nullable(), + indAgentRewardSumm: z.number(), + calcDoubleAgent: z.string().nullable(), + calcDoubleAgentRewardCondition: z.string().nullable(), + calcDoubleAgentRewardSumm: z.number(), + calcBroker: z.string().nullable(), + calcBrokerRewardCondition: z.string().nullable(), + calcBrokerRewardSum: z.number(), + calcFinDepartment: z.string().nullable(), + finDepartmentRewardCondtion: z.string().nullable(), + finDepartmentRewardSumm: z.number(), + GPSBrand: z.string().nullable(), + GPSModel: z.string().nullable(), + regionRegistration: z.string().nullable(), + townRegistration: z.string().nullable(), + infuranceOPF: z.number().nullable(), + insKaskoType: z.number().nullable(), + insDecentral: z.boolean(), + insFranchise: z.number(), + insUnlimitDrivers: z.boolean(), + insAgeDrivers: z.number(), + insExpDrivers: z.number(), + INNForCalc: z.number(), + lastPaymentRedemption: z.boolean(), + priceWithDiscount: z.boolean(), + fullPriceWithDiscount: z.boolean(), + costIncrease: z.boolean(), + insurance: z.boolean(), + registrationQuote: z.boolean(), + technicalCardQuote: z.boolean(), + NSIB: z.boolean(), + quoteName: z.string().nullable(), + quoteContactGender: z.number().nullable(), + quoteRedemptionGraph: z.boolean(), + showFinGAP: z.boolean(), + tarif: z.string().nullable(), + creditRate: z.number(), + rate: z.string().nullable(), + requirementTelematic: z.number().nullable(), + minPriceChange: z.number(), + maxPriceChange: z.number(), + importerRewardPerc: z.number(), + importerRewardRub: z.number(), + disableChecks: z.boolean(), + registration: z.string().nullable(), + insNSIB: z.string().nullable(), + technicalCard: z.string().nullable(), + telematic: z.string().nullable(), + tracker: z.string().nullable(), + mileage: z.number(), + calcType: z.number().nullable(), + totalPayments: z.number(), + objectRegistration: z.number().nullable(), + objectRegionRegistration: z.string().nullable(), + vehicleTaxInYear: z.number(), + vehicleTaxInLeasingPeriod: z.number(), + objectCategoryTax: z.number().nullable(), + objectTypeTax: z.number().nullable(), + typePTS: z.number().nullable(), + legalClientRegion: z.string().nullable(), + legalClientTown: z.string().nullable(), + subsidy: z.string().nullable(), + fuelCard: z.string().nullable(), + leaseObjectPriceWthtVAT: z.number(), + VATInLeaseObjectPrice: z.number(), + engineHours: z.number(), + bonusCoefficient: z.number(), + + /** Link Values */ + kpUrl: z.string().nullable(), + leadUrl: z.string().nullable(), + opportunityUrl: z.string().nullable(), + quoteUrl: z.string().nullable(), + + /** Readonly Values */ + leaseObjectRiskName: z.string().nullable(), + insKaskoPriceLeasePeriod: z.number(), + irrInfo: z.string().nullable(), + registrationDescription: z.string().nullable(), + depreciationGroup: z.string().nullable(), + subsidySum: z.number(), + plPriceRub: z.number(), + discountRub: z.number(), +}); + +export default ValuesSchema; diff --git a/graphql/crm.types.ts b/graphql/crm.types.ts index c3cf351..6354d29 100644 --- a/graphql/crm.types.ts +++ b/graphql/crm.types.ts @@ -238,6 +238,35 @@ export type GetCurrencyIsoCodeQueryVariables = Exact<{ export type GetCurrencyIsoCodeQuery = { __typename?: 'Query', transactioncurrency?: { __typename?: 'transactioncurrency', isocurrencycode?: string | null } | null }; +export type GetAgentsDataFromQuoteQueryVariables = Exact<{ + quoteId: Scalars['Uuid']; +}>; + + +export type GetAgentsDataFromQuoteQuery = { __typename?: 'Query', quote?: { __typename?: 'quote', evo_supplier_accountid?: any | null, evo_dealer_person_accountid?: any | null, evo_dealer_reward_conditionid?: any | null, evo_dealer_reward_total?: any | null, evo_dealer_broker_accountid?: any | null, evo_dealer_broker_reward_conditionid?: any | null, evo_dealer_broker_reward_total?: any | null, evo_agent_accountid?: any | null, evo_agent_reward_conditionid?: any | null, evo_agent_reward_total?: any | null, evo_double_agent_accountid?: any | null, evo_double_agent_reward_conditionid?: any | null, evo_double_agent_reward_total?: any | null, evo_broker_accountid?: any | null, evo_broker_reward_conditionid?: any | null, evo_broker_reward_total?: any | null, evo_fin_department_accountid?: any | null, evo_fin_department_reward_conditionid?: any | null, evo_fin_department_reward_total?: any | null } | null }; + +export type GetRewardConditionsQueryVariables = Exact<{ + agentid: Scalars['Uuid']; + currentDate?: InputMaybe; +}>; + + +export type GetRewardConditionsQuery = { __typename?: 'Query', evo_reward_conditions?: Array<{ __typename?: 'evo_reward_condition', evo_reward_summ?: any | null, label?: string | null, value?: any | null } | null> | null }; + +export type GetRewardSummQueryVariables = Exact<{ + conditionId: Scalars['Uuid']; +}>; + + +export type GetRewardSummQuery = { __typename?: 'Query', evo_reward_condition?: { __typename?: 'evo_reward_condition', evo_reward_summ?: any | null } | null }; + +export type GetRewardConditionQueryVariables = Exact<{ + conditionId: Scalars['Uuid']; +}>; + + +export type GetRewardConditionQuery = { __typename?: 'Query', evo_reward_condition?: { __typename?: 'evo_reward_condition', evo_reward_summ?: any | null, evo_reduce_reward?: boolean | null, evo_min_reward_summ?: any | null } | null }; + export type GetAgentAccountIdFromLeadQueryVariables = Exact<{ leadid: Scalars['Uuid']; }>; @@ -273,21 +302,6 @@ export type GetAgentQueryVariables = Exact<{ export type GetAgentQuery = { __typename?: 'Query', agent?: { __typename?: 'account', label?: string | null, value?: any | null } | null }; -export type GetRewardConditionsQueryVariables = Exact<{ - agentid: Scalars['Uuid']; - currentDate?: InputMaybe; -}>; - - -export type GetRewardConditionsQuery = { __typename?: 'Query', evo_reward_conditions?: Array<{ __typename?: 'evo_reward_condition', evo_reward_summ?: any | null, label?: string | null, value?: any | null } | null> | null }; - -export type GetRewardSummQueryVariables = Exact<{ - conditionId: Scalars['Uuid']; -}>; - - -export type GetRewardSummQuery = { __typename?: 'Query', evo_reward_condition?: { __typename?: 'evo_reward_condition', evo_reward_summ?: any | null } | null }; - export type GetRewardWithoutOtherAgentQueryVariables = Exact<{ conditionId: Scalars['Uuid']; }>; @@ -295,13 +309,6 @@ export type GetRewardWithoutOtherAgentQueryVariables = Exact<{ export type GetRewardWithoutOtherAgentQuery = { __typename?: 'Query', evo_reward_condition?: { __typename?: 'evo_reward_condition', evo_agency_agreementidData?: { __typename?: 'evo_agency_agreement', evo_reward_without_other_agent?: boolean | null } | null } | null }; -export type GetRewardConditionQueryVariables = Exact<{ - conditionId: Scalars['Uuid']; -}>; - - -export type GetRewardConditionQuery = { __typename?: 'Query', evo_reward_condition?: { __typename?: 'evo_reward_condition', evo_reward_summ?: any | null, evo_reduce_reward?: boolean | null, evo_min_reward_summ?: any | null } | null }; - export type GetDealerPersonQueryVariables = Exact<{ dealerId: Scalars['Uuid']; }>; diff --git a/next.config.js b/next.config.js index af96319..45a8a9c 100644 --- a/next.config.js +++ b/next.config.js @@ -16,7 +16,6 @@ const nextConfig = { compiler: { styledComponents: true, }, - pageExtensions: ['tsx', 'jsx'], rewrites: process.env.NODE_ENV === 'development' && async function rewrites() { diff --git a/package.json b/package.json index 18e03df..ef2fd00 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,11 @@ "@ant-design/icons": "^4.7.0", "@apollo/client": "^3.7.0", "@fontsource/montserrat": "^4.5.12", - "@tanstack/react-query": "^4.13.0", + "@tanstack/react-query": "^4.14.1", + "@trpc/client": "^10.0.0-rc.3", + "@trpc/next": "^10.0.0-rc.3", + "@trpc/react-query": "^10.0.0-rc.3", + "@trpc/server": "^10.0.0-rc.3", "antd": "^4.21.3", "axios": "^1.1.3", "dayjs": "^1.11.2", @@ -40,7 +44,9 @@ "rebass": "^4.0.7", "sharp": "^0.30.4", "styled-components": "^5.3.5", - "use-debounce": "^8.0.1" + "superjson": "^1.11.0", + "use-debounce": "^8.0.1", + "zod": "^3.19.1" }, "devDependencies": { "@graphql-codegen/cli": "2.12.0", diff --git a/pages/_app.jsx b/pages/_app.jsx index 554cab5..6535411 100644 --- a/pages/_app.jsx +++ b/pages/_app.jsx @@ -9,6 +9,7 @@ import 'normalize.css'; import { useMemo } from 'react'; import StoreProvider from 'stores/Provider'; import { ThemeProvider } from 'styled-components'; +import { trpcClient } from 'trpc/client'; import { GlobalStyle } from 'UIKit/colors'; import theme from 'UIKit/theme'; import '../styles/fonts.css'; @@ -51,4 +52,4 @@ function App({ Component, pageProps }) { ); } -export default App; +export default trpcClient.withTRPC(App); diff --git a/pages/api/trpc/[trpc].ts b/pages/api/trpc/[trpc].ts new file mode 100644 index 0000000..47e091a --- /dev/null +++ b/pages/api/trpc/[trpc].ts @@ -0,0 +1,8 @@ +import * as trpcNext from '@trpc/server/adapters/next'; +import appRouter from 'trpc/routers'; + +export default trpcNext.createNextApiHandler({ + router: appRouter, + // eslint-disable-next-line object-curly-newline + createContext: () => ({}), +}); diff --git a/pages/index.jsx b/pages/index.jsx index fc5ad4a..7101151 100644 --- a/pages/index.jsx +++ b/pages/index.jsx @@ -11,6 +11,7 @@ import injectDefaultReactions from 'process/init/inject-reactions/default'; import { useEffect } from 'react'; import { useStore } from 'stores/hooks'; import styled from 'styled-components'; +import { trpcPureClient } from 'trpc/client'; import { Box } from 'UIKit/grid'; import { min } from 'UIKit/mq'; @@ -45,7 +46,7 @@ function Home() { useEffect(() => { getData(apolloClient, store); - injectDefaultReactions(store, apolloClient, queryClient); + injectDefaultReactions(store, apolloClient, queryClient, trpcPureClient); }, []); return ( diff --git a/process/init/inject-reactions/default.js b/process/init/inject-reactions/default.js index 31b0b3d..64bd802 100644 --- a/process/init/inject-reactions/default.js +++ b/process/init/inject-reactions/default.js @@ -1,3 +1,4 @@ +import loadKpReactions from 'process/load-kp/reactions'; import * as calculateReactions from '../../calculate/reactions'; import * as fingapReactions from '../../fingap/reactions'; import * as leadOpportunityReactions from '../../lead-opportunity/reactions'; @@ -7,7 +8,7 @@ import * as agentsReactions from '../../supplier-agent/reactions/agents'; import * as supplierReactions from '../../supplier-agent/reactions/supplier'; import setInitialValuesReactions from '../set-values/reactions'; -export default function injectDefaultReactions(store, apolloClient, queryClient) { +export default function injectDefaultReactions(store, apolloClient, queryClient, trpcClient) { leadOpportunityReactions.common(store, apolloClient, queryClient); leadOpportunityReactions.urls(store, apolloClient, queryClient); paymentsReactions(store, apolloClient, queryClient); @@ -21,4 +22,5 @@ export default function injectDefaultReactions(store, apolloClient, queryClient) fingapReactions.common(store, apolloClient, queryClient); fingapReactions.validation(store, apolloClient, queryClient); setInitialValuesReactions(store, apolloClient, queryClient); + loadKpReactions(store, apolloClient, queryClient, trpcClient); } diff --git a/process/load-kp/reactions.ts b/process/load-kp/reactions.ts new file mode 100644 index 0000000..d0d2c6a --- /dev/null +++ b/process/load-kp/reactions.ts @@ -0,0 +1,45 @@ +import type { ApolloClient } from '@apollo/client'; +import type { QueryClient } from '@tanstack/react-query'; +import message from 'Elements/message'; +import { reaction, toJS } from 'mobx'; +import type RootStore from 'stores/root'; +import type { TRPCPureClient } from 'trpc/types'; + +export default function loadKpReactions( + store: RootStore, + apolloClient: ApolloClient, + queryClient: QueryClient, + trpcClient: TRPCPureClient +) { + const { $calculation, $process } = store; + + reaction( + () => $calculation.element('selectQuote').getValue(), + (quoteId) => { + if (!quoteId) return; + + $process.add('LoadKP'); + + const quoteName = $calculation.element('selectQuote').getOption()?.label; + + trpcClient.quote.getValues + .query({ + values: toJS($calculation.$values.values), + }) + .then(({ values }) => { + $calculation.$values.hydrate(values); + message.success({ + content: `КП ${quoteName} загружено`, + }); + }) + .catch(() => { + message.error({ + content: `Ошибка во время загрузки КП ${quoteName}`, + }); + }) + .finally(() => { + $process.delete('LoadKP'); + }); + } + ); +} diff --git a/process/supplier-agent/get-data-from-kp.ts b/process/supplier-agent/get-data-from-kp.ts new file mode 100644 index 0000000..e9ebc4e --- /dev/null +++ b/process/supplier-agent/get-data-from-kp.ts @@ -0,0 +1,63 @@ +import { gql } from '@apollo/client'; +import initializeApollo from 'apollo/client'; +import type { + GetAgentsDataFromQuoteQuery, + GetAgentsDataFromQuoteQueryVariables, +} from 'graphql/crm.types'; +import type { CalculationValues } from 'stores/calculation/values/types'; + +const QUERY_GET_AGENTS_DATA_FROM_QUOTE = gql` + query GetAgentsDataFromQuote($quoteId: Uuid!) { + quote(quoteId: $quoteId) { + evo_supplier_accountid + evo_dealer_person_accountid + evo_dealer_reward_conditionid + evo_dealer_reward_total + evo_dealer_broker_accountid + evo_dealer_broker_reward_conditionid + evo_dealer_broker_reward_total + evo_agent_accountid + evo_agent_reward_conditionid + evo_agent_reward_total + evo_double_agent_accountid + evo_double_agent_reward_conditionid + evo_double_agent_reward_total + evo_broker_accountid + evo_broker_reward_conditionid + evo_broker_reward_total + evo_fin_department_accountid + evo_fin_department_reward_conditionid + evo_fin_department_reward_total + } + } +`; + +type SupplierData = { + values: Partial; +}; + +export default async function getSupplierAgentsDataFromKP( + values: CalculationValues +): Promise { + const apolloClient = initializeApollo(); + + const { + data: { quote }, + } = await apolloClient.query({ + query: QUERY_GET_AGENTS_DATA_FROM_QUOTE, + variables: { + quoteId: values.quote!, + }, + }); + + return { + values: { + dealer: quote?.evo_supplier_accountid, + dealerRewardCondition: quote?.evo_dealer_reward_conditionid, + dealerBroker: quote?.evo_dealer_broker_accountid, + dealerBrokerRewardCondition: quote?.evo_dealer_broker_reward_conditionid, + calcDoubleAgent: quote?.evo_double_agent_accountid, + calcDoubleAgentRewardCondition: quote?.evo_double_agent_reward_conditionid, + }, + }; +} diff --git a/process/supplier-agent/lib/create-reactions.ts b/process/supplier-agent/lib/create-reactions.ts index 77f8043..e3a464f 100644 --- a/process/supplier-agent/lib/create-reactions.ts +++ b/process/supplier-agent/lib/create-reactions.ts @@ -9,7 +9,6 @@ import { reaction } from 'mobx'; import type RootStore from 'stores/root'; import ValidationHelper from 'stores/validation/helper'; import { normalizeOptions } from 'tools/entity'; -import { makeDisposable } from 'tools/mobx'; dayjs.extend(utc); @@ -40,37 +39,33 @@ export function fillAgentRewardReaction( const { $calculation, $process } = store; const { agentField, rewardConditionField } = agentParams; - makeDisposable( - () => - reaction( - () => $calculation.element(agentField).getValue(), - async (agentId) => { - if (!agentId) { - $calculation.element(rewardConditionField).reset(); + reaction( + () => $calculation.element(agentField).getValue(), + async (agentId) => { + if (!agentId) { + $calculation.element(rewardConditionField).reset(); - return; - } - const { - data: { evo_reward_conditions }, - } = await apolloClient.query< - CRMTypes.GetRewardConditionsQuery, - CRMTypes.GetRewardConditionsQueryVariables - >({ - query: QUERY_GET_REWARD_CONDITIONS, - variables: { - agentid: agentId, - currentDate: dayjs().toISOString(), - }, - }); + return; + } + const { + data: { evo_reward_conditions }, + } = await apolloClient.query< + CRMTypes.GetRewardConditionsQuery, + CRMTypes.GetRewardConditionsQueryVariables + >({ + query: QUERY_GET_REWARD_CONDITIONS, + variables: { + agentid: agentId, + currentDate: dayjs().toISOString(), + }, + }); - if (evo_reward_conditions?.length) { - $calculation - .element(rewardConditionField) - .setOptions(normalizeOptions(evo_reward_conditions)); - } - } - ), - () => $process.has('LoadKP') + if (evo_reward_conditions?.length) { + $calculation + .element(rewardConditionField) + .setOptions(normalizeOptions(evo_reward_conditions)); + } + } ); } @@ -93,35 +88,33 @@ export function fillAgentRewardSummReaction( const { $calculation, $process } = store; const { rewardConditionField, rewardSummField } = agentParams; - makeDisposable( - () => - reaction( - () => $calculation.element(rewardConditionField).getValue(), - async (rewardConditionId) => { - if (!rewardConditionId) { - $calculation.element(rewardSummField).reset(); + reaction( + () => $calculation.element(rewardConditionField).getValue(), + async (rewardConditionId) => { + if (!rewardConditionId) { + $calculation.element(rewardSummField).reset(); - return; - } + return; + } - const { - data: { evo_reward_condition }, - } = await apolloClient.query< - CRMTypes.GetRewardSummQuery, - CRMTypes.GetRewardSummQueryVariables - >({ - query: QUERY_GET_REWARD_SUMM, - variables: { - conditionId: rewardConditionId, - }, - }); + const { + data: { evo_reward_condition }, + } = await apolloClient.query< + CRMTypes.GetRewardSummQuery, + CRMTypes.GetRewardSummQueryVariables + >({ + query: QUERY_GET_REWARD_SUMM, + variables: { + conditionId: rewardConditionId, + }, + }); - $calculation.element(rewardSummField).setValue(evo_reward_condition?.evo_reward_summ); + if (!$process.has('LoadKP')) { + $calculation.element(rewardSummField).setValue(evo_reward_condition?.evo_reward_summ); + } - $calculation.element(rewardSummField).unblock(); - } - ), - () => $process.has('LoadKP') + $calculation.element(rewardSummField).unblock(); + } ); } diff --git a/process/supplier-agent/reactions/supplier.ts b/process/supplier-agent/reactions/supplier.ts index 044f516..aa14393 100644 --- a/process/supplier-agent/reactions/supplier.ts +++ b/process/supplier-agent/reactions/supplier.ts @@ -7,7 +7,6 @@ import type * as CRMTypes from 'graphql/crm.types'; import { reaction } from 'mobx'; import type RootStore from 'stores/root'; import { normalizeOptions } from 'tools/entity'; -import { makeDisposable } from 'tools/mobx'; import * as createReactions from '../lib/create-reactions'; import * as query from '../lib/query'; @@ -32,39 +31,36 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient - reaction( - () => $calculation.element('selectDealer').getValue(), - async (dealerId) => { - if (!dealerId) { - $calculation.element('selectDealerPerson').reset(); - $calculation.element('selectDealerBroker').reset(); + reaction( + () => $calculation.element('selectDealer').getValue(), + async (dealerId) => { + if (!dealerId) { + $calculation.element('selectDealerPerson').reset(); + $calculation.element('selectDealerBroker').reset(); - return; - } + return; + } - const { - data: { salon_providers }, - } = await apolloClient.query< - CRMTypes.GetDealerPersonQuery, - CRMTypes.GetDealerPersonQueryVariables - >({ - query: QUERY_GET_DEALER_PERSON, - variables: { - dealerId, - }, - }); + const { + data: { salon_providers }, + } = await apolloClient.query< + CRMTypes.GetDealerPersonQuery, + CRMTypes.GetDealerPersonQueryVariables + >({ + query: QUERY_GET_DEALER_PERSON, + variables: { + dealerId, + }, + }); - if (salon_providers?.length) { - $calculation - .element('selectDealerPerson') - .setOptions(normalizeOptions(salon_providers)) - .setValue(salon_providers[0]?.value); - } + if (salon_providers?.length) { + $calculation.element('selectDealerPerson').setOptions(normalizeOptions(salon_providers)); + + if (!$process.has('LoadKP')) { + $calculation.element('selectDealerPerson').setValue(salon_providers[0]?.value); } - ), - () => $process.has('LoadKP') + } + } ); /** @@ -79,49 +75,46 @@ export function commonReactions(store: RootStore, apolloClient: ApolloClient - reaction( - () => $calculation.element('selectDealerPerson').getValue(), - async (dealerPersonId) => { - if (!dealerPersonId) { - return; - } + reaction( + () => $calculation.element('selectDealerPerson').getValue(), + async (dealerPersonId) => { + if (!dealerPersonId) { + return; + } - const { - data: { dealer }, - } = await apolloClient.query< - CRMTypes.GetBrokerAccountIdFromDealerQuery, - CRMTypes.GetBrokerAccountIdFromDealerQueryVariables - >({ - query: QUERY_GET_BROKER_ACCOUNTID_FROM_DEALER, - variables: { - dealerId: dealerPersonId, - }, - }); + const { + data: { dealer }, + } = await apolloClient.query< + CRMTypes.GetBrokerAccountIdFromDealerQuery, + CRMTypes.GetBrokerAccountIdFromDealerQueryVariables + >({ + query: QUERY_GET_BROKER_ACCOUNTID_FROM_DEALER, + variables: { + dealerId: dealerPersonId, + }, + }); - if (dealer?.evo_broker_accountid) { - const { - data: { agent: dealerBroker }, - } = await apolloClient.query({ - query: query.QUERY_GET_AGENT, - variables: { - agentid: dealer?.evo_broker_accountid, - }, - }); + if (dealer?.evo_broker_accountid) { + const { + data: { agent: dealerBroker }, + } = await apolloClient.query({ + query: query.QUERY_GET_AGENT, + variables: { + agentid: dealer?.evo_broker_accountid, + }, + }); - if (dealerBroker) { - $calculation - .element('selectDealerBroker') - .setOptions(normalizeOptions([dealerBroker])) - .setValue(dealerBroker.value); - } - } else { - $calculation.element('selectDealerBroker').reset(); + if (dealerBroker) { + $calculation.element('selectDealerBroker').setOptions(normalizeOptions([dealerBroker])); + + if (!$process.has('LoadKP')) { + $calculation.element('selectDealerBroker').setValue(dealerBroker.value); } } - ), - () => $process.has('LoadKP') + } else { + $calculation.element('selectDealerBroker').reset(); + } + } ); // Заполняем selectDealerRewardCondition diff --git a/stores/calculation/values/types.ts b/stores/calculation/values/types.ts index 1bb332a..74383c8 100644 --- a/stores/calculation/values/types.ts +++ b/stores/calculation/values/types.ts @@ -1,142 +1,5 @@ -export type CalculationValues = { - lead: string | null; - opportunity: string | null; - quote: string | null; - recalcWithRevision: boolean; - product: string | null; - clientRisk: string | null; - clientType: string | null; - leaseObjectPrice: number; - supplierCurrency: string | null; - supplierDiscountRub: number; - supplierDiscountPerc: number; - leasingPeriod: number; - firstPaymentPerc: number; - firstPaymentRub: number; - lastPaymentPerc: number; - lastPaymentRub: number; - lastPaymentRule: number | null; - importProgram: string | null; - importProgramSum: number; - addEquipmentPrice: number; - redemptionPaymentSum: number; - balanceHolder: number | null; - graphType: number | null; - parmentsDecreasePercent: number; - seasonType: number | null; - highSeasonStart: number | null; - comissionPerc: number; - comissionRub: number; - saleBonus: number; - IRR_Perc: number; - leaseObjectType: string | null; - deliveryTime: number | null; - leaseObjectCount: number; - withTrailer: boolean; - leaseObjectUsed: boolean; - maxMass: number; - countSeats: number; - maxSpeed: number; - brand: string | null; - model: string | null; - configuration: string | null; - leaseObjectYear: number; - engineType: number | null; - leaseObjectCategory: number | null; - leaseObjectMotorPower: number; - engineVolume: number; - leaseObjectUseFor: number | null; - dealer: string | null; - dealerPerson: string | null; - dealerRewardCondition: string | null; - dealerRewardSumm: number; - dealerBroker: string | null; - dealerBrokerRewardCondition: string | null; - dealerBrokerRewardSumm: number; - indAgent: string | null; - indAgentRewardCondition: string | null; - indAgentRewardSumm: number; - calcDoubleAgent: string | null; - calcDoubleAgentRewardCondition: string | null; - calcDoubleAgentRewardSumm: number; - calcBroker: string | null; - calcBrokerRewardCondition: string | null; - calcBrokerRewardSum: number; - calcFinDepartment: string | null; - finDepartmentRewardCondtion: string | null; - finDepartmentRewardSumm: number; - GPSBrand: string | null; - GPSModel: string | null; - regionRegistration: string | null; - townRegistration: string | null; - infuranceOPF: number | null; - insKaskoType: number | null; - insDecentral: boolean; - insFranchise: number; - insUnlimitDrivers: boolean; - insAgeDrivers: number; - insExpDrivers: number; - INNForCalc: number; - lastPaymentRedemption: boolean; - priceWithDiscount: boolean; - fullPriceWithDiscount: boolean; - costIncrease: boolean; - insurance: boolean; - registrationQuote: boolean; - technicalCardQuote: boolean; - NSIB: boolean; - quoteName: string | null; - quoteContactGender: number | null; - quoteRedemptionGraph: boolean; - showFinGAP: boolean; - tarif: string | null; - creditRate: number; - rate: string | null; - requirementTelematic: number | null; - minPriceChange: number; - maxPriceChange: number; - importerRewardPerc: number; - importerRewardRub: number; - disableChecks: boolean; - registration: string | null; - insNSIB: string | null; - technicalCard: string | null; - telematic: string | null; - tracker: string | null; - mileage: number; - calcType: number | null; - totalPayments: number; - objectRegistration: number | null; - objectRegionRegistration: string | null; - vehicleTaxInYear: number; - vehicleTaxInLeasingPeriod: number; - objectCategoryTax: number | null; - objectTypeTax: number | null; - typePTS: number | null; - legalClientRegion: string | null; - legalClientTown: string | null; - subsidy: string | null; - fuelCard: string | null; - leaseObjectPriceWthtVAT: number; - VATInLeaseObjectPrice: number; - engineHours: number; - bonusCoefficient: number; - - /** Link Values */ - kpUrl: string | null; - leadUrl: string | null; - opportunityUrl: string | null; - quoteUrl: string | null; - - /** Readonly Values */ - leaseObjectRiskName: string | null; - insKaskoPriceLeasePeriod: number; - irrInfo: string | null; - registrationDescription: string | null; - depreciationGroup: string | null; - subsidySum: number; - plPriceRub: number; - discountRub: number; -}; +import type ValuesSchema from 'config/schema/values'; +import type { z } from 'zod'; +export type CalculationValues = z.infer; export type Values = keyof CalculationValues; diff --git a/trpc/client.ts b/trpc/client.ts new file mode 100644 index 0000000..9270270 --- /dev/null +++ b/trpc/client.ts @@ -0,0 +1,35 @@ +import { createTRPCProxyClient, httpBatchLink } from '@trpc/client'; +import { createTRPCNext } from '@trpc/next'; +import SuperJSON from 'superjson'; +import type { AppRouter } from './routers'; + +function getBaseUrl() { + if (typeof window !== 'undefined') { + return ''; + } + + return `http://localhost:${process.env.PORT ?? 3000}`; +} + +export const trpcClient = createTRPCNext({ + config() { + return { + links: [ + httpBatchLink({ + url: `${getBaseUrl()}/api/trpc`, + }), + ], + transformer: SuperJSON, + }; + }, + ssr: true, +}); + +export const trpcPureClient = createTRPCProxyClient({ + links: [ + httpBatchLink({ + url: `${getBaseUrl()}/api/trpc`, + }), + ], + transformer: SuperJSON, +}); diff --git a/trpc/routers/index.ts b/trpc/routers/index.ts new file mode 100644 index 0000000..190e228 --- /dev/null +++ b/trpc/routers/index.ts @@ -0,0 +1,10 @@ +import { t } from '../server'; +import quoteRouter from './quote'; + +const appRouter = t.router({ + quote: quoteRouter, +}); + +export type AppRouter = typeof appRouter; + +export default appRouter; diff --git a/trpc/routers/quote.ts b/trpc/routers/quote.ts new file mode 100644 index 0000000..51811f9 --- /dev/null +++ b/trpc/routers/quote.ts @@ -0,0 +1,39 @@ +import defaultValues from 'config/default-values'; +import ValuesSchema from 'config/schema/values'; +import getSupplierAgentsDataFromKP from 'process/supplier-agent/get-data-from-kp'; +import { z } from 'zod'; +import { t } from '../server'; + +const quoteRouter = t.router({ + getValues: t.procedure + .input( + z.object({ + values: ValuesSchema, + }) + ) + .output( + z.object({ + values: ValuesSchema, + }) + ) + .query(async ({ input }) => { + const { values } = await getSupplierAgentsDataFromKP(input.values); + + const { lead, opportunity, quote, leadUrl, opportunityUrl, quoteUrl } = input.values; + + return { + values: { + ...defaultValues, + lead, + leadUrl, + opportunity, + opportunityUrl, + quote, + quoteUrl, + ...values, + }, + }; + }), +}); + +export default quoteRouter; diff --git a/trpc/server.ts b/trpc/server.ts new file mode 100644 index 0000000..6adacac --- /dev/null +++ b/trpc/server.ts @@ -0,0 +1,7 @@ +/* eslint-disable import/prefer-default-export */ +import { initTRPC } from '@trpc/server'; +import SuperJSON from 'superjson'; + +export const t = initTRPC.create({ + transformer: SuperJSON, +}); diff --git a/trpc/types.ts b/trpc/types.ts new file mode 100644 index 0000000..b52d155 --- /dev/null +++ b/trpc/types.ts @@ -0,0 +1,5 @@ +import type { trpcClient, trpcPureClient } from './client'; + +export type TRPCClient = typeof trpcClient; + +export type TRPCPureClient = typeof trpcPureClient; diff --git a/yarn.lock b/yarn.lock index 839e250..847303c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2078,17 +2078,17 @@ dependencies: tslib "^2.4.0" -"@tanstack/query-core@4.13.0": - version "4.13.0" - resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-4.13.0.tgz#89153096d1fce42c0294fa1d1ae4b3e72aa5353b" - integrity sha512-PzmLQcEgC4rl2OzkiPHYPC9O79DFcMGaKsOzDEP+U4PJ+tbkcEP+Z+FQDlfvX8mCwYC7UNH7hXrQ5EdkGlJjVg== +"@tanstack/query-core@4.14.1": + version "4.14.1" + resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-4.14.1.tgz#a74c4da03e79a8be07fa5ab2ebc12865146915e2" + integrity sha512-mUejKoFDe4NZB8jQJR1uuAl6IwvkUpOD2m8NcuTVPOu0pcxeeFPdrnHaljwOEFPtlqXoiiIIQGYy6whjCMN+iQ== -"@tanstack/react-query@^4.13.0": - version "4.13.0" - resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-4.13.0.tgz#13797d590a6c0708545881e38aea5eb39b960c28" - integrity sha512-dI/5hJ/pGQ74P5hxBLC9h6K0/Cap2T3k0ZjjjFLBCNnohDYgl7LNmMopzrRzBHk2mMjf2hgXHIzcKNG8GOZ5hg== +"@tanstack/react-query@^4.14.1": + version "4.14.1" + resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-4.14.1.tgz#336545119b191e2096c394a3f2df2744cdc5e041" + integrity sha512-cRgNzigw4GSPwGlTEkXi8hi/xgUnSEt9jCkiC8oAT3PEIdsQ50onZcpXd+JNJcZk2RTh8KM1fGyWz6xYLiY8bg== dependencies: - "@tanstack/query-core" "4.13.0" + "@tanstack/query-core" "4.14.1" use-sync-external-store "^1.2.0" "@testing-library/dom@^8.5.0": @@ -2139,6 +2139,28 @@ resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-2.0.0.tgz#f544a148d3ab35801c1f633a7441fd87c2e484bf" integrity sha512-XCuKFP5PS55gnMVu3dty8KPatLqUoy/ZYzDzAGCQ8JNFCkLXzmI7vNHCR+XpbZaMWQK/vQubr7PkYq8g470J/A== +"@trpc/client@^10.0.0-rc.3": + version "10.0.0-rc.3" + resolved "https://registry.yarnpkg.com/@trpc/client/-/client-10.0.0-rc.3.tgz#22cea038dd5e38ce0e39ff4a339793bb5808ab18" + integrity sha512-gZYVfZra1YB2DluDY+GcIrL7KqPXCI9y0mF4utXYAUAl+jrxfLyG63pCv30USRRYlwwSKaoLJNpjQB9k05aYaw== + +"@trpc/next@^10.0.0-rc.3": + version "10.0.0-rc.3" + resolved "https://registry.yarnpkg.com/@trpc/next/-/next-10.0.0-rc.3.tgz#2585aa271c2916afb2f7291583c9299c324f799d" + integrity sha512-L9sHV93dPA4lZ/I8opfn3BJONnlnnJzJERggv7srt8H3aEhKVlNWIqxciNLHGXSq9q0C9i3Q6ND4F5kpRSQ5YA== + dependencies: + react-ssr-prepass "^1.5.0" + +"@trpc/react-query@^10.0.0-rc.3": + version "10.0.0-rc.3" + resolved "https://registry.yarnpkg.com/@trpc/react-query/-/react-query-10.0.0-rc.3.tgz#34a0449c62544522d1fa0f07844d1f11cf38eb55" + integrity sha512-nEBqkTdKsd+vIE4RuDRX/b8U5ayxENp0G2vnh/fn4hiIszrLiPTGvnhGCZm/fT2GGW5nk7EEBcqf2o0z3r8Ftg== + +"@trpc/server@^10.0.0-rc.3": + version "10.0.0-rc.3" + resolved "https://registry.yarnpkg.com/@trpc/server/-/server-10.0.0-rc.3.tgz#4ddc4410305b17a47248b3bfa881df3b52202e60" + integrity sha512-aSs3uDQ5wumwVjtxj855WLDjlDC+v50QoqHzvuptI33NhJZV7meu0pU2EJkrSZXFt27CSNfP9o2ZPfoReoPUuw== + "@tsconfig/node10@^1.0.7": version "1.0.9" resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.9.tgz#df4907fc07a886922637b15e02d4cebc4c0021b2" @@ -3897,6 +3919,13 @@ copy-anything@^2.0.1: dependencies: is-what "^3.14.1" +copy-anything@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/copy-anything/-/copy-anything-3.0.2.tgz#7189171ff5e1893b2287e8bf574b8cd448ed50b1" + integrity sha512-CzATjGXzUQ0EvuvgOCI6A4BGOo2bcVx8B+eC2nF862iv9fopnPQwlrbACakNCHRIJbCSBj+J/9JeDf60k64MkA== + dependencies: + is-what "^4.1.6" + copy-to-clipboard@^3.2.0: version "3.3.1" resolved "https://registry.yarnpkg.com/copy-to-clipboard/-/copy-to-clipboard-3.3.1.tgz#115aa1a9998ffab6196f93076ad6da3b913662ae" @@ -5928,6 +5957,11 @@ is-what@^3.14.1: resolved "https://registry.yarnpkg.com/is-what/-/is-what-3.14.1.tgz#e1222f46ddda85dead0fd1c9df131760e77755c1" integrity sha512-sNxgpk9793nzSs7bA6JQJGeIuRBQhAaNGG77kzYQgMkrID+lS6SlK07K5LaptscDlSaIgH+GPFzf+d75FVxozA== +is-what@^4.1.6: + version "4.1.7" + resolved "https://registry.yarnpkg.com/is-what/-/is-what-4.1.7.tgz#c41dc1d2d2d6a9285c624c2505f61849c8b1f9cc" + integrity sha512-DBVOQNiPKnGMxRMLIYSwERAS5MVY1B7xYiGnpgctsOFvVDz9f9PFXXxMcTOHuoqYp4NK9qFYQaIC1NRRxLMpBQ== + is-windows@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d" @@ -8315,6 +8349,11 @@ react-is@^18.0.0: resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.2.0.tgz#199431eeaaa2e09f86427efbb4f1473edb47609b" integrity sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w== +react-ssr-prepass@^1.5.0: + version "1.5.0" + resolved "https://registry.yarnpkg.com/react-ssr-prepass/-/react-ssr-prepass-1.5.0.tgz#bc4ca7fcb52365e6aea11cc254a3d1bdcbd030c5" + integrity sha512-yFNHrlVEReVYKsLI5lF05tZoHveA5pGzjFbFJY/3pOqqjGOmMmqx83N4hIjN2n6E1AOa+eQEUxs3CgRnPmT0RQ== + react@^18.2.0: version "18.2.0" resolved "https://registry.yarnpkg.com/react/-/react-18.2.0.tgz#555bd98592883255fa00de14f1151a917b5d77d5" @@ -9178,6 +9217,13 @@ styled-system@^5.0.0, styled-system@^5.1.5: "@styled-system/variant" "^5.1.5" object-assign "^4.1.1" +superjson@^1.11.0: + version "1.11.0" + resolved "https://registry.yarnpkg.com/superjson/-/superjson-1.11.0.tgz#f6e2ae0d8fbac61c3fca09ab6739ac9678414d1b" + integrity sha512-6PfAg1FKhqkwWvPb2uXhH4MkMttdc17eJ91+Aoz4s1XUEDZFmLfFx/xVA3wgkPxAGy5dpozgGdK6V/n20Wj9yg== + dependencies: + copy-anything "^3.0.2" + supports-color@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7" @@ -10028,3 +10074,8 @@ zen-observable@0.8.15, zen-observable@^0.8.0: version "0.8.15" resolved "https://registry.yarnpkg.com/zen-observable/-/zen-observable-0.8.15.tgz#96415c512d8e3ffd920afd3889604e30b9eaac15" integrity sha512-PQ2PC7R9rslx84ndNBZB/Dkv8V8fZEpk83RLgXtYd0fwUgEjseMn1Dgajh2x6S8QbZAFa9p2qVCEuYZNgve0dQ== + +zod@^3.19.1: + version "3.19.1" + resolved "https://registry.yarnpkg.com/zod/-/zod-3.19.1.tgz#112f074a97b50bfc4772d4ad1576814bd8ac4473" + integrity sha512-LYjZsEDhCdYET9ikFu6dVPGp2YH9DegXjdJToSzD9rO6fy4qiRYFoyEYwps88OseJlPyl2NOe2iJuhEhL7IpEA==