Skip to content

Commit

Permalink
schema.prisma: Rename Payments to Payment
Browse files Browse the repository at this point in the history
As the name of the model is used as a type, the acceptednaming convention is to name that type in singular way
  • Loading branch information
sashko9807 committed Mar 2, 2024
1 parent 7e37d19 commit 3a63238
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class CreateAffiliateDonationDto {
@ValidateNested({ each: true })
metadata: DonationMetadataDto | undefined

public toEntity(targetVaultId: string): Prisma.PaymentsCreateInput {
public toEntity(targetVaultId: string): Prisma.PaymentCreateInput {
return {
type: PaymentType.single,
status: PaymentStatus.guaranteed,
Expand Down
18 changes: 9 additions & 9 deletions apps/api/src/campaign/campaign.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Campaign,
CampaignState,
CampaignType,
Donation,
PaymentStatus,
DonationType,
Vault,
Expand All @@ -12,8 +11,8 @@ import {
NotificationList,
EmailType,
CampaignTypeCategory,
Payments,
PaymentType,
Payment,
} from '@prisma/client'
import {
BadRequestException,
Expand Down Expand Up @@ -51,6 +50,7 @@ import { ConfigService } from '@nestjs/config'
import { DateTime } from 'luxon'
import { CampaignSubscribeDto } from './dto/campaign-subscribe.dto'
import { MarketingNotificationsService } from '../notifications/notifications.service'
import type { PaymentWithDonation } from '../donations/types/donation'

@Injectable()
export class CampaignService {
Expand Down Expand Up @@ -566,8 +566,8 @@ export class CampaignService {
return donations
}

async getDonationByIntentId(paymentIntentId: string): Promise<Payments | null> {
return this.prisma.payments.findFirst({ where: { extPaymentIntentId: paymentIntentId } })
async getPaymentByIntentId(paymentIntentId: string): Promise<Payment | null> {
return this.prisma.payment.findFirst({ where: { extPaymentIntentId: paymentIntentId } })
}

/**
Expand Down Expand Up @@ -624,13 +624,13 @@ export class CampaignService {

private async updateDonationIfAllowed(
tx: Prisma.TransactionClient,
payment: Prisma.PaymentsGetPayload<{ include: { donations: true } }>,
payment: PaymentWithDonation,
newDonationStatus: PaymentStatus,
paymentData: PaymentData,
) {
if (shouldAllowStatusChange(payment.status, newDonationStatus)) {
try {
const updatedDonation = await tx.payments.update({
const updatedDonation = await tx.payment.update({
where: {
id: payment.id,
},
Expand Down Expand Up @@ -708,7 +708,7 @@ export class CampaignService {
const targetVaultData = { connect: { id: vault.id } }

try {
const donation = await tx.payments.create({
const donation = await tx.payment.create({
data: {
amount: paymentData.netAmount,
chargedAmount: paymentData.chargedAmount,
Expand Down Expand Up @@ -753,7 +753,7 @@ export class CampaignService {

private async findExistingDonation(tx: Prisma.TransactionClient, paymentData: PaymentData) {
//first try to find by paymentIntentId
let donation = await tx.payments.findUnique({
let donation = await tx.payment.findUnique({
where: { extPaymentIntentId: paymentData.paymentIntentId },
include: { donations: true },
})
Expand All @@ -764,7 +764,7 @@ export class CampaignService {
if (!donation && paymentData.personId && paymentData.personId.length === 36) {
// search for a subscription donation
// for subscriptions, we don't have a paymentIntentId
donation = await tx.payments.findFirst({
donation = await tx.payment.findFirst({
where: {
status: PaymentStatus.initial,
chargedAmount: paymentData.chargedAmount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AffiliateStatus } from '@prisma/client'
import { Company } from '../../company/entities/company.entity'
import { Payments } from '../../payments/entities/payments.entity'
import { Payment } from '../../payment/entities/payment.entity'

export class Affiliate {
id: string
Expand All @@ -10,5 +10,5 @@ export class Affiliate {
createdAt: Date
updatedAt: Date | null
company?: Company
payments?: Payments[]
payments?: Payment[]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Person } from '../../person/entities/person.entity'
import { Vault } from '../../vault/entities/vault.entity'
import { DonationWish } from '../../donationWish/entities/donationWish.entity'
import { DonationMetadata } from '../../donationMetadata/entities/donationMetadata.entity'
import { Payments } from '../../payments/entities/payments.entity'
import { Payment } from '../../payment/entities/payment.entity'

export class Donation {
id: string
Expand All @@ -18,5 +18,5 @@ export class Donation {
targetVault?: Vault
DonationWish?: DonationWish | null
metadata?: DonationMetadata | null
payment?: Payments
payment?: Payment
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class ConnectPaymentDto {
id?: string
extPaymentIntentId?: string
}
12 changes: 12 additions & 0 deletions apps/api/src/domain/generated/payment/dto/create-payment.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PaymentType } from '@prisma/client'
import { ApiProperty } from '@nestjs/swagger'

export class CreatePaymentDto {
extCustomerId: string
extPaymentIntentId: string
extPaymentMethodId: string
@ApiProperty({ enum: PaymentType })
type: PaymentType
billingEmail?: string
billingName?: string
}
3 changes: 3 additions & 0 deletions apps/api/src/domain/generated/payment/dto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './connect-payment.dto'
export * from './create-payment.dto'
export * from './update-payment.dto'
12 changes: 12 additions & 0 deletions apps/api/src/domain/generated/payment/dto/update-payment.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PaymentType } from '@prisma/client'
import { ApiProperty } from '@nestjs/swagger'

export class UpdatePaymentDto {
extCustomerId?: string
extPaymentIntentId?: string
extPaymentMethodId?: string
@ApiProperty({ enum: PaymentType })
type?: PaymentType
billingEmail?: string
billingName?: string
}
1 change: 1 addition & 0 deletions apps/api/src/domain/generated/payment/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './payment.entity'
23 changes: 23 additions & 0 deletions apps/api/src/domain/generated/payment/entities/payment.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { PaymentType, Currency, PaymentStatus, PaymentProvider } from '@prisma/client'
import { Affiliate } from '../../affiliate/entities/affiliate.entity'
import { Donation } from '../../donation/entities/donation.entity'

export class Payment {
id: string
extCustomerId: string
extPaymentIntentId: string
extPaymentMethodId: string
type: PaymentType
currency: Currency
status: PaymentStatus
provider: PaymentProvider
affiliateId: string | null
createdAt: Date
updatedAt: Date | null
chargedAmount: number
amount: number
billingEmail: string | null
billingName: string | null
affiliate?: Affiliate | null
donations?: Donation[]
}
20 changes: 9 additions & 11 deletions apps/api/src/donations/donations.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
PaymentProvider,
Person,
Vault,
Payments,
Prisma,
Payment,
} from '@prisma/client'
import { CampaignService } from '../campaign/campaign.service'
import { ExportService } from '../export/export.service'
Expand All @@ -26,8 +25,7 @@ import { CreateSessionDto } from './dto/create-session.dto'
import { UpdatePaymentDto } from './dto/update-payment.dto'
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import { MarketingNotificationsModule } from '../notifications/notifications.module'

type PaymentWithDonation = Prisma.PaymentsGetPayload<{ include: { donations: true } }>
import type { PaymentWithDonation } from './types/donation'

describe('DonationsController', () => {
let controller: DonationsController
Expand Down Expand Up @@ -221,14 +219,14 @@ describe('DonationsController', () => {
.spyOn(vaultService, 'incrementVaultAmount')
.mockImplementation()

prismaMock.payments.findFirst.mockResolvedValueOnce(existingPayment)
prismaMock.payment.findFirst.mockResolvedValueOnce(existingPayment)
prismaMock.person.findFirst.mockResolvedValueOnce(existingTargetPerson)

// act
await controller.update('123', updatePaymentDto)

// assert
expect(prismaMock.payments.update).toHaveBeenCalledWith({
expect(prismaMock.payment.update).toHaveBeenCalledWith({
where: { id: '123' },
data: {
status: existingPayment.status,
Expand Down Expand Up @@ -280,16 +278,16 @@ describe('DonationsController', () => {

jest.spyOn(prismaMock, '$transaction').mockImplementation((callback) => callback(prismaMock))

prismaMock.payments.findFirst.mockResolvedValueOnce(existingPayment)
prismaMock.payment.findFirst.mockResolvedValueOnce(existingPayment)
prismaMock.person.findFirst.mockResolvedValueOnce(existingTargetPerson)
prismaMock.payments.update.mockResolvedValueOnce(expectedUpdatedPayment)
prismaMock.payment.update.mockResolvedValueOnce(expectedUpdatedPayment)
prismaMock.vault.update.mockResolvedValueOnce({ id: '1000', campaignId: '111' } as Vault)

// act
await controller.update('123', updatePaymentDto)

// assert
expect(prismaMock.payments.update).toHaveBeenCalledWith({
expect(prismaMock.payment.update).toHaveBeenCalledWith({
where: { id: '123' },
data: {
status: PaymentStatus.succeeded,
Expand Down Expand Up @@ -329,11 +327,11 @@ describe('DonationsController', () => {
const existingPayment = { ...mockPayment, status: PaymentStatus.succeeded }
jest.spyOn(prismaMock, '$transaction').mockImplementation((callback) => callback(prismaMock))

prismaMock.payments.findFirstOrThrow.mockResolvedValueOnce(existingPayment)
prismaMock.payment.findFirstOrThrow.mockResolvedValueOnce(existingPayment)

await controller.invalidate('123')

expect(prismaMock.payments.update).toHaveBeenCalledWith({
expect(prismaMock.payment.update).toHaveBeenCalledWith({
where: { id: '123' },
data: {
status: PaymentStatus.invalid,
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/donations/donations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,14 @@ export class DonationsController {
) {
return await this.donationsService.listPayments(
query?.paymentId,
query?.campaignId,
query?.status,
query?.provider,
query?.minAmount,
query?.maxAmount,
query?.from,
query?.to,
query?.search,
query?.sortBy,
query?.sortOrder,
query?.pageindex,
Expand Down
Loading

0 comments on commit 3a63238

Please sign in to comment.