Inventory Module | Stock Location Module #297
alsherif-khalaf
started this conversation in
General
Replies: 3 comments
-
export const getVaraintStock = cache(async function (variantID: any) {
let MEDUSA_BACKEND_URL = "http://localhost:9000"
if (process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL) {
MEDUSA_BACKEND_URL = process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL
}
const medusa = new Medusa({
baseUrl: MEDUSA_BACKEND_URL,
publishableApiKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
axiosAdapter: fetchAdapter as AxiosAdapter, // Cast fetchAdapter to AxiosAdapter type
maxRetries: 3,
})
await medusa.admin.auth
.getToken({
email: process.env.MEDUSA_ADMIN!,
password: process.env.MEDUSA_ADMIN_PASSWORD!,
})
.catch((err) => {
return null
})
const variantInventory = medusa.admin.variants
.getInventory(variantID)
.then((res) => res.variant.inventory)
.catch((err) => {
throw err
})
return variantInventory
}) I added this function to the /src/lib/data.ts [
{
id: 'ilev_01HT2G9CCHZG47Y2267T0QT734',
created_at: '2024-03-28T12:24:21.137Z',
updated_at: '2024-03-28T13:46:38.378Z',
deleted_at: null,
inventory_item_id: 'iitem_01HT2G9C7TWVNB8S8GY15R3GW5',
location_id: 'sloc_01HQGWH0QKHPD7J8JRDATP39BM',
stocked_quantity: 8,
reserved_quantity: 0,
incoming_quantity: 0,
metadata: null,
available_quantity: 8
},
{
id: 'ilev_01HT2GC0Y8XGF4F25P13NVWG18',
created_at: '2024-03-28T12:25:47.717Z',
updated_at: '2024-03-28T14:26:58.090Z',
deleted_at: null,
inventory_item_id: 'iitem_01HT2G9C7TWVNB8S8GY15R3GW5',
location_id: 'sloc_01HQGWJPNF6QKJC959VKDC8RT6',
stocked_quantity: 0,
reserved_quantity: 0,
incoming_quantity: 0,
metadata: null,
available_quantity: 0
}
] but it this secure ? |
Beta Was this translation helpful? Give feedback.
0 replies
-
I created this function : export const getVaraintStock = cache(async function (variants: PricedVariant[]) {
let MEDUSA_BACKEND_URL = "http://localhost:9000"
if (process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL) {
MEDUSA_BACKEND_URL = process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL
}
const medusa = new Medusa({
baseUrl: MEDUSA_BACKEND_URL,
publishableApiKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
axiosAdapter: fetchAdapter as AxiosAdapter, // Cast fetchAdapter to AxiosAdapter type
maxRetries: 3,
})
await medusa.admin.auth
.getToken({
email: process.env.MEDUSA_ADMIN!,
password: process.env.MEDUSA_ADMIN_PASSWORD!,
})
.catch((err) => {
return null
})
// medusa.admin.stockLocations.list().then((res) => {
// console.log("stock_locations LIST : " ,res.stock_locations)
// })
medusa.admin.stockLocations.list().then((res) => {
console.log("stock_locations LIST : " , res.stock_locations)
})
const inventoryPromises = variants.flatMap(variant =>
medusa.admin.variants
.getInventory(variant.id!)
.then((res) => res.variant.inventory.map((inventoryItem) => ({
id: inventoryItem.id,
location_levels: inventoryItem.location_levels
})))
.catch((err) => {
throw err
})
);
const inventories = await Promise.all(inventoryPromises);
return inventories.flat();
}) and the result was : getInvertory [
{
id: 'ilev_01HT3867SE9GKMP33AEY7ABWKF',
created_at: '2024-03-28T19:22:03.950Z',
updated_at: '2024-03-28T19:22:03.950Z',
deleted_at: null,
inventory_item_id: 'iitem_01HT3866VE3YNQ7X3EWJQRN63F',
location_id: 'sloc_01HQGWH0QKHPD7J8JRDATP39BM',
stocked_quantity: 20,
reserved_quantity: 0,
incoming_quantity: 0,
metadata: null,
available_quantity: 20
},
{
id: 'ilev_01HT3867RQ0ED644T2RHVFW1WZ',
created_at: '2024-03-28T19:22:03.926Z',
updated_at: '2024-03-28T19:23:08.288Z',
deleted_at: null,
inventory_item_id: 'iitem_01HT3866VE3YNQ7X3EWJQRN63F',
location_id: 'sloc_01HQGWJPNF6QKJC959VKDC8RT6',
stocked_quantity: 0,
reserved_quantity: 0,
incoming_quantity: 0,
metadata: null,
available_quantity: 0
}
] How can I know the location_id belongs to country code ? |
Beta Was this translation helpful? Give feedback.
0 replies
-
this worked export const getVaraintStock = cache(async function (
variants: PricedVariant[]
) {
let MEDUSA_BACKEND_URL = "http://localhost:9000"
if (process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL) {
MEDUSA_BACKEND_URL = process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL
}
const medusa = new Medusa({
baseUrl: MEDUSA_BACKEND_URL,
publishableApiKey: process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY,
axiosAdapter: fetchAdapter as AxiosAdapter, // Cast fetchAdapter to AxiosAdapter type
maxRetries: 3,
})
await medusa.admin.auth
.getToken({
email: process.env.MEDUSA_ADMIN!,
password: process.env.MEDUSA_ADMIN_PASSWORD!,
})
.catch((err) => {
return null
})
const stockLocations = await medusa.admin.stockLocations.list(
{ expand: "address" }
)
const inventoryPromises = variants.flatMap((variant) =>
medusa.admin.variants
.getInventory(variant.id!)
.then((res) =>
res.variant.inventory.map((inventoryItem) => ({
id: inventoryItem.id,
location_levels: inventoryItem.location_levels,
}))
)
.catch((err) => {
throw err
})
)
const inventories = await Promise.all(inventoryPromises)
const variantLocations = inventories.map((inventory) =>
inventory.map((item) => {
const locationId = item?.location_levels ? item?.location_levels[0]?.location_id : 'unkown'
const location = stockLocations.stock_locations.find(loc => loc.id === locationId)
return {
...item,
country_code: location?.address?.country_code
}
})
)
return variantLocations.flat()
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
After add Inventory Module & Stock Location Module to the back-end , we get only inventory_quantity with total inventory levels , so i created a new function :
so it unauthorized , should i add my admin user name and password to authenticate ?
I feel that inventory module is useless
Beta Was this translation helpful? Give feedback.
All reactions