fff1b3ef9c
**What** Currently when a workflow fail it will throw an error which contains the messages of all error thrown durring the workflow lifetime. Therefore, in our cases we are always preventing workflow to throw and grab the first error that we then re throw. This pr eliminate that need by throwing back the first error among the error thrown by a workflow as it is the main case. In case someone need a special handling they can still set the option throwOnError to false and handle the error the way they need
62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
import {
|
|
AuthenticatedMedusaRequest,
|
|
MedusaResponse,
|
|
} from "../../../types/routing"
|
|
import { createCustomerGroupsWorkflow } from "@medusajs/core-flows"
|
|
import {
|
|
ContainerRegistrationKeys,
|
|
remoteQueryObjectFromString,
|
|
} from "@medusajs/utils"
|
|
import { AdminCreateCustomerGroupType } from "./validators"
|
|
import { refetchCustomerGroup } from "./helpers"
|
|
|
|
export const GET = async (
|
|
req: AuthenticatedMedusaRequest,
|
|
res: MedusaResponse
|
|
) => {
|
|
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
|
|
|
const query = remoteQueryObjectFromString({
|
|
entryPoint: "customer_group",
|
|
variables: {
|
|
filters: req.filterableFields,
|
|
...req.remoteQueryConfig.pagination,
|
|
},
|
|
fields: req.remoteQueryConfig.fields,
|
|
})
|
|
|
|
const { rows: customer_groups, metadata } = await remoteQuery(query)
|
|
|
|
res.json({
|
|
customer_groups,
|
|
count: metadata.count,
|
|
offset: metadata.skip,
|
|
limit: metadata.take,
|
|
})
|
|
}
|
|
|
|
export const POST = async (
|
|
req: AuthenticatedMedusaRequest<AdminCreateCustomerGroupType>,
|
|
res: MedusaResponse
|
|
) => {
|
|
const createGroups = createCustomerGroupsWorkflow(req.scope)
|
|
const customersData = [
|
|
{
|
|
...req.validatedBody,
|
|
created_by: req.auth_context.actor_id,
|
|
},
|
|
]
|
|
|
|
const { result } = await createGroups.run({
|
|
input: { customersData },
|
|
})
|
|
|
|
const customerGroup = await refetchCustomerGroup(
|
|
result[0].id,
|
|
req.scope,
|
|
req.remoteQueryConfig.fields
|
|
)
|
|
|
|
res.status(200).json({ customer_group: customerGroup })
|
|
}
|