// import { produits } from './produits'; import prisma from '../../lib/prisma' import { Chance } from 'chance' import { Cadeaux } from '../../enum/cadeaux.enum'; let chance = new Chance(); export const generateGains = async () => { //génération d'un code cadeau const randomNoGain = (Math.random() + 1).toString(36).substring(7).toUpperCase() //création d'un gain const gainCreated = await prisma.gain.create({ data: { codeCadeau: randomNoGain, } }) //crétion d'un id de produit de façon pondérée let weightedRandomGain = chance.weighted([ Cadeaux.CoffretDecouvert69, Cadeaux.CoffretDecouvert39, Cadeaux.TheSignature, Cadeaux.TheDetox, Cadeaux.Infuseur], [4, 6, 10, 20, 60]); //mise à jour du gain avec l'id de produit pondéré const gainUpdated = await prisma.gain.update({ where: { id: gainCreated.id }, data: { produitId: weightedRandomGain, } }) //on retire 1 ticket await prisma.ticketsRestants.update({ where: { id: 1 }, data: { total: { decrement: 1 } } }) console.log(`Created gain ! : ${gainUpdated.codeCadeau}`) }