Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db/seed: Adapt seeding scripts to new payment structure #605

Merged
merged 12 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions db/seed/donations/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Factory } from 'fishery'
import { faker } from '@faker-js/faker'

import { Donation } from '.prisma/client'
import { DonationType } from '@prisma/client'

export const donationFactory = Factory.define<Donation>(({ associations }) => ({
id: faker.datatype.uuid(),
paymentId: associations.paymentId || faker.datatype.uuid(),
type: faker.helpers.arrayElement(Object.values(DonationType)),
targetVaultId: associations.targetVaultId || faker.datatype.uuid(),
personId: associations.personId || null,
amount: parseInt(faker.finance.amount(2000, 20000)),
createdAt: faker.date.past(),
updatedAt: faker.date.recent(),
}))
4 changes: 2 additions & 2 deletions db/seed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { campaignTypesSeed } from './campaignType/seed'
import { bankAccountSeed } from './bankAccount/seed'
import { vaultSeed } from './vault/seed'
import { expenseSeed } from './expense/seed'
import { donationsSeed } from './donation/seed'
import { paymentsSeed } from './payment/seed'
import { companySeed } from './company/seed'
import { donationsWishesSeed } from './donationWish/seed'
import { campaignNewsSeed } from './campaignNews/seed'
Expand Down Expand Up @@ -53,7 +53,7 @@ async function seedDevData() {
await bankAccountSeed()
await vaultSeed()
await expenseSeed()
await donationsSeed()
await paymentsSeed()
await donationsWishesSeed()
await campaignNewsSeed()
}
Expand Down
11 changes: 4 additions & 7 deletions db/seed/donation/factory.ts → db/seed/payment/factory.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { Factory } from 'fishery'
import { faker } from '@faker-js/faker'

import { Donation } from '.prisma/client'
import { Currency, DonationStatus, DonationType, PaymentProvider } from '@prisma/client'
import { Currency, PaymentStatus, PaymentProvider, PaymentType, Payment } from '@prisma/client'

export const donationFactory = Factory.define<Donation>(({ associations }) => ({
export const paymentFactory = Factory.define<Payment>(({ associations }) => ({
id: faker.datatype.uuid(),
affiliateId: null,
type: faker.helpers.arrayElement(Object.values(DonationType)),
status: faker.helpers.arrayElement(Object.values(DonationStatus)),
type: faker.helpers.arrayElement(Object.values(PaymentType)),
status: faker.helpers.arrayElement(Object.values(PaymentStatus)),
provider: faker.helpers.arrayElement(Object.values(PaymentProvider)),
targetVaultId: associations.targetVaultId || faker.datatype.uuid(),
extCustomerId: 'cus_' + faker.random.alphaNumeric(8),
extPaymentIntentId: 'pi_' + faker.random.alphaNumeric(8),
extPaymentMethodId: 'pm_' + faker.random.alphaNumeric(8),
currency: Currency.BGN,
personId: associations.personId || null,
billingEmail: faker.internet.email(),
billingName: faker.name.fullName(),
amount: parseInt(faker.finance.amount(2000, 20000)),
Expand Down
104 changes: 75 additions & 29 deletions db/seed/donation/seed.ts → db/seed/payment/seed.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import {
PrismaClient,
PaymentProvider,
DonationStatus,
DonationType,
PaymentStatus,
Person,
CampaignState,
PaymentType,
DonationType,
} from '@prisma/client'

import { donationFactory } from './factory'
import { paymentFactory } from './factory'
import { faker } from '@faker-js/faker'
import { donationFactory } from '../donations/factory'

const prisma = new PrismaClient()

const SEED_COMPLETED_CAMPAIGN_DONATIONS = 5
const SEED_HEAVILY_FUNDED_CAMPAIGN_DONATIONS = 25

export async function donationsSeed() {
console.log('Donations seed')
export async function paymentsSeed() {
console.log('Payments seed')

const person = await prisma.person.findFirst()
if (!person) {
Expand All @@ -36,46 +39,68 @@ async function seedRandomDonations({ person }: SeedData) {
if (!vault) {
throw new Error('There are no vaults created yet!')
}

const donationFactoryOptions = {
associations: {
personId: person.id,
targetVaultId: vault.id,
},
}

const randomPaymentsData = [
paymentFactory.build({
type: PaymentType.single,
provider: PaymentProvider.stripe,
status: PaymentStatus.succeeded,
}),
paymentFactory.build({
type: PaymentType.single,
provider: PaymentProvider.stripe,
status: PaymentStatus.declined,
}),
paymentFactory.build({
type: PaymentType.single,
provider: PaymentProvider.bank,
status: PaymentStatus.initial,
}),
]

const randomDonationsData = [
donationFactory.build(
{
type: DonationType.donation,
provider: PaymentProvider.stripe,
status: DonationStatus.succeeded,
paymentId: randomPaymentsData[0].id,
createdAt: randomPaymentsData[0].createdAt,
updatedAt: randomPaymentsData[0].updatedAt,
},
donationFactoryOptions,
),
donationFactory.build(
{
type: DonationType.donation,
provider: PaymentProvider.stripe,
status: DonationStatus.declined,
paymentId: randomPaymentsData[1].id,
createdAt: randomPaymentsData[1].createdAt,
updatedAt: randomPaymentsData[1].updatedAt,
},
donationFactoryOptions,
),
donationFactory.build(
{
type: DonationType.donation,
provider: PaymentProvider.bank,
status: DonationStatus.initial,
paymentId: randomPaymentsData[2].id,
createdAt: randomPaymentsData[2].createdAt,
updatedAt: randomPaymentsData[2].updatedAt,
},
donationFactoryOptions,
),
]
const insertRandomPayments = await prisma.payment.createMany({
data: randomPaymentsData,
skipDuplicates: true,
})

const insertRandomDonations = await prisma.donation.createMany({
data: randomDonationsData,
skipDuplicates: true,
})

console.log({ insertRandomPayments })
console.log({ insertRandomDonations })

console.log('{ Updating first campaign vault: %s }', vault.id)
Expand All @@ -102,21 +127,33 @@ async function seedDonationsForCompletedCampaign({ person }: SeedData) {
},
}

const completedCampaignDonationsData = donationFactory.buildList(
const completedCampaignPaymentsData = paymentFactory.buildList(
SEED_COMPLETED_CAMPAIGN_DONATIONS,
{
type: DonationType.donation,
type: PaymentType.single,
provider: PaymentProvider.stripe,
status: DonationStatus.succeeded,
status: PaymentStatus.succeeded,
},
donationFactoryOptions,
)

const completedCampaignDonationData = completedCampaignPaymentsData.map((payment) =>
donationFactory.build(
{ paymentId: payment.id, updatedAt: payment.updatedAt, createdAt: payment.createdAt },
donationFactoryOptions,
),
)

const insertCompletedCampaignPayments = await prisma.payment.createMany({
data: completedCampaignPaymentsData,
skipDuplicates: true,
})

const insertCompletedCampaignDonations = await prisma.donation.createMany({
data: completedCampaignDonationsData,
data: completedCampaignDonationData,
skipDuplicates: true,
})

console.log({ insertCompletedCampaignPayments })
console.log({ insertCompletedCampaignDonations })

console.log('{ Updating completed campaign vault: %s }', completedCampaignVault.id)
Expand Down Expand Up @@ -145,21 +182,30 @@ async function seedDonationsForHeavilyFundedCampaign({ person }: SeedData) {
},
}

const heavilyFundedCampaignDonationsData = donationFactory.buildList(
const heavilyFundedCampaignPaymentData = paymentFactory.buildList(
SEED_HEAVILY_FUNDED_CAMPAIGN_DONATIONS,
{
type: DonationType.donation,
type: PaymentType.single,
provider: PaymentProvider.stripe,
status: DonationStatus.succeeded,
status: PaymentStatus.succeeded,
},
donationFactoryOptions,
)
const heavilyFundedCampaignDonationData = heavilyFundedCampaignPaymentData.map((payment) =>
donationFactory.build(
{ paymentId: payment.id, createdAt: payment.createdAt, updatedAt: payment.updatedAt },
donationFactoryOptions,
),
)

const insertHeavilyFundedCampaignPayments = await prisma.payment.createMany({
data: heavilyFundedCampaignPaymentData,
skipDuplicates: true,
})
const insertHeavilyFundedCampaignDonations = await prisma.donation.createMany({
data: heavilyFundedCampaignDonationsData,
data: heavilyFundedCampaignDonationData,
skipDuplicates: true,
})

console.log({ insertHeavilyFundedCampaignPayments })
console.log({ insertHeavilyFundedCampaignDonations })

console.log('{ Updating heavily-funded campaign vault: %s }', heavilyFundedCampaignVault.id)
Expand All @@ -172,13 +218,13 @@ async function seedDonationsForHeavilyFundedCampaign({ person }: SeedData) {
* @param vaultId
*/
async function updateVault(vaultId: string) {
const totalDonationsAmount = await prisma.donation.aggregate({
const totalDonationsAmount = await prisma.payment.aggregate({
_sum: {
amount: true,
},
where: {
targetVaultId: vaultId,
status: DonationStatus.succeeded,
donations: { some: { targetVaultId: vaultId } },
status: PaymentStatus.succeeded,
},
})

Expand Down
Loading