Skip to content

Commit

Permalink
feat: add proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Brasileiro authored and Fabio Brasileiro committed Sep 24, 2024
1 parent 6c5bc56 commit dbc8f19
Show file tree
Hide file tree
Showing 14 changed files with 330 additions and 56 deletions.
File renamed without changes.
26 changes: 26 additions & 0 deletions migrations/20240924014531_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- CreateEnum
CREATE TYPE "ProposalStatus" AS ENUM ('PENDING', 'APPROVED', 'REJECTED');

-- AlterEnum
ALTER TYPE "UserRole" ADD VALUE 'PENDING';

-- AlterTable
ALTER TABLE "User" ALTER COLUMN "role" SET DEFAULT 'PENDING';

-- CreateTable
CREATE TABLE "Proposal" (
"id" SERIAL NOT NULL,
"companyName" TEXT NOT NULL,
"companyDocument" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
"status" "ProposalStatus" NOT NULL DEFAULT 'PENDING',
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "Proposal_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Proposal_userId_key" ON "Proposal"("userId");

-- AddForeignKey
ALTER TABLE "Proposal" ADD CONSTRAINT "Proposal_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
3 changes: 3 additions & 0 deletions migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -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"
83 changes: 83 additions & 0 deletions prisma/migrations/20240924023020_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
-- CreateEnum
CREATE TYPE "ProposalStatus" AS ENUM ('PENDING', 'APPROVED', 'REJECTED');

-- CreateEnum
CREATE TYPE "UserRole" AS ENUM ('PENDING', 'USER', 'ADMIN');

-- CreateTable
CREATE TABLE "Product" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"price" DOUBLE PRECISION NOT NULL,
"description" TEXT,
"imageUrl" TEXT,
"companyId" INTEGER NOT NULL,

CONSTRAINT "Product_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Company" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"document" TEXT NOT NULL,

CONSTRAINT "Company_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Unit" (
"id" SERIAL NOT NULL,
"name" TEXT NOT NULL,
"companyId" INTEGER NOT NULL,

CONSTRAINT "Unit_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Proposal" (
"id" SERIAL NOT NULL,
"companyName" TEXT NOT NULL,
"companyDocument" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
"status" "ProposalStatus" NOT NULL DEFAULT 'PENDING',
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "Proposal_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"email" TEXT NOT NULL,
"name" TEXT NOT NULL,
"password" TEXT NOT NULL,
"role" "UserRole" NOT NULL DEFAULT 'PENDING',
"companyId" INTEGER,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Company_name_key" ON "Company"("name");

-- CreateIndex
CREATE UNIQUE INDEX "Company_document_key" ON "Company"("document");

-- CreateIndex
CREATE UNIQUE INDEX "Proposal_userId_key" ON "Proposal"("userId");

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- AddForeignKey
ALTER TABLE "Product" ADD CONSTRAINT "Product_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Unit" ADD CONSTRAINT "Unit_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Proposal" ADD CONSTRAINT "Proposal_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "User" ADD CONSTRAINT "User_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "Company"("id") ON DELETE SET NULL ON UPDATE CASCADE;
12 changes: 12 additions & 0 deletions prisma/migrations/20240924025207_update_register/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- Added the required column `proposalValue` to the `Proposal` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Proposal" ADD COLUMN "proposalValue" DOUBLE PRECISION NOT NULL,
ADD COLUMN "unitId" INTEGER;

-- AddForeignKey
ALTER TABLE "Proposal" ADD CONSTRAINT "Proposal_unitId_fkey" FOREIGN KEY ("unitId") REFERENCES "Unit"("id") ON DELETE SET NULL ON UPDATE CASCADE;
51 changes: 38 additions & 13 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
Expand All @@ -26,31 +29,53 @@ model Product {
model Company {
id Int @id @default(autoincrement())
name String @unique
document String @unique // Certifique-se de que esta linha existe
document String @unique
users User[] // Relação com os usuários
units Unit[]
products Product[]
}


model User {
id Int @id @default(autoincrement())
email String @unique
name String
password String
role UserRole
company Company? @relation(fields: [companyId], references: [id])
companyId Int?
}

model Unit {
id Int @id @default(autoincrement())
name String
company Company @relation(fields: [companyId], references: [id])
companyId Int
Proposal Proposal[]
}

model Proposal {
id Int @id @default(autoincrement())
companyName String
companyDocument String
user User @relation(fields: [userId], references: [id], name: "ProposalToUser")
userId Int @unique
status ProposalStatus @default(PENDING)
createdAt DateTime @default(now())
proposalValue Float
unitId Int? // Unidade da empresa relacionada a esta proposta
unit Unit? @relation(fields: [unitId], references: [id])
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String
password String
role UserRole @default(PENDING)
proposal Proposal? @relation(name: "ProposalToUser")
company Company? @relation(fields: [companyId], references: [id])
companyId Int?
}

enum ProposalStatus {
PENDING
APPROVED
REJECTED
}

enum UserRole {
PENDING // Usuários que ainda não foram aprovados
USER
ADMIN
}
}
4 changes: 3 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import paymentRoutes from './routes/paymentRoutes.js'
import productRoutes from './routes/productRoutes.js'
import createUserRouter from './routes/createUserRoutes.js'
import createUnitRouter from './routes/createUnitRoutes.js'
import proposalRoutes from './routes/proposalRoutes.js'

const app = express()

Expand All @@ -19,9 +20,10 @@ app.get('/', (_req, res) => {
// Defina as rotas para produtos e pagamentos
app.use('/api/products', productRoutes)
app.use('/api/payments', paymentRoutes)
app.use('/api', authRoutes) // Adicione '/api' como prefixo para as rotas de autenticação
app.use('/auth', authRoutes) // Adicione '/api' como prefixo para as rotas de autenticação
app.use('/api', createUserRouter) // Adicione '/api' como prefixo para as rotas de autenticação
app.use('/api', createUnitRouter) // Adicione '/api' como prefixo para as rotas de autenticação
app.use('/proposal', proposalRoutes) // Adicione '/api' como prefixo para as rotas de autenticação


export default app
Loading

0 comments on commit dbc8f19

Please sign in to comment.