feature/ga
parent
f1a9f5e2f4
commit
230e758598
@ -0,0 +1,94 @@
|
|||||||
|
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';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//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';
|
||||||
|
|
||||||
|
//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,36 @@
|
|||||||
|
|
||||||
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { getSession } from 'next-auth/react';
|
||||||
|
import { Role } from '../../../enum/role.enum';
|
||||||
|
import prisma from '../../../lib/prisma';
|
||||||
|
|
||||||
|
|
||||||
|
//api qui va rendre un utilisateur admin (pour les developpeurs)
|
||||||
|
//--> http://localhost:3000/api/superadmin/make-admin?pass={pass} (a effacer)
|
||||||
|
|
||||||
|
export default async function makeMeAdmin(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
const { pass } = req.query;
|
||||||
|
const session = await getSession({ req })
|
||||||
|
const goodPassword = pass === 'DPH58fa2mRvZAFSMN41ptwWn3CThjucEy9BxUJd7KQkzVerYqG';
|
||||||
|
|
||||||
|
//si utilisateur connecté et que le parametre 'pass' est '1234'
|
||||||
|
if (session && goodPassword) {
|
||||||
|
|
||||||
|
//MAJ de l'utilisateur en superadmin
|
||||||
|
await prisma.user.update({
|
||||||
|
where: { email: session.user.email },
|
||||||
|
data: {
|
||||||
|
role: Role.SUPERADMIN,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return res.status(200).send({ message: 'Profiled updated welcome ADMIN !' });
|
||||||
|
} else {
|
||||||
|
res.status(401).send({ message: 'Unauthorized' })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
|
||||||
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { getSession } from 'next-auth/react';
|
||||||
|
import { Role } from '../../../enum/role.enum';
|
||||||
|
import prisma from '../../../lib/prisma';
|
||||||
|
|
||||||
|
|
||||||
|
//api qui va rendre un utilisateur super admin (pour les developpeurs)
|
||||||
|
//--> http://localhost:3000/api/super/superadmin?pass={pass} (a effacer)
|
||||||
|
|
||||||
|
export default async function makeMeSuper(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
const { pass } = req.query;
|
||||||
|
const session = await getSession({ req })
|
||||||
|
const goodPassword = pass === 'DPH58fa2mRvZAFSMN41ptwWn3CThjucEy9BxUJd7KQkzVerYqG';
|
||||||
|
|
||||||
|
//si utilisateur connecté et que le parametre 'pass' est '1234'
|
||||||
|
if (session && goodPassword) {
|
||||||
|
|
||||||
|
//MAJ de l'utilisateur en superadmin
|
||||||
|
await prisma.user.update({
|
||||||
|
where: { email: session.user.email },
|
||||||
|
data: {
|
||||||
|
role: Role.SUPERADMIN,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return res.status(200).send({ message: 'Profiled updated welcome SUPERADMIN !' });
|
||||||
|
} else {
|
||||||
|
res.status(401).send({ message: 'Unauthorized' })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
import prisma from '../../../lib/prisma';
|
||||||
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
|
import { getSession } from 'next-auth/react';
|
||||||
|
import { Role } from '../../../enum/role.enum';
|
||||||
|
|
||||||
|
export default async function updateUser(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
const session = await getSession({ req })
|
||||||
|
const { selectedUserRole, selectedUserEmail, selectedUserName,
|
||||||
|
selectedUserSurname,
|
||||||
|
selectedUserAddress,
|
||||||
|
selectedUserZipCode,
|
||||||
|
selectedUserPhoneNumber } = req.body;
|
||||||
|
|
||||||
|
// est-ce la méthode est un POST ou PUT?
|
||||||
|
if (req.method !== ("POST" || 'PUT')) {
|
||||||
|
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: session.user.email },
|
||||||
|
})
|
||||||
|
|
||||||
|
// est-ce que utilisateur est un client classique ? Donc interdit pour lui de faire des updates
|
||||||
|
// A ACTIVER EN PRODUCTION
|
||||||
|
// if (user.role === 'USER') {
|
||||||
|
// res.status(401).json({ err: "Vous n'avez pas l'autorisation" });
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//mise a jour des données de l'utilisateur séléctionné
|
||||||
|
const updatedUser = await prisma.user.update({
|
||||||
|
where: {
|
||||||
|
email: selectedUserEmail,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
name: selectedUserName || undefined,
|
||||||
|
surname: selectedUserSurname || undefined,
|
||||||
|
address: selectedUserAddress || undefined,
|
||||||
|
zipCode: selectedUserZipCode || undefined,
|
||||||
|
phoneNumber: selectedUserPhoneNumber || undefined,
|
||||||
|
role: Role[selectedUserRole] || Role.USER
|
||||||
|
},
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
//renvoi la liste des users mis à jour
|
||||||
|
const users = await prisma.user.findMany({})
|
||||||
|
|
||||||
|
console.log(updatedUser);
|
||||||
|
res.status(200).send({ message: `Les informations de ${selectedUserEmail} ont été mise à jours !`, res: users });
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
.bloc-content {
|
||||||
|
padding: 5vh;
|
||||||
|
border-radius: 10px;
|
||||||
|
background-position: 50% 50%;
|
||||||
|
background-repeat: no-repeat;
|
||||||
|
background-size: contain;
|
||||||
|
background-color: rgba(255, 255, 255, 0.329);
|
||||||
|
/* animation: zoomInOut 3s ease-in-out infinite; */
|
||||||
|
z-index: 8888
|
||||||
|
}
|
||||||
|
|
||||||
|
.masthead {
|
||||||
|
padding-top: 5vh;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
Loading…
Reference in new issue