Skip to content

Commit

Permalink
api workflows up.1
Browse files Browse the repository at this point in the history
  • Loading branch information
zobkazi committed Apr 24, 2024
1 parent bb6f0ee commit 93f4558
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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")
);
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"
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions packages/services/auth/prisma/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"
4 changes: 2 additions & 2 deletions packages/services/auth/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum AccountStatus {
SUSPENDED
}

model User {
model AuthUser {
id String @id @default(cuid())
name String
email String @unique
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/services/auth/src/controllers/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/services/auth/src/controllers/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/services/auth/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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");
3 changes: 3 additions & 0 deletions packages/services/user/prisma/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"
1 change: 1 addition & 0 deletions packages/services/user/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

generator client {
provider = "prisma-client-js"
output = "../../../../node_modules/.prisma/client"
}

datasource db {
Expand Down
6 changes: 6 additions & 0 deletions packages/services/user/src/controllers/allUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Request, Response, NextFunction } from "express";
import { UserCreateSchema } from "@/utils/schemas";
import prisma from "@/utils/prisma";



2 changes: 1 addition & 1 deletion packages/services/user/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 93f4558

Please sign in to comment.