#TS errors in nested writes

3 messages · Page 1 of 1 (latest)

opaque knoll
#

Hi, I'm having some TS errors in nested writes that I can't figure how to resolve.

Here is the function :

const savePayment = await prisma.payments.create({
        data: {
            id: responseObj.id,
            userId: responseObj.customer.external_id,
            orderId: 1,
            amount: responseObj.amount,
            // on this nested write, I'd like to connect only to records from my bank
            // response code table where all the codes and description has been already
            //set. But a simple connect throws an error, so I tried to be smart and
            // create a null code
            responseCode: {
                connectOrCreate: {
                    where: {
                        code: responseObj.response ? responseObj.response : "000",
                    },
                    create: {
                        code: "000",
                        description: "Null",
                    }
                }
            },
            status: responseObj.status ? responseObj.status as Status : Status.NULL,
            createdAt: date,
            // on this nested write, I create a card unless it already exists in the db
            card: {
                connectOrCreate: {
                    where: {
                        id: responseObj.card.id,
                    },
                    create: {
                    id: responseObj.card.id,
                    last4: responseObj.card.last4,
                    brand: responseObj.card.brand,
                    expMonth: responseObj.card.exp_month,
                    expYear: responseObj.card.exp_year,
                    name: responseObj.card.name,
                    userId: responseObj.customer.external_id,
                    createdAt: date,
                    }
                },
            }
        },
        include: {
            responseCode: true,
            cardUsed: true
        
        }
    });

more below...

#

Here is my schema :

model Payments {
  id             String           @id
  userId         String
  orderId        Int
  amount         Int
  status         Status?
  cardId         String
  responseCodeId Int
  createdAt      DateTime
  updatedAt      DateTime         @updatedAt
  cardUsed       Cards            @relation(fields: [cardId], references: [id])
  order          Orders           @relation(fields: [orderId], references: [id])
  responseCode   BankResponseCode @relation(fields: [responseCodeId], references: [id])
  user           User             @relation(fields: [userId], references: [id])

  @@index([cardId], map: "Payments_cardId_fkey")
  @@index([orderId], map: "Payments_orderId_fkey")
  @@index([responseCodeId], map: "Payments_responseCodeId_fkey")
  @@index([userId], map: "Payments_userId_fkey")
}
model Cards {
  id                 String     @id
  last4              String
  brand              String
  expMonth           Int
  expYear            Int
  name               String
  userId             String
  createdAt          DateTime
  user               User       @relation(fields: [userId], references: [id])
  associatedPayments Payments[]

  @@index([userId], map: "Cards_userId_fkey")
}
model BankResponseCode {
  id                   Int        @id @default(autoincrement())
  code                 String     @unique
  description          String
  paymentsWithThisCode Payments[]
}

2/3

#

Finally, here is the TS errors :

Type '{
  id: string;
  userId: string;
  orderId: number;
  amount: number;
  responseCode: { 
    connectOrCreate:
      {
        where: { code: string; };
        create: { code: string; description: string; }; }; };
  status: $Enums.Status;
  createdAt: Date; card: { ...; }; }'

is not assignable to type '(Without<PaymentsCreateInput, PaymentsUncheckedCreateInput> & PaymentsUncheckedCreateInput) | (Without<...> & PaymentsCreateInput)'.
  Types of property 'responseCode' are incompatible.
    Type '{ connectOrCreate: { where: { code: string; }; create: { code: string; description: string; }; }; }' is not assignable to type 'undefined'.ts(2322)
index.d.ts(7801, 5): The expected type comes from property 'data' which is declared here on type '{ select?: PaymentsSelect<DefaultArgs> | null | undefined; include?: PaymentsInclude<DefaultArgs> | null | undefined; data: (Without<...> & PaymentsUncheckedCreateInput) | (Without<...> & PaymentsCreateInput); relationLoadStrategy?: "query" | undefined; }'
(property) data: (Prisma.Without<Prisma.PaymentsCreateInput, Prisma.PaymentsUncheckedCreateInput> & Prisma.PaymentsUncheckedCreateInput) | (Prisma.Without<Prisma.PaymentsUncheckedCreateInput, Prisma.PaymentsCreateInput> & Prisma.PaymentsCreateInput)

3/3