|
1
|
import { MedusaError } from "@medusajs/framework/utils" |
|
2
|
|
|
3
|
export function resolveAllowedOptionValues<T>({ |
|
4
|
optionTitle, |
|
5
|
value, |
|
6
|
optionValues, |
|
7
|
allowedValueIds, |
|
8
|
}: { |
|
9
|
optionTitle: string, |
|
10
|
value: unknown, |
|
11
|
optionValues: T[] | undefined, |
|
12
|
allowedValueIds?: Set<string> |
|
13
|
}): T { |
|
14
|
const valueNameOf = (v: any): unknown => v?.value?.value ?? v?.value |
|
15
|
const idOf = (v: any): string | undefined => v?.value?.id ?? v?.id |
|
16
|
|
|
17
|
const candidateValues = allowedValueIds |
|
18
|
? optionValues?.filter((v) => { |
|
19
|
const id = idOf(v) |
|
20
|
return id ? allowedValueIds.has(id) : true |
|
21
|
}) |
|
22
|
: optionValues |
|
23
|
|
|
24
|
const match = candidateValues?.find((v) => valueNameOf(v) === value) |
|
25
|
|
|
26
|
if (!match) { |
|
27
|
throw new MedusaError( |
|
28
|
MedusaError.Types.INVALID_DATA, |
|
29
|
`Option value ${value} does not exist for option ${optionTitle}` |
|
30
|
) |
|
31
|
} |
|
32
|
|
|
33
|
return match |
|
34
|
} |