Skip to content

Commit

Permalink
revert(cms): revert "refactor!(cms): Sync Order schema from package/t…
Browse files Browse the repository at this point in the history
…ypes (#142)"

This reverts commit bb51b86.
  • Loading branch information
mrzzy committed May 7, 2024
1 parent 4ee4bc6 commit 485e795
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 125 deletions.
103 changes: 99 additions & 4 deletions apps/cms/src/collections/Orders.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { CollectionConfig } from "payload/types";
import { OrderSchema } from "types";
import { toPayloadZod } from "../utilities/zodInteropt";

import Media from "./Media";

/** Orders collection stores merch orders from users. */
const Orders: CollectionConfig = {
Expand All @@ -16,6 +14,103 @@ const Orders: CollectionConfig = {
],
description: "Merchandise orders from users.",
},
fields: toPayloadZod(OrderSchema),
fields: [
// by default, payload generates an 'id' field each order automatically
{
name: "paymentGateway",
type: "text",
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,
},
{
name: "customerEmail",
type: "email",
required: true,
},
{
name: "transactionID",
label: "Transaction ID",
admin: {
description: "Transaction ID provided by Payment Gateway",
},
type: "text",
required: true,
},
{
name: "orderDateTime",
label: "Ordered On",
type: "date",
admin: {
date: {
pickerAppearance: "dayAndTime",
},
},
required: true,
},
// ordered items for this order
{
name: "orderItems",
type: "array",
fields: [
{
name: "image",
type: "upload",
relationTo: Media.slug,
// validation: only allow image filetypes
filterOptions: {
mimeType: { contains: "image" },
},
},
{
name: "quantity",
type: "number",
required: true,
},
{
name: "size",
type: "text",
required: true,
},
{
name: "price",
type: "number",
required: true,
},
{
name: "name",
type: "text",
required: true,
},
{
name: "colorway",
type: "text",
required: true,
},
],
// direct paylaod to generate a OrderItem type
interfaceName: "OrderItem",
// validate: orders should not be empty
minRows: 1,
},
],
};
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
30 changes: 16 additions & 14 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 = {
image?: string | Media;
quantity: number;
size: string;
price: number;
name: string;
colorway: string;
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;
}[];
transaction_id: string;
transaction_time: string;
payment_method: string;
customer_email: string;
status: '1' | '2' | '3';
paymentGateway: string;
status: 'pending' | 'paid' | 'delivered';
customerEmail: string;
transactionID: string;
orderDateTime: string;
orderItems?: OrderItem;
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 485e795

Please sign in to comment.