import { Prisma } from '@prisma/client' ; import { date } from 'faker' ; import { random , randomItems } from '../lib/random' ; import { makeBooleanIterator } from '../lib/util' ; import { prisma } from 'src/prisma' ; const createRsvps = async ( eventIds , userIds ) => { const rsvps = [ ] ; const userEventRoles = [ ] ; const eventIterator = makeBooleanIterator ( ) ; for ( const eventId of eventIds ) { const eventUserIds = randomItems ( userIds , userIds . length / 2 ) ; const numberWaiting = 1 + random ( eventUserIds . length - 2 ) ; const numberCanceled = 1 + random ( eventUserIds . length - numberWaiting - 1 ) ; const organizerIterator = makeBooleanIterator ( eventIterator . next ( ) . value ) ; for ( let i = 0 ; i < eventUserIds . length ; i ++ ) { const on_waitlist = i < numberWaiting ; const canceled = ! on_waitlist && i < numberWaiting + numberCanceled ; const rsvp = { event_id : eventId , user_id : eventUserIds [ i ] , date : date . future ( ) , on_waitlist , canceled , confirmed_at : new Date ( ) , } ; const attendee = { user_id : eventUserIds [ i ] , event_id : eventId , role_name : 'attendee' , subscribed : true , } ; if ( organizerIterator . next ( ) . value ) { const organizer = { user_id : eventUserIds [ i ] , event_id : eventId , role_name : 'organizer' , subscribed : true , } ; userEventRoles . push ( organizer ) ; } rsvps . push ( rsvp ) ; userEventRoles . push ( attendee ) ; } } try { await prisma . rsvps . createMany ( { data : rsvps } ) ; } catch ( e ) { console . error ( e ) ; throw new Error ( 'Error seeding rsvps' ) ; } try { await prisma . user_event_roles . createMany ( { data : userEventRoles } ) ; } catch ( e ) { console . error ( e ) ; throw new Error ( 'Error seeding user-event-roles' ) ; } } ; export default createRsvps ;