@cranberry-money/shared-services
Version:
Platform-agnostic API services with pure functions and dependency injection. Includes auth, portfolios, instruments, countries, sectors, and more.
64 lines • 2.62 kB
JavaScript
import { CASH_ACCOUNT_TRANSACTION_ENDPOINTS, DEFAULT_RECENT_TRANSACTIONS_DAYS, MILLISECONDS_PER_DAY, DATE_ISO_TIME_SEPARATOR, } from '@cranberry-money/shared-constants';
import { formatTransactionAmount, getTransactionTypeLabel } from '@cranberry-money/shared-utils';
let configuredApiClient = null;
export const configureCashAccountTransactions = (apiClient) => {
configuredApiClient = apiClient;
};
const getConfiguredClient = () => {
if (!configuredApiClient) {
throw new Error('Cash account transactions service not configured. Call configureCashAccountTransactions(apiClient) before using cash account transaction functions.');
}
return configuredApiClient;
};
export const getCashAccountTransactions = (params) => {
const queryParams = {};
if (params?.cash_account) {
queryParams.cash_account = params.cash_account;
}
if (params?.transaction_type) {
queryParams.transaction_type = params.transaction_type;
}
if (params?.start_date) {
queryParams.start_date = params.start_date;
}
if (params?.end_date) {
queryParams.end_date = params.end_date;
}
if (params?.min_amount !== undefined) {
queryParams.min_amount = params.min_amount.toString();
}
if (params?.max_amount !== undefined) {
queryParams.max_amount = params.max_amount.toString();
}
if (params?.order_by) {
queryParams.order_by = params.order_by;
}
return getConfiguredClient().get(CASH_ACCOUNT_TRANSACTION_ENDPOINTS.BASE, {
params: queryParams,
});
};
export const getCashAccountTransactionByUuid = (uuid) => {
return getConfiguredClient().get(`${CASH_ACCOUNT_TRANSACTION_ENDPOINTS.BASE}${uuid}/`);
};
export const getCashAccountTransactionsByAccount = (cashAccountUuid, params) => {
return getCashAccountTransactions({
...params,
cash_account: cashAccountUuid,
});
};
export const getTransactionsByDateRange = (startDate, endDate, params) => {
return getCashAccountTransactions({
...params,
start_date: startDate,
end_date: endDate,
});
};
export const getRecentTransactions = (days = DEFAULT_RECENT_TRANSACTIONS_DAYS, params) => {
const endDate = new Date().toISOString().split(DATE_ISO_TIME_SEPARATOR)[0];
const startDate = new Date(Date.now() - days * MILLISECONDS_PER_DAY)
.toISOString()
.split(DATE_ISO_TIME_SEPARATOR)[0];
return getTransactionsByDateRange(startDate, endDate, params);
};
export { formatTransactionAmount, getTransactionTypeLabel };
//# sourceMappingURL=cashAccountTransactions.js.map