import * as CRMTypes from '@/graphql/crm.types'; import type { ApolloClient } from '@apollo/client'; import { getCurrentISODate } from './date'; type Context = { apolloClient: ApolloClient; }; 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: getCurrentISODate(), }, }); const evo_currencychange = evo_currencychanges?.find( (x) => x?.evo_ref_transactioncurrency === currencyid ); return value * (evo_currencychange?.evo_currencychange || 0); }, }; }