51 lines
1.2 KiB
TypeScript

import * as CRMTypes from '@/graphql/crm.types';
import type { ApolloClient } from '@apollo/client';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
dayjs.extend(utc);
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: dayjs().utc(false).format('YYYY-MM-DD'),
},
});
const evo_currencychange = evo_currencychanges?.find(
(x) => x?.evo_ref_transactioncurrency === currencyid
);
return value * (evo_currencychange?.evo_currencychange || 0);
},
};
}