Skip to content

Commit 93fd04c

Browse files
refactor(OUT-3604): address PR #266 review nits
- invoice.service: simplify Undeposited-Funds ref to a ternary. - payment.service: type paymentLines and build the fee line via push when feeTotal > 0 instead of a spread. - webhook.service: accumulate gross/fee cents in a single reduce. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent f801f3a commit 93fd04c

3 files changed

Lines changed: 34 additions & 27 deletions

File tree

‎src/app/api/quickbooks/invoice/invoice.service.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -950,19 +950,17 @@ export class InvoiceService extends BaseService {
950950

951951
const intuitApi = new IntuitAPI(qbTokenInfo)
952952

953-
let depositToAccountRef: { value: string } | undefined
954-
if (useBankDepositFlow) {
955-
const undepositedFundsRef = await intuitApi.getUndepositedFundsAccountId()
956-
depositToAccountRef = { value: undepositedFundsRef }
957-
}
953+
const depositToAccountRef = useBankDepositFlow
954+
? await intuitApi.getUndepositedFundsAccountId()
955+
: undefined
958956

959957
const qbPaymentPayload = {
960958
TotalAmt: invoiceAmount,
961959
CustomerRef: {
962960
value: existingCustomer.qbCustomerId,
963961
},
964962
...(depositToAccountRef && {
965-
DepositToAccountRef: depositToAccountRef,
963+
DepositToAccountRef: { value: depositToAccountRef },
966964
}),
967965
Line: [
968966
{

‎src/app/api/quickbooks/payment/payment.service.ts‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -215,32 +215,35 @@ export class PaymentService extends BaseService {
215215
feeTotal: opts.feeTotal,
216216
})
217217

218-
const paymentLines = opts.lines.map((line) => ({
219-
Amount: line.amount,
220-
LinkedTxn: [
221-
{
222-
TxnId: line.qbPaymentId,
223-
TxnType: 'Payment' as const,
224-
TxnLineId: '0',
225-
},
226-
],
227-
}))
218+
const paymentLines: Required<QBDepositCreatePayloadType>['Line'] =
219+
opts.lines.map((line) => ({
220+
Amount: line.amount,
221+
LinkedTxn: [
222+
{
223+
TxnId: line.qbPaymentId,
224+
TxnType: 'Payment' as const,
225+
TxnLineId: '0',
226+
},
227+
],
228+
}))
228229

229-
const feeLine = {
230-
Amount: -opts.feeTotal,
231-
DetailType: 'DepositLineDetail' as const,
232-
DepositLineDetail: {
233-
AccountRef: { value: opts.expenseAccountRef },
234-
},
235-
Description: 'Stripe processing fees',
230+
// feeTotal is always >= 0 (caller rejects negative): 0 = no fee line.
231+
if (opts.feeTotal > 0) {
232+
paymentLines.push({
233+
Amount: -opts.feeTotal,
234+
DetailType: 'DepositLineDetail' as const,
235+
DepositLineDetail: {
236+
AccountRef: { value: opts.expenseAccountRef },
237+
},
238+
Description: 'Stripe processing fees',
239+
})
236240
}
237241

238242
const depositPayload: QBDepositCreatePayloadType = {
239243
DepositToAccountRef: { value: opts.bankAccountRef },
240244
PrivateNote: opts.privateNote,
241245
TxnDate: opts.txnDate,
242-
// feeTotal is always >= 0 (caller rejects negative): 0 = no fee line.
243-
Line: opts.feeTotal > 0 ? [...paymentLines, feeLine] : paymentLines,
246+
Line: paymentLines,
244247
}
245248

246249
const parsedPayload = QBDepositCreatePayloadSchema.parse(depositPayload)

‎src/app/api/quickbooks/webhook/webhook.service.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,14 @@ export class WebhookService extends BaseService {
628628
}
629629

630630
// Computed before the try so the FAILED-log path can record the amounts.
631-
const grossCents = lineItems.reduce((sum, l) => sum + l.grossAmount, 0)
632-
const feeCents = lineItems.reduce((sum, l) => sum + l.feeAmount, 0)
631+
const { grossCents, feeCents } = lineItems.reduce(
632+
(acc, line) => {
633+
acc.grossCents += line.grossAmount
634+
acc.feeCents += line.feeAmount
635+
return acc
636+
},
637+
{ grossCents: 0, feeCents: 0 },
638+
)
633639

634640
try {
635641
validateAccessToken(qbTokenInfo)

0 commit comments

Comments
 (0)