You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
// import { produits } from './produits';
|
|
import prisma from '../../lib/prisma'
|
|
import { Chance } from 'chance'
|
|
import { Cadeaux } from '../../enum/cadeaux.enum';
|
|
import cryptoRandomString from 'crypto-random-string';
|
|
let chance = new Chance();
|
|
|
|
export const generateGains = async () => {
|
|
//génération d'un code cadeau
|
|
const randomNoGain = cryptoRandomString({ length: 10, type: 'distinguishable' });
|
|
|
|
//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.GrandCoffretDecouverte,
|
|
Cadeaux.CoffretDecouverte,
|
|
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}`)
|
|
}
|
|
|
|
|