372 lines
9.4 KiB
TypeScript
372 lines
9.4 KiB
TypeScript
/* eslint-disable @typescript-eslint/naming-convention */
|
|
import { gql } from '@apollo/client';
|
|
import * as CRMTypes from 'graphql/crm.types';
|
|
import { reaction } from 'mobx';
|
|
import type { ReactionsContext } from 'process/types';
|
|
import { intersects } from 'radash';
|
|
import { normalizeOptions } from 'tools';
|
|
|
|
const QUERY_GET_MODELS = gql`
|
|
query GetModels_ProcessLeasingObject($brandid: Uuid!) {
|
|
evo_models(statecode: 0, evo_brandid: $brandid) {
|
|
label: evo_name
|
|
value: evo_modelid
|
|
evo_modelid
|
|
evo_vehicle_type
|
|
}
|
|
}
|
|
`;
|
|
|
|
const QUERY_GET_LEASE_OBJECT_TYPE = gql`
|
|
query GetLeaseObjectType_ProcessLeasingObject($leaseObjectTypeId: Uuid!) {
|
|
leaseObjectType: evo_leasingobject_type(evo_leasingobject_typeid: $leaseObjectTypeId) {
|
|
evo_vehicle_type
|
|
}
|
|
}
|
|
`;
|
|
|
|
const QUERY_GET_SUBSIDY = gql`
|
|
query GetSubsidy_ProcessLeasingObject($subsidyId: Uuid!) {
|
|
subsidy: evo_subsidy(evo_subsidyid: $subsidyId) {
|
|
evo_brands {
|
|
evo_brandid
|
|
}
|
|
evo_models {
|
|
evo_modelid
|
|
}
|
|
}
|
|
}
|
|
`;
|
|
|
|
const QUERY_GET_IMPORT_PROGRAM = gql`
|
|
query GetImportProgram_ProcessLeasingObject($importProgramId: Uuid!) {
|
|
importProgram: evo_subsidy(evo_subsidyid: $importProgramId) {
|
|
evo_brands {
|
|
evo_brandid
|
|
}
|
|
evo_models {
|
|
evo_modelid
|
|
}
|
|
}
|
|
}
|
|
`;
|
|
|
|
const QUERY_GET_PRODUCT = gql`
|
|
query GetProduct_ProcessLeasingObject($productId: Uuid!) {
|
|
evo_baseproduct(evo_baseproductid: $productId) {
|
|
evo_brands {
|
|
evo_brandid
|
|
}
|
|
}
|
|
}
|
|
`;
|
|
|
|
const QUERY_GET_CONFIGURATIONS = gql`
|
|
query GetConfigurations_ProcessLeasingObject($modelId: Uuid) {
|
|
evo_equipments(statecode: 0, evo_modelid: $modelId) {
|
|
label: evo_name
|
|
value: evo_equipmentid
|
|
}
|
|
}
|
|
`;
|
|
|
|
export default function commonReactions({ store, apolloClient }: ReactionsContext) {
|
|
const { $calculation } = store;
|
|
|
|
reaction(
|
|
() => {
|
|
const { brand, subsidy, importProgram, leaseObjectType } = $calculation.$values.values;
|
|
|
|
return {
|
|
brandId: brand,
|
|
subsidyId: subsidy,
|
|
importProgramId: importProgram,
|
|
leaseObjectTypeId: leaseObjectType,
|
|
};
|
|
},
|
|
async ({ brandId, subsidyId, importProgramId, leaseObjectTypeId }) => {
|
|
if (!brandId) {
|
|
$calculation.element('selectModel').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_models },
|
|
} = await apolloClient.query<
|
|
CRMTypes.GetModels_ProcessLeasingObjectQuery,
|
|
CRMTypes.GetModels_ProcessLeasingObjectQueryVariables
|
|
>({
|
|
query: QUERY_GET_MODELS,
|
|
variables: {
|
|
brandid: brandId,
|
|
},
|
|
});
|
|
|
|
let models = evo_models;
|
|
|
|
if (!models) {
|
|
$calculation.element('selectModel').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
if (leaseObjectTypeId) {
|
|
const {
|
|
data: { leaseObjectType },
|
|
} = await apolloClient.query<
|
|
CRMTypes.GetLeaseObjectType_ProcessLeasingObjectQuery,
|
|
CRMTypes.GetLeaseObjectType_ProcessLeasingObjectQueryVariables
|
|
>({
|
|
query: QUERY_GET_LEASE_OBJECT_TYPE,
|
|
variables: {
|
|
leaseObjectTypeId,
|
|
},
|
|
});
|
|
|
|
models = models?.filter((model) => {
|
|
if (
|
|
model?.evo_vehicle_type &&
|
|
leaseObjectType?.evo_vehicle_type?.includes(model.evo_vehicle_type)
|
|
) {
|
|
return model;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (subsidyId) {
|
|
const {
|
|
data: { subsidy },
|
|
} = await apolloClient.query<
|
|
CRMTypes.GetSubsidy_ProcessLeasingObjectQuery,
|
|
CRMTypes.GetSubsidy_ProcessLeasingObjectQueryVariables
|
|
>({
|
|
query: QUERY_GET_SUBSIDY,
|
|
variables: {
|
|
subsidyId,
|
|
},
|
|
});
|
|
|
|
models = models?.filter((model) => {
|
|
if (
|
|
model &&
|
|
(!subsidy?.evo_models?.length ||
|
|
subsidy?.evo_models?.filter(
|
|
(subsidy_evo_model) => subsidy_evo_model?.evo_modelid === model.evo_modelid
|
|
)?.length)
|
|
) {
|
|
return model;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (importProgramId) {
|
|
const {
|
|
data: { importProgram },
|
|
} = await apolloClient.query<
|
|
CRMTypes.GetImportProgram_ProcessLeasingObjectQuery,
|
|
CRMTypes.GetImportProgram_ProcessLeasingObjectQueryVariables
|
|
>({
|
|
query: QUERY_GET_IMPORT_PROGRAM,
|
|
variables: {
|
|
importProgramId,
|
|
},
|
|
});
|
|
|
|
models = models?.filter((model) => {
|
|
if (
|
|
model &&
|
|
(!importProgram?.evo_models?.length ||
|
|
importProgram?.evo_models?.filter(
|
|
(importProgram_evo_model) =>
|
|
importProgram_evo_model?.evo_modelid === model.evo_modelid
|
|
)?.length)
|
|
) {
|
|
return model;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
$calculation.element('selectModel').setOptions(normalizeOptions(models));
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => {
|
|
const { product, subsidy, importProgram, leaseObjectType } = $calculation.$values.values;
|
|
|
|
return {
|
|
productId: product,
|
|
subsidyId: subsidy,
|
|
importProgramId: importProgram,
|
|
leaseObjectTypeId: leaseObjectType,
|
|
};
|
|
},
|
|
async ({ productId, subsidyId, importProgramId, leaseObjectTypeId }) => {
|
|
const {
|
|
data: { evo_brands },
|
|
} = await apolloClient.query({
|
|
query: CRMTypes.GetBrandsDocument,
|
|
});
|
|
|
|
let brands = evo_brands;
|
|
|
|
if (!brands) {
|
|
$calculation.element('selectBrand').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
if (productId) {
|
|
const {
|
|
data: { evo_baseproduct },
|
|
} = await apolloClient.query<
|
|
CRMTypes.GetProduct_ProcessLeasingObjectQuery,
|
|
CRMTypes.GetProduct_ProcessLeasingObjectQueryVariables
|
|
>({
|
|
query: QUERY_GET_PRODUCT,
|
|
variables: {
|
|
productId,
|
|
},
|
|
});
|
|
|
|
brands = brands?.filter((brand) => {
|
|
if (
|
|
!evo_baseproduct?.evo_brands?.length ||
|
|
evo_baseproduct.evo_brands.filter(
|
|
(baseproduct_evo_brand) => baseproduct_evo_brand?.evo_brandid === brand?.evo_brandid
|
|
)?.length
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (leaseObjectTypeId) {
|
|
const {
|
|
data: { leaseObjectType },
|
|
} = await apolloClient.query<
|
|
CRMTypes.GetLeaseObjectType_ProcessLeasingObjectQuery,
|
|
CRMTypes.GetLeaseObjectType_ProcessLeasingObjectQueryVariables
|
|
>({
|
|
query: QUERY_GET_LEASE_OBJECT_TYPE,
|
|
variables: {
|
|
leaseObjectTypeId,
|
|
},
|
|
});
|
|
|
|
brands = brands?.filter((brand) => {
|
|
if (
|
|
brand?.evo_vehicle_type?.length &&
|
|
leaseObjectType?.evo_vehicle_type &&
|
|
intersects(
|
|
brand.evo_vehicle_type?.filter((x) => x > 0),
|
|
leaseObjectType?.evo_vehicle_type
|
|
)
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (subsidyId) {
|
|
const {
|
|
data: { subsidy },
|
|
} = await apolloClient.query<
|
|
CRMTypes.GetSubsidy_ProcessLeasingObjectQuery,
|
|
CRMTypes.GetSubsidy_ProcessLeasingObjectQueryVariables
|
|
>({
|
|
query: QUERY_GET_SUBSIDY,
|
|
variables: {
|
|
subsidyId,
|
|
},
|
|
});
|
|
|
|
brands = brands?.filter((brand) => {
|
|
if (
|
|
!subsidy?.evo_brands?.length ||
|
|
subsidy?.evo_brands?.filter(
|
|
(subsidy_evo_brand) => subsidy_evo_brand?.evo_brandid === brand?.evo_brandid
|
|
)?.length
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
if (importProgramId) {
|
|
const {
|
|
data: { importProgram },
|
|
} = await apolloClient.query<
|
|
CRMTypes.GetImportProgram_ProcessLeasingObjectQuery,
|
|
CRMTypes.GetImportProgram_ProcessLeasingObjectQueryVariables
|
|
>({
|
|
query: QUERY_GET_IMPORT_PROGRAM,
|
|
variables: {
|
|
importProgramId,
|
|
},
|
|
});
|
|
|
|
brands = brands?.filter((brand) => {
|
|
if (
|
|
!importProgram?.evo_brands?.length ||
|
|
importProgram?.evo_brands?.filter(
|
|
(importProgram_evo_brand) =>
|
|
importProgram_evo_brand?.evo_brandid === brand?.evo_brandid
|
|
)?.length
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
});
|
|
}
|
|
|
|
$calculation.element('selectBrand').setOptions(normalizeOptions(brands));
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectLeaseObjectType').getValue(),
|
|
(leaseObjectTypeId) => {
|
|
if (!leaseObjectTypeId) {
|
|
$calculation.element('selectBrand').resetValue();
|
|
}
|
|
}
|
|
);
|
|
|
|
reaction(
|
|
() => $calculation.element('selectModel').getValue(),
|
|
async (modelId) => {
|
|
if (!modelId) {
|
|
$calculation.element('selectConfiguration').reset();
|
|
|
|
return;
|
|
}
|
|
|
|
const {
|
|
data: { evo_equipments },
|
|
} = await apolloClient.query<
|
|
CRMTypes.GetConfigurations_ProcessLeasingObjectQuery,
|
|
CRMTypes.GetConfigurations_ProcessLeasingObjectQueryVariables
|
|
>({
|
|
query: QUERY_GET_CONFIGURATIONS,
|
|
});
|
|
|
|
$calculation.element('selectConfiguration').setOptions(normalizeOptions(evo_equipments));
|
|
}
|
|
);
|
|
}
|