From 93f455885688b9ae6a8d3b3355e6a68b024b7a9f Mon Sep 17 00:00:00 2001 From: zobkazi Date: Wed, 24 Apr 2024 20:53:39 +0600 Subject: [PATCH] api workflows up.1 --- .../20240423194911_init/migration.sql | 51 +++++++++++++++++++ .../prisma/migrations/migration_lock.toml | 3 ++ .../20240423201305_init/migration.sql | 40 +++++++++++++++ .../prisma/migrations/migration_lock.toml | 3 ++ packages/services/auth/prisma/schema.prisma | 4 +- .../services/auth/src/controllers/login.ts | 2 +- .../services/auth/src/controllers/register.ts | 4 +- packages/services/auth/src/routes/index.ts | 4 +- .../20240423194747_init/migration.sql | 25 +++++++++ .../prisma/migrations/migration_lock.toml | 3 ++ packages/services/user/prisma/schema.prisma | 1 + .../services/user/src/controllers/allUsers.ts | 6 +++ packages/services/user/tsconfig.json | 2 +- 13 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 packages/services/adoption/prisma/migrations/20240423194911_init/migration.sql create mode 100644 packages/services/adoption/prisma/migrations/migration_lock.toml create mode 100644 packages/services/auth/prisma/migrations/20240423201305_init/migration.sql create mode 100644 packages/services/auth/prisma/migrations/migration_lock.toml create mode 100644 packages/services/user/prisma/migrations/20240423194747_init/migration.sql create mode 100644 packages/services/user/prisma/migrations/migration_lock.toml create mode 100644 packages/services/user/src/controllers/allUsers.ts diff --git a/packages/services/adoption/prisma/migrations/20240423194911_init/migration.sql b/packages/services/adoption/prisma/migrations/20240423194911_init/migration.sql new file mode 100644 index 0000000..9fe1352 --- /dev/null +++ b/packages/services/adoption/prisma/migrations/20240423194911_init/migration.sql @@ -0,0 +1,51 @@ +-- CreateEnum +CREATE TYPE "PetGender" AS ENUM ('MALE', 'FEMALE'); + +-- CreateEnum +CREATE TYPE "PetCategory" AS ENUM ('DOG', 'CAT', 'BIRD'); + +-- CreateEnum +CREATE TYPE "PetSize" AS ENUM ('SMALL', 'MEDIUM', 'LARGE', 'EX_LARGE'); + +-- CreateEnum +CREATE TYPE "PetColor" AS ENUM ('WHITE', 'BLACK', 'BROWN', 'GRAY'); + +-- CreateEnum +CREATE TYPE "PetBreed" AS ENUM ('HUSKY', 'BEAGLE', 'DOGOT', 'LABRADO', 'POMERANIAN'); + +-- CreateTable +CREATE TABLE "Pet" ( + "id" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "name" TEXT NOT NULL, + "breed" "PetBreed" NOT NULL DEFAULT 'HUSKY', + "age" DOUBLE PRECISION NOT NULL DEFAULT 2, + "color" "PetColor" NOT NULL DEFAULT 'BLACK', + "size" "PetSize" NOT NULL DEFAULT 'MEDIUM', + "description" TEXT NOT NULL, + "image" TEXT, + "category" "PetCategory" NOT NULL DEFAULT 'DOG', + "gender" "PetGender" NOT NULL DEFAULT 'MALE', + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "Pet_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "PetAdoption" ( + "id" TEXT NOT NULL, + "petId" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "description" TEXT, + "status" TEXT, + "size" "PetSize" NOT NULL DEFAULT 'MEDIUM', + "color" "PetColor" NOT NULL DEFAULT 'BLACK', + "breed" "PetBreed" NOT NULL DEFAULT 'HUSKY', + "age" DOUBLE PRECISION NOT NULL DEFAULT 2, + "category" "PetCategory" NOT NULL DEFAULT 'DOG', + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "PetAdoption_pkey" PRIMARY KEY ("id") +); diff --git a/packages/services/adoption/prisma/migrations/migration_lock.toml b/packages/services/adoption/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..fbffa92 --- /dev/null +++ b/packages/services/adoption/prisma/migrations/migration_lock.toml @@ -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" \ No newline at end of file diff --git a/packages/services/auth/prisma/migrations/20240423201305_init/migration.sql b/packages/services/auth/prisma/migrations/20240423201305_init/migration.sql new file mode 100644 index 0000000..d017c66 --- /dev/null +++ b/packages/services/auth/prisma/migrations/20240423201305_init/migration.sql @@ -0,0 +1,40 @@ +-- CreateEnum +CREATE TYPE "Role" AS ENUM ('USER', 'ADMIN'); + +-- CreateEnum +CREATE TYPE "AccountStatus" AS ENUM ('PENDING', 'ACTIVE', 'INACTIVE', 'SUSPENDED'); + +-- CreateEnum +CREATE TYPE "LoginAttempt" AS ENUM ('SUCCESS', 'FAILED'); + +-- CreateTable +CREATE TABLE "AuthUser" ( + "id" TEXT NOT NULL, + "name" TEXT NOT NULL, + "email" TEXT NOT NULL, + "password" TEXT NOT NULL, + "role" "Role" NOT NULL DEFAULT 'USER', + "verified" BOOLEAN NOT NULL DEFAULT false, + "status" "AccountStatus" NOT NULL DEFAULT 'PENDING', + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "AuthUser_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "LoginHistory" ( + "id" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "ip" TEXT, + "userAgent" TEXT, + "attempt" "LoginAttempt" NOT NULL DEFAULT 'SUCCESS', + + CONSTRAINT "LoginHistory_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "AuthUser_email_key" ON "AuthUser"("email"); + +-- AddForeignKey +ALTER TABLE "LoginHistory" ADD CONSTRAINT "LoginHistory_userId_fkey" FOREIGN KEY ("userId") REFERENCES "AuthUser"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/packages/services/auth/prisma/migrations/migration_lock.toml b/packages/services/auth/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..fbffa92 --- /dev/null +++ b/packages/services/auth/prisma/migrations/migration_lock.toml @@ -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" \ No newline at end of file diff --git a/packages/services/auth/prisma/schema.prisma b/packages/services/auth/prisma/schema.prisma index 9a66098..3b63a67 100644 --- a/packages/services/auth/prisma/schema.prisma +++ b/packages/services/auth/prisma/schema.prisma @@ -25,7 +25,7 @@ enum AccountStatus { SUSPENDED } -model User { +model AuthUser { id String @id @default(cuid()) name String email String @unique @@ -46,7 +46,7 @@ enum LoginAttempt { model LoginHistory { id String @id @default(cuid()) userId String - user User @relation(fields: [userId], references: [id], onDelete: Cascade) + user AuthUser @relation(fields: [userId], references: [id], onDelete: Cascade) ip String? userAgent String? attempt LoginAttempt @default(SUCCESS) diff --git a/packages/services/auth/src/controllers/login.ts b/packages/services/auth/src/controllers/login.ts index a72ba8e..3754fce 100644 --- a/packages/services/auth/src/controllers/login.ts +++ b/packages/services/auth/src/controllers/login.ts @@ -44,7 +44,7 @@ const UserLogin = async (req: Request, res: Response, next: NextFunction) => { const userAgent = (req.headers["user-agent"] as string) || ""; // check if user exists - const user = await prisma.user.findUnique({ + const user = await prisma.authUser.findUnique({ where: { email: parsedBody.data.email, }, diff --git a/packages/services/auth/src/controllers/register.ts b/packages/services/auth/src/controllers/register.ts index 22583f8..fdf31cb 100644 --- a/packages/services/auth/src/controllers/register.ts +++ b/packages/services/auth/src/controllers/register.ts @@ -19,7 +19,7 @@ const UserRegister = async ( } // check if user already exists\\ - const existingUser = await prisma.user.findUnique({ + const existingUser = await prisma.authUser.findUnique({ where: { email: parsedBody.data.email, }, @@ -34,7 +34,7 @@ const UserRegister = async ( // create auth user - const user = await prisma.user.create({ + const user = await prisma.authUser.create({ data: { ...parsedBody.data, password: hashedPassword, diff --git a/packages/services/auth/src/routes/index.ts b/packages/services/auth/src/routes/index.ts index 42fe9be..68fd706 100644 --- a/packages/services/auth/src/routes/index.ts +++ b/packages/services/auth/src/routes/index.ts @@ -1,8 +1,8 @@ import express from "express"; const router = express.Router(); -import { UserRegistration, UserLogin, authToken } from "@/controllers"; +import { UserRegister, UserLogin, authToken } from "@/controllers"; -router.post("/registration", UserRegistration); +router.post("/registration", UserRegister); router.post("/login", authToken, UserLogin); router.post("/auth-token", authToken); diff --git a/packages/services/user/prisma/migrations/20240423194747_init/migration.sql b/packages/services/user/prisma/migrations/20240423194747_init/migration.sql new file mode 100644 index 0000000..94ea48b --- /dev/null +++ b/packages/services/user/prisma/migrations/20240423194747_init/migration.sql @@ -0,0 +1,25 @@ +-- CreateTable +CREATE TABLE "User" ( + "id" TEXT NOT NULL, + "authUserId" TEXT NOT NULL, + "name" TEXT NOT NULL, + "email" TEXT NOT NULL, + "phoneNumber" TEXT, + "address" TEXT NOT NULL DEFAULT 'Gazipur, Dhaka', + "city" TEXT NOT NULL DEFAULT 'Dhaka', + "state" TEXT NOT NULL DEFAULT 'Dhaka', + "country" TEXT NOT NULL DEFAULT 'Bangladesh', + "postalCode" TEXT NOT NULL DEFAULT '1000', + "isActive" BOOLEAN NOT NULL DEFAULT true, + "isDeleted" BOOLEAN NOT NULL DEFAULT false, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "User_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "User_authUserId_key" ON "User"("authUserId"); + +-- CreateIndex +CREATE INDEX "User_authUserId_idx" ON "User"("authUserId"); diff --git a/packages/services/user/prisma/migrations/migration_lock.toml b/packages/services/user/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..fbffa92 --- /dev/null +++ b/packages/services/user/prisma/migrations/migration_lock.toml @@ -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" \ No newline at end of file diff --git a/packages/services/user/prisma/schema.prisma b/packages/services/user/prisma/schema.prisma index eda44a6..869fd28 100644 --- a/packages/services/user/prisma/schema.prisma +++ b/packages/services/user/prisma/schema.prisma @@ -6,6 +6,7 @@ generator client { provider = "prisma-client-js" + output = "../../../../node_modules/.prisma/client" } datasource db { diff --git a/packages/services/user/src/controllers/allUsers.ts b/packages/services/user/src/controllers/allUsers.ts new file mode 100644 index 0000000..812e654 --- /dev/null +++ b/packages/services/user/src/controllers/allUsers.ts @@ -0,0 +1,6 @@ +import { Request, Response, NextFunction } from "express"; +import { UserCreateSchema } from "@/utils/schemas"; +import prisma from "@/utils/prisma"; + + + diff --git a/packages/services/user/tsconfig.json b/packages/services/user/tsconfig.json index 2b41d87..96f79b9 100644 --- a/packages/services/user/tsconfig.json +++ b/packages/services/user/tsconfig.json @@ -14,7 +14,7 @@ "moduleResolution": "node", "resolveJsonModule": true, "allowSyntheticDefaultImports": true, - "typeRoots": ["./src/types", "../../../node_modules/@types"], + "typeRoots": ["./src/types", "../../../"], "sourceMap": true, "types": ["node", "express"], "noImplicitAny": false,