217 lines
6.3 KiB
TypeScript
217 lines
6.3 KiB
TypeScript
import { ApolloClient, gql, HttpLink, InMemoryCache } from '@apollo/client';
|
|
import axios from 'axios';
|
|
import { getServerUrl } from 'client/common/urls';
|
|
import {
|
|
CORE_PROXY_URL,
|
|
CRM_GRAPHQL_PROXY_URL,
|
|
CRM_GRAPHQL_URL,
|
|
} from 'core/constants/urls';
|
|
import { convertEntityToOption } from 'core/tools/entities';
|
|
import { convertJSONToGQLQuery } from 'core/tools/query';
|
|
import {
|
|
IGetCalculationRequest,
|
|
IGetEntitiesRequest,
|
|
IRequestToCRMGQL,
|
|
} from 'core/types/Calculation/Requests';
|
|
import {
|
|
IGetCalculationResponse,
|
|
IGetEntitiesResponse,
|
|
IGetUserResponse,
|
|
} from 'core/types/Calculation/Responses';
|
|
import { IBaseOption } from 'core/types/Calculation/Store/options';
|
|
import { TCRMEntity } from 'core/types/Entities/crmEntities';
|
|
import { TEntities } from 'core/types/Entities/crmEntityNames';
|
|
import { isPlural, singular } from 'pluralize';
|
|
|
|
const client = new ApolloClient({
|
|
uri: CRM_GRAPHQL_URL,
|
|
cache: new InMemoryCache(),
|
|
|
|
link: new HttpLink({
|
|
uri: getServerUrl(String.prototype.concat('/proxy', CRM_GRAPHQL_PROXY_URL)),
|
|
}),
|
|
|
|
defaultOptions: {
|
|
query: {
|
|
fetchPolicy: 'no-cache',
|
|
errorPolicy: 'all',
|
|
},
|
|
},
|
|
});
|
|
|
|
class CalculationService {
|
|
static getUser = (): Promise<IGetUserResponse> =>
|
|
new Promise((resolve, reject) => {
|
|
axios.get('/api/users/getUser').then(res => {
|
|
resolve(res.data);
|
|
});
|
|
});
|
|
|
|
static calculate = (
|
|
preparedData: IGetCalculationRequest,
|
|
): Promise<IGetCalculationResponse> =>
|
|
new Promise((resolve, reject) => {
|
|
axios
|
|
.post<IGetCalculationResponse>(
|
|
getServerUrl(
|
|
String.prototype.concat(
|
|
'/proxy',
|
|
CORE_PROXY_URL,
|
|
'/api',
|
|
'/v1',
|
|
'/calculation',
|
|
'/calculate',
|
|
),
|
|
),
|
|
preparedData,
|
|
)
|
|
.then(res => {
|
|
resolve(res.data);
|
|
})
|
|
.catch(err => {
|
|
reject(err);
|
|
});
|
|
});
|
|
|
|
static crmgqlquery = ({
|
|
query,
|
|
toOptions,
|
|
variables,
|
|
}: IRequestToCRMGQL): Promise<IGetEntitiesResponse> =>
|
|
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<TCRMEntity | TCRMEntity[]> = res.data;
|
|
|
|
Object.keys(resEntities).forEach(targetName => {
|
|
//@ts-ignore
|
|
const targetEnt: TCRMEntity | TCRMEntity[] =
|
|
//@ts-ignore
|
|
resEntities[targetName];
|
|
|
|
if (toOptions)
|
|
//@ts-ignore
|
|
if (toOptions.includes(targetName)) {
|
|
//@ts-ignore
|
|
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
|
|
isPlural(targetName) ? singular(targetName) : targetName
|
|
] = optionatedEntities;
|
|
} else {
|
|
const entityOption = convertEntityToOption(
|
|
targetEnt,
|
|
targetEnt.__typename,
|
|
);
|
|
//@ts-ignore
|
|
resEntities[
|
|
//@ts-ignore
|
|
isPlural(targetName) ? singular(targetName) : targetName
|
|
] = entityOption;
|
|
}
|
|
}
|
|
});
|
|
|
|
resolve({ entities: resEntities });
|
|
})
|
|
.catch(err => {
|
|
throw err;
|
|
});
|
|
});
|
|
|
|
/**
|
|
* @deprecated
|
|
*/
|
|
static getEntities = ({
|
|
queries,
|
|
}: IGetEntitiesRequest): Promise<IGetEntitiesResponse> =>
|
|
new Promise(async (resolve, reject) => {
|
|
// remove queries with invalid where
|
|
queries = queries.filter(query => {
|
|
return Object.values(query.where).some(
|
|
x => x !== null && x !== undefined,
|
|
);
|
|
});
|
|
|
|
const convertedQuery = convertJSONToGQLQuery(queries);
|
|
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);
|
|
let resEntities: TEntities<TCRMEntity | TCRMEntity[]> = res.data;
|
|
|
|
Object.keys(resEntities).forEach(targetName => {
|
|
//@ts-ignore
|
|
const targetEnt: TCRMEntity | TCRMEntity[] =
|
|
//@ts-ignore
|
|
resEntities[targetName];
|
|
|
|
//@ts-ignore
|
|
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
|
|
isPlural(targetName) ? singular(targetName) : targetName
|
|
] = optionatedEntities;
|
|
} else {
|
|
const entityOption = convertEntityToOption(
|
|
targetEnt,
|
|
targetEnt.__typename,
|
|
);
|
|
//@ts-ignore
|
|
resEntities[
|
|
//@ts-ignore
|
|
isPlural(targetName) ? singular(targetName) : targetName
|
|
] = entityOption;
|
|
}
|
|
}
|
|
});
|
|
resolve({ entities: resEntities });
|
|
})
|
|
.catch(err => {
|
|
reject(err);
|
|
});
|
|
});
|
|
}
|
|
|
|
export default CalculationService;
|