Merge branch 'master' of https://git.dsp-archiwebo21-ad-ec-cb-mt.fr/cedric/The_Tip_Top
commit
3de3355a13
@ -0,0 +1,15 @@
|
|||||||
|
import styles from '../styles/Loader.module.css'
|
||||||
|
import { MoonLoader } from 'react-spinners'
|
||||||
|
|
||||||
|
const Loader = () => {
|
||||||
|
return (
|
||||||
|
<div className={styles.wrapper}>
|
||||||
|
<MoonLoader
|
||||||
|
color="#eeeeee"
|
||||||
|
size={80}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Loader;
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
import { createContext, ReactNode, useContext, useState } from "react";
|
||||||
|
|
||||||
|
export type IGlobalContextProps = {
|
||||||
|
role: string;
|
||||||
|
setRole: (role: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const globalContextDefaultValues: IGlobalContextProps = {
|
||||||
|
role: null,
|
||||||
|
setRole: () => { },
|
||||||
|
}
|
||||||
|
|
||||||
|
export const GlobalContext = createContext<IGlobalContextProps>(globalContextDefaultValues);
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
children: ReactNode;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const GlobalContextProvider = (props: Props) => {
|
||||||
|
const [role, setCurrentRole] = useState(null)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<GlobalContext.Provider
|
||||||
|
value={{
|
||||||
|
role: role,
|
||||||
|
setRole: setCurrentRole,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{props.children}
|
||||||
|
</GlobalContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
// import { useGlobalContext } from 'context';
|
||||||
|
// const { user } = useGlobalContext();
|
||||||
|
|
||||||
|
// Pour utiliser le context n'importe ou
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
import { useContext } from 'react';
|
||||||
|
|
||||||
|
import { GlobalContext } from './globalContext';
|
||||||
|
|
||||||
|
export const useGlobalContext = () => useContext(GlobalContext);
|
||||||
@ -1,5 +1,6 @@
|
|||||||
export enum Role {
|
export enum Role {
|
||||||
USER = 'USER',
|
USER = 'USER',
|
||||||
ADMIN = 'ADMIN',
|
ADMIN = 'ADMIN',
|
||||||
|
SELLER = 'SELLER',
|
||||||
SUPERADMIN = 'SUPERADMIN'
|
SUPERADMIN = 'SUPERADMIN'
|
||||||
}
|
}
|
||||||
@ -0,0 +1,354 @@
|
|||||||
|
import React, { useEffect, useState, useSyncExternalStore } from "react";
|
||||||
|
import prisma from '../lib/prisma'
|
||||||
|
|
||||||
|
|
||||||
|
import Image from 'next/image';
|
||||||
|
import * as Yup from 'yup';
|
||||||
|
import Head from "next/head";
|
||||||
|
import { getCsrfToken, getSession, signIn, useSession } from "next-auth/react";
|
||||||
|
import { yupResolver } from "@hookform/resolvers/yup";
|
||||||
|
import "yup-phone";
|
||||||
|
import { SubmitHandler, useForm } from "react-hook-form";
|
||||||
|
|
||||||
|
import { AiOutlineCheck } from 'react-icons/ai'
|
||||||
|
|
||||||
|
import Router from "next/router";
|
||||||
|
|
||||||
|
|
||||||
|
import style from '../styles/Connexion.module.css';
|
||||||
|
|
||||||
|
import imageConnexion from '../public/images/connexion_image.png'
|
||||||
|
|
||||||
|
|
||||||
|
import Link from "next/link";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import { TfiInfoAlt } from 'react-icons/tfi'
|
||||||
|
import { TfiGift } from 'react-icons/tfi'
|
||||||
|
|
||||||
|
|
||||||
|
import { toast } from "react-toastify";
|
||||||
|
import { Token } from "nodemailer/lib/xoauth2";
|
||||||
|
import { isGeneratorFunction } from "util/types";
|
||||||
|
import { UserProps } from "../types/user";
|
||||||
|
import { UserGainProps } from "../types/userGain";
|
||||||
|
import { Gain, Produit, Role, User } from "@prisma/client";
|
||||||
|
|
||||||
|
type FormData = {
|
||||||
|
email: string;
|
||||||
|
name: string;
|
||||||
|
surname: string;
|
||||||
|
address: string;
|
||||||
|
zipCode: number;
|
||||||
|
phoneNumber: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function getServerSideProps(context) {
|
||||||
|
const session = await getSession(context)
|
||||||
|
const isAdmin = ['SELLER', 'ADMIN', 'SUPERADMIN'].includes(session?.user['role']);
|
||||||
|
|
||||||
|
if (!session || !isAdmin) {
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: '/',
|
||||||
|
permanent: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const users = await prisma.user.findMany({
|
||||||
|
include: {
|
||||||
|
gains: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const user = await prisma.user.findUnique({
|
||||||
|
where: {
|
||||||
|
email: session?.user.email
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const produits = await prisma.produit.findMany({})
|
||||||
|
|
||||||
|
return {
|
||||||
|
props: {
|
||||||
|
user: JSON.parse(JSON.stringify(user)),
|
||||||
|
produits: JSON.parse(JSON.stringify(produits)),
|
||||||
|
users: JSON.parse(JSON.stringify(users))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
user: User;
|
||||||
|
produits: Produit[];
|
||||||
|
users: User[]
|
||||||
|
}
|
||||||
|
|
||||||
|
const Admin = (props: Props) => {
|
||||||
|
const { data: session, status } = useSession();
|
||||||
|
const [response, setResponse] = useState(null)
|
||||||
|
const [currentUser, setCurrentUser] = useState(null)
|
||||||
|
const [users, setUsers] = useState(props.users)
|
||||||
|
|
||||||
|
const { register, setValue, handleSubmit, formState: { errors } } = useForm<FormData>({
|
||||||
|
});
|
||||||
|
|
||||||
|
const fetcher = async (url, method, body = {}) => {
|
||||||
|
try {
|
||||||
|
await fetch(url, {
|
||||||
|
method,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then(async (data) => {
|
||||||
|
setResponse(data);
|
||||||
|
setUsers(data.res)
|
||||||
|
if (data.err) {
|
||||||
|
toast.error(data.err, {
|
||||||
|
position: "top-center",
|
||||||
|
autoClose: 2000,
|
||||||
|
hideProgressBar: false,
|
||||||
|
closeOnClick: true,
|
||||||
|
pauseOnHover: true,
|
||||||
|
draggable: true,
|
||||||
|
progress: undefined,
|
||||||
|
theme: "light",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
toast.success(data.message, {
|
||||||
|
position: "top-center",
|
||||||
|
autoClose: 5000,
|
||||||
|
hideProgressBar: false,
|
||||||
|
closeOnClick: true,
|
||||||
|
pauseOnHover: true,
|
||||||
|
draggable: true,
|
||||||
|
progress: undefined,
|
||||||
|
theme: "light",
|
||||||
|
});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
console.error('ERROR :', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const onSubmit = async ({
|
||||||
|
email,
|
||||||
|
name,
|
||||||
|
surname,
|
||||||
|
address,
|
||||||
|
zipCode,
|
||||||
|
phoneNumber }: FormData) => {
|
||||||
|
const body = {
|
||||||
|
selectedUserEmail: email,
|
||||||
|
selectedUserName: name,
|
||||||
|
selectedUserSurname: surname,
|
||||||
|
selectedUserAddress: address,
|
||||||
|
selectedUserZipCode: Number(zipCode),
|
||||||
|
selectedUserPhoneNumber: Number(phoneNumber),
|
||||||
|
};
|
||||||
|
fetcher('/api/user/update', 'POST', body)
|
||||||
|
};
|
||||||
|
|
||||||
|
const onChangeRole = async (e: React.ChangeEvent<HTMLSelectElement>, user: User) => {
|
||||||
|
const body = { selectedUserEmail: user.email, selectedUserRole: e.target.value }
|
||||||
|
fetcher('/api/user/update', 'PUT', body)
|
||||||
|
};
|
||||||
|
|
||||||
|
const onDeleteUser = async (userEmail: string) => {
|
||||||
|
const body = { selectedUserEmail: userEmail }
|
||||||
|
if (props.user.email === userEmail) {
|
||||||
|
toast.error('Vous devez être déconnecté pour supprimer votre compte', {
|
||||||
|
position: "top-center",
|
||||||
|
autoClose: 2000,
|
||||||
|
hideProgressBar: false,
|
||||||
|
closeOnClick: true,
|
||||||
|
pauseOnHover: true,
|
||||||
|
draggable: true,
|
||||||
|
progress: undefined,
|
||||||
|
theme: "light",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
fetcher('/api/user/delete', 'DELETE', body)
|
||||||
|
};
|
||||||
|
|
||||||
|
const selectUser = (user: User) => {
|
||||||
|
setCurrentUser(user);
|
||||||
|
setValue("name", user.name)
|
||||||
|
setValue("email", user.email)
|
||||||
|
setValue("surname", user.surname)
|
||||||
|
setValue("address", user.address)
|
||||||
|
setValue("zipCode", user.zipCode)
|
||||||
|
setValue("phoneNumber", user.phoneNumber)
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Head>
|
||||||
|
<title>Administration</title>
|
||||||
|
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
||||||
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="" />
|
||||||
|
</Head>
|
||||||
|
<div className={`${style['masthead']} d-flex justify-content-center align-items-center text-center`}>
|
||||||
|
<div className="container align-items-center d-flex flex-column">
|
||||||
|
<div className={`${style['bloc-content']} col-12 col-md-12`}>
|
||||||
|
<div className="row">
|
||||||
|
<div className="col-md-12 col-sm-12">
|
||||||
|
<hr />
|
||||||
|
<h1 className="fw-light">Utilisateurs <TfiInfoAlt size={40} /></h1>
|
||||||
|
<hr />
|
||||||
|
{users.length <= 0 && <p>Pas encore d'utilisateur</p>}
|
||||||
|
{users.length >= 1 &&
|
||||||
|
<table className={`table table-hover`}>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col">#</th>
|
||||||
|
<th scope="col">Nom</th>
|
||||||
|
<th scope="col">Prénom</th>
|
||||||
|
<th scope="col">Email</th>
|
||||||
|
<th scope="col">Types</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{users.map((info: User, index: number) => {
|
||||||
|
return <tr key={index} className={`${style.cursor}`} onClick={() => selectUser(info)}>
|
||||||
|
<th scope="row">{++index}</th>
|
||||||
|
<td>{info.surname}</td>
|
||||||
|
<td>{info.name}</td>
|
||||||
|
<td>{info.email}</td>
|
||||||
|
<td>
|
||||||
|
<select className="custom-select" defaultValue={info.role} onChange={(e) => onChangeRole(e, info)}>
|
||||||
|
<option value="USER">Client</option>
|
||||||
|
<option value="SELLER">Vendeur</option>
|
||||||
|
<option value="ADMIN">Administrateur</option>
|
||||||
|
</select>
|
||||||
|
</td>
|
||||||
|
<td><button className="btn btn-primary" onClick={() => onDeleteUser(info.email)}>Supprimer</button></td>
|
||||||
|
</tr>
|
||||||
|
})}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<div className="row">
|
||||||
|
<div className="col-md-6 col-sm-12">
|
||||||
|
<hr />
|
||||||
|
<h1 className="fw-light">{currentUser?.email || 'utilisateur'} <TfiInfoAlt size={40} /></h1>
|
||||||
|
<hr />
|
||||||
|
{currentUser && <form onSubmit={handleSubmit(onSubmit)}>
|
||||||
|
<div className="form-group">
|
||||||
|
|
||||||
|
<div className="form-group row">
|
||||||
|
<label htmlFor="staticEmail" className="col-sm-3 col-form-label">Email :</label>
|
||||||
|
<div className="col-sm-9">
|
||||||
|
<input type="text" readOnly className="form-control-plaintext text-sm-center" id="staticEmail" value={currentUser?.email || ''} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="form-group row">
|
||||||
|
<label htmlFor="inputName" className="col-sm-3 col-form-label">Nom :</label>
|
||||||
|
<div className="col-sm-9">
|
||||||
|
<input {...register("name")} className={`form-control ${errors.name ? 'is-invalid' : ''}`} defaultValue={currentUser?.name || ''} id="inputName" placeholder="John..." />
|
||||||
|
<div className="invalid-feedback alert alert-danger mt-2">{errors.name?.message}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="form-group row">
|
||||||
|
<label htmlFor="surname" className="col-sm-3 col-form-label">Prenom :</label>
|
||||||
|
<div className="col-sm-9">
|
||||||
|
<input {...register("surname")} className={`form-control ${errors.surname ? 'is-invalid' : ''}`} defaultValue={currentUser?.surname || ''} id="surname" placeholder="Doe..." />
|
||||||
|
<div className="invalid-feedback alert alert-danger mt-2">{errors.surname?.message}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="form-group row">
|
||||||
|
<label htmlFor="address" className="col-sm-3 col-form-label">Adresse :</label>
|
||||||
|
<div className="col-sm-9">
|
||||||
|
<input {...register("address")} className={`form-control ${errors.address ? 'is-invalid' : ''}`} defaultValue={currentUser?.address || ''} id="address" placeholder="1 rue de la paix..." />
|
||||||
|
<div className="invalid-feedback alert alert-danger mt-2">{errors.address?.message}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="form-group row">
|
||||||
|
<label htmlFor="zipCode" className="col-sm-3 col-form-label">Code postale :</label>
|
||||||
|
<div className="col-sm-9">
|
||||||
|
<input {...register("zipCode")} className={`form-control ${errors.zipCode ? 'is-invalid' : ''}`} defaultValue={currentUser?.zipCode || ''} id="zipCode" placeholder="95000..." />
|
||||||
|
<div className="invalid-feedback alert alert-danger mt-2">{errors.zipCode?.message}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="form-group row">
|
||||||
|
<label htmlFor="phoneNumber" className="col-sm-3 col-form-label">Téléphone :</label>
|
||||||
|
<div className="col-sm-9">
|
||||||
|
<input {...register("phoneNumber")} className={`form-control ${errors.phoneNumber ? 'is-invalid' : ''}`} defaultValue={currentUser?.phoneNumber || ''} id="phoneNumber" placeholder="0123456789..." />
|
||||||
|
<div className="invalid-feedback alert alert-danger mt-2">{errors.phoneNumber?.message}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="form-group">
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
className="btn btn-primary mt-2"
|
||||||
|
>
|
||||||
|
Sauvegarder mes changements
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</form>}
|
||||||
|
{!currentUser && <p>Séléctionner un utilisateur</p>}
|
||||||
|
</div>
|
||||||
|
<div className="col-md-6 col-sm-12">
|
||||||
|
<hr />
|
||||||
|
<h1 className="fw-light">Cadeaux de {currentUser?.email || "l'utilisateur"} <TfiGift size={40} /></h1>
|
||||||
|
<hr />
|
||||||
|
{currentUser?.gains?.length <= 0 && <p>Pas encore de cadeaux gagné</p>}
|
||||||
|
{currentUser?.gains?.length >= 1 &&
|
||||||
|
<table className="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col">#</th>
|
||||||
|
<th scope="col">Codes joués</th>
|
||||||
|
<th scope="col">Cadeaux</th>
|
||||||
|
<th scope="col">Retiré en magasin</th>
|
||||||
|
<th scope="col">Retrait</th>
|
||||||
|
<th scope="col">Date Retrait</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{currentUser?.gains.map((gain: Gain, index: number) => {
|
||||||
|
return <tr key={index}>
|
||||||
|
<th scope="row">{++index}</th>
|
||||||
|
<td>{gain?.codeCadeau}</td>
|
||||||
|
<td>{props.produits[gain.produitId - 1].nomProduit}</td>
|
||||||
|
<td>{gain?.retrait ? '❌ déja récupéré' : '✔️ disponible'}</td>
|
||||||
|
<td>
|
||||||
|
<button className="btn btn-primary">OUI</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
})}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div >
|
||||||
|
</div>
|
||||||
|
</>)
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Admin;
|
||||||
|
|
||||||
|
|
||||||
@ -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/admin?pass=1234 (a effacer)
|
||||||
|
|
||||||
|
export default async function makeMeAdmin(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
const { pass } = req.query;
|
||||||
|
const session = await getSession({ req })
|
||||||
|
const goodPassword = pass === '1234';
|
||||||
|
|
||||||
|
//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,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=1234 (a effacer)
|
||||||
|
|
||||||
|
export default async function makeMeSuper(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
const { pass } = req.query;
|
||||||
|
const session = await getSession({ req })
|
||||||
|
const goodPassword = pass === '1234';
|
||||||
|
|
||||||
|
//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,74 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
export default async function deleteUser(req: NextApiRequest, res: NextApiResponse) {
|
||||||
|
|
||||||
|
|
||||||
|
const session = await getSession({ req })
|
||||||
|
const { selectedUserEmail } = req.body;
|
||||||
|
|
||||||
|
// est-ce la méthode est DELETE ?
|
||||||
|
if (req.method !== ("DELETE")) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//supprime l'utilisateur connecté
|
||||||
|
const user = await prisma.user.delete({
|
||||||
|
where: { email: selectedUserEmail },
|
||||||
|
})
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
// }
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//renvoi la liste des users mis à jour
|
||||||
|
const users = await prisma.user.findMany({})
|
||||||
|
|
||||||
|
res.status(200).send({ message: `L'utilisateur ${user.name} a été supprimé !`, res: users });
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,102 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
export default async function getCurrent(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;
|
||||||
|
// }
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//récupération des informations de l'utilisateur selectionné
|
||||||
|
const selectedUser = await prisma.user.findFirst({
|
||||||
|
where: { email: selectedUserEmail },
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log('selectedUser', selectedUser);
|
||||||
|
|
||||||
|
|
||||||
|
//mise a jour des données pour l'utilisateur
|
||||||
|
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,
|
||||||
|
email: selectedUserEmail || undefined,
|
||||||
|
role: selectedUserRole || undefined
|
||||||
|
},
|
||||||
|
|
||||||
|
})
|
||||||
|
res.status(200).send({ message: `Les informations de ${selectedUserName} ont été mise à jours !`, res: updatedUser });
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,96 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
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,117 @@
|
|||||||
|
-- CreateEnum
|
||||||
|
CREATE TYPE "Role" AS ENUM ('USER', 'ADMIN', 'SUPERADMIN');
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "accounts" (
|
||||||
|
"id" SERIAL NOT NULL,
|
||||||
|
"user_id" INTEGER NOT NULL,
|
||||||
|
"type" TEXT NOT NULL,
|
||||||
|
"provider" TEXT NOT NULL,
|
||||||
|
"provider_account_id" TEXT NOT NULL,
|
||||||
|
"refresh_token" TEXT,
|
||||||
|
"access_token" TEXT,
|
||||||
|
"expires_at" INTEGER,
|
||||||
|
"token_type" TEXT,
|
||||||
|
"scope" TEXT,
|
||||||
|
"id_token" TEXT,
|
||||||
|
"session_state" TEXT,
|
||||||
|
"oauth_token_secret" TEXT,
|
||||||
|
"oauth_token" TEXT,
|
||||||
|
|
||||||
|
CONSTRAINT "accounts_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "sessions" (
|
||||||
|
"id" SERIAL NOT NULL,
|
||||||
|
"session_token" TEXT NOT NULL,
|
||||||
|
"user_id" INTEGER NOT NULL,
|
||||||
|
"expires" TIMESTAMP(3) NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "sessions_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "users" (
|
||||||
|
"id" SERIAL NOT NULL,
|
||||||
|
"name" TEXT,
|
||||||
|
"surname" TEXT,
|
||||||
|
"address" TEXT,
|
||||||
|
"zipCode" INTEGER,
|
||||||
|
"phoneNumber" INTEGER,
|
||||||
|
"email" TEXT,
|
||||||
|
"email_verified" TIMESTAMP(3),
|
||||||
|
"image" TEXT,
|
||||||
|
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"updated_at" TIMESTAMP(3) NOT NULL,
|
||||||
|
"role" "Role" NOT NULL DEFAULT 'USER',
|
||||||
|
|
||||||
|
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "TicketsRestants" (
|
||||||
|
"id" SERIAL NOT NULL,
|
||||||
|
"total" INTEGER NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "TicketsRestants_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "Gain" (
|
||||||
|
"id" SERIAL NOT NULL,
|
||||||
|
"codeCadeau" TEXT NOT NULL,
|
||||||
|
"dateRetrait" TIMESTAMP(3),
|
||||||
|
"produitId" INTEGER,
|
||||||
|
"userId" INTEGER,
|
||||||
|
"actif" BOOLEAN NOT NULL DEFAULT true,
|
||||||
|
|
||||||
|
CONSTRAINT "Gain_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "Produit" (
|
||||||
|
"id" SERIAL NOT NULL,
|
||||||
|
"actif" BOOLEAN,
|
||||||
|
"nomProduit" TEXT,
|
||||||
|
"prixTtc" INTEGER,
|
||||||
|
|
||||||
|
CONSTRAINT "Produit_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "verificationtokens" (
|
||||||
|
"identifier" TEXT NOT NULL,
|
||||||
|
"token" TEXT NOT NULL,
|
||||||
|
"expires" TIMESTAMP(3) NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "accounts_provider_provider_account_id_key" ON "accounts"("provider", "provider_account_id");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "sessions_session_token_key" ON "sessions"("session_token");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "users_email_key" ON "users"("email");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "Gain_codeCadeau_key" ON "Gain"("codeCadeau");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "verificationtokens_token_key" ON "verificationtokens"("token");
|
||||||
|
|
||||||
|
-- CreateIndex
|
||||||
|
CREATE UNIQUE INDEX "verificationtokens_identifier_token_key" ON "verificationtokens"("identifier", "token");
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "accounts" ADD CONSTRAINT "accounts_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Gain" ADD CONSTRAINT "Gain_produitId_fkey" FOREIGN KEY ("produitId") REFERENCES "Produit"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "Gain" ADD CONSTRAINT "Gain_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
# Please do not edit this file manually
|
||||||
|
# It should be added in your version-control system (i.e. Git)
|
||||||
|
provider = "postgresql"
|
||||||
|
After Width: | Height: | Size: 30 KiB |
|
After Width: | Height: | Size: 61 KiB |
|
After Width: | Height: | Size: 497 KiB |
@ -0,0 +1,34 @@
|
|||||||
|
.wrapper {
|
||||||
|
width: 100%;
|
||||||
|
height: 100vh;
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
background-color: rgb(0, 0, 0, 0.3);
|
||||||
|
backdrop-filter: blur(10px);
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
z-index: 9999;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*code for custom loading icon*/
|
||||||
|
|
||||||
|
.loader {
|
||||||
|
border: 10px solid #f3f3f3;
|
||||||
|
border-radius: 50%;
|
||||||
|
border-top: 10px solid #505050;
|
||||||
|
width: 60px;
|
||||||
|
height: 60px;
|
||||||
|
animation: spin 2s linear infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes spin {
|
||||||
|
0% {
|
||||||
|
transform: rotate(0deg);
|
||||||
|
}
|
||||||
|
|
||||||
|
100% {
|
||||||
|
transform: rotate(360deg);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,26 +1,31 @@
|
|||||||
.aCard {
|
.aCard {
|
||||||
/* width:200px; */
|
/* width:200px; */
|
||||||
max-width:300px;
|
max-width: 300px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.aCard img {
|
.aCard img {
|
||||||
width:fit-content;
|
width: fit-content;
|
||||||
block-size: -moz-fit-content;
|
block-size: -moz-fit-content;
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.aCard img, .grosLot img {
|
.aCard img,
|
||||||
height:fit-content;
|
.grosLot img {
|
||||||
|
height: fit-content;
|
||||||
}
|
}
|
||||||
|
|
||||||
.grosLot img {
|
.grosLot img {
|
||||||
border-radius:0.375rem;
|
border-radius: 0.375rem;
|
||||||
}
|
}
|
||||||
@media (min-width: 992px){
|
|
||||||
.explicationContainer{
|
@media (min-width: 992px) {
|
||||||
--padding-x:15%;
|
.explicationContainer {
|
||||||
|
--padding-x: 15%;
|
||||||
padding-left: var(--padding-x);
|
padding-left: var(--padding-x);
|
||||||
padding-right: var(--padding-x);
|
padding-right: var(--padding-x);
|
||||||
}
|
}
|
||||||
.aCard div {
|
|
||||||
|
.aCard div {
|
||||||
min-height: 205px;
|
min-height: 205px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in new issue