parent
141d9e53e8
commit
e85117b2c7
@ -0,0 +1,41 @@
|
||||
version: '3.5'
|
||||
|
||||
services:
|
||||
postgres:
|
||||
container_name: postgres_container
|
||||
image: postgres
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-postgres}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgrespwd}
|
||||
PGDATA: /data/postgres
|
||||
volumes:
|
||||
- postgres:/data/postgres
|
||||
ports:
|
||||
- "5432:5432"
|
||||
networks:
|
||||
- postgres
|
||||
restart: unless-stopped
|
||||
|
||||
pgadmin:
|
||||
container_name: pgadmin_container
|
||||
image: dpage/pgadmin4
|
||||
environment:
|
||||
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4@pgadmin.org}
|
||||
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
|
||||
PGADMIN_CONFIG_SERVER_MODE: 'False'
|
||||
volumes:
|
||||
- pgadmin:/var/lib/pgadmin
|
||||
|
||||
ports:
|
||||
- "${PGADMIN_PORT:-5050}:80"
|
||||
networks:
|
||||
- postgres
|
||||
restart: unless-stopped
|
||||
|
||||
networks:
|
||||
postgres:
|
||||
driver: bridge
|
||||
|
||||
volumes:
|
||||
postgres:
|
||||
pgadmin:
|
||||
@ -0,0 +1,7 @@
|
||||
export enum Cadeaux {
|
||||
CoffretDecouvert69 = 1,
|
||||
CoffretDecouvert39 = 2,
|
||||
TheSignature = 3,
|
||||
TheDetox = 4,
|
||||
Infuseur = 5,
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
export enum Role {
|
||||
USER = 'USER',
|
||||
ADMIN = 'ADMIN',
|
||||
SUPERADMIN = 'SUPERADMIN'
|
||||
}
|
||||
@ -0,0 +1,107 @@
|
||||
import prisma from '../../../lib/prisma';
|
||||
import chance from 'chance'
|
||||
|
||||
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 generationGain(req: NextApiRequest, res: NextApiResponse) {
|
||||
const session = await getSession({ req })
|
||||
|
||||
//récuperation des tickets disponible
|
||||
const getTickets = await prisma.ticketsRestants.findFirst({
|
||||
where: {
|
||||
id: 1
|
||||
},
|
||||
})
|
||||
|
||||
const { total } = getTickets;
|
||||
const isTicketsAvaible = total > 0;
|
||||
|
||||
//récupération des informations de l'utilisateur connecté
|
||||
const getCurrentUser = await prisma.user.findMany({
|
||||
where: { email: session.user.email },
|
||||
})
|
||||
|
||||
//récupération du role
|
||||
const { role } = getCurrentUser[0];
|
||||
const isAdmin = role === Role.ADMIN;
|
||||
const isSuperAdmin = Role.SUPERADMIN;
|
||||
|
||||
if (session && isTicketsAvaible) {
|
||||
if (isAdmin || isSuperAdmin) {
|
||||
|
||||
//creation d'un numéro de gain aléatoire
|
||||
const gainNumber = (Math.random() + 1).toString(36).substring(7).toUpperCase()
|
||||
|
||||
//création d'un id de produit de façon pondérée
|
||||
let weightedRandomGainId = chance().weighted([
|
||||
Cadeaux.CoffretDecouvert69,
|
||||
Cadeaux.CoffretDecouvert39,
|
||||
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
|
||||
}
|
||||
}
|
||||
})
|
||||
res.status(200).send({ message: 'Gain has been generated !', result: gainResult });
|
||||
}
|
||||
} 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/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' })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,60 +1,87 @@
|
||||
import React from "react";
|
||||
import type { GetServerSideProps } from "next";
|
||||
import Layout from "../components/Layout";
|
||||
import Post, { PostProps } from "../components/Post";
|
||||
import prisma from '../lib/prisma'
|
||||
|
||||
export const getServerSideProps: GetServerSideProps = async () => {
|
||||
const feed = await prisma.post.findMany({
|
||||
where: {
|
||||
published: true,
|
||||
},
|
||||
include: {
|
||||
author: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
import { yupResolver } from "@hookform/resolvers/yup";
|
||||
import { useForm } from "react-hook-form";
|
||||
import * as Yup from 'yup';
|
||||
import { useSession } from "next-auth/react";
|
||||
|
||||
type CodeSubmitForm = {
|
||||
codeCadeau: string;
|
||||
};
|
||||
|
||||
const MainPage = () => {
|
||||
const { data: session } = useSession();
|
||||
|
||||
const validationSchema = Yup.object().shape({
|
||||
codeCadeau: Yup.string().trim().required("Vous n'avez pas rentrer de code cadeau !"),
|
||||
});
|
||||
return {
|
||||
props: { feed },
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
formState: { errors }
|
||||
} = useForm<CodeSubmitForm>({
|
||||
resolver: yupResolver(validationSchema)
|
||||
});
|
||||
|
||||
const onSubmit = async (data: CodeSubmitForm) => {
|
||||
|
||||
if (!session) {
|
||||
return <Layout>
|
||||
<p>Vous devez vous connecter pour tester votre !</p>
|
||||
</Layout>
|
||||
}
|
||||
|
||||
|
||||
|
||||
// try {
|
||||
// const body = { bioValue: data.bio };
|
||||
// await fetch(`/api/profile`, {
|
||||
// method: "POST",
|
||||
// headers: { "Content-Type": "application/json" },
|
||||
// body: JSON.stringify(body),
|
||||
// })
|
||||
// .then((res) => res.json())
|
||||
// .then((data) => {
|
||||
// setResponse(data);
|
||||
// console.log('response', data);
|
||||
// })
|
||||
// await Router.push("/profile");
|
||||
// } catch (error) {
|
||||
// console.error(error);
|
||||
// }
|
||||
};
|
||||
};
|
||||
|
||||
type Props = {
|
||||
feed: PostProps[];
|
||||
};
|
||||
|
||||
const Blog = (props : Props) => {
|
||||
|
||||
return (
|
||||
<Layout>
|
||||
<div className="page">
|
||||
<h1>Public Feed</h1>
|
||||
<h1>TheTipTop - jeu concours !</h1>
|
||||
<main>
|
||||
{props.feed.map((post) => (
|
||||
<div key={post.id} className="post">
|
||||
<Post post={post} />
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<div className="form-group">
|
||||
<label>Entre votre code :</label>
|
||||
<input
|
||||
placeholder="A123456B..."
|
||||
type="text"
|
||||
{...register('codeCadeau')}
|
||||
className={`form-control ${errors.codeCadeau ? 'is-invalid' : ''}`}
|
||||
/>
|
||||
<div className="invalid-feedback">{errors.codeCadeau?.message}</div>
|
||||
</div>
|
||||
<div className="form-group">
|
||||
<button type="submit" className="btn btn-primary">
|
||||
Tester mon code cadeau !
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</form>
|
||||
</main>
|
||||
</div>
|
||||
<style jsx>{`
|
||||
.post {
|
||||
background: white;
|
||||
transition: box-shadow 0.1s ease-in;
|
||||
}
|
||||
|
||||
.post:hover {
|
||||
box-shadow: 1px 1px 3px #aaa;
|
||||
}
|
||||
|
||||
.post + .post {
|
||||
margin-top: 2rem;
|
||||
}
|
||||
`}</style>
|
||||
</Layout>
|
||||
);
|
||||
};
|
||||
|
||||
export default Blog;
|
||||
export default MainPage;
|
||||
|
||||
Binary file not shown.
@ -1,70 +0,0 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "Post" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"title" TEXT NOT NULL,
|
||||
"content" TEXT,
|
||||
"published" BOOLEAN NOT NULL DEFAULT false,
|
||||
"authorId" INTEGER,
|
||||
CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "users" ("id") ON DELETE SET NULL ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "accounts" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"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_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "sessions" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"session_token" TEXT NOT NULL,
|
||||
"user_id" INTEGER NOT NULL,
|
||||
"expires" DATETIME NOT NULL,
|
||||
CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "users" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"name" TEXT,
|
||||
"email" TEXT,
|
||||
"email_verified" DATETIME,
|
||||
"image" TEXT,
|
||||
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updated_at" DATETIME NOT NULL
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "verificationtokens" (
|
||||
"identifier" TEXT NOT NULL,
|
||||
"token" TEXT NOT NULL,
|
||||
"expires" DATETIME 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 "verificationtokens_token_key" ON "verificationtokens"("token");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "verificationtokens_identifier_token_key" ON "verificationtokens"("identifier", "token");
|
||||
@ -1,10 +0,0 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "Profile" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"bio" TEXT,
|
||||
"userId" INTEGER NOT NULL,
|
||||
CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Profile_userId_key" ON "Profile"("userId");
|
||||
@ -1,21 +0,0 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the `Profile` table. If the table is not empty, all the data it contains will be lost.
|
||||
|
||||
*/
|
||||
-- DropTable
|
||||
PRAGMA foreign_keys=off;
|
||||
DROP TABLE "Profile";
|
||||
PRAGMA foreign_keys=on;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Profil" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"bio" TEXT,
|
||||
"userId" INTEGER NOT NULL,
|
||||
CONSTRAINT "Profil_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Profil_userId_key" ON "Profil"("userId");
|
||||
@ -1,21 +0,0 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the `Profil` table. If the table is not empty, all the data it contains will be lost.
|
||||
|
||||
*/
|
||||
-- DropTable
|
||||
PRAGMA foreign_keys=off;
|
||||
DROP TABLE "Profil";
|
||||
PRAGMA foreign_keys=on;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Profile" (
|
||||
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
"bio" TEXT,
|
||||
"userId" INTEGER NOT NULL,
|
||||
CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Profile_userId_key" ON "Profile"("userId");
|
||||
@ -0,0 +1,144 @@
|
||||
-- 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,
|
||||
"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 "Post" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"title" TEXT NOT NULL,
|
||||
"content" TEXT,
|
||||
"published" BOOLEAN NOT NULL DEFAULT false,
|
||||
"authorId" INTEGER,
|
||||
|
||||
CONSTRAINT "Post_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Profile" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"bio" TEXT,
|
||||
"userId" INTEGER NOT NULL,
|
||||
|
||||
CONSTRAINT "Profile_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,
|
||||
|
||||
CONSTRAINT "Gain_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Produit" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"actif" BOOLEAN,
|
||||
"nomProduit" TEXT,
|
||||
"prixTtc" DECIMAL(65,30),
|
||||
|
||||
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 "Profile_userId_key" ON "Profile"("userId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Gain_codeCadeau_key" ON "Gain"("codeCadeau");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Gain_userId_key" ON "Gain"("userId");
|
||||
|
||||
-- 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 "Post" ADD CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Profile" ADD CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE RESTRICT 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;
|
||||
@ -1,3 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "sqlite"
|
||||
provider = "postgresql"
|
||||
@ -0,0 +1,48 @@
|
||||
// import { produits } from './produits';
|
||||
import prisma from '../../lib/prisma'
|
||||
import { Chance } from 'chance'
|
||||
import { Cadeaux } from '../../enum/cadeaux.enum';
|
||||
let chance = new Chance();
|
||||
|
||||
export const generateGains = async () => {
|
||||
//génération d'un code cadeau
|
||||
const randomNoGain = (Math.random() + 1).toString(36).substring(7).toUpperCase()
|
||||
|
||||
//création d'un gain
|
||||
const gainCreated = await prisma.gain.create({
|
||||
data: {
|
||||
codeCadeau: randomNoGain,
|
||||
}
|
||||
})
|
||||
|
||||
//crétion d'un id de produit de façon pondérée
|
||||
let weightedRandomGain = chance.weighted([
|
||||
Cadeaux.CoffretDecouvert69,
|
||||
Cadeaux.CoffretDecouvert39,
|
||||
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}`)
|
||||
}
|
||||
|
||||
@ -0,0 +1,24 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
export const produitsData: Prisma.ProduitCreateInput[] = [
|
||||
{
|
||||
nomProduit: 'coffret découverte à 69€',
|
||||
prixTtc: 69,
|
||||
|
||||
},
|
||||
{
|
||||
nomProduit: 'coffret découverte à 39€',
|
||||
prixTtc: 39,
|
||||
},
|
||||
{
|
||||
nomProduit: 'thé signature',
|
||||
prixTtc: 10,
|
||||
},
|
||||
{
|
||||
nomProduit: 'thé détox ou infusion',
|
||||
prixTtc: 6,
|
||||
},
|
||||
{
|
||||
nomProduit: 'infuseur à thé',
|
||||
prixTtc: 3,
|
||||
}]
|
||||
@ -0,0 +1,47 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
export const userData: Prisma.UserCreateInput[] = [
|
||||
{
|
||||
name: 'Alice',
|
||||
email: 'alice@prisma.io',
|
||||
posts: {
|
||||
create: [
|
||||
{
|
||||
title: 'Join the Prisma Slack',
|
||||
content: 'https://slack.prisma.io',
|
||||
published: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Nilu',
|
||||
email: 'nilu@prisma.io',
|
||||
posts: {
|
||||
create: [
|
||||
{
|
||||
title: 'Follow Prisma on Twitter',
|
||||
content: 'https://www.twitter.com/prisma',
|
||||
published: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Mahmoud',
|
||||
email: 'mahmoud@prisma.io',
|
||||
posts: {
|
||||
create: [
|
||||
{
|
||||
title: 'Ask a question about Prisma on GitHub',
|
||||
content: 'https://www.github.com/prisma/prisma/discussions',
|
||||
published: true,
|
||||
},
|
||||
{
|
||||
title: 'Prisma on YouTube',
|
||||
content: 'https://pris.ly/youtube',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
Loading…
Reference in new issue