fix - modifications importantes
This commit is contained in:
1 parent
10e415e53e
commit
34700a46d9
14 files changed
+265
-321
No files matched your search
@@ -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' })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+10
-15
@@ -23,7 +23,7 @@ export default async function testCode(req: NextApiRequest, res: NextApiResponse
|
||||
//est-ce que le code tapé par l'utilisateur est valide ?
|
||||
const isValidCodeCadeau = codeCadeau.match(regex);
|
||||
if (!isValidCodeCadeau) {
|
||||
res.status(401).json({ err: 'Ce code est invalide' });
|
||||
res.status(406).json({ err: 'Ce code est invalide' });
|
||||
return
|
||||
}
|
||||
|
||||
@@ -32,9 +32,15 @@ export default async function testCode(req: NextApiRequest, res: NextApiResponse
|
||||
where: { codeCadeau }
|
||||
})
|
||||
|
||||
//si l'API gain n'a pas instancié une date pour ce gain
|
||||
if (!codeCadeauInfo.dateAchat) {
|
||||
res.status(406).json({ err: 'Ce code est invalide' });
|
||||
return;
|
||||
}
|
||||
|
||||
//check si le code a déja été utilisé
|
||||
if (!codeCadeauInfo.codeActif) {
|
||||
res.status(401).json({ err: 'Ce code a déja été utilisé' });
|
||||
res.status(406).json({ err: 'Ce code a déja été utilisé' });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -45,27 +51,16 @@ export default async function testCode(req: NextApiRequest, res: NextApiResponse
|
||||
})
|
||||
|
||||
//ajout du gain au profil de l'utilisateur
|
||||
const joinGain = await prisma.gain.update({
|
||||
await prisma.gain.update({
|
||||
where: { codeCadeau: codeCadeauInfo.codeCadeau },
|
||||
data: {
|
||||
userId: getCurrentUser[0].id
|
||||
}
|
||||
})
|
||||
|
||||
//retire un ticket du total de ticket restant
|
||||
const ticketUpdate = await prisma.ticketsRestants.update({
|
||||
where: {
|
||||
id: 1
|
||||
},
|
||||
data: {
|
||||
total: {
|
||||
decrement: 1
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
//passage du code en non-actif
|
||||
const codeUpdate = await prisma.gain.update({
|
||||
await prisma.gain.update({
|
||||
where: {
|
||||
id: codeCadeauInfo.id
|
||||
},
|
||||
|
||||
@@ -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 });
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ export default async function updtateGift(req: NextApiRequest, res: NextApiRespo
|
||||
include: { gains: true }
|
||||
})
|
||||
|
||||
return res.status(200).send({ message: `Le gain a ont été mis à jour !`, res: updatedCurrentUser });
|
||||
return res.status(200).send({ message: `Le gain a été mis à jour !`, res: updatedCurrentUser });
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user