diff --git a/Components/Calculation/Form/Insurance/FinGAPTable/types.ts b/Components/Calculation/Form/Insurance/FinGAPTable/types.ts index b7c2efd..0438b92 100644 --- a/Components/Calculation/Form/Insurance/FinGAPTable/types.ts +++ b/Components/Calculation/Form/Insurance/FinGAPTable/types.ts @@ -1,10 +1,4 @@ -export type Risk = { - key: string; - riskId: string; - riskName: string; - calcType: number; - premiumPerc: number; - sum: number; - premium: number; - keys?: string[]; -}; +import type { RiskSchema } from 'config/schema/fingap'; +import type { z } from 'zod'; + +export type Risk = z.infer; diff --git a/Components/Calculation/Form/Insurance/InsuranceTable/types.ts b/Components/Calculation/Form/Insurance/InsuranceTable/types.ts index 163f6b4..9056b86 100644 --- a/Components/Calculation/Form/Insurance/InsuranceTable/types.ts +++ b/Components/Calculation/Form/Insurance/InsuranceTable/types.ts @@ -1,15 +1,10 @@ +import type { KeysSchema, RowSchema } from 'config/schema/insurance'; import type { BaseOption, Status } from 'Elements/types'; +import type { z } from 'zod'; -export type Keys = 'osago' | 'kasko' | 'fingap'; +export type Keys = z.infer; -export type RowValues = { - key: Keys; - policyType: string; - insuranceCompany: string | null; - insured: 100_000_000 | 100_000_001 | null; - insCost: number; - insTerm: 100_000_000 | 100_000_001 | null; -}; +export type RowValues = z.infer; export type Values = Exclude; diff --git a/config/schema/fingap.ts b/config/schema/fingap.ts new file mode 100644 index 0000000..186a617 --- /dev/null +++ b/config/schema/fingap.ts @@ -0,0 +1,13 @@ +/* eslint-disable import/prefer-default-export */ +import { z } from 'zod'; + +export const RiskSchema = z.object({ + key: z.string(), + riskId: z.string(), + riskName: z.string(), + calcType: z.number(), + premiumPerc: z.number(), + sum: z.number(), + premium: z.number(), + keys: z.array(z.string()).optional(), +}); diff --git a/config/schema/insurance.ts b/config/schema/insurance.ts new file mode 100644 index 0000000..5d7a55b --- /dev/null +++ b/config/schema/insurance.ts @@ -0,0 +1,12 @@ +import { z } from 'zod'; + +export const KeysSchema = z.union([z.literal('osago'), z.literal('kasko'), z.literal('fingap')]); + +export const RowSchema = z.object({ + key: KeysSchema, + policyType: z.string(), + insuranceCompany: z.string().nullable(), + insured: z.number().nullable(), + insCost: z.number(), + insTerm: z.number().nullable(), +}); diff --git a/graphql/crm.schema.graphql b/graphql/crm.schema.graphql index 9b8cc4a..a04ef6e 100644 --- a/graphql/crm.schema.graphql +++ b/graphql/crm.schema.graphql @@ -1222,7 +1222,7 @@ type quote { evo_price_without_nds_supplier_currency: Decimal evo_price_with_discount: Boolean evo_price_wthout_discount_nds_sup_currency: Decimal - evo_product_risks: [evo_product_risk] + evo_product_risks: [evo_product_risk!] evo_programsolution: Int evo_program_import_subsidyid: Uuid evo_program_import_subsidy_sum: Decimal diff --git a/graphql/crm.types.ts b/graphql/crm.types.ts index 8a9255c..df68d0f 100644 --- a/graphql/crm.types.ts +++ b/graphql/crm.types.ts @@ -122,6 +122,13 @@ export type GetCurrencySymbolQueryVariables = Exact<{ export type GetCurrencySymbolQuery = { __typename?: 'Query', transactioncurrency: { __typename?: 'transactioncurrency', currencysymbol: string | null } | null }; +export type GetRisksDataFromQuoteQueryVariables = Exact<{ + quoteId: Scalars['Uuid']; +}>; + + +export type GetRisksDataFromQuoteQuery = { __typename?: 'Query', quote: { __typename?: 'quote', evo_fingap_accountid: string | null, evo_fingap_payer: number | null, evo_fingap_period: number | null, evo_product_risks: Array<{ __typename?: 'evo_product_risk', evo_addproduct_typeid: string | null }> | null } | null }; + export type GetFinGapAddProductTypesQueryVariables = Exact<{ currentDate: InputMaybe; }>; diff --git a/process/fingap/get-kp-data.ts b/process/fingap/get-kp-data.ts new file mode 100644 index 0000000..34ab45c --- /dev/null +++ b/process/fingap/get-kp-data.ts @@ -0,0 +1,77 @@ +import { gql } from '@apollo/client'; +import initializeApollo from 'apollo/client'; +import { RowSchema } from 'config/schema/insurance'; +import { defaultValues } from 'config/tables/insurance-table'; +import type * as CRMTypes from 'graphql/crm.types'; +import { z } from 'zod'; +import type { GetQuoteDataInput } from '../load-kp/types'; +import { GetQuoteDataOutputSchema } from '../load-kp/types'; + +const DEFAULT_FINGAP_ROW = defaultValues.find((x) => x.key === 'fingap')!; + +const QUERY_GET_RISKS = gql` + query GetRisksDataFromQuote($quoteId: Uuid!) { + quote(quoteId: $quoteId) { + evo_fingap_accountid + evo_fingap_payer + evo_fingap_period + evo_product_risks { + evo_addproduct_typeid + } + } + } +`; + +const QuoteFingapProcessDataSchema = z.object({ + insurance: z.object({ + values: z.object({ + fingap: RowSchema, + }), + }), + fingap: GetQuoteDataOutputSchema.shape.fingap, +}); + +type QuoteFingapProcessData = z.infer; + +export default async function getFingapDataFromKP({ + values: { quote: quoteId }, +}: GetQuoteDataInput): Promise { + const apolloClient = initializeApollo(); + + const { + data: { quote }, + } = await apolloClient.query< + CRMTypes.GetRisksDataFromQuoteQuery, + CRMTypes.GetRisksDataFromQuoteQueryVariables + >({ + query: QUERY_GET_RISKS, + variables: { + quoteId, + }, + }); + + const values: Array<{ key: string }> = []; + quote?.evo_product_risks?.forEach((x) => { + if (x.evo_addproduct_typeid) { + values.push({ + key: x.evo_addproduct_typeid, + }); + } + }); + + return { + fingap: { + values, + }, + insurance: { + values: { + fingap: { + ...DEFAULT_FINGAP_ROW, + insuranceCompany: quote?.evo_fingap_accountid || null, + insured: quote?.evo_fingap_payer || null, + insTerm: quote?.evo_fingap_period || null, + }, + }, + }, + }; +} diff --git a/process/load-kp/reactions.ts b/process/load-kp/reactions.ts index 04f203d..8a2146d 100644 --- a/process/load-kp/reactions.ts +++ b/process/load-kp/reactions.ts @@ -2,6 +2,7 @@ import message from 'Elements/message'; import { reaction } from 'mobx'; import type { ReactionsContext } from 'process/types'; import { pick } from 'radash'; +import extend from 'stores/tables/insurance/tools'; const key = 'KP_LOADING_INFO'; @@ -31,7 +32,7 @@ export default function loadKpReactions({ store, trpcClient }: ReactionsContext) trpcClient.quote.getData .query(payload) - .then(({ values, payments }) => { + .then(({ values, payments, insurance, fingap }) => { $calculation.$values.setValues({ values, exclude: ['lead', 'opportunity', 'quote', 'leadUrl', 'opportunityUrl', 'quoteUrl'], @@ -39,6 +40,18 @@ export default function loadKpReactions({ store, trpcClient }: ReactionsContext) $tables.payments.setValues(payments.values); + if (insurance.values.osago) { + extend($tables.insurance).setRowValues(insurance.values.osago); + } + if (insurance.values.kasko) { + extend($tables.insurance).setRowValues(insurance.values.kasko); + } + if (insurance.values.fingap) { + extend($tables.insurance).setRowValues(insurance.values.fingap); + } + + if (fingap) $tables.fingap.setSelectedKeys(fingap.values?.map((x) => x.key)); + message.success({ key, content: `КП ${quoteName} загружено`, diff --git a/process/load-kp/types.ts b/process/load-kp/types.ts index 127480f..2254cbf 100644 --- a/process/load-kp/types.ts +++ b/process/load-kp/types.ts @@ -1,24 +1,36 @@ +import { RiskSchema } from 'config/schema/fingap'; +import { KeysSchema, RowSchema } from 'config/schema/insurance'; import PaymentsSchema from 'config/schema/payments'; import ValuesSchema from 'config/schema/values'; import { z } from 'zod'; +const { quote, recalcWithRevision } = ValuesSchema.shape; export const GetQuoteDataInputSchema = z.object({ - values: ValuesSchema.pick({ - recalcWithRevision: true, - }).extend({ - quote: ValuesSchema.shape.quote.unwrap(), - }), + values: z + .object({ + quote: quote.unwrap(), + recalcWithRevision, + }) + .required(), }); export type GetQuoteDataInput = z.infer; export const GetQuoteDataOutputSchema = z.object({ values: ValuesSchema, payments: PaymentsSchema, + insurance: z + .object({ + values: z.record(KeysSchema, RowSchema), + }) + .required(), + fingap: z + .object({ + values: z.array( + RiskSchema.pick({ + key: true, + }) + ), + }) + .optional(), }); export type GetQuoteDataOutput = z.infer; - -export const QuoteProcessDataSchema = z.object({ - values: ValuesSchema.partial(), - payments: PaymentsSchema.optional(), -}); -export type QuoteProcessData = z.infer; diff --git a/process/payments/get-kp-data.ts b/process/payments/get-kp-data.ts index 23d31bd..03ff82e 100644 --- a/process/payments/get-kp-data.ts +++ b/process/payments/get-kp-data.ts @@ -3,7 +3,9 @@ import { gql } from '@apollo/client'; import initializeApollo from 'apollo/client'; import type * as CRMTypes from 'graphql/crm.types'; import { sort } from 'radash'; -import type { GetQuoteDataInput, QuoteProcessData } from '../load-kp/types'; +import { z } from 'zod'; +import type { GetQuoteDataInput } from '../load-kp/types'; +import { GetQuoteDataOutputSchema } from '../load-kp/types'; const QUERY_GET_PAYMENTS_DATA_FROM_QUOTE = gql` query GetPaymentsDataFromQuote($quoteId: Uuid!) { @@ -27,9 +29,16 @@ const QUERY_GET_PAYMENTS_DATA_FROM_QUOTE = gql` } `; +const { values, payments } = GetQuoteDataOutputSchema.shape; +const QuotePaymentsProcessDataSchema = z.object({ + values: values.partial(), + payments, +}); +type QuotePaymentsProcessData = z.infer; + export default async function getPaymentsDataFromKP({ values: { quote: quoteId, recalcWithRevision }, -}: GetQuoteDataInput): Promise { +}: GetQuoteDataInput): Promise { const apolloClient = initializeApollo(); const { @@ -48,9 +57,9 @@ export default async function getPaymentsDataFromKP({ ? Math.min(quote?.evo_period ?? 0, quote?.evo_accept_period ?? 0) : quote?.evo_period ?? 0; - let payments: Array = []; + let paymentsValues: Array = []; if (quote?.evo_graphs) { - payments = + paymentsValues = sort(quote?.evo_graphs, (evo_graph) => Date.parse(evo_graph?.createdon)) .at(0) ?.evo_planpayments?.slice(1, -1) @@ -69,7 +78,11 @@ export default async function getPaymentsDataFromKP({ lastPaymentPerc: quote?.evo_last_payment_perc || 0, }, payments: { - values: [quote?.evo_first_payment_perc || 0, ...payments, quote?.evo_last_payment_perc || 0], + values: [ + quote?.evo_first_payment_perc || 0, + ...paymentsValues, + quote?.evo_last_payment_perc || 0, + ], }, }; } diff --git a/process/supplier-agent/get-kp-values/index.ts b/process/supplier-agent/get-kp-values/index.ts index fd4f766..a6dea03 100644 --- a/process/supplier-agent/get-kp-values/index.ts +++ b/process/supplier-agent/get-kp-values/index.ts @@ -1,7 +1,9 @@ import { gql } from '@apollo/client'; import initializeApollo from 'apollo/client'; import type * as CRMTypes from 'graphql/crm.types'; -import type { GetQuoteDataInput, QuoteProcessData } from '../../load-kp/types'; +import { z } from 'zod'; +import type { GetQuoteDataInput } from '../../load-kp/types'; +import { GetQuoteDataOutputSchema } from '../../load-kp/types'; import getSums from './get-sums'; const QUERY_GET_AGENTS_DATA_FROM_QUOTE = gql` @@ -37,10 +39,14 @@ const QUERY_GET_AGENTS_DATA_FROM_QUOTE = gql` `; export type Quote = NonNullable; +const QuoteSupplierAgentProcessDataSchema = z.object({ + values: GetQuoteDataOutputSchema.shape.values.partial(), +}); +type QuoteSupplierAgentProcessData = z.infer; export default async function getSupplierAgentsDataFromKP({ values: { quote: quoteId }, -}: GetQuoteDataInput): Promise { +}: GetQuoteDataInput): Promise { const apolloClient = initializeApollo(); const { diff --git a/stores/tables/insurance/tools.ts b/stores/tables/insurance/tools.ts new file mode 100644 index 0000000..fee4aa6 --- /dev/null +++ b/stores/tables/insurance/tools.ts @@ -0,0 +1,12 @@ +import type * as Insurance from 'Components/Calculation/Form/Insurance/InsuranceTable/types'; +import type InsuranceTable from '.'; + +export default function extend(insuranceTable: InsuranceTable) { + return { + setRowValues(row: Insurance.RowValues) { + (Object.keys(row) as Insurance.Values[]).forEach((valueName) => { + insuranceTable.row(row.key).setValue(valueName, row[valueName]); + }); + }, + }; +} diff --git a/trpc/routers/quote.ts b/trpc/routers/quote.ts index 184ff8d..f40f2f9 100644 --- a/trpc/routers/quote.ts +++ b/trpc/routers/quote.ts @@ -1,21 +1,22 @@ import defaultValues from 'config/default-values'; +import { defaultValues as defaultInsuranceValues } from 'config/tables/insurance-table'; +import getFingapDataFromKP from 'process/fingap/get-kp-data'; import { GetQuoteDataInputSchema, GetQuoteDataOutputSchema } from 'process/load-kp/types'; import getPaymentsDataFromKP from 'process/payments/get-kp-data'; import getSupplierAgentsDataFromKP from 'process/supplier-agent/get-kp-values'; import { t } from '../server'; +const DEFAULT_OSAGO_ROW = defaultInsuranceValues.find((x) => x.key === 'osago'); +const DEFAULT_KASKO_ROW = defaultInsuranceValues.find((x) => x.key === 'kasko'); + const quoteRouter = t.router({ getData: t.procedure .input(GetQuoteDataInputSchema) .output(GetQuoteDataOutputSchema) .query(async ({ input }) => { const { values: supplierAgentsValues } = await getSupplierAgentsDataFromKP(input); - const { - values: paymentsValues, - payments = { - values: [], - }, - } = await getPaymentsDataFromKP(input); + const { values: paymentsValues, payments } = await getPaymentsDataFromKP(input); + const { fingap, insurance: fingapInsurance } = await getFingapDataFromKP(input); return { values: { @@ -24,6 +25,14 @@ const quoteRouter = t.router({ ...paymentsValues, }, payments, + insurance: { + values: { + osago: DEFAULT_OSAGO_ROW, + kasko: DEFAULT_KASKO_ROW, + fingap: fingapInsurance.values.fingap, + }, + }, + fingap, }; }), });