@prisma/client#AddressCreateManyWithoutCustomerInput TypeScript Examples

The following examples show how to use @prisma/client#AddressCreateManyWithoutCustomerInput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: createCustomer.ts    From mayoor with MIT License 6 votes vote down vote up
CreateCustomer = mutationField("createCustomer", {
  type: "Customer",
  args: {
    input: arg({ type: CreateCustomerInput, nullable: false }),
  },
  resolve: async (_, { input }, ctx) => {
    const user = await ctx.user.getCurrentUser();

    const { addresses, allowedBankPayments, ...otherArgs } = input;
    const primaryAddresses = addresses?.filter((address) => address.isPrimary);

    if (primaryAddresses?.length && primaryAddresses.length > 1) {
      throw new UserInputError("Only one address can be primary.");
    }

    const newAddresses: AddressCreateManyWithoutCustomerInput = {
      create: addresses?.map((addressInput) => ({
        ...addressInput,
        isPrimary: addressInput.isPrimary ?? undefined,
        createdBy: { connect: { id: user.id } },
      })),
    };

    return ctx.prisma.customer.create({
      data: {
        ...otherArgs,
        allowedBankPayments: allowedBankPayments ?? undefined,
        address: newAddresses,
        createdBy: {
          connect: {
            id: user.id,
          },
        },
      },
    });
  },
})