Skip to content

Commit

Permalink
revert(cms): Revert Order API Sync (#153)
Browse files Browse the repository at this point in the history
* revert(cms): revert "refactor!(cms): Sync Order schema from package/types (#142)"

This reverts commit bb51b86.

* refactor(cms): manually sync orders api with Orders & OrderItem from merch

* chore: remove unused import of media
  • Loading branch information
mrzzy authored May 7, 2024
1 parent 4ee4bc6 commit 10080ff
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 122 deletions.
100 changes: 96 additions & 4 deletions apps/cms/src/collections/Orders.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { CollectionConfig } from "payload/types";
import { OrderSchema } from "types";
import { toPayloadZod } from "../utilities/zodInteropt";


/** Orders collection stores merch orders from users. */
const Orders: CollectionConfig = {
Expand All @@ -16,6 +13,101 @@ const Orders: CollectionConfig = {
],
description: "Merchandise orders from users.",
},
fields: toPayloadZod(OrderSchema),
fields: [
// by default, payload generates an 'id' field each order automatically
// order items
{
name: "items",
type: "array",
fields: [
// by default, payload generates an 'id' field each order automatically
{
name: "name",
type: "text",
required: true,
},
{
name: "image",
type: "text",
},
{
name: "color",
type: "text",
required: true,
},
{
name: "size",
type: "text",
required: true,
},
{
name: "price",
type: "number",
required: true,
},
{
name: "quantity",
type: "number",
required: true,
},
],
// direct paylaod to generate a OrderItem type
interfaceName: "OrderItem",
// validate: orders should not be empty
minRows: 1,
},
{
name: "transaction_id",
label: "Transaction ID",
admin: {
description: "Transaction ID provided by Payment Gateway",
},
type: "text",
required: true,
},
{
name: "transaction_time",
label: "Transaction Time",
type: "date",
admin: {
date: {
pickerAppearance: "dayAndTime",
},
},
required: true,
},
{
name: "payment_method",
label: "Payment Method",
type: "text",
required: true,
},
{
name: "customerEmail",
label: "Customer Email",
type: "email",
required: true,
},
{
name: "status",
label: "Order Status",
type: "select",
options: [
{
value: "pending",
label: "Pending Payment",
},
{
value: "paid",
label: "Payment Completed",
},
{
value: "delivered",
label: "Order Completed",
},
],
required: true,
},
],
};
export default Orders;
81 changes: 0 additions & 81 deletions apps/cms/src/utilities/zodInteropt.ts

This file was deleted.

3 changes: 1 addition & 2 deletions apps/cms/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
"jsx": "react",
"allowSyntheticDefaultImports": true,
"paths": {
// Ensure this matches the path to your typescript outputFile
"payload/generated-types": [
"../../packages/types/src/index.ts"
"../../packages/types/lib/cms.ts" // Ensure this matches the path to your typescript outputFile
]
}
},
Expand Down
2 changes: 2 additions & 0 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"version": "0.0.1",
"private": true,
"main": "./dist/index.js",
"source": "./src/index.ts",
"types": "./dist/index.d.ts",
"files": [
"dist/**"
],
Expand Down
24 changes: 13 additions & 11 deletions packages/types/src/lib/cms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
* and re-run `payload generate:types` to regenerate this file.
*/

export type OrderItem = {
name: string;
image?: string;
color: string;
size: string;
price: number;
quantity: number;
id?: string;
}[];

export interface Config {
collections: {
categories: Category;
Expand Down Expand Up @@ -91,20 +101,12 @@ export interface User {
}
export interface Order {
id: string;
items: {
id: string;
name: string;
image: string;
color: string;
size: string;
price: number;
quantity: number;
}[];
items?: OrderItem;
transaction_id: string;
transaction_time: string;
payment_method: string;
customer_email: string;
status: '1' | '2' | '3';
customerEmail: string;
status: 'pending' | 'paid' | 'delivered';
updatedAt: string;
createdAt: string;
}
43 changes: 19 additions & 24 deletions packages/types/src/lib/merch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,21 @@ export interface Product {
}

// Order
// use zod to define order types to allow runtime reflection of type structure.
// by default, typescript types lose type information time at compile time.
export enum OrderStatus {
PENDING_PAYMENT = 1,
PAYMENT_COMPLETED = 2,
ORDER_COMPLETED = 3,
}
const OrderStatusSchema = z.nativeEnum(OrderStatus);

const OrderItem = z.object({
id: z.string(),
name: z.string(),
image: z.string().optional(),
color: z.string(),
size: z.string(),
price: z.number(),
quantity: z.number(),
});
export type OrderItem = z.infer<typeof OrderItem>;

export const OrderSchema = z.object({
id: z.string(),
items: z.array(OrderItem),
transaction_id: z.string(),
transaction_time: z.string().optional(),
payment_method: z.string(),
customer_email: z.string(),
status: OrderStatusSchema,
});
export type Order = z.infer<typeof OrderSchema>;
export interface Order {
id: string;
items: OrderItem[];
transaction_id: string;
transaction_time: string | null;
payment_method: string;
customer_email: string;
status: OrderStatus;
}

// Cart
export type CartState = {
Expand All @@ -73,6 +58,16 @@ export type Cart = z.infer<typeof Cart>;
export type CartItem = z.infer<typeof CartItem>;

// Promotion
export interface OrderItem {
id: string;
name: string;
image?: string;
color: string;
size: string;
price: number;
quantity: number;
}

export interface Promotion {
promoCode: string;
maxRedemptions: number;
Expand Down

0 comments on commit 10080ff

Please sign in to comment.