From 0a50ebe9524a335cbf108f5f332b07aa56f4b328 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 8 Dec 2022 12:52:03 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20boutton=20admin=20retrait?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 28 ++++++++++ package.json | 2 + pages/admin.tsx | 42 ++++++++++++--- pages/api/superadmin/make-admin.ts | 4 +- pages/api/user/update-gift.ts | 84 ++++++++++++++++++++++++++++++ prisma/seed.ts | 4 +- styles/Index.module.css | 5 +- 7 files changed, 157 insertions(+), 12 deletions(-) create mode 100644 pages/api/user/update-gift.ts diff --git a/package-lock.json b/package-lock.json index 29a3213..faac378 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,8 @@ "bootstrap": "^5.2.2", "chance": "^1.1.9", "crypto-random-string": "^3.0.0", + "date-format": "^4.0.14", + "dateformat": "^5.0.3", "js-confetti": "^0.11.0", "next": "13.0.2", "next-auth": "4.16.4", @@ -900,6 +902,22 @@ "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.1.tgz", "integrity": "sha512-DJR/VvkAvSZW9bTouZue2sSxDwdTN92uHjqeKVm+0dAqdfNykRzQ95tay8aXMBAAPpUiq4Qcug2L7neoRh2Egw==" }, + "node_modules/date-format": { + "version": "4.0.14", + "resolved": "https://registry.npmjs.org/date-format/-/date-format-4.0.14.tgz", + "integrity": "sha512-39BOQLs9ZjKh0/patS9nrT8wc3ioX3/eA/zgbKNopnF2wCqJEoxywwwElATYvRsXdnOxA/OQeQoFZ3rFjVajhg==", + "engines": { + "node": ">=4.0" + } + }, + "node_modules/dateformat": { + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-5.0.3.tgz", + "integrity": "sha512-Kvr6HmPXUMerlLcLF+Pwq3K7apHpYmGDVqrxcDasBg86UcKeTSNWbEzU8bwdXnxnR44FtMhJAxI4Bov6Y/KUfA==", + "engines": { + "node": ">=12.20" + } + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -4339,6 +4357,16 @@ "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.1.tgz", "integrity": "sha512-DJR/VvkAvSZW9bTouZue2sSxDwdTN92uHjqeKVm+0dAqdfNykRzQ95tay8aXMBAAPpUiq4Qcug2L7neoRh2Egw==" }, + "date-format": { + "version": "4.0.14", + "resolved": "https://registry.npmjs.org/date-format/-/date-format-4.0.14.tgz", + "integrity": "sha512-39BOQLs9ZjKh0/patS9nrT8wc3ioX3/eA/zgbKNopnF2wCqJEoxywwwElATYvRsXdnOxA/OQeQoFZ3rFjVajhg==" + }, + "dateformat": { + "version": "5.0.3", + "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-5.0.3.tgz", + "integrity": "sha512-Kvr6HmPXUMerlLcLF+Pwq3K7apHpYmGDVqrxcDasBg86UcKeTSNWbEzU8bwdXnxnR44FtMhJAxI4Bov6Y/KUfA==" + }, "debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", diff --git a/package.json b/package.json index 53365ac..c896b3d 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,8 @@ "bootstrap": "^5.2.2", "chance": "^1.1.9", "crypto-random-string": "^3.0.0", + "date-format": "^4.0.14", + "dateformat": "^5.0.3", "js-confetti": "^0.11.0", "next": "13.0.2", "next-auth": "4.16.4", diff --git a/pages/admin.tsx b/pages/admin.tsx index 5141444..cfbaf3f 100644 --- a/pages/admin.tsx +++ b/pages/admin.tsx @@ -1,6 +1,8 @@ import React, { useEffect, useState, useSyncExternalStore } from "react"; import prisma from '../lib/prisma' +import dateFormat, { masks } from "dateformat"; + import Image from 'next/image'; import * as Yup from 'yup'; @@ -14,6 +16,8 @@ import { AiOutlineCheck } from 'react-icons/ai' import Router from "next/router"; +import { useRouter } from 'next/navigation'; + import style from '../styles/Connexion.module.css'; @@ -92,11 +96,14 @@ const Admin = (props: Props) => { const [response, setResponse] = useState(null) const [currentUser, setCurrentUser] = useState(null) const [users, setUsers] = useState(props.users) + const [currentUserGain, setCurrentUserGain] = useState(null) + const router = useRouter() + const { register, setValue, handleSubmit, formState: { errors } } = useForm({ }); - const fetcher = async (url, method, body = {}) => { + const fetcher = async (url, method, body = {}, callback?: any) => { try { await fetch(url, { method, @@ -106,7 +113,7 @@ const Admin = (props: Props) => { .then((res) => res.json()) .then(async (data) => { setResponse(data); - setUsers(data.res) + callback; if (data.err) { toast.error(data.err, { position: "top-center", @@ -178,8 +185,10 @@ const Admin = (props: Props) => { fetcher('/api/user/delete', 'DELETE', body) }; - const selectUser = (user: User) => { + const selectUser = (user: any) => { + console.log('d', user.gains) setCurrentUser(user); + setCurrentUserGain(user.gains) setValue("name", user.name) setValue("email", user.email) setValue("surname", user.surname) @@ -188,6 +197,26 @@ const Admin = (props: Props) => { setValue("phoneNumber", user.phoneNumber) } + const changeGiftStatus = async (gain: Gain) => { + const body = { gain } + fetcher('/api/user/update-gift', 'PUT', body) + console.log('g', gain); + + // setCurrentUserGain(gain) + // router.refresh(); + }; + + + + + + + + + + + + return ( <> @@ -217,6 +246,7 @@ const Admin = (props: Props) => { {users.map((info: User, index: number) => { + return selectUser(info)}> {++index} {info.surname} @@ -320,19 +350,19 @@ const Admin = (props: Props) => { Codes joués Cadeaux Retiré en magasin - Retrait Date Retrait - {currentUser?.gains.map((gain: Gain, index: number) => { + {currentUserGain.map((gain: Gain, index: number) => { return {++index} {gain?.codeCadeau} {props.produits[gain.produitId - 1].nomProduit} {gain?.retrait ? '❌ déja récupéré' : '✔️ disponible'} + {gain?.dateRetrait ? dateFormat(gain?.dateRetrait, "dd/mm/yyyy") : ''} - + })} diff --git a/pages/api/superadmin/make-admin.ts b/pages/api/superadmin/make-admin.ts index 2f189b6..048d4cc 100644 --- a/pages/api/superadmin/make-admin.ts +++ b/pages/api/superadmin/make-admin.ts @@ -6,7 +6,7 @@ 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) +//--> http://localhost:3000/api/superadmin/make-admin?pass=1234 (a effacer) export default async function makeMeAdmin(req: NextApiRequest, res: NextApiResponse) { const { pass } = req.query; @@ -23,7 +23,7 @@ export default async function makeMeAdmin(req: NextApiRequest, res: NextApiRespo role: Role.SUPERADMIN, }, }) - return res.status(200).send({ message: 'Profiled updated welcome SUPERADMIN !' }); + return res.status(200).send({ message: 'Profiled updated welcome ADMIN !' }); } else { res.status(401).send({ message: 'Unauthorized' }) } diff --git a/pages/api/user/update-gift.ts b/pages/api/user/update-gift.ts new file mode 100644 index 0000000..55d219b --- /dev/null +++ b/pages/api/user/update-gift.ts @@ -0,0 +1,84 @@ +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 { Gain } from '.prisma/client'; + + + +//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 updtateGift(req: NextApiRequest, res: NextApiResponse) { + + + + const session = await getSession({ req }) + + const { gain } = 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 gain de l'utilisateur et mise à jour + const updatedGain: Gain = await prisma.gain.update({ + where: { id: gain.id }, + data: { + retrait: true, + dateRetrait: new Date() + }, + }) + + const updatedCurrentUser = await prisma.user.findFirst({ + where: { id: updatedGain.userId }, + include: { gains: true } + }) + + // 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; + // } + + //////////////////////////////////////////////// + + + + + res.status(200).send({ message: `Le gain a ont été mis à jour !`, res: updatedCurrentUser }); + +} + + + + + + + + + + + + + + + + + + diff --git a/prisma/seed.ts b/prisma/seed.ts index 117a53e..b885125 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -37,8 +37,8 @@ async function main() { console.log(`Created user with id: ${user.id}`) } - //création de 5 gains - for (let i = 1; i <= 5; i++) { + //création de gains + for (let i = 1; i <= (50); i++) { await generateGains(); } diff --git a/styles/Index.module.css b/styles/Index.module.css index 5653a74..cba96bf 100644 --- a/styles/Index.module.css +++ b/styles/Index.module.css @@ -91,6 +91,7 @@ } @keyframes blinking { + 0%, 50%, 100% { @@ -113,7 +114,7 @@ .aButton { position: relative; - z-index: 9992; + z-index: 9000; } .aButton:after { @@ -276,4 +277,4 @@ 50% { right: 0; } -} */ +} */ \ No newline at end of file