Skip to content

Commit

Permalink
feat: verified order status
Browse files Browse the repository at this point in the history
  • Loading branch information
mrevanzak committed Dec 4, 2023
1 parent 37e0d1c commit ac70288
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 52 deletions.
72 changes: 40 additions & 32 deletions apps/expo/src/app/(app)/order/[orderId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AnimatedImage,
BorderRadiuses,
Button,
Colors,
Spacings,
Text,
TouchableOpacity,
Expand Down Expand Up @@ -56,36 +57,18 @@ export default function OrderDetailScreen() {
});

const utils = api.useUtils();
const confirmOrder = api.order.confirmOrder.useMutation();
const onConfirmOrder = () => {
confirmOrder.mutate(
{
id: orderId as string,
},
{
onSuccess: () => void utils.order.invalidate(),
},
);
};

const confirmDelivered = api.order.confirmDelivered.useMutation();
const onConfirmDelivered = () => {
confirmDelivered.mutate(
{
id: orderId as string,
},
{
onSuccess: () => void utils.order.invalidate(),
},
);
};
const confirmOrder = api.order.confirmOrder.useMutation({
onSuccess: () => void utils.order.invalidate(),
});

const getStatus = (status: z.infer<typeof schema.orderStatusEnum>) => {
switch (status) {
case "pending":
return "Pesanan dibuat";
case "payment":
return "Pembayaran berhasil";
case "verified":
return "Pembayaran diverifikasi";
case "confirmed":
return "Pesanan dikonfirmasi";
case "shipped":
Expand All @@ -98,15 +81,35 @@ export default function OrderDetailScreen() {
};

const renderSellerAction = () => {
if (orders?.status === "payment") {
if (orders?.status === "verified") {
return (
<Button
label="Terima pesanan"
bg-secondary
primary
borderRadius={BorderRadiuses.br30}
onPress={onConfirmOrder}
/>
<View className="space-y-4">
<Button
label="Tolak pesanan"
bg-red20
outline
outlineColor={Colors.red20}
borderRadius={BorderRadiuses.br30}
onPress={() =>
confirmOrder.mutate({
id: orderId as string,
status: "cancelled",
})
}
/>
<Button
label="Terima pesanan"
bg-secondary
primary
borderRadius={BorderRadiuses.br30}
onPress={() =>
confirmOrder.mutate({
id: orderId as string,
status: "confirmed",
})
}
/>
</View>
);
}
if (orders?.status === "confirmed") {
Expand Down Expand Up @@ -300,7 +303,12 @@ export default function OrderDetailScreen() {
bg-secondary
primary
borderRadius={BorderRadiuses.br30}
onPress={onConfirmDelivered}
onPress={() =>
confirmOrder.mutate({
id: orderId as string,
status: "done",
})
}
/>
)}
{isSeller && !!+isSeller && renderSellerAction()}
Expand Down
2 changes: 2 additions & 0 deletions apps/expo/src/app/(app)/order/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default function OrdersScreen() {
return "Menunggu pembayaran";
case "payment":
return "Menunggu konfirmasi";
case "verified":
return "Pembayaran diverifikasi";
case "confirmed":
return "Menunggu pengiriman";
case "shipped":
Expand Down
25 changes: 5 additions & 20 deletions packages/api/src/router/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { addresses } from "@vivat/db/schema/addresses";
import { logOrders } from "@vivat/db/schema/log-orders";
import {
insertOrderParams,
orderConfirmParams,
orderIdSchema,
orders,
} from "@vivat/db/schema/orders";
Expand Down Expand Up @@ -98,7 +99,7 @@ export const orderRouter = createTRPCRouter({
shipping: true,
logOrders: {
orderBy: (logOrders, { asc }) => asc(logOrders.timestamp),
}
},
},
where: (order, { eq }) => eq(order.id, input.id),
})) ?? null
Expand Down Expand Up @@ -173,17 +174,17 @@ export const orderRouter = createTRPCRouter({
});
}),
confirmOrder: protectedProcedure
.input(orderIdSchema)
.input(orderConfirmParams)
.mutation(async ({ input, ctx }) => {
return await ctx.db.transaction(async (tx) => {
await tx.insert(logOrders).values({
orderId: input.id,
status: "confirmed",
status: input.status,
});
await tx
.update(orders)
.set({
status: "confirmed",
status: input.status,
})
.where(eq(orders.id, input.id));
});
Expand Down Expand Up @@ -211,20 +212,4 @@ export const orderRouter = createTRPCRouter({
});
});
}),
confirmDelivered: protectedProcedure
.input(orderIdSchema)
.mutation(async ({ input, ctx }) => {
return await ctx.db.transaction(async (tx) => {
await tx.insert(logOrders).values({
orderId: input.id,
status: "done",
});
await tx
.update(orders)
.set({
status: "done",
})
.where(eq(orders.id, input.id));
});
}),
});
1 change: 1 addition & 0 deletions packages/db/schema/log-orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const logOrders = mySqlTable("log_orders", {
status: mysqlEnum("status", [
"pending",
"payment",
"verified",
"confirmed",
"shipped",
"cancelled",
Expand Down
5 changes: 5 additions & 0 deletions packages/db/schema/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const orders = mySqlTable("orders", {
status: mysqlEnum("status", [
"pending",
"payment",
"verified",
"confirmed",
"shipped",
"cancelled",
Expand Down Expand Up @@ -67,3 +68,7 @@ export const orderStatusEnum = createSelectSchema(orders)
status: true,
})
.transform((schema) => schema.status);
export const orderConfirmParams = createSelectSchema(orders).pick({
id: true,
status: true,
});

0 comments on commit ac70288

Please sign in to comment.