insurance-table: fetch data from gql

This commit is contained in:
Chika 2022-07-11 16:34:11 +03:00
parent b72afae4c7
commit c01162eeef
8 changed files with 124 additions and 26 deletions

View File

@ -21,7 +21,7 @@ export const columns: ColumnsType<Insurance.RowValues> = [
render: (_, record) => {
const Component = buildOptionComponent(record.key, Select, 'insuranceCompany');
return <Component />;
return <Component showSearch optionFilterProp="label" />;
},
},
{

View File

@ -15,7 +15,7 @@ export type Values = Exclude<keyof RowValues, 'key'>;
export type RowOptions = {
[ValueName in Values]?: BaseOption<RowValues[ValueName]>[];
} & { key: Keys };
};
export type RowStatuses = Record<Values, Status> & {
key: Keys;

View File

@ -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<Insurance.Keys, Insurance.RowOptions> = {
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[] = [
{

View File

@ -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(),
},

View File

@ -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;
}

View File

@ -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<NormalizedCache>, user: User) {
const {
data: { selectLead, selectOpportunity },
@ -44,11 +76,18 @@ export async function getCRMData(apolloClient: ApolloClient<NormalizedCache>, us
query: QUERY_GET_TRANSACTION_CURRENCIES,
});
const { data: insuranceData } = await apolloClient.query<GetInsuranceData>({
query: QUERY_GET_INSURANCE_DATA,
});
return {
options: {
selectLead,
selectOpportunity,
selectSupplierCurrency,
},
tables: {
insurance: insuranceData,
},
};
}

View File

@ -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<Insurance.Keys, Insurance.RowOptions>;
statuses?: Insurance.RowStatuses[];
}
export default class InsuranceTable {
root: RootStore;
validation: Validation;
values: Insurance.RowValues[] = insuranceTableConfig.defaultValues;
options: Insurance.RowOptions[] = insuranceTableConfig.defaultOptions;
options: Record<Insurance.Keys, Insurance.RowOptions> = 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<Insurance.RowOptions>) => {
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) {

8
tools/entity.ts Normal file
View File

@ -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[];
}