Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: editOrderItem API #202

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions src/lib/actions/order-item.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { createOrderItem } from '@/lib/actions/order-item';
import { createOrderItem, editOrderItem } from '@/lib/actions/order-item';
import { fakeOrderItem } from '@/lib/fakes/order-item';
import { prismaMock } from '../../../test-setup/prisma-mock.setup';
import { fakeBook } from '@/lib/fakes/book';
import { fakeOrder } from '@/lib/fakes/order';
import { computeTax } from '@/lib/money';
import { ProductType } from '@prisma/client';
import { Order, ProductType } from '@prisma/client';

describe('order item actions', () => {
const order1 = fakeOrder();
const orderItem1 = fakeOrderItem();
const orderItem2 = fakeOrderItem();
const book1 = fakeBook();

describe('createOrderItem', () => {
it('should create order item', async () => {
prismaMock.$transaction.mockImplementation((cb) => cb(prismaMock));
prismaMock.book.findUniqueOrThrow.mockResolvedValue(book1);
prismaMock.order.findUniqueOrThrow.mockResolvedValue(order1);
prismaMock.order.findUniqueOrThrow.mockResolvedValue({
...order1,
orderItems: [orderItem1, orderItem2],
} as Order);
prismaMock.order.update.mockResolvedValue(order1);
orderItem1.orderId = order1.id;
orderItem1.bookId = book1.id;
Expand All @@ -42,7 +46,7 @@ describe('order item actions', () => {
});

const subTotalInCents =
order1.subTotalInCents + orderItem1.totalPriceInCents;
orderItem1.totalPriceInCents + orderItem2.totalPriceInCents;
const taxInCents = computeTax(subTotalInCents);
const totalInCents = subTotalInCents + taxInCents;
expect(prismaMock.order.update).toHaveBeenCalledWith({
Expand All @@ -69,4 +73,56 @@ describe('order item actions', () => {
);
});
});

describe('editOrderItem', () => {
it('should edit an order item', async () => {
prismaMock.$transaction.mockImplementation((cb) => cb(prismaMock));
prismaMock.order.findUniqueOrThrow.mockResolvedValue({
...order1,
orderItems: [
orderItem2,
{
...orderItem1,
productPriceInCents: 10,
quantity: 2,
totalPriceInCents: 20,
},
],
} as Order);
orderItem1.orderId = order1.id;
prismaMock.orderItem.update.mockResolvedValue(orderItem1);

const result = await editOrderItem({
orderItemId: order1.id,
orderItemUpdate: {
productPriceInCents: 10,
quantity: 2,
totalPriceInCents: 20,
},
});

expect(prismaMock.orderItem.update).toHaveBeenCalledWith({
data: {
productPriceInCents: 10,
quantity: 2,
totalPriceInCents: 20,
},
where: { id: order1.id },
});

const subTotalInCents = 20 + orderItem2.totalPriceInCents;
const taxInCents = computeTax(subTotalInCents);
const totalInCents = subTotalInCents + taxInCents;
expect(prismaMock.order.update).toHaveBeenCalledWith({
data: {
subTotalInCents,
taxInCents,
totalInCents,
},
where: { id: order1.id },
});

expect(result).toEqual(orderItem1);
});
});
});
34 changes: 33 additions & 1 deletion src/lib/actions/order-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { recomputeOrderTotals } from '@/lib/actions/order';
import logger from '@/lib/logger';
import prisma from '@/lib/prisma';
import OrderItemCreateInput from '@/types/OrderItemCreateInput';
import OrderItemUpdateInput from '@/types/OrderItemUpdateInput';
import { OrderItem, Prisma, ProductType } from '@prisma/client';

export async function createOrderItem(
Expand Down Expand Up @@ -41,7 +42,7 @@ export async function createOrderItem(

logger.trace('created order item in DB: %j', createdOrderItem);

await recomputeOrderTotals({ orderItem: createdOrderItem, tx });
await recomputeOrderTotals({ orderId: createdOrderItem.orderId, tx });

return createdOrderItem;
},
Expand All @@ -50,3 +51,34 @@ export async function createOrderItem(
},
);
}

export async function editOrderItem({
orderItemId,
orderItemUpdate,
}: {
orderItemId: OrderItem['id'];
orderItemUpdate: OrderItemUpdateInput;
}): Promise<OrderItem> {
return prisma.$transaction(
async (tx) => {
const { productPriceInCents, quantity, totalPriceInCents } =
orderItemUpdate;

const orderItem = await tx.orderItem.update({
data: {
productPriceInCents,
quantity,
totalPriceInCents,
},
where: { id: orderItemId },
});

await recomputeOrderTotals({ orderId: orderItem.orderId, tx });

return orderItem;
},
{
isolationLevel: Prisma.TransactionIsolationLevel.Serializable,
},
);
}
22 changes: 10 additions & 12 deletions src/lib/actions/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ import OrderHydrated from '@/types/OrderHydrated';
import OrderWithItemsHydrated from '@/types/OrderWithItemsHydrated';
import PageInfo from '@/types/PageInfo';
import PaginationQuery from '@/types/PaginationQuery';
import {
Order,
OrderItem,
OrderState,
Prisma,
ProductType,
} from '@prisma/client';
import { Order, OrderState, Prisma, ProductType } from '@prisma/client';
import { format } from 'date-fns';

export async function createOrder(): Promise<OrderWithItemsHydrated> {
Expand Down Expand Up @@ -50,19 +44,23 @@ export async function createOrder(): Promise<OrderWithItemsHydrated> {
}

export async function recomputeOrderTotals({
orderItem,
orderId,
tx,
}: {
orderItem: OrderItem;
orderId: Order['id'];
tx: Prisma.TransactionClient;
}): Promise<void> {
const { orderId } = orderItem;

const order = await tx.order.findUniqueOrThrow({
include: {
orderItems: true,
},
where: { id: orderId },
});

const subTotalInCents = order.subTotalInCents + orderItem.totalPriceInCents;
const subTotalInCents = order.orderItems.reduce(
(total, item) => total + item.totalPriceInCents,
0,
);
const taxInCents = computeTax(subTotalInCents);
const totalInCents = subTotalInCents + taxInCents;

Expand Down
7 changes: 7 additions & 0 deletions src/types/OrderItemUpdateInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { OrderItem } from '@prisma/client';

type OrderItemUpdateInput = Pick<
OrderItem,
'productPriceInCents' | 'quantity' | 'totalPriceInCents'
>;
export default OrderItemUpdateInput;