|
1
|
import { MedusaError } from "@medusajs/framework/utils"; |
|
2
|
import { createStep } from "@medusajs/framework/workflows-sdk"; |
|
3
|
|
|
4
|
/** |
|
5
|
* Input to validate that a required entity is present. |
|
6
|
*/ |
|
7
|
export interface ValidatePresenceOfStepInput { |
|
8
|
/** |
|
9
|
* The entity to check for presence. |
|
10
|
*/ |
|
11
|
entity: any |
|
12
|
/** |
|
13
|
* The human-readable name of the entity type, used in error messages. |
|
14
|
*/ |
|
15
|
entityName: string |
|
16
|
/** |
|
17
|
* The identifier of the entity (e.g. its ID), used in error messages. |
|
18
|
*/ |
|
19
|
identifier: string |
|
20
|
} |
|
21
|
|
|
22
|
/** |
|
23
|
* This step validates that an entity is present. It throws a NOT_FOUND error if |
|
24
|
* the entity is falsy. |
|
25
|
* |
|
26
|
* @example |
|
27
|
* const data = validatePresenceOfStep({ |
|
28
|
* entity: customer, |
|
29
|
* entityName: "Customer", |
|
30
|
* identifier: "cust_123", |
|
31
|
* }) |
|
32
|
*/ |
|
33
|
export const validatePresenceOfStep = createStep( |
|
34
|
"validate-presence-of", |
|
35
|
async function ({ |
|
36
|
entity, |
|
37
|
entityName, |
|
38
|
identifier, |
|
39
|
}: ValidatePresenceOfStepInput) { |
|
40
|
if (!entity) { |
|
41
|
throw new MedusaError( |
|
42
|
MedusaError.Types.NOT_FOUND, |
|
43
|
`${entityName} ${identifier} not found` |
|
44
|
); |
|
45
|
} |
|
46
|
} |
|
47
|
); |