2023-03-28 09:33:17 +03:00

38 lines
925 B
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: { 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);
},
};
}