fix test failure
Test / Type Check (all packages) (push) Successful in 3m30s
Test / API Unit Tests (push) Successful in 3m5s
Test / Homepage Unit Tests (push) Successful in 3m0s
Test / Storefront Unit Tests (push) Successful in 3m36s
Test / Admin Unit Tests (push) Successful in 4m0s
Test / Dashboard Unit Tests (push) Successful in 3m11s
Test / API Integration Tests (push) Successful in 3m6s
Build & Deploy / Build & Push Docker Image (push) Failing after 14s
Build & Deploy / Deploy to VPS (push) Has been skipped

This commit is contained in:
root
2026-06-30 00:23:31 -04:00
parent f22e0d45e1
commit 9ad92765c1
3 changed files with 107 additions and 25 deletions
@@ -12,6 +12,17 @@ export function listByReservation(reservationId: string, companyId: string) {
return repo.findByReservation(reservationId, companyId)
}
function sumSucceededPayments(payments: any[], type: 'CHARGE' | 'DEPOSIT') {
return payments.reduce((total: number, payment: any) => {
if (payment.type !== type || payment.status !== 'SUCCEEDED') return total
return total + payment.amount
}, 0)
}
function getInvoicePaid(reservation: any, rentalPayments: any[]) {
return Math.max(sumSucceededPayments(rentalPayments, 'CHARGE'), reservation.paidAmount ?? 0)
}
async function applyAmanpayWebhook(event: any) {
const transactionId = event.transaction_id ?? event.id
const status = event.status?.toUpperCase()
@@ -70,22 +81,17 @@ export async function initCharge(reservationId: string, companyId: string, body:
}) {
const reservation = await repo.findReservationOrThrow(reservationId, companyId)
const rentalPayments = (reservation as any).rentalPayments ?? []
const invoicePaid = rentalPayments.reduce((total: number, payment: any) => {
if (payment.type !== 'CHARGE' || payment.status !== 'SUCCEEDED') return total
return total + payment.amount
}, 0)
const depositCollected = rentalPayments.reduce((total: number, payment: any) => {
if (payment.type !== 'DEPOSIT' || payment.status !== 'SUCCEEDED') return total
return total + payment.amount
}, 0)
const chargeRemaining = Math.max(reservation.totalAmount - invoicePaid, 0)
const depositRemaining = Math.max(reservation.depositAmount - depositCollected, 0)
const amount = body.type === 'DEPOSIT' ? depositRemaining : chargeRemaining
const invoicePaid = getInvoicePaid(reservation, rentalPayments)
const depositCollected = sumSucceededPayments(rentalPayments, 'DEPOSIT')
const balanceDue = body.type === 'DEPOSIT'
? reservation.depositAmount - depositCollected
: reservation.totalAmount - invoicePaid
if (amount <= 0) {
if (balanceDue <= 0) {
throw new ConflictError(body.type === 'DEPOSIT' ? 'Security deposit is already fully collected' : 'Reservation is already fully paid')
}
const amount = balanceDue
const description = `${body.type === 'DEPOSIT' ? 'Deposit' : 'Rental'}: ${reservation.vehicle.make} ${reservation.vehicle.model}`
const orderId = `${reservationId}-${body.type}-${Date.now()}`
const webhookBase = process.env.API_URL ?? 'http://localhost:4000'
@@ -132,21 +138,18 @@ export async function recordManualPayment(reservationId: string, companyId: stri
}) {
const reservation = await repo.findReservation(reservationId, companyId)
const rentalPayments = (reservation as any).rentalPayments ?? []
const invoicePaid = rentalPayments.reduce((total: number, payment: any) => {
if (payment.type !== 'CHARGE' || payment.status !== 'SUCCEEDED') return total
return total + payment.amount
}, 0)
const depositCollected = rentalPayments.reduce((total: number, payment: any) => {
if (payment.type !== 'DEPOSIT' || payment.status !== 'SUCCEEDED') return total
return total + payment.amount
}, 0)
const chargeRemaining = Math.max(reservation.totalAmount - invoicePaid, 0)
const depositRemaining = Math.max(reservation.depositAmount - depositCollected, 0)
const remaining = body.type === 'DEPOSIT' ? depositRemaining : chargeRemaining
const invoicePaid = getInvoicePaid(reservation, rentalPayments)
const depositCollected = sumSucceededPayments(rentalPayments, 'DEPOSIT')
const remaining = body.type === 'DEPOSIT'
? reservation.depositAmount - depositCollected
: reservation.totalAmount - invoicePaid
if (remaining <= 0) {
throw new ConflictError(body.type === 'DEPOSIT' ? 'Security deposit is already fully collected' : 'Reservation is already fully paid')
}
if (body.amount <= 0) {
throw new ValidationError('Payment amount must be greater than zero')
}
if (body.amount > remaining) {
throw new ValidationError(body.type === 'DEPOSIT' ? 'Payment amount exceeds deposit outstanding' : 'Payment amount exceeds remaining balance')
}