parent
10e415e53e
commit
34700a46d9
@ -1,7 +1,2 @@
|
|||||||
{
|
{
|
||||||
"workbench.colorCustomizations": {
|
|
||||||
"activityBar.background": "#4C1F25",
|
|
||||||
"titleBar.activeBackground": "#6A2C34",
|
|
||||||
"titleBar.activeForeground": "#FEFCFC"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -1,99 +0,0 @@
|
|||||||
import prisma from '../../../lib/prisma';
|
|
||||||
import chance from 'chance'
|
|
||||||
import cryptoRandomString from 'crypto-random-string';
|
|
||||||
|
|
||||||
|
|
||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
|
||||||
import { getSession } from 'next-auth/react';
|
|
||||||
import { Cadeaux } from '../../../enum/cadeaux.enum';
|
|
||||||
import { Role } from '../../../enum/role.enum';
|
|
||||||
import process from 'process';
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//API gain qui va generer un code cadeau qui sera sur la facture du client
|
|
||||||
//On part du principe qu'une 'API caisse'(qu'on ne gere pas) déclenche l'appel à l'API gain
|
|
||||||
|
|
||||||
//--> http://localhost:3000/api/apiGain?pass=... (a effacer)
|
|
||||||
|
|
||||||
export default async function apiGain(req: NextApiRequest, res: NextApiResponse) {
|
|
||||||
const { pass } = req.query;
|
|
||||||
const goodPassword = pass === 'P9RDK2b47x1XQV5s3fSaW6erGpzTNudk8jZwCycJYtMHAqhEnm';
|
|
||||||
|
|
||||||
if (!process.env.ACTION_API) {
|
|
||||||
return res.status(401).send({ message: 'Unauthorized' })
|
|
||||||
}
|
|
||||||
|
|
||||||
//récuperation des tickets disponible
|
|
||||||
const getTickets = await prisma.ticketsRestants.findFirst({
|
|
||||||
where: {
|
|
||||||
id: 1
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const { total } = getTickets;
|
|
||||||
const isTicketsAvaible = total > 0;
|
|
||||||
|
|
||||||
if (isTicketsAvaible && goodPassword) {
|
|
||||||
|
|
||||||
//creation d'un numéro de gain aléatoire
|
|
||||||
const gainNumber = cryptoRandomString({ length: 10, type: 'distinguishable' });
|
|
||||||
|
|
||||||
|
|
||||||
//création d'un id de produit de façon pondérée
|
|
||||||
let weightedRandomGainId = chance().weighted([
|
|
||||||
Cadeaux.GrandCoffretDecouverte,
|
|
||||||
Cadeaux.CoffretDecouverte,
|
|
||||||
Cadeaux.TheSignature,
|
|
||||||
Cadeaux.TheDetox,
|
|
||||||
Cadeaux.Infuseur],
|
|
||||||
[4, 6, 10, 20, 60]);
|
|
||||||
|
|
||||||
//création d'un gain
|
|
||||||
const gainCreated = await prisma.gain.create({
|
|
||||||
data: {
|
|
||||||
codeCadeau: gainNumber,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
//mise à jour du gain avec l'id de produit pondéré
|
|
||||||
const gainResult = await prisma.gain.update({
|
|
||||||
where: { id: gainCreated.id },
|
|
||||||
data: {
|
|
||||||
produitId: weightedRandomGainId,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
//on retire 1 ticket
|
|
||||||
await prisma.ticketsRestants.update({
|
|
||||||
where: {
|
|
||||||
id: getTickets.id
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
total: {
|
|
||||||
decrement: 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return res.status(200).send({ message: 'Gain has been generated !', result: gainResult });
|
|
||||||
} else {
|
|
||||||
return res.status(401).send({ message: 'Unauthorized' })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
import prisma from '../../../lib/prisma';
|
||||||
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { getSession } from 'next-auth/react';
|
||||||
|
|
||||||
|
export default async function updateOrCreate(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
const session = await getSession({ req })
|
||||||
|
const { targetUserMail } = req.body;
|
||||||
|
|
||||||
|
// est-ce la méthode est un POST ou PUT?
|
||||||
|
if (!(['POST'].indexOf(req.method) > -1)) {
|
||||||
|
throw new Error(
|
||||||
|
`The HTTP ${req.method} method is not supported at this route.`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// est-ce que utilisateur est connecté ?
|
||||||
|
if (!session) {
|
||||||
|
res.status(401).json({ err: "Vous devez être connecté" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//récupération des informations de l'utilisateur connecté
|
||||||
|
const user = await prisma.user.findFirst({
|
||||||
|
where: { email: targetUserMail },
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
res.status(200).send({ message: 'user :', res: user });
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -1,52 +0,0 @@
|
|||||||
// 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,
|
|
||||||
codeActif: true,
|
|
||||||
retrait: false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
//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}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in new issue