-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
creating service and filtering products
- Loading branch information
1 parent
4a46c53
commit 10654d3
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." }); | ||
} | ||
} |