fix architecture and write new tests
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
vi.mock('../../lib/prisma', () => ({
|
||||
prisma: {
|
||||
rentalPayment: { findMany: vi.fn(), findFirst: vi.fn(), findFirstOrThrow: vi.fn(), update: vi.fn(), updateMany: vi.fn(), create: vi.fn() },
|
||||
reservation: { findFirstOrThrow: vi.fn(), update: vi.fn() },
|
||||
},
|
||||
}))
|
||||
|
||||
import { prisma } from '../../lib/prisma'
|
||||
import * as repo from './payment.repo'
|
||||
|
||||
describe('payment.repo edge queries', () => {
|
||||
beforeEach(() => vi.clearAllMocks())
|
||||
|
||||
it('loads recent company payments with reservation, customer and vehicle context', async () => {
|
||||
await repo.findByCompany('company_1')
|
||||
|
||||
expect(prisma.rentalPayment.findMany).toHaveBeenCalledWith({
|
||||
where: { companyId: 'company_1' },
|
||||
include: { reservation: { include: { customer: true, vehicle: true } } },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: 100,
|
||||
})
|
||||
})
|
||||
|
||||
it('finds payment by id only within company and reservation scope', async () => {
|
||||
await repo.findPaymentOrThrow('payment_1', 'company_1', 'reservation_1')
|
||||
|
||||
expect(prisma.rentalPayment.findFirstOrThrow).toHaveBeenCalledWith({
|
||||
where: { id: 'payment_1', companyId: 'company_1', reservationId: 'reservation_1' },
|
||||
})
|
||||
})
|
||||
|
||||
it('marks a payment as succeeded with a fresh paidAt timestamp', async () => {
|
||||
vi.useFakeTimers()
|
||||
vi.setSystemTime(new Date('2026-08-01T12:00:00.000Z'))
|
||||
|
||||
await repo.markPaymentSucceeded('payment_1')
|
||||
|
||||
expect(prisma.rentalPayment.update).toHaveBeenCalledWith({
|
||||
where: { id: 'payment_1' },
|
||||
data: { status: 'SUCCEEDED', paidAt: new Date('2026-08-01T12:00:00.000Z') },
|
||||
})
|
||||
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('increments paid amount and promotes reservation payment status to paid', async () => {
|
||||
await repo.incrementReservationPaid('reservation_1', 2500)
|
||||
|
||||
expect(prisma.reservation.update).toHaveBeenCalledWith({
|
||||
where: { id: 'reservation_1' },
|
||||
data: { paymentStatus: 'PAID', paidAmount: { increment: 2500 } },
|
||||
})
|
||||
})
|
||||
|
||||
it('maps partial refunds to the correct payment status', async () => {
|
||||
await repo.setPaymentRefunded('payment_1', true)
|
||||
await repo.setPaymentRefunded('payment_2', false)
|
||||
|
||||
expect(prisma.rentalPayment.update).toHaveBeenNthCalledWith(1, {
|
||||
where: { id: 'payment_1' },
|
||||
data: { status: 'PARTIALLY_REFUNDED' },
|
||||
})
|
||||
expect(prisma.rentalPayment.update).toHaveBeenNthCalledWith(2, {
|
||||
where: { id: 'payment_2' },
|
||||
data: { status: 'REFUNDED' },
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user