Skip to content

Commit

Permalink
ensure all campaigns have single vault and heavily-funded campaign ha…
Browse files Browse the repository at this point in the history
…s approved expenses
  • Loading branch information
quantum-grit committed Sep 26, 2023
1 parent 1403b52 commit 529e439
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 128 deletions.
2 changes: 1 addition & 1 deletion db/seed/campaign/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const prisma = new PrismaClient()
const SEED_ACTIVE_CAMPAIGNS = 5
const SEED_RANDOM_CAMPAIGNS = 5
const SEED_COMPLETED_CAMPAIGNS = 3
const SEED_HEAVILY_FUNDED_CAMPAIGNS = 3
const SEED_HEAVILY_FUNDED_CAMPAIGNS = 1

export async function campaignSeed() {
console.log('Campaigns seed')
Expand Down
19 changes: 14 additions & 5 deletions db/seed/donation/seed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { PrismaClient, PaymentProvider, DonationStatus, DonationType, Person } from '@prisma/client'
import {
PrismaClient,
PaymentProvider,
DonationStatus,
DonationType,
Person,
CampaignState,
} from '@prisma/client'

import { donationFactory } from './factory'

Expand Down Expand Up @@ -78,8 +85,8 @@ async function seedRandomDonations({ person }: SeedData) {
async function seedDonationsForCompletedCampaign({ person }: SeedData) {
const completedCampaignVault = await prisma.vault.findFirst({
where: {
name: {
contains: 'completed',
campaign: {
state: CampaignState.complete,
},
},
})
Expand Down Expand Up @@ -119,8 +126,10 @@ async function seedDonationsForCompletedCampaign({ person }: SeedData) {
async function seedDonationsForHeavilyFundedCampaign({ person }: SeedData) {
const heavilyFundedCampaignVault = await prisma.vault.findFirst({
where: {
name: {
contains: 'heavily-funded',
campaign: {
title: {
contains: 'heavily-funded',
},
},
},
})
Expand Down
4 changes: 2 additions & 2 deletions db/seed/expense/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export const expenseFactory = Factory.define<Expense>(({ associations }) => ({
vaultId: associations.vaultId || faker.datatype.uuid(),
documentId: associations.documentId || null,
approvedById: associations.approvedById || null,
amount: faker.datatype.number(),
amount: faker.datatype.number({ min: 1, max: 20000 }),
currency: faker.helpers.arrayElement(Object.values(Currency)),
status: faker.helpers.arrayElement(Object.values(ExpenseStatus)),
status: ExpenseStatus.pending,
deleted: faker.datatype.boolean(),
spentAt: faker.date.past(),
}))
19 changes: 17 additions & 2 deletions db/seed/expense/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,33 @@ const prisma = new PrismaClient()
export async function expenseSeed() {
console.log('Expense seed')

const vault = await prisma.vault.findFirst()
const vault = await prisma.vault.findFirst({
where: {
campaign: {
title: {
contains: 'heavily-funded',
},
},
},
})

if (!vault) {
throw new Error('There are no vaults created yet!')
}

const coordinator = await prisma.coordinator.findFirst()

if (!coordinator) {
throw new Error('There are no coordinators created yet!')
}

const expensesData: Expense[] = expenseFactory.buildList(
20,
11,
{},
{
associations: {
vaultId: vault.id,
approvedById: coordinator.personId,
},
},
)
Expand Down
150 changes: 32 additions & 118 deletions db/seed/vault/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,123 +7,37 @@ const prisma = new PrismaClient()
export async function vaultSeed() {
console.log('Vault seed')

await seedVaultsForRandomCampaign()
await seedVaultForCompletedCampaign()
await seedVaultForHeavilyFundedCampaign()
}

async function seedVaultsForRandomCampaign() {
const campaign = await prisma.campaign
.findMany()
.then((campaigns) => faker.helpers.arrayElement(campaigns))

if (!campaign) {
throw new Error('There are no campaigns created yet!')
}

const randomCampaignVaultsData: Vault[] = vaultFactory.buildList(
20,
{
currency: Currency.BGN,
amount: 0, // Initializing with 0 and fill the correct amount after donations have been seeded
},
{
associations: {
campaignId: campaign.id,
},
},
)

const insertRandomCampaignVaults = await prisma.vault.createMany({
data: randomCampaignVaultsData,
skipDuplicates: true,
})

console.log({ insertRandomCampaignVaults })
}

async function seedVaultForCompletedCampaign() {
const completedCampaign = await prisma.campaign.findFirst({
where: {
state: CampaignState.complete,
},
})

if (!completedCampaign) {
throw new Error('There is no completed campaign created')
}

const completedCampaignVault = await prisma.vault.findFirst({
where: {
campaignId: completedCampaign.id,
},
})

if (completedCampaignVault) {
console.log('{ Completed campaign vault already exists }')
return
await seedVaultsForCampaigns()

async function seedVaultsForCampaigns() {
const campaigns = await prisma.campaign.findMany()

if (!campaigns) {
throw new Error('There are no campaigns created yet!')
}

let vaults: Vault[] = []
campaigns.map(async (campaign) => {
vaults.push(
vaultFactory.build(
{
currency: Currency.BGN,
amount: 0, // Initializing with 0 and fill the correct amount after donations have been seeded
},
{
associations: {
campaignId: campaign.id,
},
},
),
)
})

const insertRandomCampaignVaults = await prisma.vault.createMany({
data: vaults,
skipDuplicates: true,
})

console.log({ insertRandomCampaignVaults })
}

const completedCampaignVaultData: Vault = vaultFactory.build(
{
name: faker.finance.accountName() + ' completed',
currency: Currency.BGN,
amount: 0, // Initializing with 0 and fill the correct amount after donations have been seeded
},
{
associations: {
campaignId: completedCampaign.id,
},
},
)

const insertCompletedCampaignVault = await prisma.vault.create({
data: completedCampaignVaultData,
})

console.log(`{ insertCompletedCampaignVault: ${!!insertCompletedCampaignVault} }`)
}

async function seedVaultForHeavilyFundedCampaign() {
const heavilyFundedCampaign = await prisma.campaign.findFirst({
where: {
title: {
contains: 'heavily-funded',
},
},
})

if (!heavilyFundedCampaign) {
throw new Error('There is no heavily funded campaign created')
}

const heavilyFundedCampaignVault = await prisma.vault.findFirst({
where: {
campaignId: heavilyFundedCampaign.id,
},
})

if (heavilyFundedCampaignVault) {
console.log('{ Heavily-funded campaign vault already exists }')
return
}

const heavilyFundedCampaignVaultData: Vault = vaultFactory.build(
{
name: faker.finance.accountName() + ' heavily-funded',
currency: Currency.BGN,
amount: 0, // Initializing with 0 and fill the correct amount after donations have been seeded
},
{
associations: {
campaignId: heavilyFundedCampaign.id,
},
},
)

const insertHeavilyFundedCampaignVault = await prisma.vault.create({
data: heavilyFundedCampaignVaultData,
})

console.log(`{ insertHeavilyFundedCampaignVault: ${!!insertHeavilyFundedCampaignVault} }`)
}

0 comments on commit 529e439

Please sign in to comment.