Skip to content

Commit

Permalink
Remove from stock when order is placed
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Romero committed Nov 26, 2023
1 parent 8e224fd commit 35f4be0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 28 deletions.
16 changes: 13 additions & 3 deletions apps/expo/src/screens/cart-list.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useAtom, useAtomValue } from "jotai";
import { cartAtom, cartTotalAtom } from "../atoms/cart";
import CartItem from "../components/CartItem";
import { Image, Pressable, Text, View } from "react-native";
import { ActivityIndicator, Image, Pressable, Text, View } from "react-native";
import { FlatList } from "react-native-gesture-handler";
import { useMemo } from "react";
import { HomeTabScreenProps } from "../types/navigation";
Expand Down Expand Up @@ -60,6 +60,7 @@ function CartHeader({
const total = useAtomValue(cartTotalAtom);
const [cart, setCart] = useAtom(cartAtom);
const { mutate, isLoading } = trpc.order.create.useMutation();
const trpcUtils = trpc.useContext();

const onCreateOrder = () => {
const cartEntries = Object.entries(cart).map(([key, value]) => ({
Expand All @@ -74,6 +75,7 @@ function CartHeader({
};
mutate(input, {
onSuccess: () => {
trpcUtils.item.all.invalidate();
setCart({});
},
});
Expand All @@ -87,12 +89,20 @@ function CartHeader({
{total.toFixed(2)}
</Text>
<Pressable
className="rounded bg-emerald-500 p-1.5 active:bg-emerald-600"
className="w-18 rounded bg-emerald-500 p-2 active:bg-emerald-600"
onPress={!isSignedIn ? () => navigation.push("Sign In") : onCreateOrder}
disabled={isLoading}
>
<Text className="text-white">
{isSignedIn ? "Create Order" : "Log In to Buy"}
{isLoading ? (
<ActivityIndicator
color={"white"}
className="w-18 px-8"
size={"small"}
/>
) : (
<>{isSignedIn ? "Create Order" : "Log In to Buy"}</>
)}
</Text>
</Pressable>
</View>
Expand Down
76 changes: 51 additions & 25 deletions packages/api/src/router/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,60 @@ export const orderRouter = router({
}),
)
.mutation(async ({ ctx, input }) => {
const itemIds = input.items.map((item) => item.id);
const items = await ctx.prisma.items.findMany({
where: {
id: { in: itemIds },
},
});
return ctx.prisma.$transaction(async (tx) => {
const itemIds = input.items.map((item) => item.id);
const items = await tx.items.findMany({
where: {
id: { in: itemIds },
},
});

const q: Record<number, number> = {};
input.items.forEach((item) => {
q[item.id] = item.quantity;
});
const q: Record<number, number> = {};
input.items.forEach((item) => {
q[item.id] = item.quantity;
});

return await ctx.prisma.orders.create({
data: {
status: "Placed",
total: input.total,
user_id: ctx.auth.userId,
orderitems: {
create: items.map((item) => ({
name: item.name,
price: item.price,
quantity: q[item.id] ?? 0,
})),
const order = await tx.orders.create({
data: {
status: "Placed",
total: input.total,
user_id: ctx.auth.userId,
orderitems: {
create: items.map((item) => ({
itemId: item.id,
name: item.name,
price: item.price,
quantity: q[item.id] ?? 0,
})),
},
},
},
include: {
orderitems: true,
},
include: {
orderitems: true,
},
});

const updatePromises = order.orderitems.map((orderItem) => {
const itemId = orderItem.itemId ?? -1;
const item = items.find((item) => item.id === orderItem.itemId);

return tx.items.update({
where: {
id: itemId,
},
data: {
stock:
(item?.stock.toNumber() ?? 0) - orderItem.quantity.toNumber(),
},
});
});

await Promise.all(updatePromises);

return order;
});
}),

allMyOrders: protectedProcedure.query(({ ctx }) => {
return ctx.prisma.orders.findMany();
}),
});

0 comments on commit 35f4be0

Please sign in to comment.