Skip to content

Commit

Permalink
creating service and filtering products
Browse files Browse the repository at this point in the history
  • Loading branch information
andresverjan committed May 21, 2024
1 parent 4a46c53 commit 10654d3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/api/middlewares.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type {
MiddlewaresConfig,
MedusaRequest,
MedusaResponse,
MedusaNextFunction,
} from "@medusajs/medusa";

import { getProductFilters } from "./store/custom/getProductFilters";
import { getProductsList } from "./store/custom/getProducts";

async function logger(
req: MedusaRequest,
res: MedusaResponse,
next: MedusaNextFunction
) {
next();
}

export const config: MiddlewaresConfig = {
routes: [
{
matcher: "/store/custom",
method: "GET",
middlewares: [logger],
},
{
matcher: "/store/getProductFilters",
method: "GET",
middlewares: [getProductFilters],
},
{
matcher: "/store/getProducts",
method: "GET",
middlewares: [getProductsList],
},
],
};
20 changes: 20 additions & 0 deletions src/api/store/custom/getProductFilters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa";

export async function getProductFilters(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
try {
const filterList = {
colors: ["red", "blue", "green", "yellow", "purple"],
styles: ["HEM", "MATTER", "RIMA", "BASAL", "ART"],
rooms: ["Foyer/EntranceHall", "KitchenRoom", "FamilyRoom", "DiningRoom", "LivingRoom", "MasterBedroom", "Bathroom", "Laundry Room", "Guest Room", "Home Office", "Library"]
};
res.status(200).json({ filterList });
} catch (error) {
console.log(error)
res
.status(500)
.json({ error: "An error occurred while fetching products filter." });
}
}
34 changes: 34 additions & 0 deletions src/api/store/custom/getProducts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { MedusaRequest, MedusaResponse } from "@medusajs/medusa";

export async function getProductsList(
req: MedusaRequest,
res: MedusaResponse
): Promise<void> {
const productService = req.scope.resolve("productService");
const { colors, styles, rooms} = req.query;

const colorsArray = typeof colors === 'string' ? colors.split(',').map((color: string) => color.trim().toLowerCase()) : Array.isArray(colors) ? colors : [];
const stylesArray = typeof styles === 'string' ? styles.split(',').map((style: string) => style.trim().toLowerCase()) : Array.isArray(styles) ? styles : [];
const roomsArray = typeof rooms === 'string' ? rooms.split(',').map((room: string) => room.trim().toLowerCase()) : Array.isArray(rooms) ? rooms : [];

try {
let products = await productService.list({}, { relations: ["variants"] });
if (colorsArray.length > 0 || stylesArray.length > 0 || roomsArray.length > 0) {
products = products.filter((product: any) => {
const variantTitles = product.variants.map((variant: any) => variant.title.toLowerCase());
let hasValidColor = colorsArray.length === 0;
let hasValidStyle = stylesArray.length === 0;
let hasValidRoom = roomsArray.length === 0;
if (colorsArray.length > 0) hasValidColor = colorsArray.some((color: any) => variantTitles.includes(color));
if (stylesArray.length > 0) hasValidStyle = stylesArray.some((style: any) => variantTitles.includes(style));
if (roomsArray.length > 0) hasValidRoom = roomsArray.some((room: any) => variantTitles.includes(room));

return hasValidColor && hasValidStyle && hasValidRoom;
});
}
res.status(200).json({ products });
} catch (error) {
console.log(error);
res.status(500).json({ error: "An error occurred while fetching products." });
}
}

0 comments on commit 10654d3

Please sign in to comment.