model Product {
productId Int @id @default(autoincrement())
name String
description String?
price Int
status ProductStatusType @default(ACTIVE)
createdAt DateTime @default(now())
storeId Int
store Store @relation(fields: [storeId], references: [storeId])
payments PaymentItem[]
}
enum ProductStatusType {
ACTIVE
DELETED
}
enum PaymentStatusType {
PENDING
SUCCESS
FAILED
}
model Payment {
paymentId Int @id @default(autoincrement())
storeId Int
store Store @relation(fields: [storeId], references: [storeId])
clientName String
clientEmail String
coupon String @default("")
value Int
status PaymentStatusType
createdAt DateTime @default(now())
paymentItems PaymentItem[]
}
model PaymentItem {
paymentId Int
productId Int
quantity Int
price Int
payment Payment @relation(fields: [paymentId], references: [paymentId])
product Product @relation(fields: [productId], references: [productId])
@@unique([paymentId, productId])
}
I have these 3 tables and I'm having difficulty getting the 5 best-selling products in a store.
Returning the product name, total product sales and total value.
I've already tried using findMany but it won't let me use _sum to add up the value of products in payments.
Can anyone help me tell which path I should follow?