Prisma: How to findmany with condition and groupby

37 Views Asked by At

First of all, I apologize for my poor english. actually i'm facing problem to query in prisma i have two relation table one is for invoice and another was for test

Invoice Table

id refName
1 Dr. foo
2 Dr. fa
3 Dr. foo

Test Table

id invoice_id test_name test_price
1 1 test(A) 200
2 1 test(B) 30
3 1 test(C) 50
4 2 test(A) 200
5 2 test(C) 50
6 3 test(B) 30
7 3 test(c) 50

So here is what I want: (i don't want this based on signle invoice, i want to get all)

[
   { 
     refName: Dr.foo,
     test: {
       test(A): 1,
       test(B): 2,
       test(C): 2,
     }
     total: 280,
   },
   { 
     refName: Dr.fa,
     test: {
       test(A): 0,
       test(B): 1,
       test(C): 1,
     }
     total: 80,
   }
]

here is my prisma Schema:

model Invoice {
  id             Int      @id @default(autoincrement())
  invoice_date   DateTime @db.Date  
  ref_name       String
  createdAt      DateTime @default(now()) @db.Timestamp(0)
  updatedAt      DateTime @default(now()) @updatedAt @db.Timestamp(0)
  
  test   Test[]
}

model Test {
  id             Int      @id @default(autoincrement())
  invoice_id     Int
  test_name      String
  test_cost      Int
  createdAt      DateTime @default(now()) @db.Timestamp(0)
  updatedAt      DateTime @default(now()) @updatedAt @db.Timestamp(0)

  invoice Invoice @relation(fields: [invoice_id], references: [id], onDelete: Restrict, onUpdate: Cascade)
}

So my question is there a way Prisma can do the trick and get me this?

0

There are 0 best solutions below