Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/mobile #1

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,39 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/common": "10.0.0",
"@nestjs/core": "10.0.0",
"@nestjs/platform-express": "10.0.0",
"@prisma/client": "5.17.0",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
"bcrypt": "5.1.1",
"jsonwebtoken": "^9.0.2",
"reflect-metadata": "0.2.0",
"rxjs": "7.8.1"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^7.0.0",
"@typescript-eslint/parser": "^7.0.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"prisma": "^5.17.0",
"source-map-support": "^0.5.21",
"supertest": "^7.0.0",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
"@nestjs/cli": "10.0.0",
"@nestjs/schematics": "10.0.0",
"@nestjs/testing": "10.0.0",
"@types/express": "4.17.17",
"@types/jest": "29.5.2",
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "20.3.1",
"@types/supertest": "6.0.0",
"@typescript-eslint/eslint-plugin": "7.0.0",
"@typescript-eslint/parser": "7.0.0",
"eslint": "8.42.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-prettier": "5.0.0",
"jest": "29.5.0",
"prettier": "3.0.0",
"prisma": "5.17.0",
"source-map-support": "0.5.21",
"supertest": "7.0.0",
"ts-jest": "29.1.0",
"ts-loader": "9.4.3",
"ts-node": "10.9.1",
"tsconfig-paths": "4.2.0",
"typescript": "5.1.3"
},
"jest": {
"moduleFileExtensions": [
Expand All @@ -60,7 +63,7 @@
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
".+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Warnings:

- You are about to drop the column `emailCliente` on the `agendamentos` table. All the data in the column will be lost.
- Added the required column `usuarioId` to the `agendamentos` table without a default value. This is not possible if the table is not empty.

*/
-- AlterTable
ALTER TABLE "agendamentos" DROP COLUMN "emailCliente",
ADD COLUMN "usuarioId" INTEGER NOT NULL,
ALTER COLUMN "data" SET DATA TYPE TIMESTAMPTZ(3);

-- CreateTable
CREATE TABLE "usuario" (
"id" SERIAL NOT NULL,
"nome" TEXT NOT NULL,
"email" TEXT NOT NULL,
"senha" TEXT NOT NULL,
"telefone" TEXT NOT NULL,
"barbeiro" BOOLEAN NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "usuario_email_key" ON "usuario"("email");

-- AddForeignKey
ALTER TABLE "agendamentos" ADD CONSTRAINT "agendamentos_usuarioId_fkey" FOREIGN KEY ("usuarioId") REFERENCES "usuario"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Warnings:

- You are about to drop the `agendamentos` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `profissionais` table. If the table is not empty, all the data it contains will be lost.
- You are about to drop the `servicos` table. If the table is not empty, all the data it contains will be lost.

*/
-- DropForeignKey
ALTER TABLE "_AgendamentoToServico" DROP CONSTRAINT "_AgendamentoToServico_A_fkey";

-- DropForeignKey
ALTER TABLE "_AgendamentoToServico" DROP CONSTRAINT "_AgendamentoToServico_B_fkey";

-- DropForeignKey
ALTER TABLE "agendamentos" DROP CONSTRAINT "agendamentos_profissionalId_fkey";

-- DropForeignKey
ALTER TABLE "agendamentos" DROP CONSTRAINT "agendamentos_usuarioId_fkey";

-- DropTable
DROP TABLE "agendamentos";

-- DropTable
DROP TABLE "profissionais";

-- DropTable
DROP TABLE "servicos";

-- CreateTable
CREATE TABLE "profissional" (
"id" SERIAL NOT NULL,
"nome" TEXT NOT NULL,
"descricao" TEXT NOT NULL,
"imagemUrl" TEXT NOT NULL,
"avaliacao" DOUBLE PRECISION NOT NULL,
"quantidadeAvaliacoes" INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE "servico" (
"id" SERIAL NOT NULL,
"nome" TEXT NOT NULL,
"descricao" TEXT NOT NULL,
"preco" DOUBLE PRECISION NOT NULL,
"qtdeSlots" INTEGER NOT NULL,
"imagemURL" TEXT NOT NULL,

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

-- CreateTable
CREATE TABLE "agendamento" (
"id" SERIAL NOT NULL,
"data" TIMESTAMPTZ(3) NOT NULL,
"profissionalId" INTEGER NOT NULL,
"usuarioId" INTEGER NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "profissional_nome_key" ON "profissional"("nome");

-- CreateIndex
CREATE UNIQUE INDEX "servico_nome_key" ON "servico"("nome");

-- AddForeignKey
ALTER TABLE "agendamento" ADD CONSTRAINT "agendamento_profissionalId_fkey" FOREIGN KEY ("profissionalId") REFERENCES "profissional"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "agendamento" ADD CONSTRAINT "agendamento_usuarioId_fkey" FOREIGN KEY ("usuarioId") REFERENCES "usuario"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_AgendamentoToServico" ADD CONSTRAINT "_AgendamentoToServico_A_fkey" FOREIGN KEY ("A") REFERENCES "agendamento"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_AgendamentoToServico" ADD CONSTRAINT "_AgendamentoToServico_B_fkey" FOREIGN KEY ("B") REFERENCES "servico"("id") ON DELETE CASCADE ON UPDATE CASCADE;
63 changes: 38 additions & 25 deletions apps/backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,50 @@ datasource db {
url = env("DATABASE_URL")
}

model Usuario {
id Int @id @default(autoincrement())
nome String
email String @unique
senha String
telefone String
barbeiro Boolean
agendamentos Agendamento[]

@@map("usuario")
}

model Profissional {
id Int @id @default(autoincrement())
nome String
descricao String
imageURL String
avaliacao Float
quantidadeAvaliacoes Int
Agendamento Agendamento[]

@@map("profissionais")
id Int @id @default(autoincrement())
nome String @unique
descricao String
imagemUrl String
avaliacao Float
quantidadeAvaliacoes Int
agendamentos Agendamento[]

@@map("profissional")
}

model Servico {
id Int @id @default(autoincrement())
nome String @unique
descricao String @unique
preco Float
qtdeSlots Int
imageURL String
Agendamento Agendamento[]

@@map("servicos")
id Int @id @default(autoincrement())
nome String @unique
descricao String
preco Float
qtdeSlots Int
imagemURL String
agendamentos Agendamento[]

@@map("servico")
}

model Agendamento{
id Int @id @default(autoincrement())
emailCliente String
data DateTime @db.Timestamp(3)
profissional Profissional @relation(fields: [profissionalId], references: [id])
model Agendamento {
id Int @id @default(autoincrement())
data DateTime @db.Timestamptz(3)
profissional Profissional @relation(fields: [profissionalId], references: [id])
profissionalId Int
servicos Servico[]
servicos Servico[]
usuario Usuario @relation(fields: [usuarioId], references: [id])
usuarioId Int

@@map("agendamentos")
@@map("agendamento")
}
15 changes: 14 additions & 1 deletion apps/backend/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Profissional as PrismaProfissional,
Servico as PrismaServico,
} from 'prisma/prisma-client';
import { servicos, profissionais } from '@barba/core';
import { servicos, profissionais, Usuario } from '@barba/core';

const prisma = new PrismaClient();

Expand All @@ -12,6 +12,19 @@ async function seed() {
data: profissionais as PrismaProfissional[],
});
await prisma.servico.createMany({ data: servicos as PrismaServico[] });

const usuarios: Partial<Usuario>[] = [
{
nome: 'Marcão Machadada',
email: '[email protected]',
// senha é... #Senha123
senha: '$2b$10$9LQTRK3LRzIddKYW2C4MTelydFzk5Ys4JoROPajNqvYshhrn1PRa6',
telefone: '11999999999',
barbeiro: true,
},
];

await prisma.usuario.createMany({ data: usuarios as any });
}

seed();
43 changes: 40 additions & 3 deletions apps/backend/src/agendamento/agendamento.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { Agendamento, ObterHorariosOcupados } from '@barba/core';
import { Agendamento, ObterHorariosOcupados, Usuario } from '@barba/core';
import { AgendamentoRepository } from './agendamento.repository';
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
HttpException,
Param,
Post,
} from '@nestjs/common';
import { UsuarioLogado } from 'src/usuario/usuario.decorator';

@Controller('agendamentos')
export class AgendamentoController {
constructor(private readonly repo: AgendamentoRepository) {}

@Post()
criar(@Body() agendamento: Agendamento) {
criar(
@Body() agendamento: Agendamento,
@UsuarioLogado() usuarioLogado: Usuario,
) {
if (agendamento.usuario.id !== usuarioLogado.id) {
throw new HttpException('Usuário não autorizado', 401);
}
return this.repo.criar(agendamento);
}

Expand All @@ -24,4 +39,26 @@ export class AgendamentoController {
const casoDeUso = new ObterHorariosOcupados(this.repo);
return casoDeUso.executar(+profissional, new Date(dataParam));
}

@Get(':profissional/:data')
buscarPorProfissionalEData(
@Param('profissional') profissional: string,
@Param('data') dataParam: string,
) {
return this.repo.buscarPorProfissionalEData(
+profissional,
new Date(dataParam),
);
}

@Delete(':id')
async excluir(
@Param('id') id: string,
@UsuarioLogado() usuarioLogado: Usuario,
) {
if (!usuarioLogado.barbeiro) {
throw new HttpException('Usuário não autorizado', 401);
}
await this.repo.excluir(+id);
}
}
12 changes: 9 additions & 3 deletions apps/backend/src/agendamento/agendamento.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { Module } from '@nestjs/common';
import { MiddlewareConsumer, Module } from '@nestjs/common';
import { AgendamentoController } from './agendamento.controller';
import { AgendamentoRepository } from './agendamento.repository';
import { DbModule } from 'src/db/db.module';
import { UsuarioModule } from 'src/usuario/usuario.module';
import { UsuarioMiddleware } from 'src/usuario/usuario.middleware';

@Module({
imports: [DbModule],
imports: [DbModule, UsuarioModule],
controllers: [AgendamentoController],
providers: [AgendamentoRepository],
})
export class AgendamentoModule {}
export class AgendamentoModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(UsuarioMiddleware).forRoutes(AgendamentoController);
}
}
Loading