2024-04-23 14:13:53 +03:00

48 lines
1.1 KiB
TypeScript

import { getCurrentDateString } from './date';
import * as CRMTypes from '@/graphql/crm.types';
import type { ApolloClient } from '@apollo/client';
type Context = {
apolloClient: ApolloClient<object>;
};
type Input = {
currencyid: string;
value: number;
};
export function createCurrencyUtility({ apolloClient }: Context) {
return {
async RUB({ currencyid, value }: Input) {
const {
data: { transactioncurrency },
} = await apolloClient.query({
query: CRMTypes.GetTransactionCurrencyDocument,
variables: {
currencyid,
},
});
if (transactioncurrency?.isocurrencycode === 'RUB') {
return value;
}
const {
data: { evo_currencychanges },
} = await apolloClient.query({
fetchPolicy: 'network-only',
query: CRMTypes.GetCurrencyChangesDocument,
variables: {
currentDate: getCurrentDateString(),
},
});
const evo_currencychange = evo_currencychanges?.find(
(x) => x?.evo_ref_transactioncurrency === currencyid
);
return value * (evo_currencychange?.evo_currencychange || 0);
},
};
}