From c01162eeefc07307fe46b22a85a625a421305406 Mon Sep 17 00:00:00 2001 From: Chika Date: Mon, 11 Jul 2022 16:34:11 +0300 Subject: [PATCH] insurance-table: fetch data from gql --- .../Form/Insurance/InsuranceTable/config.tsx | 2 +- .../Form/Insurance/InsuranceTable/types.ts | 2 +- config/tables/insurance-table.ts | 13 +++--- pages/index.tsx | 17 ++++++- .../init/__generated__/GetInsuranceData.ts | 45 +++++++++++++++++++ process/init/get-data.ts | 39 ++++++++++++++++ stores/tables/insurance/index.ts | 24 ++++------ tools/entity.ts | 8 ++++ 8 files changed, 124 insertions(+), 26 deletions(-) create mode 100644 process/init/__generated__/GetInsuranceData.ts create mode 100644 tools/entity.ts diff --git a/Components/Calculation/Form/Insurance/InsuranceTable/config.tsx b/Components/Calculation/Form/Insurance/InsuranceTable/config.tsx index 98e196b..36a8d35 100644 --- a/Components/Calculation/Form/Insurance/InsuranceTable/config.tsx +++ b/Components/Calculation/Form/Insurance/InsuranceTable/config.tsx @@ -21,7 +21,7 @@ export const columns: ColumnsType = [ render: (_, record) => { const Component = buildOptionComponent(record.key, Select, 'insuranceCompany'); - return ; + return ; }, }, { diff --git a/Components/Calculation/Form/Insurance/InsuranceTable/types.ts b/Components/Calculation/Form/Insurance/InsuranceTable/types.ts index 3805ba7..d3d56bc 100644 --- a/Components/Calculation/Form/Insurance/InsuranceTable/types.ts +++ b/Components/Calculation/Form/Insurance/InsuranceTable/types.ts @@ -15,7 +15,7 @@ export type Values = Exclude; export type RowOptions = { [ValueName in Values]?: BaseOption[]; -} & { key: Keys }; +}; export type RowStatuses = Record & { key: Keys; diff --git a/config/tables/insurance-table.ts b/config/tables/insurance-table.ts index 4e60d5e..2bf71b7 100644 --- a/config/tables/insurance-table.ts +++ b/config/tables/insurance-table.ts @@ -1,9 +1,8 @@ /* eslint-disable object-curly-newline */ import type * as Insurance from 'Components/Calculation/Form/Insurance/InsuranceTable/types'; -export const defaultOptions: Insurance.RowOptions[] = [ - { - key: 'osago', +export const defaultOptions: Record = { + osago: { insured: [ { label: 'ЛП', @@ -25,8 +24,7 @@ export const defaultOptions: Insurance.RowOptions[] = [ }, ], }, - { - key: 'kasko', + kasko: { insured: [ { label: 'ЛП', @@ -48,8 +46,7 @@ export const defaultOptions: Insurance.RowOptions[] = [ }, ], }, - { - key: 'finGAP', + finGAP: { insured: [ { label: 'ЛП', @@ -71,7 +68,7 @@ export const defaultOptions: Insurance.RowOptions[] = [ }, ], }, -]; +}; export const defaultValues: Insurance.RowValues[] = [ { diff --git a/pages/index.tsx b/pages/index.tsx index e49c38d..4916e31 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -4,6 +4,7 @@ import initializeApollo from 'apollo/client'; import * as Calculation from 'Components/Calculation'; import Output from 'Components/Output'; import defaultOptions from 'config/default-options'; +import * as InsuranceTableConfig from 'config/tables/insurance-table'; import type { GetServerSideProps } from 'next'; import Head from 'next/head'; import calculateValidationReactions from 'process/calculate/reactions/validation'; @@ -13,6 +14,7 @@ import paymentsReactions from 'process/payments/reactions'; import { fetchUser } from 'services/user'; import { useStore } from 'stores/hooks'; import styled from 'styled-components'; +import { normalizeOptions } from 'tools/entity'; import { Box } from 'UIKit/grid'; import { min } from 'UIKit/mq'; @@ -73,7 +75,15 @@ export const getServerSideProps: GetServerSideProps = async ({ req }) => { }); const apolloClient = initializeApollo(); - const { options: crmOptions } = await getCRMData(apolloClient, user); + const { + options: crmOptions, + tables: { insurance }, + } = await getCRMData(apolloClient, user); + + const insuranceOptions = InsuranceTableConfig.defaultOptions; + insuranceOptions.osago.insuranceCompany = normalizeOptions(insurance.osago); + insuranceOptions.kasko.insuranceCompany = normalizeOptions(insurance.kasko); + insuranceOptions.finGAP.insuranceCompany = normalizeOptions(insurance.finGAP); return { props: { @@ -81,6 +91,11 @@ export const getServerSideProps: GetServerSideProps = async ({ req }) => { calculation: { options: { ...crmOptions, ...defaultOptions }, }, + tables: { + insurance: { + options: insuranceOptions, + }, + }, initialApolloState: apolloClient.cache.extract(), }, diff --git a/process/init/__generated__/GetInsuranceData.ts b/process/init/__generated__/GetInsuranceData.ts new file mode 100644 index 0000000..b09d3f1 --- /dev/null +++ b/process/init/__generated__/GetInsuranceData.ts @@ -0,0 +1,45 @@ +/* tslint:disable */ +/* eslint-disable */ +// @generated +// This file was automatically generated and should not be edited. + +// ==================================================== +// GraphQL query operation: GetInsuranceData +// ==================================================== + +export interface GetInsuranceData_osago { + __typename: "account"; + value: any | null; + label: string | null; +} + +export interface GetInsuranceData_kasko { + __typename: "account"; + value: any | null; + label: string | null; +} + +export interface GetInsuranceData_finGAP { + __typename: "account"; + value: any | null; + label: string | null; +} + +export interface GetInsuranceData { + /** + * Контрагенты. statecode по умолчанию 0 + */ + osago: (GetInsuranceData_osago | null)[] | null; + /** + * Контрагенты. statecode по умолчанию 0 + */ + kasko: (GetInsuranceData_kasko | null)[] | null; + /** + * Контрагенты. statecode по умолчанию 0 + */ + finGAP: (GetInsuranceData_finGAP | null)[] | null; +} + +export interface GetInsuranceDataVariables { + evo_account_type?: number[] | null; +} diff --git a/process/init/get-data.ts b/process/init/get-data.ts index 16163d2..b907d16 100644 --- a/process/init/get-data.ts +++ b/process/init/get-data.ts @@ -3,6 +3,7 @@ import type { ApolloClient, NormalizedCache } from '@apollo/client'; import { gql } from '@apollo/client'; import { getDomainName } from 'services/user/tools'; import type { User } from 'services/user/types'; +import type { GetInsuranceData } from './__generated__/GetInsuranceData'; import type { GetOwnerData, GetOwnerDataVariables } from './__generated__/GetOwnerData'; import type { GetTransactionCurrencies } from './__generated__/GetTransactionCurrencies'; @@ -29,6 +30,37 @@ const QUERY_GET_TRANSACTION_CURRENCIES = gql` } `; +const QUERY_GET_INSURANCE_DATA = gql` + query GetInsuranceData($evo_account_type: [Int!]) { + osago: accounts( + evo_account_type: $evo_account_type + evo_type_ins_policy: [100000001] + statecode: 0 + ) { + value: accountid + label: name + } + + kasko: accounts( + evo_account_type: $evo_account_type + evo_type_ins_policy: [100000000] + statecode: 0 + ) { + value: accountid + label: name + } + + finGAP: accounts( + evo_account_type: $evo_account_type + evo_type_ins_policy: [100000002] + statecode: 0 + ) { + value: accountid + label: name + } + } +`; + export async function getCRMData(apolloClient: ApolloClient, user: User) { const { data: { selectLead, selectOpportunity }, @@ -44,11 +76,18 @@ export async function getCRMData(apolloClient: ApolloClient, us query: QUERY_GET_TRANSACTION_CURRENCIES, }); + const { data: insuranceData } = await apolloClient.query({ + query: QUERY_GET_INSURANCE_DATA, + }); + return { options: { selectLead, selectOpportunity, selectSupplierCurrency, }, + tables: { + insurance: insuranceData, + }, }; } diff --git a/stores/tables/insurance/index.ts b/stores/tables/insurance/index.ts index 750d1a0..59a8bfb 100644 --- a/stores/tables/insurance/index.ts +++ b/stores/tables/insurance/index.ts @@ -7,16 +7,16 @@ import type RootStore from 'stores/root'; import Validation from '../validation'; export interface InsuranceTableData { - values: Insurance.RowValues[]; - options: Insurance.RowOptions[]; - statuses: Insurance.RowStatuses[]; + values?: Insurance.RowValues[]; + options?: Record; + statuses?: Insurance.RowStatuses[]; } export default class InsuranceTable { root: RootStore; validation: Validation; values: Insurance.RowValues[] = insuranceTableConfig.defaultValues; - options: Insurance.RowOptions[] = insuranceTableConfig.defaultOptions; + options: Record = insuranceTableConfig.defaultOptions; statuses: Insurance.RowStatuses[] = insuranceTableConfig.defaultStatuses; constructor(rootStore: RootStore) { @@ -33,9 +33,9 @@ export default class InsuranceTable { options: initialOptions, statuses: initialStatuses, }: InsuranceTableData) => { - this.values = initialValues; - this.options = initialOptions; - this.statuses = initialStatuses; + if (initialValues) this.values = initialValues; + if (initialOptions) this.options = initialOptions; + if (initialStatuses) this.statuses = initialStatuses; }; getRowValue(key: Insurance.Keys, valueName: Insurance.Values) { @@ -53,17 +53,11 @@ export default class InsuranceTable { }; getRowOptions(key: Insurance.Keys) { - const rowIndex = this.options.findIndex((x) => x.key === key); - - return this.options[rowIndex]; + return this.options[key]; } setRowOptions = (key: Insurance.Keys, rowOptions: Partial) => { - const rowIndex = this.options.findIndex((x) => x.key === key); - - if (rowIndex >= 0) { - mergeWith(this.options[rowIndex], rowOptions); - } + mergeWith(this.options[key], rowOptions); }; getRowStatuses(key: Insurance.Keys) { diff --git a/tools/entity.ts b/tools/entity.ts new file mode 100644 index 0000000..eb6d5f0 --- /dev/null +++ b/tools/entity.ts @@ -0,0 +1,8 @@ +/* eslint-disable import/prefer-default-export */ +import type { BaseOption } from 'Elements/types'; + +export function normalizeOptions(options: any[] | null) { + if (options === null) return []; + + return options as BaseOption[]; +}