|
1
|
import { |
|
2
|
BaseFilterable, |
|
3
|
Context, |
|
4
|
CurrencyTypes, |
|
5
|
DAL, |
|
6
|
FilterableCurrencyProps, |
|
7
|
FindConfig, |
|
8
|
ICurrencyModuleService, |
|
9
|
InternalModuleDeclaration, |
|
10
|
ModulesSdkTypes, |
|
11
|
} from "@medusajs/framework/types" |
|
12
|
|
|
13
|
import { MedusaService, normalizeCurrencyCode } from "@medusajs/framework/utils" |
|
14
|
import { Currency } from "@models" |
|
15
|
|
|
16
|
type InjectedDependencies = { |
|
17
|
baseRepository: DAL.RepositoryService |
|
18
|
currencyService: ModulesSdkTypes.IMedusaInternalService<typeof Currency> |
|
19
|
} |
|
20
|
|
|
21
|
export default class CurrencyModuleService |
|
22
|
extends MedusaService<{ |
|
23
|
Currency: { dto: CurrencyTypes.CurrencyDTO; model: typeof Currency } |
|
24
|
}>({ Currency }) |
|
25
|
implements ICurrencyModuleService |
|
26
|
{ |
|
27
|
protected baseRepository_: DAL.RepositoryService |
|
28
|
protected readonly currencyService_: ModulesSdkTypes.IMedusaInternalService< |
|
29
|
typeof Currency |
|
30
|
> |
|
31
|
|
|
32
|
constructor( |
|
33
|
{ baseRepository, currencyService }: InjectedDependencies, |
|
34
|
protected readonly moduleDeclaration: InternalModuleDeclaration |
|
35
|
) { |
|
36
|
// @ts-ignore |
|
37
|
super(...arguments) |
|
38
|
this.baseRepository_ = baseRepository |
|
39
|
this.currencyService_ = currencyService |
|
40
|
} |
|
41
|
|
|
42
|
// @ts-expect-error |
|
43
|
async retrieveCurrency( |
|
44
|
code: string, |
|
45
|
config?: FindConfig<CurrencyTypes.CurrencyDTO>, |
|
46
|
sharedContext?: Context |
|
47
|
): Promise<CurrencyTypes.CurrencyDTO> { |
|
48
|
return await super.retrieveCurrency( |
|
49
|
CurrencyModuleService.normalizeFilters({ code: [code] })!.code![0], |
|
50
|
config, |
|
51
|
sharedContext |
|
52
|
) |
|
53
|
} |
|
54
|
|
|
55
|
// @ts-expect-error |
|
56
|
async listCurrencies( |
|
57
|
filters?: FilterableCurrencyProps, |
|
58
|
config?: FindConfig<CurrencyTypes.CurrencyDTO>, |
|
59
|
sharedContext?: Context |
|
60
|
): Promise<CurrencyTypes.CurrencyDTO[]> { |
|
61
|
return await super.listCurrencies( |
|
62
|
CurrencyModuleService.normalizeFilters(filters), |
|
63
|
config, |
|
64
|
sharedContext |
|
65
|
) |
|
66
|
} |
|
67
|
|
|
68
|
// @ts-expect-error |
|
69
|
async listAndCountCurrencies( |
|
70
|
filters?: FilterableCurrencyProps, |
|
71
|
config?: FindConfig<CurrencyTypes.CurrencyDTO>, |
|
72
|
sharedContext?: Context |
|
73
|
): Promise<[CurrencyTypes.CurrencyDTO[], number]> { |
|
74
|
return await super.listAndCountCurrencies( |
|
75
|
CurrencyModuleService.normalizeFilters(filters), |
|
76
|
config, |
|
77
|
sharedContext |
|
78
|
) |
|
79
|
} |
|
80
|
|
|
81
|
protected static normalizeFilters( |
|
82
|
filters: FilterableCurrencyProps | undefined |
|
83
|
): FilterableCurrencyProps | undefined { |
|
84
|
return normalizeFilterable< |
|
85
|
CurrencyTypes.CurrencyDTO, |
|
86
|
FilterableCurrencyProps |
|
87
|
>(filters, (fieldName, value) => { |
|
88
|
if (fieldName === "code" && !!value) { |
|
89
|
return normalizeCurrencyCode(value) |
|
90
|
} |
|
91
|
|
|
92
|
return value |
|
93
|
}) |
|
94
|
} |
|
95
|
} |
|
96
|
|
|
97
|
// TODO: Move normalizer support to `buildQuery` so we don't even need to override the list/retrieve methods just for normalization |
|
98
|
const normalizeFilterable = <TModel, TFilter extends BaseFilterable<TFilter>>( |
|
99
|
filters: TFilter | undefined, |
|
100
|
normalizer: (fieldName: keyof TModel, value: any) => any |
|
101
|
): TFilter | undefined => { |
|
102
|
if (!filters) { |
|
103
|
return filters |
|
104
|
} |
|
105
|
|
|
106
|
const normalizedFilters = {} as TFilter |
|
107
|
for (const key in filters) { |
|
108
|
if (key === "$and" || key === "$or") { |
|
109
|
normalizedFilters[key] = (filters[key] as any).map((filter) => |
|
110
|
normalizeFilterable(filter, normalizer) |
|
111
|
) |
|
112
|
} else if (filters[key] !== undefined) { |
|
113
|
if (Array.isArray(filters[key])) { |
|
114
|
normalizedFilters[key] = (filters[key] as any).map((val) => |
|
115
|
normalizer(key as any, val) |
|
116
|
) |
|
117
|
} else { |
|
118
|
normalizedFilters[key] = normalizer(key as any, filters[key]) |
|
119
|
} |
|
120
|
} |
|
121
|
} |
|
122
|
|
|
123
|
return normalizedFilters |
|
124
|
} |