48 lines
1.1 KiB
TypeScript
48 lines
1.1 KiB
TypeScript
import { getCurrentISODate } 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: getCurrentISODate(),
|
|
},
|
|
});
|
|
|
|
const evo_currencychange = evo_currencychanges?.find(
|
|
(x) => x?.evo_ref_transactioncurrency === currencyid
|
|
);
|
|
|
|
return value * (evo_currencychange?.evo_currencychange || 0);
|
|
},
|
|
};
|
|
}
|