⚡️ add - api generationGain , api superadmin , readme , fichier de seed

This commit is contained in:
eric committed 2022-11-12 20:23:15 +01:00
1 parent 141d9e53e8
commit e85117b2c7
25 files changed
+768 -310

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
// 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}`)
}