⚡️ add - api generationGain , api superadmin , readme , fichier de seed

This commit is contained in:
eric committed 2022-11-12 20:23:15 +01:00
1 parent 141d9e53e8
commit e85117b2c7
25 files changed
+768 -310

No files matched your search

BIN
View File
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 -1
View File
@@ -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"
+33 -2
View File
@@ -3,8 +3,8 @@ generator client {
}
datasource db {
provider = "sqlite"
url = "file:./dev.db"
provider = "postgresql"
url = env("DATABASE_URL")
}
model Account {
@@ -51,6 +51,8 @@ model User {
accounts Account[]
sessions Session[]
profile Profile?
gain Gain?
role Role @default(USER)
@@map(name: "users")
}
@@ -71,6 +73,35 @@ model Profile {
userId Int @unique
}
model TicketsRestants {
id Int @id @default(autoincrement())
total Int
}
model Gain {
id Int @id @default(autoincrement())
codeCadeau String @unique
dateRetrait DateTime?
produit Produit? @relation(fields: [produitId], references: [id])
produitId Int?
user User? @relation(fields: [userId], references: [id])
userId Int? @unique
}
model Produit {
id Int @id @default(autoincrement())
actif Boolean?
nomProduit String?
prixTtc Decimal?
refGain Gain[]
}
enum Role {
USER
ADMIN
SUPERADMIN
}
model VerificationToken {
identifier String
token String @unique
+38 -52
View File
@@ -1,64 +1,50 @@
import { PrismaClient, Prisma } from '@prisma/client'
// import prisma from '../lib/prisma'
import { PrismaClient } from '@prisma/client'
import { generateGains } from './seed/gains'
import { produitsData } from './seed/produits'
// import { userData } from './seed/user'
const prisma = new PrismaClient()
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',
},
],
},
},
]
const prisma = new PrismaClient();
async function main() {
console.log(`Start seeding ...`)
for (const u of userData) {
const user = await prisma.user.create({
data: u,
//création des tickets restants
const tickets = await prisma.ticketsRestants.create({
data: {
total: 1500000
},
})
console.log(`Created ticket: ${tickets.total}`)
//création des produits
for (const p of produitsData) {
const produit = await prisma.produit.create({
data: p,
})
console.log(`Created user with id: ${user.id}`)
console.log(`Created produit: ${produit.nomProduit}`)
}
/////////////////////////////////////////////////////
//création des utilisateurs
// for (const u of userData) {
// const user = await prisma.user.create({
// data: u,
// })
// console.log(`Created user with id: ${user.id}`)
// }
//création de 5 gains
for (let i = 1; i <= 5; i++) {
await generateGains();
}
console.log(`Seeding finished.`)
}
main()
.then(async () => {
await prisma.$disconnect()
+48
View File
@@ -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}`)
}
+24
View File
@@ -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,
}]
+47
View File
@@ -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',
},
],
},
},
]