Commit 253f5929 authored by ThinhNC's avatar ThinhNC

refactor(prisma): improve wallet management schema

parent 85e41667
......@@ -13,31 +13,32 @@ enum TransactionType {
}
model User {
id String @id @default(uuid()) @db.Uuid
email String? @unique
id String @id @default(uuid()) @db.Uuid
email String? @unique
password String?
fullName String? @map("full_name")
avatarUrl String? @map("avatar_url")
phoneNumber String? @unique @map("phone_number")
fullName String? @map("full_name")
avatarUrl String? @map("avatar_url")
phoneNumber String? @unique @map("phone_number")
isActive Boolean @default(false) @map("is_active")
deletedAt DateTime? @map("deleted_at")
deletedBy String? @map("deleted_by") @db.Uuid
roleId String @map("role_id") @db.Uuid
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
// Relations
role Role @relation(fields: [roleId], references: [id], onDelete: Cascade)
wallets Wallet[]
categories Category[]
transactions Transaction[]
budgets Budget[]
refreshTokens RefreshToken[]
socialAccounts UserSocial[]
verificationTokens VerificationToken[]
role Role @relation(fields: [roleId], references: [id], onDelete: Restrict)
wallets Wallet[]
categories Category[]
transactions Transaction[]
budgets Budget[]
refreshTokens RefreshToken[]
socialAccounts UserSocial[]
verificationTokens VerificationToken[]
passwordResetTokens PasswordResetToken[]
devices UserDevice[]
devices UserDevice[]
@@index([roleId])
@@map("users")
}
......@@ -52,33 +53,47 @@ model Role {
}
model Wallet {
id String @id @default(uuid()) @db.Uuid
userId String @map("user_id") @db.Uuid
name String
balance Float
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
id String @id @default(uuid()) @db.Uuid
userId String @map("user_id") @db.Uuid
name String
balance Decimal @default(0) @db.Decimal(18, 2)
currency String @default("VND") @db.VarChar(3)
icon String?
color String?
description String?
isDefault Boolean @default(false) @map("is_default")
isArchived Boolean @default(false) @map("is_archived")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
transactions Transaction[]
@@unique([userId, name])
@@index([userId])
@@map("wallets")
}
model Category {
id String @id @default(uuid()) @db.Uuid
userId String @map("user_id") @db.Uuid
parentId String? @map("parent_id") @db.Uuid
name String
type TransactionType
icon String?
color String?
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
parent Category? @relation("CategoryHierarchy", fields: [parentId], references: [id], onDelete: SetNull)
children Category[] @relation("CategoryHierarchy")
transactions Transaction[]
budgets Budget[]
@@unique([userId, name, type])
@@index([userId])
@@index([parentId])
@@map("categories")
}
......@@ -87,17 +102,25 @@ model Transaction {
userId String @map("user_id") @db.Uuid
walletId String @map("wallet_id") @db.Uuid
categoryId String @map("category_id") @db.Uuid
amount Float
amount Decimal @db.Decimal(18, 2)
type TransactionType
description String?
receiptUrl String? @map("receipt_url")
location String?
date DateTime
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
wallet Wallet @relation(fields: [walletId], references: [id], onDelete: Cascade)
wallet Wallet @relation(fields: [walletId], references: [id], onDelete: Restrict)
category Category @relation(fields: [categoryId], references: [id], onDelete: Restrict)
@@index([userId])
@@index([walletId])
@@index([categoryId])
@@index([date])
@@index([userId, date])
@@index([userId, type])
@@map("transactions")
}
......@@ -105,16 +128,17 @@ model Budget {
id String @id @default(uuid()) @db.Uuid
userId String @map("user_id") @db.Uuid
categoryId String @map("category_id") @db.Uuid
name String
amount Float
amount Decimal @db.Decimal(18, 2)
startDate DateTime @map("start_date")
endDate DateTime @map("end_date")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
category Category @relation(fields: [categoryId], references: [id], onDelete: Cascade)
category Category @relation(fields: [categoryId], references: [id], onDelete: Restrict)
@@index([categoryId])
@@index([userId, startDate, endDate])
@@map("budgets")
}
......@@ -136,13 +160,14 @@ model RefreshToken {
model UserSocial {
id String @id @default(uuid()) @db.Uuid
userId String @map("user_id") @db.Uuid
provider String // "ZALO", "GOOGLE", etc.
provider String // "ZALO", "GOOGLE", etc.
providerUserId String @map("provider_user_id")
createdAt DateTime @default(now()) @map("created_at")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerUserId])
@@index([userId])
@@map("user_socials")
}
......@@ -155,6 +180,7 @@ model VerificationToken {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
@@map("verification_tokens")
}
......@@ -167,6 +193,7 @@ model PasswordResetToken {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
@@map("password_reset_tokens")
}
......@@ -179,10 +206,8 @@ model UserDevice {
createdAt DateTime @default(now()) @map("created_at")
lastLoginAt DateTime @default(now()) @map("last_login_at")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([userId, deviceHash])
@@map("user_devices")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment