|
1
|
import { |
|
2
|
Context, |
|
3
|
CreateSalesChannelDTO, |
|
4
|
DAL, |
|
5
|
FilterableSalesChannelProps, |
|
6
|
InferEntityType, |
|
7
|
InternalModuleDeclaration, |
|
8
|
ISalesChannelModuleService, |
|
9
|
ModuleJoinerConfig, |
|
10
|
ModulesSdkTypes, |
|
11
|
SalesChannelDTO, |
|
12
|
UpdateSalesChannelDTO, |
|
13
|
UpsertSalesChannelDTO, |
|
14
|
} from "@medusajs/framework/types" |
|
15
|
import { |
|
16
|
EmitEvents, |
|
17
|
InjectManager, |
|
18
|
InjectTransactionManager, |
|
19
|
isString, |
|
20
|
MedusaContext, |
|
21
|
MedusaService, |
|
22
|
promiseAll, |
|
23
|
} from "@medusajs/framework/utils" |
|
24
|
|
|
25
|
import { SalesChannel } from "@models" |
|
26
|
import { UpdateSalesChanneInput } from "@types" |
|
27
|
import { joinerConfig } from "../joinfer-config" |
|
28
|
|
|
29
|
type InjectedDependencies = { |
|
30
|
baseRepository: DAL.RepositoryService |
|
31
|
salesChannelService: ModulesSdkTypes.IMedusaInternalService<any> |
|
32
|
} |
|
33
|
|
|
34
|
export default class SalesChannelModuleService |
|
35
|
extends MedusaService<{ SalesChannel: { dto: SalesChannelDTO } }>({ |
|
36
|
SalesChannel, |
|
37
|
}) |
|
38
|
implements ISalesChannelModuleService |
|
39
|
{ |
|
40
|
protected baseRepository_: DAL.RepositoryService |
|
41
|
protected readonly salesChannelService_: ModulesSdkTypes.IMedusaInternalService< |
|
42
|
InferEntityType<typeof SalesChannel> |
|
43
|
> |
|
44
|
|
|
45
|
constructor( |
|
46
|
{ baseRepository, salesChannelService }: InjectedDependencies, |
|
47
|
protected readonly moduleDeclaration: InternalModuleDeclaration |
|
48
|
) { |
|
49
|
// @ts-ignore |
|
50
|
super(...arguments) |
|
51
|
this.baseRepository_ = baseRepository |
|
52
|
this.salesChannelService_ = salesChannelService |
|
53
|
} |
|
54
|
|
|
55
|
__joinerConfig(): ModuleJoinerConfig { |
|
56
|
return joinerConfig |
|
57
|
} |
|
58
|
|
|
59
|
// @ts-expect-error |
|
60
|
async createSalesChannels( |
|
61
|
data: CreateSalesChannelDTO[], |
|
62
|
sharedContext?: Context |
|
63
|
): Promise<SalesChannelDTO[]> |
|
64
|
// @ts-expect-error |
|
65
|
async createSalesChannels( |
|
66
|
data: CreateSalesChannelDTO, |
|
67
|
sharedContext?: Context |
|
68
|
): Promise<SalesChannelDTO> |
|
69
|
|
|
70
|
@InjectManager() |
|
71
|
@EmitEvents() |
|
72
|
// @ts-expect-error |
|
73
|
async createSalesChannels( |
|
74
|
data: CreateSalesChannelDTO | CreateSalesChannelDTO[], |
|
75
|
@MedusaContext() sharedContext: Context = {} |
|
76
|
): Promise<SalesChannelDTO | SalesChannelDTO[]> { |
|
77
|
const input = Array.isArray(data) ? data : [data] |
|
78
|
|
|
79
|
const result = await this.createSalesChannels_(input, sharedContext) |
|
80
|
|
|
81
|
return await this.baseRepository_.serialize<SalesChannelDTO[]>( |
|
82
|
Array.isArray(data) ? result : result[0] |
|
83
|
) |
|
84
|
} |
|
85
|
|
|
86
|
@InjectTransactionManager() |
|
87
|
async createSalesChannels_( |
|
88
|
data: CreateSalesChannelDTO[], |
|
89
|
@MedusaContext() sharedContext: Context |
|
90
|
): Promise<InferEntityType<typeof SalesChannel>[]> { |
|
91
|
return await this.salesChannelService_.create(data, sharedContext) |
|
92
|
} |
|
93
|
|
|
94
|
// @ts-expect-error |
|
95
|
async updateSalesChannels( |
|
96
|
id: string, |
|
97
|
data: UpdateSalesChannelDTO, |
|
98
|
sharedContext?: Context |
|
99
|
): Promise<SalesChannelDTO> |
|
100
|
// @ts-expect-error |
|
101
|
async updateSalesChannels( |
|
102
|
selector: FilterableSalesChannelProps, |
|
103
|
data: UpdateSalesChannelDTO, |
|
104
|
sharedContext?: Context |
|
105
|
): Promise<SalesChannelDTO[]> |
|
106
|
|
|
107
|
@InjectManager() |
|
108
|
@EmitEvents() |
|
109
|
// @ts-expect-error |
|
110
|
async updateSalesChannels( |
|
111
|
idOrSelector: string | FilterableSalesChannelProps, |
|
112
|
data: UpdateSalesChannelDTO | UpdateSalesChannelDTO[], |
|
113
|
@MedusaContext() sharedContext: Context = {} |
|
114
|
): Promise<SalesChannelDTO | SalesChannelDTO[]> { |
|
115
|
let normalizedInput: UpdateSalesChanneInput[] = [] |
|
116
|
if (isString(idOrSelector)) { |
|
117
|
normalizedInput = [{ id: idOrSelector, ...data }] |
|
118
|
} else { |
|
119
|
const channels = await this.salesChannelService_.list( |
|
120
|
idOrSelector, |
|
121
|
{}, |
|
122
|
sharedContext |
|
123
|
) |
|
124
|
|
|
125
|
normalizedInput = channels.map((channel) => ({ |
|
126
|
id: channel.id, |
|
127
|
...data, |
|
128
|
})) |
|
129
|
} |
|
130
|
|
|
131
|
const result = await this.updateSalesChannels_( |
|
132
|
normalizedInput, |
|
133
|
sharedContext |
|
134
|
) |
|
135
|
|
|
136
|
return await this.baseRepository_.serialize<SalesChannelDTO[]>( |
|
137
|
Array.isArray(data) ? result : result[0] |
|
138
|
) |
|
139
|
} |
|
140
|
|
|
141
|
@InjectTransactionManager() |
|
142
|
async updateSalesChannels_( |
|
143
|
data: UpdateSalesChannelDTO[], |
|
144
|
sharedContext: Context |
|
145
|
) { |
|
146
|
return await this.salesChannelService_.update(data, sharedContext) |
|
147
|
} |
|
148
|
|
|
149
|
async upsertSalesChannels( |
|
150
|
data: UpsertSalesChannelDTO[], |
|
151
|
sharedContext?: Context |
|
152
|
): Promise<SalesChannelDTO[]> |
|
153
|
async upsertSalesChannels( |
|
154
|
data: UpsertSalesChannelDTO, |
|
155
|
sharedContext?: Context |
|
156
|
): Promise<SalesChannelDTO> |
|
157
|
@InjectManager() |
|
158
|
@EmitEvents() |
|
159
|
@InjectTransactionManager() |
|
160
|
async upsertSalesChannels( |
|
161
|
data: UpsertSalesChannelDTO | UpsertSalesChannelDTO[], |
|
162
|
@MedusaContext() sharedContext: Context = {} |
|
163
|
): Promise<SalesChannelDTO | SalesChannelDTO[]> { |
|
164
|
const result = await this.upsertSalesChannels_(data, sharedContext) |
|
165
|
|
|
166
|
return await this.baseRepository_.serialize< |
|
167
|
SalesChannelDTO[] | SalesChannelDTO |
|
168
|
>(Array.isArray(data) ? result : result[0]) |
|
169
|
} |
|
170
|
|
|
171
|
@InjectTransactionManager() |
|
172
|
protected async upsertSalesChannels_( |
|
173
|
data: UpsertSalesChannelDTO | UpsertSalesChannelDTO[], |
|
174
|
@MedusaContext() sharedContext: Context = {} |
|
175
|
): Promise<InferEntityType<typeof SalesChannel>[]> { |
|
176
|
const input = Array.isArray(data) ? data : [data] |
|
177
|
const forUpdate = input.filter( |
|
178
|
(channel): channel is UpdateSalesChannelDTO => !!channel.id |
|
179
|
) |
|
180
|
const forCreate = input.filter( |
|
181
|
(channel): channel is CreateSalesChannelDTO => !channel.id |
|
182
|
) |
|
183
|
|
|
184
|
const operations: Promise<InferEntityType<typeof SalesChannel>[]>[] = [] |
|
185
|
|
|
186
|
if (forCreate.length) { |
|
187
|
operations.push(this.createSalesChannels_(forCreate, sharedContext)) |
|
188
|
} |
|
189
|
if (forUpdate.length) { |
|
190
|
operations.push(this.updateSalesChannels_(forUpdate, sharedContext)) |
|
191
|
} |
|
192
|
|
|
193
|
const result = (await promiseAll(operations)).flat() |
|
194
|
|
|
195
|
return result |
|
196
|
} |
|
197
|
} |