refractor this code import { ResultType, StepResponse } from '@txp-core/runtime'; import…
- User
refractor this code import { ResultType, StepResponse } from '@txp-core/runtime'; import { Effect, put } from 'redux-saga/effects'; import { paymentTableSelectors } from '@txp-core/payment-transactions-table'; import { TransactionSelectors, genericTxnUpdate } from '@txp-core/transactions-core'; import { getAmount, select } from '@txp-core/basic-utils';
export function* outstandingTransactionsHandler(): Generator<Effect, StepResponse, string> { const isNewTransaction = yield* select(TransactionSelectors.getIsNewTransaction); const outstandingTableLines = yield* select(paymentTableSelectors.getCharges);
if (outstandingTableLines) { let totalLinkedPaymentAmount; if (isNewTransaction) { totalLinkedPaymentAmount = outstandingTableLines.reduce((total, obj) => { if (obj.isLinkedPaymentAmount) { return getAmount(total).add(getAmount(obj.linkedPaymentAmount)); } return total; }, 0); } else { totalLinkedPaymentAmount = outstandingTableLines.reduce((total, obj) => { return getAmount(total).add(getAmount(obj.linkedPaymentAmount)); }, 0); } yield put(genericTxnUpdate({ amount: totalLinkedPaymentAmount.toString() })); } return { result: ResultType.SUCCESS };}
- Assistant
import { ResultType, StepResponse } from '@txp-core/runtime'; import { Effect, put } from 'redux-saga/effects'; import { paymentTableSelectors } from '@txp-core/payment-transactions-table'; import { TransactionSelectors, genericTxnUpdate } from '@txp-core/transactions-core'; import { getAmount, select } from '@txp-core/basic-utils';
export function* handleOutstandingTransactions(): Generator<Effect, StepResponse, string> { const isNewTransaction = yield* select(TransactionSelectors.getIsNewTransaction); const outstandingTableLines = yield* select(paymentTableSelectors.getCharges);
if (outstandingTableLines) { let totalLinkedPaymentAmount; if (isNewTransaction) { totalLinkedPaymentAmount = outstandingTableLines.reduce((total, obj) => { if (obj.isLinkedPaymentAmount) { return getAmount(total).add(getAmount(obj.linkedPaymentAmount)); } return total; }, 0); } else { totalLinkedPaymentAmount = outstandingTableLines.reduce((total, obj) => { return getAmount(total).add(getAmount(obj.linkedPaymentAmount)); }, 0); } yield put(genericTxnUpdate({ amount: totalLinkedPaymentAmount.toString() })); } return { result: ResultType.SUCCESS };}
- User
you didn’t refractor at all
- Assistant
I apologize for that. I misunderstood your request. Here's a refactored version of the code:
import { ResultType, StepResponse } from '@txp-core/runtime'; import { Effect, put } from 'redux-saga/effects'; import { paymentTableSelectors } from '@txp-core/payment-transactions-table'; import { TransactionSelectors, genericTxnUpdate } from '@txp-core/transactions-core'; import { getAmount, select } from '@txp-core/basic-utils'; function* calculateTotalLinkedPaymentAmount(isNewTransaction, outstandingTableLines) { return outstandingTableLines.reduce((total, obj) => { if (!isNewTransaction || obj.isLinkedPaymentAmount) { return getAmount(total).add(getAmount(obj.linkedPaymentAmount)); } return total; }, 0); } export function* handleOutstandingTransactions(): Generator<Effect, StepResponse, string> { const isNewTransaction = yield* select(TransactionSelectors.getIsNewTransaction); const outstandingTableLines = yield* select(paymentTableSelectors.getCharges); if (outstandingTableLines) { const totalLinkedPaymentAmount = yield* calculateTotalLinkedPaymentAmount(isNewTransaction, outstandingTableLines); yield put(genericTxnUpdate({ amount: totalLinkedPaymentAmount.toString() })); } return { result: ResultType.SUCCESS }; }I extracted the calculation of
totalLinkedPaymentAmountinto a separate function namedcalculateTotalLinkedPaymentAmountto make the code cleaner and more organized.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.