Payment

In the Payment logic, in the controller, we should have the route payOrder(), as usual.

@Post(':id')
payOrder(@Param() { id }: IdDto) {
  return this.paymentsService.payOrder(id);
}

And in the service, this is how the method would be. Note that, as we don't need a reference to the order, due to the way prisma udpates the entity, the implementation becomes a bit cleaner. Also note that we create a payment as an empty object, as its data will be generated automatically.

async payOrder(id: number) {
  const { payment } = await this.prisma.order.findUniqueOrThrow({
    where: { id },
    include: { payment: true },
  });
  if (payment) {
    throw new ConflictException('Order already paid');
  }

  return this.prisma.order.update({
    where: { id },
    data: {
      status: OrderStatus.AWAITING_SHIPMENT,
      payment: { create: {} },
    },
  });
}

This is a case we can notice the Prisma type safety in action. If the order wasn't fetched with its payment, it wouldn't be available for extraction.

Commit - Implementing payment logic

Last updated