diff --git a/src/client/services/CalculationService.ts b/src/client/services/CalculationService.ts index e2e4d60..8f70e97 100644 --- a/src/client/services/CalculationService.ts +++ b/src/client/services/CalculationService.ts @@ -3,7 +3,10 @@ import { API_HOSTNAME } from 'client/common/constants'; import { CRM_GRAPHQL_PROXY_URL, CRM_GRAPHQL_URL } from 'core/constants/urls'; import { convertEntityToOption } from 'core/tools/entities'; import { convertJSONToGQLQuery } from 'core/tools/query'; -import { IGetEntitiesRequest } from 'core/types/Calculation/Requests'; +import { + IGetEntitiesRequest, + IRequestToCRMGQL, +} from 'core/types/Calculation/Requests'; import { IGetEntitiesResponse } from 'core/types/Calculation/Responses'; import { IBaseOption } from 'core/types/Calculation/Store/options'; import { TCRMEntity } from 'core/types/Entities/crmEntities'; @@ -37,10 +40,81 @@ const client = new ApolloClient({ }); class CalculationService { + static crmgqlquery = ({ + query, + toOptions, + variables, + }: IRequestToCRMGQL): Promise => + new Promise(async (resolve, reject) => { + await client + .query({ + query, + variables, + }) + .then(res => { + if (!res.data || res.errors) { + throw res.error || res.errors; + } + let resEntities: TEntities = res.data; + + Object.keys(resEntities).forEach(targetName => { + //@ts-ignore + const targetEnt: TCRMEntity | TCRMEntity[] = + //@ts-ignore + resEntities[targetName]; + + if (toOptions) + if (toOptions.includes(targetName)) { + if (Array.isArray(targetEnt)) { + let optionatedEntities: (TCRMEntity & IBaseOption)[] = []; + for (let entity of targetEnt) { + const entityOption = convertEntityToOption( + entity, + entity.__typename, + ); + if (entityOption) { + optionatedEntities.push(entityOption); + } + } + //@ts-ignore + resEntities[ + //@ts-ignore + !CRMEntityAliases.includes(targetName) && + isPlural(targetName) + ? singular(targetName) + : targetName + ] = optionatedEntities; + } else { + const entityOption = convertEntityToOption( + targetEnt, + targetEnt.__typename, + ); + //@ts-ignore + resEntities[ + //@ts-ignore + !CRMEntityAliases.includes(targetName) && + isPlural(targetName) + ? singular(targetName) + : targetName + ] = entityOption; + } + } + }); + + resolve({ entities: resEntities }); + }) + .catch(err => { + throw err; + }); + }); + + /** + * @deprecated + */ static getEntities = ({ queries, }: IGetEntitiesRequest): Promise => - new Promise((resolve, reject) => { + new Promise(async (resolve, reject) => { // remove queries with invalid where queries = queries.filter(query => { return Object.values(query.where).some( @@ -49,13 +123,17 @@ class CalculationService { }); const convertedQuery = convertJSONToGQLQuery(queries); - client + await client .query({ query: gql` ${convertedQuery} `, }) .then(res => { + if (!res.data || res.errors || res.error) { + throw res.error || res.errors; + } + const toOptions = queries .filter(x => x.toOption) .map(x => x.alias || x.entityName); diff --git a/src/core/types/Calculation/Requests.ts b/src/core/types/Calculation/Requests.ts index d148702..abfa5b5 100644 --- a/src/core/types/Calculation/Requests.ts +++ b/src/core/types/Calculation/Requests.ts @@ -1,3 +1,7 @@ +import { + CRMEntityAliasesUnion, + CRMEntityNames, +} from 'core/types/Entities/crmEntityNames'; import { TEntityQuery } from '../Entities/query'; import { ValuesTables } from './Store/tables'; import { TValue, TValues } from './Store/values'; @@ -6,6 +10,12 @@ export interface IGetEntitiesRequest { queries: TEntityQuery[]; } +export interface IRequestToCRMGQL { + query: any; + toOptions?: (CRMEntityNames | CRMEntityAliasesUnion)[]; + variables: { [prop: string]: any }; +} + export interface IGetCalculationRequest { values: TValues; tables: ValuesTables;