feat(workflows): Improve typings (#4689)

Just improve some typings for simpler usage
This commit is contained in:
Adrien de Peretti
2023-08-04 08:11:44 +00:00
committed by GitHub
parent ce3326c5fb
commit 43f34866c8
9 changed files with 41 additions and 36 deletions
@@ -1,10 +1,10 @@
import { InventoryItemDTO, ProductTypes } from "@medusajs/types"
import { InputAlias } from "../definitions"
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
import { WorkflowArguments } from "../helper"
import { ProductVariantInventoryItem } from "@medusajs/medusa/src"
export async function attachInventoryItems<T = ProductVariantInventoryItem[]>({
export async function attachInventoryItems({
container,
data,
}: WorkflowArguments & {
@@ -12,13 +12,13 @@ export async function attachInventoryItems<T = ProductVariantInventoryItem[]>({
variant: ProductTypes.ProductVariantDTO
[InputAlias.InventoryItems]: InventoryItemDTO
}[]
}): Promise<PipelineHandlerResult<T>> {
}): Promise<ProductVariantInventoryItem[]> {
const manager = container.resolve("manager")
const productVariantInventoryService = container
.resolve("productVariantInventoryService")
.withTransaction(manager)
return (await Promise.all(
return await Promise.all(
data
.filter((d) => d)
.map(async ({ variant, [InputAlias.InventoryItems]: inventoryItem }) => {
@@ -27,5 +27,5 @@ export async function attachInventoryItems<T = ProductVariantInventoryItem[]>({
inventoryItem.id
)
})
)) as PipelineHandlerResult<T>
)
}
@@ -5,18 +5,16 @@ import {
} from "@medusajs/types"
import { InputAlias } from "../definitions"
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
import { WorkflowArguments } from "../helper"
export async function createInventoryItems<
T = { variant: any; inventoryItem: InventoryItemDTO }[]
>({
export async function createInventoryItems({
container,
data,
}: WorkflowArguments & {
data: {
[InputAlias.Products]: ProductTypes.ProductDTO[]
}
}): Promise<PipelineHandlerResult<T>> {
}): Promise<{ variant: any; inventoryItem: InventoryItemDTO }[]> {
const manager = container.resolve("manager")
const inventoryService: IInventoryService =
container.resolve("inventoryService")
@@ -33,7 +31,7 @@ export async function createInventoryItems<
[]
)
return (await Promise.all(
return await Promise.all(
variants.map(async (variant) => {
if (!variant.manage_inventory) {
return
@@ -56,5 +54,5 @@ export async function createInventoryItems<
return { variant, inventoryItem }
})
)) as PipelineHandlerResult<T>
)
}
@@ -1,14 +1,14 @@
import { InputAlias } from "../definitions"
import { ProductTypes } from "@medusajs/types"
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
import { WorkflowArguments } from "../helper"
export async function createProducts<T = ProductTypes.ProductDTO[]>({
export async function createProducts({
container,
context,
data,
}: WorkflowArguments & {
data: { [InputAlias.Products]: ProductTypes.CreateProductDTO[] }
}): Promise<PipelineHandlerResult<T>> {
}): Promise<ProductTypes.ProductDTO[]> {
const productModuleService = container.resolve("productModuleService")
return await productModuleService.create(data[InputAlias.Products], context)
@@ -1,7 +1,6 @@
import { InputAlias } from "../../definitions"
import { PipelineHandlerResult } from "../../helper"
export function createProductsPrepareData({ data }): PipelineHandlerResult {
export function createProductsPrepareData({ data }) {
return {
alias: InputAlias.Products as string,
value: [{}],
@@ -1,26 +1,26 @@
import { InventoryItemDTO } from "@medusajs/types"
import { InputAlias } from "../definitions"
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
import { WorkflowArguments } from "../helper"
export async function removeInventoryItems<T = void[]>({
export async function removeInventoryItems({
container,
data,
}: WorkflowArguments & {
data: {
[InputAlias.InventoryItems]: InventoryItemDTO
}[]
}): Promise<PipelineHandlerResult<T>> {
}) {
const manager = container.resolve("manager")
const inventoryService = container.resolve("inventoryService")
const context = { transactionManager: manager }
return (await Promise.all(
return await Promise.all(
data.map(async ({ [InputAlias.InventoryItems]: inventoryItem }) => {
return await inventoryService!.deleteInventoryItem(
inventoryItem.id,
context
)
})
)) as PipelineHandlerResult<T>
)
}
@@ -1,15 +1,15 @@
import { InputAlias } from "../definitions"
import { ProductTypes } from "@medusajs/types"
import { PipelineHandlerResult, WorkflowArguments } from "../helper"
import { WorkflowArguments } from "../helper"
export async function removeProducts<T = any>({
export async function removeProducts({
container,
data,
}: WorkflowArguments & {
data: {
[InputAlias.Products]: ProductTypes.ProductDTO[]
}
}): Promise<PipelineHandlerResult<T>> {
}) {
const productModuleService = container.resolve("productModuleService")
return await productModuleService.softDelete(
data[InputAlias.Products].map((p) => p.id)