From cdda5b3d66959cc4f973215ac65076eca65b16db Mon Sep 17 00:00:00 2001 From: Dimitar Nizamov Date: Sun, 29 Oct 2023 12:58:56 +0100 Subject: [PATCH] Add seeding of donation wishes - seed 1 wish to each donation (#569) --- db/seed/donationWish/seed.ts | 56 ++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/db/seed/donationWish/seed.ts b/db/seed/donationWish/seed.ts index fc9f23764..b70ff273a 100644 --- a/db/seed/donationWish/seed.ts +++ b/db/seed/donationWish/seed.ts @@ -4,36 +4,42 @@ import { donationWishFactory } from './factory' const prisma = new PrismaClient() -const SEED_DONATION_WISHES = 25 - export async function donationsWishesSeed() { - console.log('Donation wishes seed') - - const person = await prisma.person.findFirst() - if (!person) { - throw new Error('There are no people created yet!') + const campaigns = await prisma.campaign.findMany() + if (!campaigns) { + throw new Error('There are no campaigns created yet!') } - const campaign = await prisma.campaign.findFirst() - if (!campaign) { - throw new Error('There are no vaults created yet!') + const donations = await prisma.donation.findMany() + if (!donations) { + throw new Error('There are no donations created yet!') } - const donationWishesData = donationWishFactory.buildList( - SEED_DONATION_WISHES, - {}, - { - associations: { - campaignId: campaign.id, - personId: person.id, + donations.forEach(async (donation) => { + const person = await prisma.person.findFirst() + if (!person) { + throw new Error('There are no people created yet!') + } + const campaign = await prisma.campaign.findFirst({ + where: { + vaults: { + some: { + id: donation.targetVaultId, + }, + }, }, - }, - ) - - const insertDonationWishes = await prisma.donationWish.createMany({ - data: donationWishesData, - skipDuplicates: true, + }) + if (!campaign) { + throw new Error('There are no campaign with this vault created yet!') + } + const donationWishData = donationWishFactory.build({ + campaignId: campaign.id, + personId: person.id, + donationId: donation.id, + }) + + await prisma.donationWish.create({ + data: donationWishData, + }) }) - - console.log({ insertDonationWishes }) }