Commit 253f5929 authored by ThinhNC's avatar ThinhNC

refactor(prisma): improve wallet management schema

parent 85e41667
......@@ -27,7 +27,7 @@ model User {
updatedAt DateTime @updatedAt @map("updated_at")
// Relations
role Role @relation(fields: [roleId], references: [id], onDelete: Cascade)
role Role @relation(fields: [roleId], references: [id], onDelete: Restrict)
wallets Wallet[]
categories Category[]
transactions Transaction[]
......@@ -38,6 +38,7 @@ model User {
passwordResetTokens PasswordResetToken[]
devices UserDevice[]
@@index([roleId])
@@map("users")
}
......@@ -55,7 +56,13 @@ model Wallet {
id String @id @default(uuid()) @db.Uuid
userId String @map("user_id") @db.Uuid
name String
balance Float
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")
......@@ -63,22 +70,30 @@ model Wallet {
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")
}
......@@ -143,6 +167,7 @@ model UserSocial {
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")
}
......@@ -184,5 +211,3 @@ model UserDevice {
@@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