remove json to gql converter

This commit is contained in:
Chika 2020-12-08 14:34:45 +03:00
parent 5d22cd74b3
commit f490244f38
11 changed files with 377 additions and 678 deletions

View File

@ -1,12 +1,11 @@
import CrmService from 'client/services/CrmService';
import CalculationStore from 'client/stores/CalculationStore';
import { currentDate } from 'client/tools/date';
import { getUser } from 'client/tools/user';
import initialOwnerQuery from './queries/ownerQuery';
import insuranceQuery from './queries/insuranceQuery';
import optionsQuery from './queries/optionsQuery';
import staticDataQuery from './queries/staticDataQuery';
import systemUserQuery from './queries/systemUserQuery';
import initialOwnerQuery from './queries/initialOwnerQuery';
export default () =>
new Promise((resolve, reject) => {
@ -14,30 +13,19 @@ export default () =>
.then(({ UserName, DomainName, FullName }) => {
Promise.all([
CrmService.crmgqlquery({
query: initialOwnerQuery,
...initialOwnerQuery,
variables: {
statecode: 0,
fullName: FullName,
},
toOptions: ['selectLead', 'selectOpportunity'],
}),
CrmService.getEntities({
queries: optionsQuery,
}),
CrmService.crmgqlquery(optionsQuery),
CrmService.crmgqlquery(staticDataQuery),
CrmService.crmgqlquery({
query: staticDataQuery,
variables: {
statecode: 0,
currentDate,
},
}),
CrmService.crmgqlquery({
query: systemUserQuery,
...systemUserQuery,
variables: { username: `${DomainName}\\${UserName}` },
}),
CrmService.getEntities({
queries: insuranceQuery,
}),
CrmService.crmgqlquery(insuranceQuery),
])
.then(
([

View File

@ -1,14 +1,24 @@
import { TEntityQuery } from 'core/types/Entities/query';
const insuranceQuery: TEntityQuery[] = [
{
//@ts-ignore
alias: 'insuranceCompany',
entityName: 'account',
where: { evo_account_type: 100000002, statecode: 0 },
fields: ['accountid', 'name', 'evo_type_ins_policy'],
many: true,
toOption: true,
},
];
import { IQueryToCRMGQL } from 'core/types/Calculation/Requests';
import { gql } from '@apollo/client';
export default insuranceQuery;
const query = gql`
query($evo_account_type: [Int!], $statecode: Int) {
insuranceCompanies: accounts(
evo_account_type: $evo_account_type
statecode: $statecode
) {
accountid
name
evo_type_ins_policy
}
}
`;
const variables = { evo_account_type: [100000002], statecode: 0 };
const toOptions = ['insuranceCompanies'];
export default {
query,
variables,
toOptions,
} as IQueryToCRMGQL;

View File

@ -1,424 +1,298 @@
import { TEntityQuery } from 'core/types/Entities/query';
import { currentDate } from 'client/tools/date';
import { IQueryToCRMGQL } from 'core/types/Calculation/Requests';
import { gql } from '@apollo/client';
const initialOptionsQuery: TEntityQuery[] = [
{
alias: 'selectSupplier',
entityName: 'account',
where: {
evo_account_type: 100000001,
statecode: 0,
evo_legal_form: 100000001,
},
fields: ['accountid', 'name', 'evo_fin_department_accountid'],
many: true,
toOption: true,
},
{
alias: 'selectSupplierCurrency',
entityName: 'transactioncurrency',
where: {
statecode: 0,
},
fields: ['transactioncurrencyid', 'isocurrencycode'],
many: true,
toOption: true,
},
{
alias: 'selectClientRisk',
entityName: 'evo_client_risk',
where: {
statecode: 0,
},
fields: ['statecode', 'evo_name', 'evo_client_riskid'],
many: true,
toOption: true,
},
{
alias: 'selectClientType',
entityName: 'evo_client_type',
where: {
statecode: 0,
},
fields: ['evo_name', 'evo_client_typeid'],
many: true,
toOption: true,
},
{
alias: 'selectDealer',
entityName: 'account',
where: {
evo_account_type: 100000001,
statecode: 0,
evo_legal_form: 100000001,
},
fields: ['accountid', 'name'],
many: true,
toOption: true,
},
{
alias: 'selectGPSBrand',
entityName: 'evo_gps_brand',
where: {
statecode: 0,
},
fields: ['evo_gps_brandid', 'evo_name'],
many: true,
toOption: true,
},
{
alias: 'selectRegionRegistration',
entityName: 'evo_region',
where: {
statecode: 0,
},
fields: ['evo_name', 'evo_regionid'],
many: true,
toOption: true,
},
const query = gql`
query(
$statecode: Int
$currentDate: DateTime
$supplier_account_type: [Int!]
$supplier_legal_form: Int
$dealer_account_type: [Int!]
$dealer_legal_form: Int
$account_account_type: [Int!]
$product_relation: [Int!]
$registration_product_type: Int
$nsib_product_type: Int
$tracker_product_type: Int
$telematic_product_type: Int
$techcard_product_type: Int
) {
selectSupplier: accounts(
evo_account_type: $supplier_account_type
statecode: $statecode
evo_legal_form: $supplier_legal_form
) {
name
accountid
evo_fin_department_accountid
}
selectSupplierCurrency: transactioncurrencies(statecode: $statecode) {
transactioncurrencyid
isocurrencycode
}
selectClientRisk: evo_client_risks(statecode: $statecode) {
evo_name
evo_client_riskid
}
selectClientType: evo_client_types(statecode: $statecode) {
evo_name
evo_client_typeid
}
selectDealer: accounts(
evo_account_type: $dealer_account_type
statecode: $statecode
evo_legal_form: $dealer_legal_form
) {
name
accountid
}
selectGPSBrand: evo_gps_brands(statecode: $statecode) {
evo_name
evo_gps_brandid
}
selectRegionRegistration: evo_regions(statecode: $statecode) {
evo_name
evo_regionid
}
selectAccount: accounts(
evo_account_type: $account_account_type
statecode: $statecode
) {
name
accountid
evo_client_riskid
}
selectBrand: evo_brands(statecode: $statecode) {
evo_name
evo_brandid
evo_importer_reward_perc
evo_importer_reward_rub
}
selectProduct: evo_baseproducts(
statecode: $statecode
evo_relation: $product_relation
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
evo_name
evo_baseproductid
evo_id
evo_leasingobject_types {
evo_name
evo_leasingobject_typeid
evo_id
}
evo_brands {
evo_name
evo_brandid
}
}
selectRegistration: evo_addproduct_types(
statecode: $statecode
evo_product_type: $registration_product_type
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
evo_id
evo_name
evo_addproduct_typeid
evo_accountid
evo_graph_price_withoutnds
evo_cost_service_provider_withoutnds
evo_retro_bonus_withoutnds
evo_prime_cost
evo_graph_price
evo_max_period
evo_min_period
evo_controls_program
}
selectInsNSIB: evo_addproduct_types(
statecode: $statecode
evo_product_type: $nsib_product_type
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
evo_id
evo_name
evo_addproduct_typeid
evo_accountid
evo_graph_price_withoutnds
evo_cost_service_provider_withoutnds
evo_retro_bonus_withoutnds
evo_prime_cost
evo_graph_price
evo_max_period
evo_min_period
evo_controls_program
}
selectTracker: evo_addproduct_types(
statecode: $statecode
evo_product_type: $tracker_product_type
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
evo_id
evo_name
evo_addproduct_typeid
evo_accountid
evo_graph_price_withoutnds
evo_cost_service_provider_withoutnds
evo_retro_bonus_withoutnds
evo_prime_cost
evo_graph_price
evo_max_period
evo_min_period
evo_controls_program
evo_planpayments {
evo_name
evo_cost_equipment_withoutnds
evo_cost_price_telematics_withoutnds
evo_cost_telematics_withoutnds
}
}
selectTelematic: evo_addproduct_types(
statecode: $statecode
evo_product_type: $telematic_product_type
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
evo_id
evo_name
evo_addproduct_typeid
evo_accountid
evo_graph_price_withoutnds
evo_cost_service_provider_withoutnds
evo_retro_bonus_withoutnds
evo_prime_cost
evo_graph_price
evo_max_period
evo_min_period
evo_controls_program
evo_planpayments {
evo_name
evo_cost_equipment_withoutnds
evo_cost_price_telematics_withoutnds
evo_cost_telematics_withoutnds
}
}
selectTechnicalCard: evo_addproduct_types(
statecode: $statecode
evo_product_type: $techcard_product_type
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
evo_id
evo_name
evo_addproduct_typeid
evo_accountid
evo_graph_price_withoutnds
evo_cost_service_provider_withoutnds
evo_retro_bonus_withoutnds
evo_prime_cost
evo_graph_price
evo_max_period
evo_min_period
evo_controls_program
}
selectTarif: evo_tarifs(
statecode: $statecode
evo_datefrom_param: { lte: $currentDate }
evo_dateto_param: { gte: $currentDate }
) {
evo_name
evo_tarifid
evo_baseproductid
evo_irr
evo_max_irr
evo_min_profit
evo_min_irr
evo_irr_plan
evo_ins_type
evo_min_period
evo_max_period
evo_client_risks {
evo_name
evo_client_riskid
}
evo_client_types {
evo_name
evo_client_typeid
}
evo_leasingobject_types {
evo_name
evo_id
evo_leasingobject_typeid
}
}
selectRate: evo_rates(
statecode: $statecode # evo_datefrom_param: { lte: $currentDate } # evo_dateto_param: { gte: $currentDate }
) {
evo_id
evo_rateid
evo_name
evo_base_rate
evo_coeff_12_23
evo_coeff_24_35
evo_coeff_36_47
evo_coeff_7_11
evo_coeff_48_60
evo_tarifs {
evo_tarifid
evo_name
}
}
selectLeaseObjectType: evo_leasingobject_types(statecode: $statecode) {
evo_name
evo_id
evo_leasingobject_typeid
evo_depreciation_rate1
evo_depreciation_rate2
evo_expluatation_period1
evo_expluatation_period2
evo_type_code
}
}
`;
{
alias: 'selectAccount',
entityName: 'account',
where: {
evo_account_type: 100000000,
statecode: 0,
},
fields: ['accountid', 'name', 'evo_client_riskid'],
many: true,
toOption: true,
},
{
alias: 'selectBrand',
entityName: 'evo_brand',
where: {
statecode: 0,
},
fields: [
'evo_name',
'evo_brandid',
'evo_importer_reward_perc',
'evo_importer_reward_rub',
],
many: true,
toOption: true,
},
{
alias: 'selectProduct',
entityName: 'evo_baseproduct',
where: {
evo_relation: 100000000,
statecode: 0,
},
whereCmp: {
evo_datefrom_param: {
lte: currentDate,
},
evo_dateto_param: {
gte: currentDate,
},
},
fields: ['evo_name', 'evo_baseproductid', 'evo_id'],
relatedEntities: [
{
entityName: 'evo_leasingobject_type',
fields: ['evo_name', 'evo_leasingobject_typeid', 'evo_id'],
many: true,
},
{
entityName: 'evo_brand',
fields: ['evo_brandid', 'evo_name'],
many: true,
},
],
many: true,
toOption: true,
},
{
alias: 'selectRegistration',
entityName: 'evo_addproduct_type',
where: {
statecode: 0,
evo_product_type: 100000001,
},
whereCmp: {
evo_datefrom_param: {
lte: currentDate,
},
evo_dateto_param: {
gte: currentDate,
},
},
fields: [
'evo_id',
'evo_name',
'evo_addproduct_typeid',
'evo_accountid',
'evo_graph_price_withoutnds',
'evo_cost_service_provider_withoutnds',
'evo_retro_bonus_withoutnds',
'evo_prime_cost',
'evo_graph_price',
'evo_max_period',
'evo_min_period',
'evo_controls_program',
],
many: true,
toOption: true,
},
{
alias: 'selectInsNSIB',
entityName: 'evo_addproduct_type',
where: {
statecode: 0,
evo_product_type: 100000002,
},
whereCmp: {
evo_datefrom_param: {
lte: currentDate,
},
evo_dateto_param: {
gte: currentDate,
},
},
fields: [
'evo_id',
'evo_name',
'evo_addproduct_typeid',
'evo_accountid',
'evo_graph_price_withoutnds',
'evo_cost_service_provider_withoutnds',
'evo_retro_bonus_withoutnds',
'evo_prime_cost',
'evo_graph_price',
'evo_max_period',
'evo_min_period',
'evo_controls_program',
],
many: true,
toOption: true,
},
{
alias: 'selectTracker',
entityName: 'evo_addproduct_type',
where: {
statecode: 0,
evo_product_type: 100000003,
},
whereCmp: {
evo_datefrom_param: {
lte: currentDate,
},
evo_dateto_param: {
gte: currentDate,
},
},
fields: [
'evo_id',
'evo_name',
'evo_addproduct_typeid',
'evo_accountid',
'evo_graph_price_withoutnds',
'evo_cost_service_provider_withoutnds',
'evo_retro_bonus_withoutnds',
'evo_prime_cost',
'evo_graph_price',
'evo_max_period',
'evo_min_period',
'evo_controls_program',
],
relatedEntities: [
{
entityName: 'evo_planpayment',
fields: [
'evo_name',
'evo_cost_equipment_withoutnds',
'evo_cost_price_telematics_withoutnds',
'evo_cost_telematics_withoutnds',
],
many: true,
},
],
many: true,
toOption: true,
},
{
alias: 'selectTelematic',
entityName: 'evo_addproduct_type',
where: {
statecode: 0,
evo_product_type: 100000004,
},
whereCmp: {
evo_datefrom_param: {
lte: currentDate,
},
evo_dateto_param: {
gte: currentDate,
},
},
fields: [
'evo_id',
'evo_name',
'evo_addproduct_typeid',
'evo_accountid',
'evo_graph_price_withoutnds',
'evo_cost_service_provider_withoutnds',
'evo_retro_bonus_withoutnds',
'evo_prime_cost',
'evo_graph_price',
'evo_max_period',
'evo_min_period',
'evo_controls_program',
],
relatedEntities: [
{
entityName: 'evo_planpayment',
fields: [
'evo_name',
'evo_cost_equipment_withoutnds',
'evo_cost_price_telematics_withoutnds',
'evo_cost_telematics_withoutnds',
],
many: true,
},
],
many: true,
toOption: true,
},
{
alias: 'selectTechnicalCard',
entityName: 'evo_addproduct_type',
where: {
statecode: 0,
evo_product_type: 100000000,
},
whereCmp: {
evo_datefrom_param: {
lte: currentDate,
},
evo_dateto_param: {
gte: currentDate,
},
},
fields: [
'evo_id',
'evo_name',
'evo_addproduct_typeid',
'evo_accountid',
'evo_graph_price_withoutnds',
'evo_cost_service_provider_withoutnds',
'evo_retro_bonus_withoutnds',
'evo_prime_cost',
'evo_graph_price',
'evo_max_period',
'evo_min_period',
'evo_controls_program',
],
many: true,
toOption: true,
},
{
alias: 'selectTarif',
entityName: 'evo_tarif',
where: {
statecode: 0,
},
whereCmp: {
evo_datefrom_param: {
lte: currentDate,
},
evo_dateto_param: {
gte: currentDate,
},
},
fields: [
'evo_id',
'evo_name',
'evo_tarifid',
'evo_baseproductid',
'evo_irr',
'evo_max_irr',
'evo_min_profit',
'evo_min_irr',
'evo_irr_plan',
'evo_ins_type',
'evo_min_period',
'evo_max_period',
],
relatedEntities: [
{
entityName: 'evo_client_risk',
fields: ['evo_client_riskid', 'evo_name'],
many: true,
},
{
entityName: 'evo_client_type',
fields: ['evo_client_typeid', 'evo_name'],
many: true,
},
{
entityName: 'evo_leasingobject_type',
fields: ['evo_leasingobject_typeid', 'evo_name', 'evo_id'],
many: true,
},
],
many: true,
toOption: true,
},
{
alias: 'selectRate',
entityName: 'evo_rate',
where: {
statecode: 0,
},
//TODO:
// whereCmp: {
// evo_datefrom_param: {
// lte: currentDate,
// },
// evo_dateto_param: {
// gte: currentDate,
// },
// },
fields: [
'evo_id',
'evo_rateid',
'evo_name',
'evo_base_rate',
'evo_coeff_12_23',
'evo_coeff_24_35',
'evo_coeff_36_47',
'evo_coeff_7_11',
'evo_coeff_48_60',
],
relatedEntities: [
{
entityName: 'evo_tarif',
fields: ['evo_tarifid', 'evo_name'],
many: true,
},
],
many: true,
toOption: true,
},
{
alias: 'selectLeaseObjectType',
entityName: 'evo_leasingobject_type',
where: { statecode: 0 },
fields: [
'evo_name',
'evo_id',
'evo_leasingobject_typeid',
'evo_depreciation_rate1',
'evo_depreciation_rate2',
'evo_expluatation_period1',
'evo_expluatation_period2',
'evo_type_code',
],
many: true,
toOption: true,
},
const variables = {
currentDate,
statecode: 0,
supplier_account_type: [100000001],
supplier_legal_form: 100000001,
dealer_account_type: [100000001],
dealer_legal_form: 100000001,
account_account_type: [100000000],
product_relation: [100000000],
registration_product_type: 100000001,
nsib_product_type: 100000002,
tracker_product_type: 100000003,
telematic_product_type: 100000004,
techcard_product_type: 100000000,
};
const toOptions = [
'selectSupplier',
'selectSupplierCurrency',
'selectClientRisk',
'selectClientType',
'selectDealer',
'selectGPSBrand',
'selectRegionRegistration',
'selectAccount',
'selectBrand',
'selectProduct',
'selectRegistration',
'selectInsNSIB',
'selectTracker',
'selectTechnicalCard',
'selectTarif',
'selectRate',
'selectLeaseObjectType',
];
export default initialOptionsQuery;
export default {
query,
variables,
toOptions,
} as IQueryToCRMGQL;

View File

@ -1,5 +1,7 @@
import { IQueryToCRMGQL } from 'core/types/Calculation/Requests';
import { gql } from '@apollo/client';
export default gql`
const query = gql`
query($statecode: Int, $fullName: String) {
selectLead: leads(statecode: $statecode, owner_domainname: $fullName) {
customerid
@ -24,3 +26,10 @@ export default gql`
}
}
`;
const toOptions = ['selectLead', 'selectOpportunity'];
export default {
query,
toOptions,
} as IQueryToCRMGQL;

View File

@ -1,6 +1,8 @@
import { IQueryToCRMGQL } from 'core/types/Calculation/Requests';
import { currentDate } from 'client/tools/date';
import { gql } from '@apollo/client';
export default gql`
const query = gql`
query($statecode: Int, $currentDate: DateTime) {
evo_impairment_group: evo_impairment_groups(statecode: $statecode) {
evo_impairment_groupid
@ -51,3 +53,13 @@ export default gql`
}
}
`;
const variables = {
statecode: 0,
currentDate,
};
export default {
query,
variables,
} as IQueryToCRMGQL;

View File

@ -1,13 +1,15 @@
import { gql } from '@apollo/client';
export default gql`
query($username: String) {
systemuser(domainname: $username) {
evo_job_titleid
businessunitid
firstname
lastname
fullname
export default {
query: gql`
query($username: String) {
systemuser(domainname: $username) {
evo_job_titleid
businessunitid
firstname
lastname
fullname
}
}
}
`;
`,
};

View File

@ -1,30 +1,27 @@
import { gql } from '@apollo/client';
import axios from 'axios';
import { getServerUrl } from 'client/common/urls';
import { CRM_PROXY_URL } from 'core/constants/urls';
import { convertEntityToOption } from './tools/entity';
import { convertJSONToGQLQuery } from './tools/query';
import {
ICreateKpRequest,
IGetEntitiesRequest,
IMutateToCRMGQL,
IQueryToCRMGQL,
} from 'core/types/Calculation/Requests';
import { IGetEntitiesResponse } from 'core/types/Calculation/Responses';
import { IGetCRMEntitiesResponse } from 'core/types/Calculation/Responses';
import { IBaseOption } from 'core/types/Calculation/Store/options';
import { IQuote, TCRMEntity } from 'core/types/Entities/crmEntities';
import { TEntities } from 'core/types/Entities/crmEntityNames';
import client from './client';
import { isPlural, singular } from 'pluralize';
import client from './client';
import { convertEntityToOption } from './tools/entity';
export default class {
static crmgqlquery = ({
query,
toOptions,
variables,
}: IQueryToCRMGQL): Promise<IGetEntitiesResponse> =>
new Promise(async (resolve, reject) => {
await client
}: IQueryToCRMGQL): Promise<IGetCRMEntitiesResponse> =>
new Promise((resolve, reject) => {
client
.query({
query,
variables,
@ -36,15 +33,11 @@ export default class {
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) {
@ -56,9 +49,8 @@ export default class {
optionatedEntities.push(entityOption);
}
}
//@ts-ignore
resEntities[
//@ts-ignore
isPlural(targetName) ? singular(targetName) : targetName
] = optionatedEntities;
} else {
@ -66,9 +58,8 @@ export default class {
targetEnt,
targetEnt.__typename,
);
//@ts-ignore
resEntities[
//@ts-ignore
isPlural(targetName) ? singular(targetName) : targetName
] = entityOption;
}
@ -85,9 +76,9 @@ export default class {
static crmgqlmutaion = ({
mutation,
variables,
}: IMutateToCRMGQL): Promise<IGetEntitiesResponse> =>
new Promise(async (resolve, reject) => {
await client
}: IMutateToCRMGQL): Promise<IGetCRMEntitiesResponse> =>
new Promise((resolve, reject) => {
client
.mutate({
mutation,
variables,
@ -105,7 +96,7 @@ export default class {
});
static createKp = (data: ICreateKpRequest): Promise<IQuote> =>
new Promise(async (resolve, reject) => {
new Promise((resolve, reject) => {
axios
.post<IQuote>(
getServerUrl('/proxy', CRM_PROXY_URL, '/offer', '/leasingcalculator'),
@ -118,79 +109,4 @@ export default class {
reject(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);
});
});
}

View File

@ -1,80 +0,0 @@
import { stringifyObject } from 'core/tools/string';
import {
ComparisonOperators,
TBaseEntityQuery,
TEntitiesKeys,
TEntityQuery,
TWhere,
} from 'core/types/Entities/query';
import { plural } from 'pluralize';
const convert = {
fields: (fields: TEntitiesKeys[]): string => {
let res_fields: string[] = [];
for (let field of fields) {
res_fields.push(stringifyObject(field).replace(/[^\w\s,:!?]/g, ''));
}
return res_fields.toString().replace(/"/, '').replace(/,/, ' ');
},
relatedEntities: (relatedEntities?: TBaseEntityQuery[]): string => {
if (!relatedEntities) {
return '';
}
let res_related: string[] = [];
if (relatedEntities && relatedEntities.length > 0) {
for (let relatedEntity of relatedEntities) {
res_related.push(convertNestedEntity(relatedEntity));
}
}
return res_related.toString().replace(/"/, '');
},
where: (where: TWhere<any>): string => {
return stringifyObject(where);
},
whereCmp: (whereCmp?: TWhere<ComparisonOperators>) => {
if (!whereCmp) {
return '';
}
return Object.keys(whereCmp).map(
//@ts-ignore
key => `${key}:{${stringifyObject(whereCmp[key])}}`,
);
},
};
function convertNestedEntity(query: TBaseEntityQuery) {
let entityQuery: string = `${
query.many ? plural(query.entityName) : query.entityName
}{
${convert.fields(query.fields)}
${convert.relatedEntities(query.relatedEntities)}
}`;
if (query.alias) {
return `${query.alias}:${entityQuery}`;
}
return entityQuery;
}
//TODO: WhereCmp
function convertQuery(query: TEntityQuery) {
let entityQuery: string = `${
query.many ? plural(query.entityName) : query.entityName
}(${convert.where(query.where)},${convert.whereCmp(query.whereCmp)}){
${convert.fields(query.fields)}
${convert.relatedEntities(query.relatedEntities)}
}`;
if (query.alias) {
return `${query.alias}:${entityQuery}`;
}
return entityQuery;
}
export const convertJSONToGQLQuery = (queries: TEntityQuery[]) => {
let res = '';
for (let query of queries) {
res += convertQuery(query);
}
return `query {
${res}
}`;
};

View File

@ -1,21 +1,19 @@
import { TCRMEntity } from 'core/types/Entities/crmEntities';
import { ColumnsNames, IColumn, PostValues } from 'core/types/Calculation/Core';
import {
PreparedPayments,
PreparedValues,
} from 'core/types/Calculation/Prepare';
import { CRMEntityNames } from 'core/types/Entities/crmEntityNames';
import { TEntityQuery } from '../Entities/query';
import { ColumnsNames, IColumn, PostValues } from 'core/types/Calculation/Core';
import { TValues } from 'core/types/Calculation/Store/values';
import { TableProps } from 'core/types/Calculation/Store/tables';
export interface IGetEntitiesRequest {
queries: TEntityQuery[];
}
import { TValues } from 'core/types/Calculation/Store/values';
import { CRMEntityNames } from 'core/types/Entities/crmEntityNames';
export interface IQueryToCRMGQL {
query: any;
toOptions?: CRMEntityNames[];
variables: { [prop: string]: any };
variables: {
[prop in keyof TCRMEntity]: any;
} & { [prop: string]: any };
}
export interface IMutateToCRMGQL {

View File

@ -3,7 +3,7 @@ import { TCRMEntity } from '../Entities/crmEntities';
import { TEntities } from '../Entities/crmEntityNames';
import { IColumn, PostValues, ColumnsNames } from './Core';
export interface IGetEntitiesResponse {
export interface IGetCRMEntitiesResponse {
entities: TEntities<TCRMEntity | TCRMEntity[]>;
}

View File

@ -1,30 +0,0 @@
// import { ElementsNames } from 'core/types/Calculation/Store/elements';
import { TCRMEntity } from './crmEntities';
import { CRMEntityNames } from './crmEntityNames';
import { ElementsNames } from 'core/types/Calculation/Store/elements';
export type ComparisonOperators = {
eq?: any;
gt?: any;
lt?: any;
gte?: any;
lte?: any;
};
export type TEntitiesKeys = keyof TCRMEntity;
export type TWhere<T> = { [prop in TEntitiesKeys]?: T };
export type TBaseEntityQuery = {
alias?: ElementsNames;
entityName: CRMEntityNames;
fields: [TEntitiesKeys, ...TEntitiesKeys[]];
relatedEntities?: TBaseEntityQuery[];
many?: boolean;
};
export type TEntityQuery = TBaseEntityQuery & {
where: TWhere<any | any[]>;
whereCmp?: TWhere<ComparisonOperators>;
toOption?: boolean;
};