Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/ntuscse/website into merch-…
Browse files Browse the repository at this point in the history
…toggle/ivan
  • Loading branch information
limivann committed Mar 12, 2024
2 parents 8b77bbd + 82dda43 commit 0426b00
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 156 deletions.
28 changes: 14 additions & 14 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# more info: https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners

* @realdyllon
* @chanbakjsd

/.github/** @realdyllon @aryans1204
/deployment/** @realdyllon @aryans1204
/.github/** @chanbakjsd @aryans1204
/deployment/** @chanbakjsd @aryans1204

/apps/cms/** @jamiegoh
/apps/cms/** @jack-thant
/apps/merch/** @chanbakjsd
/apps/web/** @xJQx
/apps/web/** @jack-thant
/apps/challenges/** @BoonHianLim
/apps/web/features/merch/** @chanbakjsd
/apps/web/pages/merch/** @chanbakjsd

/packages/eslint-custom-config/** @realdyllon
/packages/nodelogger/** @realdyllon
/packages/schemas/** @realdyllon
/packages/schemas/lib/cms.graphql @jamiegoh
/packages/tsconfig/** @realdyllon
/packages/types/** @realdyllon
/packages/types/lib/cms.ts @jamiegoh
/packages/eslint-custom-config/** @chanbakjsd
/packages/nodelogger/** @chanbakjsd
/packages/schemas/** @chanbakjsd
/packages/schemas/lib/cms.graphql @jack-thant
/packages/tsconfig/** @chanbakjsd
/packages/types/** @chanbakjsd
/packages/types/lib/cms.ts @jack-thant
/packages/types/lib/merch.ts @chanbakjsd
/packages/ui/** @xJQx
/packages/ui/merch/** @chanbakjsd
/packages/ui/** @chanbakjsd

103 changes: 4 additions & 99 deletions apps/cms/src/collections/Orders.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { CollectionConfig } from "payload/types";
import Media from "./Media";
import { OrderSchema } from "types";
import { toPayloadZod } from "../utilities/zodInteropt";


/** Orders collection stores merch orders from users. */
const Orders: CollectionConfig = {
Expand All @@ -14,103 +16,6 @@ const Orders: CollectionConfig = {
],
description: "Merchandise orders from users.",
},
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,
},
],
fields: toPayloadZod(OrderSchema),
};
export default Orders;
81 changes: 81 additions & 0 deletions apps/cms/src/utilities/zodInteropt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Field } from "payload/types";
import { z } from "zod";

/**
* Converts Zod field with given type to a corresponding Payload field.
* @param name Name of the Zod field to convert.
* @param zodType Data type of the field to convert.
* @returns Payload Field definition suitable for use in Payload collections.
*/
function toPayloadZodField(
name: string,
zodType: z.ZodFirstPartySchemaTypes
): Field {
const required = zodType.isOptional() ? {} : { required: true };
const field = {
name: name,
...required,
};

// zod types are matched by type name as matching with instanceof breaks bundler
switch (zodType._def.typeName) {
case z.ZodFirstPartyTypeKind.ZodString:
return { ...field, type: "text" };
case z.ZodFirstPartyTypeKind.ZodNumber:
return { ...field, type: "number" };
break;
case z.ZodFirstPartyTypeKind.ZodArray:
return {
...field,
type: "array",
// convert nested type stored in array
fields: toPayloadZod(
(zodType as z.ZodArray<z.ZodObject<z.ZodRawShape>>).element
),
};
case z.ZodFirstPartyTypeKind.ZodNativeEnum:
return {
...field,
type: "select",
// unpack enum entries as select options
// typescript encodes enums are encoded as bidirectional dictionary
// with both entries from option -> value and value -> option
// use zod parsing to select only the options -> value entries
options: Object.entries((zodType as z.ZodNativeEnum<z.EnumLike>).enum)
.filter(([_, right]) => zodType.safeParse(right).success)
.map(([option, value]) => {
return { label: option, value: `${value}` };
}),
};

case z.ZodFirstPartyTypeKind.ZodOptional:
return {
...toPayloadZodField(
name,
(zodType as z.ZodOptional<z.ZodTypeAny>).unwrap()
),
// override nested field required true with false
...field,
};

default:
throw new Error(
`Unable to convert unsupported Zod type: ${JSON.stringify(
zodType,
null,
2
)}`
);
}
}

/**
* Converts Zod Object into Payload field definitions.
* @param zodObject Zod Object to convert.
* @returns List of Payload fields corresponding to the fields of the Zod object.
*/
export function toPayloadZod(zodObject: z.ZodObject<z.ZodRawShape>): Field[] {
return Object.entries(zodObject.shape).map(([name, zodType]) =>
toPayloadZodField(name, zodType)
);
}
3 changes: 2 additions & 1 deletion apps/cms/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
"jsx": "react",
"allowSyntheticDefaultImports": true,
"paths": {
// Ensure this matches the path to your typescript outputFile
"payload/generated-types": [
"../../packages/types/lib/cms.ts" // Ensure this matches the path to your typescript outputFile
"../../packages/types/src/index.ts"
]
}
},
Expand Down
4 changes: 3 additions & 1 deletion apps/merch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"start": "node dist/index.js",
"start:ci": "nohup yarn run start > /dev/null 2>&1 &",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"dev": "tsup src/index.ts --format cjs --watch --onSuccess \"node dist/index.js\"",
"run": "tsup src/index.ts --format cjs --onSuccess \"node dist/index.js\"",
"dev": "yarn run run --watch",
"lint": "TIMING=1 eslint \"**/*.ts*\"",
"lint:fix": "TIMING=1 eslint --fix \"**/*.ts*\""
},
Expand All @@ -26,6 +27,7 @@
"nodelogger": "*",
"stripe": "^12.5.0",
"trpc-panel": "^1.3.4",
"types": "*",
"uuid": "^9.0.0",
"yarn": "^1.22.19",
"zod": "^3.21.4"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"packages/*"
],
"scripts": {
"clean": "turbo run clean",
"dev": "turbo run dev",
"start": "turbo run start",
"lint": "turbo run lint --continue",
Expand Down
4 changes: 2 additions & 2 deletions packages/nodelogger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
],
"license": "Apache-2.0",
"scripts": {
"build": "tsc",
"build": "tsup src/index.ts --format cjs --dts",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"dev": "tsc -w",
"dev": "yarn build --watch",
"lint": "TIMING=1 eslint \"**/*.ts*\"",
"lint:fix": "TIMING=1 eslint --fix \"**/*.ts*\""
},
Expand Down
6 changes: 2 additions & 4 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
"version": "0.0.1",
"private": true,
"main": "./dist/index.js",
"source": "./src/index.ts",
"types": "./dist/index.d.ts",
"files": [
"dist/**"
],
"license": "Apache-2.0",
"scripts": {
"build": "tsc",
"build": "tsup src/index.ts --format cjs --dts",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"dev": "tsc -w",
"dev": "yarn build -- --watch",
"lint": "TIMING=1 eslint \"**/*.ts*\"",
"lint:fix": "TIMING=1 eslint --fix \"**/*.ts*\""
},
Expand Down
30 changes: 14 additions & 16 deletions packages/types/src/lib/cms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
* 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 @@ -100,12 +90,20 @@ export interface User {
}
export interface Order {
id: string;
paymentGateway: string;
status: 'pending' | 'paid' | 'delivered';
customerEmail: string;
transactionID: string;
orderDateTime: string;
orderItems?: OrderItem;
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';
updatedAt: string;
createdAt: string;
}
Loading

0 comments on commit 0426b00

Please sign in to comment.