From f45ab7f3a9ca77d116c206a959ae2ec9a0773f33 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Wed, 22 Mar 2023 15:33:09 +0300 Subject: [PATCH] add additional data to request --- apps/web/graphql/crm.query.graphql | 1 + apps/web/graphql/crm.types.ts | 4 +- apps/web/process/calculate/lib/helper.ts | 52 ++++++++++++++ .../web/process/calculate/reactions/common.ts | 36 ++-------- apps/web/trpc/routers/calculate/index.ts | 4 +- .../calculate/{prepare-data.ts => request.ts} | 70 ++++++++++++++++--- apps/web/trpc/routers/calculate/types.ts | 13 +++- 7 files changed, 131 insertions(+), 49 deletions(-) create mode 100644 apps/web/process/calculate/lib/helper.ts rename apps/web/trpc/routers/calculate/{prepare-data.ts => request.ts} (94%) diff --git a/apps/web/graphql/crm.query.graphql b/apps/web/graphql/crm.query.graphql index d335344..5f21e40 100644 --- a/apps/web/graphql/crm.query.graphql +++ b/apps/web/graphql/crm.query.graphql @@ -98,6 +98,7 @@ query GetQuote($quoteId: Uuid!) { evo_seats evo_year evo_last_payment_perc + evo_maximum_percentage_av } } diff --git a/apps/web/graphql/crm.types.ts b/apps/web/graphql/crm.types.ts index 4e271c7..fd2c23b 100644 --- a/apps/web/graphql/crm.types.ts +++ b/apps/web/graphql/crm.types.ts @@ -175,7 +175,7 @@ export type GetQuoteQueryVariables = Exact<{ }>; -export type GetQuoteQuery = { __typename?: 'Query', quote: { __typename?: 'quote', evo_baseproductid: string | null, evo_one_year_insurance: boolean | null, evo_min_change_price: number | null, evo_max_price_change: number | null, evo_discount_supplier_currency: number | null, evo_equip_price: number | null, evo_program_import_subsidy_sum: number | null, evo_nds_in_price_supplier_currency: number | null, evo_supplier_currency_price: number | null, evo_approved_first_payment: number | null, evo_recalc_limit: number | null, evo_max_mass: number | null, evo_seats: number | null, evo_year: number | null, evo_last_payment_perc: number | null } | null }; +export type GetQuoteQuery = { __typename?: 'Query', quote: { __typename?: 'quote', evo_baseproductid: string | null, evo_one_year_insurance: boolean | null, evo_min_change_price: number | null, evo_max_price_change: number | null, evo_discount_supplier_currency: number | null, evo_equip_price: number | null, evo_program_import_subsidy_sum: number | null, evo_nds_in_price_supplier_currency: number | null, evo_supplier_currency_price: number | null, evo_approved_first_payment: number | null, evo_recalc_limit: number | null, evo_max_mass: number | null, evo_seats: number | null, evo_year: number | null, evo_last_payment_perc: number | null, evo_maximum_percentage_av: number | null } | null }; export type GetTarifsQueryVariables = Exact<{ currentDate: InputMaybe; @@ -598,7 +598,7 @@ export const GetLeadDocument = {"kind":"Document","definitions":[{"kind":"Operat export const GetOpportunityDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetOpportunity"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"opportunityid"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"opportunity"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"opportunityid"},"value":{"kind":"Variable","name":{"kind":"Name","value":"opportunityid"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_leadid"}},{"kind":"Field","name":{"kind":"Name","value":"accountidData"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_address_legalidData"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_region_fias_id"}},{"kind":"Field","name":{"kind":"Name","value":"evo_city_fias_id"}}]}}]}}]}}]}}]} as unknown as DocumentNode; export const GetOpportunitiesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetOpportunities"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"domainname"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"opportunities"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"owner_domainname"},"value":{"kind":"Variable","name":{"kind":"Name","value":"domainname"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"label"},"name":{"kind":"Name","value":"name"}},{"kind":"Field","alias":{"kind":"Name","value":"value"},"name":{"kind":"Name","value":"opportunityid"}}]}}]}}]} as unknown as DocumentNode; export const GetQuotesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuotes"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"leadid"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quotes"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"evo_leadid"},"value":{"kind":"Variable","name":{"kind":"Name","value":"leadid"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"label"},"name":{"kind":"Name","value":"evo_quotename"}},{"kind":"Field","alias":{"kind":"Name","value":"value"},"name":{"kind":"Name","value":"quoteid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_recalc_limit"}},{"kind":"Field","name":{"kind":"Name","value":"evo_statuscodeidData"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_id"}}]}},{"kind":"Field","name":{"kind":"Name","value":"evo_purchases_participation"}}]}}]}}]} as unknown as DocumentNode; -export const GetQuoteDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuote"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_baseproductid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_one_year_insurance"}},{"kind":"Field","name":{"kind":"Name","value":"evo_min_change_price"}},{"kind":"Field","name":{"kind":"Name","value":"evo_max_price_change"}},{"kind":"Field","name":{"kind":"Name","value":"evo_discount_supplier_currency"}},{"kind":"Field","name":{"kind":"Name","value":"evo_equip_price"}},{"kind":"Field","name":{"kind":"Name","value":"evo_program_import_subsidy_sum"}},{"kind":"Field","name":{"kind":"Name","value":"evo_nds_in_price_supplier_currency"}},{"kind":"Field","name":{"kind":"Name","value":"evo_supplier_currency_price"}},{"kind":"Field","name":{"kind":"Name","value":"evo_approved_first_payment"}},{"kind":"Field","name":{"kind":"Name","value":"evo_recalc_limit"}},{"kind":"Field","name":{"kind":"Name","value":"evo_max_mass"}},{"kind":"Field","name":{"kind":"Name","value":"evo_seats"}},{"kind":"Field","name":{"kind":"Name","value":"evo_year"}},{"kind":"Field","name":{"kind":"Name","value":"evo_last_payment_perc"}}]}}]}}]} as unknown as DocumentNode; +export const GetQuoteDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetQuote"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"quote"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"quoteId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"quoteId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_baseproductid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_one_year_insurance"}},{"kind":"Field","name":{"kind":"Name","value":"evo_min_change_price"}},{"kind":"Field","name":{"kind":"Name","value":"evo_max_price_change"}},{"kind":"Field","name":{"kind":"Name","value":"evo_discount_supplier_currency"}},{"kind":"Field","name":{"kind":"Name","value":"evo_equip_price"}},{"kind":"Field","name":{"kind":"Name","value":"evo_program_import_subsidy_sum"}},{"kind":"Field","name":{"kind":"Name","value":"evo_nds_in_price_supplier_currency"}},{"kind":"Field","name":{"kind":"Name","value":"evo_supplier_currency_price"}},{"kind":"Field","name":{"kind":"Name","value":"evo_approved_first_payment"}},{"kind":"Field","name":{"kind":"Name","value":"evo_recalc_limit"}},{"kind":"Field","name":{"kind":"Name","value":"evo_max_mass"}},{"kind":"Field","name":{"kind":"Name","value":"evo_seats"}},{"kind":"Field","name":{"kind":"Name","value":"evo_year"}},{"kind":"Field","name":{"kind":"Name","value":"evo_last_payment_perc"}},{"kind":"Field","name":{"kind":"Name","value":"evo_maximum_percentage_av"}}]}}]}}]} as unknown as DocumentNode; export const GetTarifsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetTarifs"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"currentDate"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"DateTime"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_tarifs"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"statecode"},"value":{"kind":"IntValue","value":"0"}},{"kind":"Argument","name":{"kind":"Name","value":"evo_datefrom_param"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"lte"},"value":{"kind":"Variable","name":{"kind":"Name","value":"currentDate"}}}]}},{"kind":"Argument","name":{"kind":"Name","value":"evo_dateto_param"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"gte"},"value":{"kind":"Variable","name":{"kind":"Name","value":"currentDate"}}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"label"},"name":{"kind":"Name","value":"evo_name"}},{"kind":"Field","alias":{"kind":"Name","value":"value"},"name":{"kind":"Name","value":"evo_tarifid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_tarifid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_baseproductid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_min_period"}},{"kind":"Field","name":{"kind":"Name","value":"evo_max_period"}},{"kind":"Field","name":{"kind":"Name","value":"evo_delivery_time"}},{"kind":"Field","name":{"kind":"Name","value":"evo_min_first_payment"}},{"kind":"Field","name":{"kind":"Name","value":"evo_max_first_payment"}},{"kind":"Field","name":{"kind":"Name","value":"evo_min_last_payment"}},{"kind":"Field","name":{"kind":"Name","value":"evo_max_last_payment"}},{"kind":"Field","name":{"kind":"Name","value":"evo_used"}}]}}]}}]} as unknown as DocumentNode; export const GetTarifDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetTarif"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"tarifId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"Uuid"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_tarif"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"evo_tarifid"},"value":{"kind":"Variable","name":{"kind":"Name","value":"tarifId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_irr"}},{"kind":"Field","name":{"kind":"Name","value":"evo_graphtype_exception"}},{"kind":"Field","name":{"kind":"Name","value":"evo_seasons_type_exception"}},{"kind":"Field","name":{"kind":"Name","value":"evo_min_decreasing_perc"}},{"kind":"Field","name":{"kind":"Name","value":"evo_min_irr"}},{"kind":"Field","name":{"kind":"Name","value":"evo_cut_irr_with_bonus_coefficient"}},{"kind":"Field","name":{"kind":"Name","value":"evo_max_irr"}},{"kind":"Field","name":{"kind":"Name","value":"evo_rates"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_datefrom"}},{"kind":"Field","name":{"kind":"Name","value":"evo_rateid"}}]}},{"kind":"Field","name":{"kind":"Name","value":"evo_irr_plan"}},{"kind":"Field","name":{"kind":"Name","value":"evo_margin_min"}}]}}]}}]} as unknown as DocumentNode; export const GetRatesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetRates"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"currentDate"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"DateTime"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_rates"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"statecode"},"value":{"kind":"IntValue","value":"0"}},{"kind":"Argument","name":{"kind":"Name","value":"evo_datefrom_param"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"lte"},"value":{"kind":"Variable","name":{"kind":"Name","value":"currentDate"}}}]}},{"kind":"Argument","name":{"kind":"Name","value":"evo_dateto_param"},"value":{"kind":"ObjectValue","fields":[{"kind":"ObjectField","name":{"kind":"Name","value":"gte"},"value":{"kind":"Variable","name":{"kind":"Name","value":"currentDate"}}}]}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","alias":{"kind":"Name","value":"label"},"name":{"kind":"Name","value":"evo_name"}},{"kind":"Field","alias":{"kind":"Name","value":"value"},"name":{"kind":"Name","value":"evo_rateid"}},{"kind":"Field","name":{"kind":"Name","value":"evo_id"}},{"kind":"Field","name":{"kind":"Name","value":"evo_tarifs"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"evo_tarifid"}}]}}]}}]}}]} as unknown as DocumentNode; diff --git a/apps/web/process/calculate/lib/helper.ts b/apps/web/process/calculate/lib/helper.ts new file mode 100644 index 0000000..a664392 --- /dev/null +++ b/apps/web/process/calculate/lib/helper.ts @@ -0,0 +1,52 @@ +import ValuesSchema from '@/config/schema/values'; +import * as CRMTypes from '@/graphql/crm.types'; +import type { ProcessContext } from '@/process/types'; +import type { z } from 'zod'; + +const InputSchema = ValuesSchema.pick({ + bonusCoefficient: true, + product: true, + tarif: true, +}); + +export default function helper({ apolloClient }: Pick) { + return { + async getIrr({ + product: productId, + tarif: tarifId, + bonusCoefficient, + }: z.infer) { + let max = 0; + let min = 0; + + if (productId && tarifId) { + const { + data: { evo_baseproduct }, + } = await apolloClient.query({ + query: CRMTypes.GetProductDocument, + variables: { + productId, + }, + }); + + const { + data: { evo_tarif }, + } = await apolloClient.query({ + query: CRMTypes.GetTarifDocument, + variables: { + tarifId, + }, + }); + + min = evo_tarif?.evo_min_irr ?? 0; + max = evo_tarif?.evo_max_irr ?? 0; + + if (evo_baseproduct?.evo_cut_irr_with_bonus && bonusCoefficient < 1) { + min -= (1 - bonusCoefficient) * (evo_tarif?.evo_cut_irr_with_bonus_coefficient ?? 0); + } + } + + return { max, min }; + }, + }; +} diff --git a/apps/web/process/calculate/reactions/common.ts b/apps/web/process/calculate/reactions/common.ts index dcc81ac..07fcc02 100644 --- a/apps/web/process/calculate/reactions/common.ts +++ b/apps/web/process/calculate/reactions/common.ts @@ -1,4 +1,4 @@ -import * as CRMTypes from '@/graphql/crm.types'; +import helper from '../lib/helper'; import type { ProcessContext } from '@/process/types'; import { reaction } from 'mobx'; import { formatter } from 'tools'; @@ -27,39 +27,11 @@ export default function reactions({ store, apolloClient }: ProcessContext) { fireImmediately: true, } ); - + const { getIrr } = helper({ apolloClient }); reaction( () => $calculation.$values.getValues(['product', 'tarif', 'bonusCoefficient']), - async ({ product: productId, tarif: tarifId, bonusCoefficient }) => { - let max = 0; - let min = 0; - - if (productId && tarifId) { - const { - data: { evo_baseproduct }, - } = await apolloClient.query({ - query: CRMTypes.GetProductDocument, - variables: { - productId, - }, - }); - - const { - data: { evo_tarif }, - } = await apolloClient.query({ - query: CRMTypes.GetTarifDocument, - variables: { - tarifId, - }, - }); - - min = evo_tarif?.evo_min_irr ?? 0; - max = evo_tarif?.evo_max_irr ?? 0; - - if (evo_baseproduct?.evo_cut_irr_with_bonus && bonusCoefficient < 1) { - min -= (1 - bonusCoefficient) * (evo_tarif?.evo_cut_irr_with_bonus_coefficient ?? 0); - } - } + async (values) => { + const { min, max } = await getIrr(values); $calculation.element('labelIrrInfo').setValue(`${formatter(min)}% - ${formatter(max)}%`); } diff --git a/apps/web/trpc/routers/calculate/index.ts b/apps/web/trpc/routers/calculate/index.ts index 49c0c51..ccafefd 100644 --- a/apps/web/trpc/routers/calculate/index.ts +++ b/apps/web/trpc/routers/calculate/index.ts @@ -1,6 +1,6 @@ import { user } from '../../middleware'; import { t } from '../../server'; -import { collectPrepareData } from './prepare-data'; +import { getRequestData } from './request'; import { CalculateInputSchema, CalculateOutputSchema } from './types'; import { validate } from './validation'; import initializeApollo from '@/apollo/client'; @@ -30,7 +30,7 @@ const calculateRouter = t.router({ }; } - const preparedData = await collectPrepareData({ + const payload = await getRequestData({ context: { apolloClient, queryClient, diff --git a/apps/web/trpc/routers/calculate/prepare-data.ts b/apps/web/trpc/routers/calculate/request.ts similarity index 94% rename from apps/web/trpc/routers/calculate/prepare-data.ts rename to apps/web/trpc/routers/calculate/request.ts index 1b93f63..c61ebd7 100644 --- a/apps/web/trpc/routers/calculate/prepare-data.ts +++ b/apps/web/trpc/routers/calculate/request.ts @@ -1,8 +1,9 @@ /* eslint-disable sonarjs/cognitive-complexity */ -import type { CalculateInput, Context, PreparedData, PreparedValues } from './types'; +import type * as Types from './types'; import type { User } from '@/api/user/types'; import { ESN, NSIB_MAX, VAT } from '@/constants/values'; import * as CRMTypes from '@/graphql/crm.types'; +import helper from '@/process/calculate/lib/helper'; import { createCurrencyUtility } from '@/utils/currency'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; @@ -11,22 +12,30 @@ import { max, sum } from 'radash'; dayjs.extend(utc); type Input = { - context: Context; - input: CalculateInput; + context: Types.Context; + input: Types.CalculateInput; user: User; }; type PreparedValuesGetters = { - [Key in keyof PreparedValues]: () => Promise; + [Key in keyof Types.PreparedValues]: () => Promise; }; -export async function collectPrepareData({ context, input, user }: Input): Promise { +type AdditionalDataGetters = { + [Key in keyof Types.AdditionalData]: () => Promise; +}; + +export async function getRequestData({ + context, + input, + user, +}: Input): Promise { const { apolloClient } = context; const { values, insurance, payments } = input; const { RUB } = createCurrencyUtility({ apolloClient }); - const preparedPayments: PreparedData['preparedPayments'] = { + const preparedPayments: Types.CalculateRequest['preparedPayments'] = { rows: payments.values.map((payment, index) => ({ gpsBasePayment: 0, gpsCostPayment: 0, @@ -104,7 +113,7 @@ export async function collectPrepareData({ context, input, user }: Input): Promi ).data.evo_coefficients : null; - const getters: PreparedValuesGetters = { + const preparedValuesGetters: PreparedValuesGetters = { async acceptSum() { const plPrice = await this.plPrice(); const discount = await this.discount(); @@ -1167,18 +1176,57 @@ export async function collectPrepareData({ context, input, user }: Input): Promi }; const preparedValuesResult = await Promise.all( - (Object.keys(getters) as Array).map( - async (key: T) => { - const value = await getters[key](); + (Object.keys(preparedValuesGetters) as Array).map( + async (key: T) => { + const value = await preparedValuesGetters[key](); return { [key]: value }; } ) ); - const preparedValues = Object.assign({}, ...preparedValuesResult) as PreparedValues; + const preparedValues = Object.assign({}, ...preparedValuesResult) as Types.PreparedValues; + + const { getIrr } = helper({ apolloClient }); + const irr = await getIrr(values); + + const additionalDataGetters: AdditionalDataGetters = { + async maxAllAgencyPerc() { + if (values.quote) { + const { + data: { quote }, + } = await apolloClient.query({ + query: CRMTypes.GetQuoteDocument, + variables: { quoteId: values.quote }, + }); + + return quote?.evo_maximum_percentage_av || null; + } + + return null; + }, + async maxCashflowMSFONominal() { + return irr.max; + }, + async minCashflowMSFONominal() { + return irr.min; + }, + }; + + const additionalDataResult = await Promise.all( + (Object.keys(additionalDataGetters) as Array).map( + async (key: T) => { + const value = await additionalDataGetters[key](); + + return { [key]: value }; + } + ) + ); + + const additionalData = Object.assign({}, ...additionalDataResult) as Types.AdditionalData; return { + additionalData, preparedPayments, preparedValues, }; diff --git a/apps/web/trpc/routers/calculate/types.ts b/apps/web/trpc/routers/calculate/types.ts index 73f7c18..0eb7bdc 100644 --- a/apps/web/trpc/routers/calculate/types.ts +++ b/apps/web/trpc/routers/calculate/types.ts @@ -152,9 +152,18 @@ const PreparedPaymentSchema = z.object({ export type PreparedPayments = z.infer; -export const PreparedDataSchema = z.object({ +const AdditionalDataSchema = z.object({ + maxAllAgencyPerc: z.number().nullable(), + maxCashflowMSFONominal: z.number().nullable(), + minCashflowMSFONominal: z.number().nullable(), +}); + +export type AdditionalData = z.infer; + +export const CalculateRequestSchema = z.object({ + additionalData: AdditionalDataSchema, preparedPayments: PreparedPaymentSchema, preparedValues: PreparedValuesSchema, }); -export type PreparedData = z.infer; +export type CalculateRequest = z.infer;