Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: test create product with workflow on serveless #22

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ VERCEL_GIT_COMMIT_AUTHOR_NAME=""
VERCEL_GIT_PULL_REQUEST_ID=""
VERCEL_URL=""
NEXT_PUBLIC_MEDUSA_API_URL="localhost:9000"
POSTGRES_URL="postgres://postgres:localhost:5432/medusa-products"
POSTGRES_URL="postgres://postgres@localhost:5432/medusa-products"
POSTGRES_URL_NON_POOLING=""
POSTGRES_PRISMA_URL=""
POSTGRES_USER=""
Expand Down
Binary file added medusajs-modules-sdk-1.8.8.tgz
Binary file not shown.
Binary file added medusajs-orchestration-0.0.2.tgz
Binary file not shown.
Binary file added medusajs-product-0.1.3.tgz
Binary file not shown.
Binary file added medusajs-types-1.8.11.tgz
Binary file not shown.
Binary file added medusajs-utils-1.9.2.tgz
Binary file not shown.
Binary file added medusajs-workflows.tgz
Binary file not shown.
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"@headlessui/react": "^1.7.14",
"@medusajs/medusa": "^1.12.0-snapshot-20230608104224",
"@medusajs/medusa-js": "6.0.1-snapshot-20230608104224",
"@medusajs/product": "^0.0.2-snapshot-20230608104224",
"@medusajs/modules-sdk": "./medusajs-modules-sdk-1.8.8.tgz",
"@medusajs/orchestration": "./medusajs-orchestration-0.0.2.tgz",
"@medusajs/product": "./medusajs-product-0.1.3.tgz",
"@medusajs/utils": "./medusajs-utils-1.9.2.tgz",
"@medusajs/workflows": "./medusajs-workflows.tgz",
"@types/gtag.js": "^0.0.12",
"@vercel/kv": "^0.2.1",
"autoprefixer": "10.4.14",
Expand All @@ -32,8 +36,9 @@
},
"devDependencies": {
"@medusajs/client-types": "^0.2.3",
"@medusajs/types": "1.8.8-snapshot-20230608104224",
"@mikro-orm/migrations": "5.7.4",
"@medusajs/types": "./medusajs-types-1.8.11.tgz",
"@mikro-orm/migrations": "5.7.12",
"@types/lodash": "^4.14.195",
"@types/node": "20.1.0",
"@types/react": "18.2.6",
"@types/react-dom": "18.2.4",
Expand All @@ -42,5 +47,12 @@
"eslint": "8.40.0",
"eslint-config-next": "13.4.1",
"typescript": "5.0.4"
},
"resolutions": {
"@medusajs/modules-sdk": "./medusajs-modules-sdk-1.8.8.tgz",
"@medusajs/orchestration": "./medusajs-orchestration-0.0.2.tgz",
"@medusajs/product": "./medusajs-product-0.1.3.tgz",
"@medusajs/utils": "./medusajs-utils-1.9.2.tgz",
"@medusajs/workflows": "./medusajs-workflows.tgz"
}
}
72 changes: 71 additions & 1 deletion src/app/api/products/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ProductTypes } from "@medusajs/types";
import { NextRequest, NextResponse } from "next/server";
import { formatContinent, isoAlpha2Countries } from "@/lib/utils";
import { UserData } from "@/types";
import { createProducts } from "@medusajs/workflows";

type Data = {
categoryId?: string;
Expand All @@ -15,7 +16,7 @@ export async function GET(req: NextRequest) {
const start = performance.now();

// If already instantiated, it will return the instance or create a new one
const productService = await ProductModuleInitialize();
await ProductModuleInitialize();

const countryCode: string = req.headers.get("x-country") ?? "US";

Expand Down Expand Up @@ -54,6 +55,26 @@ export async function GET(req: NextRequest) {
}
}

export async function POST(req: NextRequest) {
const { title } = (await req.json()) as { title: string }

const generatedProductData = generateProductData()
const data = [{ ...generatedProductData, title } as ProductTypes.CreateProductDTO]

await ProductModuleInitialize()
const createProductsWorkflow = createProducts()

try {
const { result: products } = await createProductsWorkflow.run({
input: data,
})

return new Response(JSON.stringify({ products }));
} catch (e) {
return new Response(JSON.stringify({ errors: e }));
}
}

async function queryProducts(
req: NextRequest,
continent: string
Expand Down Expand Up @@ -152,3 +173,52 @@ function orderProductByCategoryIdFirst({

return { personalizedProducts, allProducts };
}

/**
*
* WORKFLOW RELATED UTILS BELOW
*
*/

function generateRandomInteger(min: number, max: number) {
return Math.floor(min + Math.random() * (max - min + 1))
}


function generateRandomStringInteger() {
const max = 10000000
const min = 0
return generateRandomInteger(min, max).toString()
}

function generateProductData(title = '', options: any[] = [], variants: any[] = []) {
title = title || generateRandomStringInteger()

const optionTitle = generateRandomStringInteger()
options = options.length ? options : [{
title: optionTitle
}]

if (!variants.length) {
for (let i = 0; i < 25; ++i) {
const pricesData = [{
currency_code: 'usd',
amount: generateRandomInteger(1, 10000)
}]

const data = {
title: generateRandomStringInteger(),
options: [{ value: optionTitle }]
}
variants.push(data)
}
}

const data = {
title,
options,
variants,
}

return data
}
2 changes: 1 addition & 1 deletion src/components/products/details/Interactive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
PricedProduct,
PricedVariant,
} from "@medusajs/client-types";
import isEqual from "lodash/isEqual";
import { isEqual } from "lodash";
import {
PropsWithChildren,
createContext,
Expand Down
Loading