-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(medusa, stock-location-next): add list-stock-locations endpoint …
…to api-v2 (#6788) * initial create * add list for stock locations * add changeset * redo changes for stock locatino module' * add changeset * naming * prep for pr * move integration tests * fix pr feedback * add changeset * update changeset --------- Co-authored-by: Riqwan Thamir <[email protected]>
- Loading branch information
Showing
10 changed files
with
288 additions
and
1 deletion.
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,6 @@ | ||
--- | ||
"@medusajs/stock-location-next": patch | ||
"@medusajs/medusa": patch | ||
--- | ||
|
||
feat(medusa, stock-location-next): add list-stock-locations endpoint to api-v2 |
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
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
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
49 changes: 49 additions & 0 deletions
49
packages/medusa/src/api-v2/admin/stock-locations/utils/apply-sales-channel-filter.ts
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,49 @@ | ||
import { | ||
ContainerRegistrationKeys, | ||
remoteQueryObjectFromString, | ||
} from "@medusajs/utils" | ||
|
||
import { AdminGetStockLocationsParams } from "../validators" | ||
import { MedusaRequest } from "../../../../types/routing" | ||
import { Modules } from "@medusajs/modules-sdk" | ||
import { NextFunction } from "express" | ||
|
||
export function applySalesChannelsFilter() { | ||
return async (req: MedusaRequest, _, next: NextFunction) => { | ||
const filterableFields: AdminGetStockLocationsParams = req.filterableFields | ||
|
||
if (!filterableFields.sales_channel_id) { | ||
return next() | ||
} | ||
|
||
const salesChannelIds = Array.isArray(filterableFields.sales_channel_id) | ||
? filterableFields.sales_channel_id | ||
: [filterableFields.sales_channel_id] | ||
|
||
delete filterableFields.sales_channel_id | ||
|
||
const remoteLinkService = req.scope.resolve( | ||
ContainerRegistrationKeys.REMOTE_LINK | ||
) | ||
|
||
const stockLocationSalesChannelLinkModuleService = | ||
await remoteLinkService.getLinkModule( | ||
Modules.SALES_CHANNEL, | ||
"sales_channel_id", | ||
Modules.STOCK_LOCATION, | ||
"stock_location_id" | ||
) | ||
|
||
const stockLocationSalesChannelLinks = | ||
await stockLocationSalesChannelLinkModuleService.list( | ||
{ sales_channel_id: salesChannelIds }, | ||
{} | ||
) | ||
|
||
filterableFields.id = stockLocationSalesChannelLinks.map( | ||
(link) => link.stock_location_id | ||
) | ||
|
||
return next() | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils" | ||
export { StockLocationRepository } from "./stock-location" |
52 changes: 52 additions & 0 deletions
52
packages/stock-location-next/src/repositories/stock-location.ts
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,52 @@ | ||
import { Context, DAL } from "@medusajs/types" | ||
|
||
import { StockLocation } from "@models" | ||
import { mikroOrmBaseRepositoryFactory } from "@medusajs/utils" | ||
|
||
export class StockLocationRepository extends mikroOrmBaseRepositoryFactory<StockLocation>( | ||
StockLocation | ||
) { | ||
async find( | ||
findOptions: DAL.FindOptions<StockLocation & { q?: string }> = { | ||
where: {}, | ||
}, | ||
context: Context | ||
): Promise<StockLocation[]> { | ||
const findOptions_ = { ...findOptions } | ||
findOptions_.options ??= {} | ||
|
||
this.applyFreeTextSearchFilters<StockLocation>( | ||
findOptions_, | ||
this.getFreeTextSearchConstraints | ||
) | ||
|
||
return await super.find(findOptions_, context) | ||
} | ||
|
||
async findAndCount( | ||
findOptions: DAL.FindOptions<StockLocation & { q?: string }> = { | ||
where: {}, | ||
}, | ||
context: Context | ||
): Promise<[StockLocation[], number]> { | ||
const findOptions_ = { ...findOptions } | ||
findOptions_.options ??= {} | ||
|
||
this.applyFreeTextSearchFilters<StockLocation>( | ||
findOptions_, | ||
this.getFreeTextSearchConstraints | ||
) | ||
|
||
return await super.findAndCount(findOptions_, context) | ||
} | ||
|
||
protected getFreeTextSearchConstraints(q: string) { | ||
return [ | ||
{ | ||
name: { | ||
$ilike: `%${q}%`, | ||
}, | ||
}, | ||
] | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export { default as StockLocationModuleService } from "./stock-location" | ||
export { default as StockLocationModuleService } from "./stock-location-module" |
File renamed without changes.