1 import {
2 Context,
3 CustomerAddressDTO,
4 CustomerDTO,
5 CustomerGroupCustomerDTO,
6 CustomerGroupDTO,
7 CustomerTypes,
8 DAL,
9 ICustomerModuleService,
10 InferEntityType,
11 InternalModuleDeclaration,
12 ModuleJoinerConfig,
13 ModulesSdkTypes,
14 } from "@medusajs/framework/types"
15
16 import {
17 EmitEvents,
18 InjectManager,
19 InjectTransactionManager,
20 isString,
21 MedusaContext,
22 MedusaService,
23 } from "@medusajs/framework/utils"
24 import {
25 Customer,
26 CustomerAddress,
27 CustomerGroup,
28 CustomerGroupCustomer,
29 } from "@models"
30 import { joinerConfig } from "../joiner-config"
31
32 type InjectedDependencies = {
33 baseRepository: DAL.RepositoryService
34 customerService: ModulesSdkTypes.IMedusaInternalService<any>
35 customerAddressService: ModulesSdkTypes.IMedusaInternalService<any>
36 customerGroupService: ModulesSdkTypes.IMedusaInternalService<any>
37 customerGroupCustomerService: ModulesSdkTypes.IMedusaInternalService<any>
38 }
39
40 export default class CustomerModuleService
41 extends MedusaService<{
42 CustomerAddress: { dto: CustomerAddressDTO }
43 Customer: { dto: CustomerDTO }
44 CustomerGroup: { dto: CustomerGroupDTO }
45 CustomerGroupCustomer: { dto: CustomerGroupCustomerDTO }
46 }>({
47 CustomerAddress,
48 Customer,
49 CustomerGroup,
50 CustomerGroupCustomer,
51 })
52 implements ICustomerModuleService
53 {
54 protected baseRepository_: DAL.RepositoryService
55 protected customerService_: ModulesSdkTypes.IMedusaInternalService<
56 InferEntityType<typeof Customer>
57 >
58 protected customerAddressService_: ModulesSdkTypes.IMedusaInternalService<
59 InferEntityType<typeof CustomerAddress>
60 >
61 protected customerGroupService_: ModulesSdkTypes.IMedusaInternalService<
62 InferEntityType<typeof CustomerGroup>
63 >
64 protected customerGroupCustomerService_: ModulesSdkTypes.IMedusaInternalService<
65 InferEntityType<typeof CustomerGroupCustomer>
66 >
67
68 constructor(
69 {
70 baseRepository,
71 customerService,
72 customerAddressService,
73 customerGroupService,
74 customerGroupCustomerService,
75 }: InjectedDependencies,
76 protected readonly moduleDeclaration: InternalModuleDeclaration
77 ) {
78 // @ts-ignore
79 super(...arguments)
80
81 this.baseRepository_ = baseRepository
82 this.customerService_ = customerService
83 this.customerAddressService_ = customerAddressService
84 this.customerGroupService_ = customerGroupService
85 this.customerGroupCustomerService_ = customerGroupCustomerService
86 }
87
88 __joinerConfig(): ModuleJoinerConfig {
89 return joinerConfig
90 }
91
92 // @ts-expect-error
93 async createCustomers(
94 data: CustomerTypes.CreateCustomerDTO,
95 sharedContext?: Context
96 ): Promise<CustomerTypes.CustomerDTO>
97
98 async createCustomers(
99 data: CustomerTypes.CreateCustomerDTO[],
100 sharedContext?: Context
101 ): Promise<CustomerTypes.CustomerDTO[]>
102
103 @InjectManager()
104 @EmitEvents()
105 async createCustomers(
106 dataOrArray:
107 | CustomerTypes.CreateCustomerDTO
108 | CustomerTypes.CreateCustomerDTO[],
109 @MedusaContext() sharedContext: Context = {}
110 ): Promise<CustomerTypes.CustomerDTO | CustomerTypes.CustomerDTO[]> {
111 const customers = await this.createCustomers_(dataOrArray, sharedContext)
112
113 const serialized = await this.baseRepository_.serialize<
114 CustomerTypes.CustomerDTO[]
115 >(customers, {
116 populate: true,
117 })
118
119 return Array.isArray(dataOrArray) ? serialized : serialized[0]
120 }
121
122 @InjectTransactionManager()
123 async createCustomers_(
124 dataOrArray:
125 | CustomerTypes.CreateCustomerDTO
126 | CustomerTypes.CreateCustomerDTO[],
127 @MedusaContext() sharedContext: Context = {}
128 ): Promise<InferEntityType<typeof Customer>[]> {
129 const data = Array.isArray(dataOrArray) ? dataOrArray : [dataOrArray]
130 const customerAttributes = data.map(({ addresses, ...rest }) => {
131 return rest
132 })
133
134 const customers = await this.customerService_.create(
135 customerAttributes,
136 sharedContext
137 )
138
139 const addressDataWithCustomerIds = data
140 .map(({ addresses }, i) => {
141 if (!addresses) {
142 return []
143 }
144
145 return addresses.map((address) => ({
146 ...address,
147 customer_id: customers[i].id,
148 }))
149 })
150 .flat()
151
152 await this.createCustomerAddresses(
153 addressDataWithCustomerIds,
154 sharedContext
155 )
156
157 return customers
158 }
159
160 // @ts-expect-error
161 updateCustomers(
162 customerId: string,
163 data: CustomerTypes.CustomerUpdatableFields,
164 sharedContext?: Context
165 ): Promise<CustomerTypes.CustomerDTO>
166 // @ts-expect-error
167 updateCustomers(
168 customerIds: string[],
169 data: CustomerTypes.CustomerUpdatableFields,
170 sharedContext?: Context
171 ): Promise<CustomerTypes.CustomerDTO[]>
172 // @ts-expect-error
173 updateCustomers(
174 selector: CustomerTypes.FilterableCustomerProps,
175 data: CustomerTypes.CustomerUpdatableFields,
176 sharedContext?: Context
177 ): Promise<CustomerTypes.CustomerDTO[]>
178
179 @InjectManager()
180 @EmitEvents()
181 // @ts-expect-error
182 async updateCustomers(
183 idsOrSelector: string | string[] | CustomerTypes.FilterableCustomerProps,
184 data: CustomerTypes.CustomerUpdatableFields,
185 @MedusaContext() sharedContext: Context = {}
186 ): Promise<CustomerTypes.CustomerDTO | CustomerTypes.CustomerDTO[]> {
187 const customers = await this.updateCustomers_(
188 idsOrSelector,
189 data,
190 sharedContext
191 )
192
193 return await this.baseRepository_.serialize<
194 CustomerTypes.CustomerDTO | CustomerTypes.CustomerDTO[]
195 >(customers)
196 }
197
198 @InjectTransactionManager()
199 protected async updateCustomers_(
200 idsOrSelector: string | string[] | CustomerTypes.FilterableCustomerProps,
201 data: CustomerTypes.CustomerUpdatableFields,
202 @MedusaContext() sharedContext: Context = {}
203 ): Promise<
204 InferEntityType<typeof Customer>[] | InferEntityType<typeof Customer>
205 > {
206 let updateData:
207 | CustomerTypes.UpdateCustomerDTO
208 | CustomerTypes.UpdateCustomerDTO[]
209 | {
210 selector: CustomerTypes.FilterableCustomerProps
211 data: CustomerTypes.CustomerUpdatableFields
212 }
213
214 if (isString(idsOrSelector)) {
215 updateData = {
216 id: idsOrSelector,
217 ...data,
218 }
219 } else if (Array.isArray(idsOrSelector)) {
220 updateData = idsOrSelector.map((id) => ({
221 id,
222 ...data,
223 }))
224 } else {
225 updateData = {
226 selector: idsOrSelector,
227 data: data,
228 }
229 }
230
231 const customers = await this.customerService_.update(
232 updateData,
233 sharedContext
234 )
235
236 return customers
237 }
238
239 // @ts-expect-error
240 async createCustomerGroups(
241 dataOrArrayOfData: CustomerTypes.CreateCustomerGroupDTO,
242 sharedContext?: Context
243 ): Promise<CustomerTypes.CustomerGroupDTO>
244
245 // @ts-expect-error
246 async createCustomerGroups(
247 dataOrArrayOfData: CustomerTypes.CreateCustomerGroupDTO[],
248 sharedContext?: Context
249 ): Promise<CustomerTypes.CustomerGroupDTO[]>
250
251 @InjectManager()
252 @EmitEvents()
253 // @ts-expect-error
254 async createCustomerGroups(
255 dataOrArrayOfData:
256 | CustomerTypes.CreateCustomerGroupDTO
257 | CustomerTypes.CreateCustomerGroupDTO[],
258 @MedusaContext() sharedContext: Context = {}
259 ): Promise<
260 CustomerTypes.CustomerGroupDTO | CustomerTypes.CustomerGroupDTO[]
261 > {
262 const groups = await this.createCustomerGroups_(
263 dataOrArrayOfData,
264 sharedContext
265 )
266
267 return await this.baseRepository_.serialize<
268 CustomerTypes.CustomerGroupDTO | CustomerTypes.CustomerGroupDTO[]
269 >(groups)
270 }
271
272 @InjectTransactionManager()
273 protected async createCustomerGroups_(
274 dataOrArrayOfData:
275 | CustomerTypes.CreateCustomerGroupDTO
276 | CustomerTypes.CreateCustomerGroupDTO[],
277 @MedusaContext() sharedContext: Context = {}
278 ): Promise<
279 | InferEntityType<typeof CustomerGroup>[]
280 | InferEntityType<typeof CustomerGroup>
281 > {
282 const groups = await this.customerGroupService_.create(
283 dataOrArrayOfData,
284 sharedContext
285 )
286
287 return groups
288 }
289
290 // @ts-expect-error
291 async updateCustomerGroups(
292 groupId: string,
293 data: CustomerTypes.CustomerGroupUpdatableFields,
294 sharedContext?: Context
295 ): Promise<CustomerTypes.CustomerGroupDTO>
296 // @ts-expect-error
297 async updateCustomerGroups(
298 groupIds: string[],
299 data: CustomerTypes.CustomerGroupUpdatableFields,
300 sharedContext?: Context
301 ): Promise<CustomerTypes.CustomerGroupDTO[]>
302 // @ts-expect-error
303 async updateCustomerGroups(
304 selector: CustomerTypes.FilterableCustomerGroupProps,
305 data: CustomerTypes.CustomerGroupUpdatableFields,
306 sharedContext?: Context
307 ): Promise<CustomerTypes.CustomerGroupDTO[]>
308
309 @InjectManager()
310 @EmitEvents()
311 // @ts-expect-error
312 async updateCustomerGroups(
313 groupIdOrSelector:
314 | string
315 | string[]
316 | CustomerTypes.FilterableCustomerGroupProps,
317 data: CustomerTypes.CustomerGroupUpdatableFields,
318 @MedusaContext() sharedContext: Context = {}
319 ): Promise<
320 CustomerTypes.CustomerGroupDTO | CustomerTypes.CustomerGroupDTO[]
321 > {
322 const groups = await this.updateCustomerGroups_(
323 groupIdOrSelector,
324 data,
325 sharedContext
326 )
327
328 return await this.baseRepository_.serialize<
329 CustomerTypes.CustomerGroupDTO | CustomerTypes.CustomerGroupDTO[]
330 >(groups)
331 }
332
333 @InjectTransactionManager()
334 protected async updateCustomerGroups_(
335 groupIdOrSelector:
336 | string
337 | string[]
338 | CustomerTypes.FilterableCustomerGroupProps,
339 data: CustomerTypes.CustomerGroupUpdatableFields,
340 @MedusaContext() sharedContext: Context = {}
341 ): Promise<
342 | InferEntityType<typeof CustomerGroup>[]
343 | InferEntityType<typeof CustomerGroup>
344 > {
345 let updateData:
346 | CustomerTypes.UpdateCustomerGroupDTO
347 | CustomerTypes.UpdateCustomerGroupDTO[]
348 | {
349 selector: CustomerTypes.FilterableCustomerGroupProps
350 data: CustomerTypes.CustomerGroupUpdatableFields
351 }
352
353 if (isString(groupIdOrSelector) || Array.isArray(groupIdOrSelector)) {
354 const groupIdOrSelectorArray = Array.isArray(groupIdOrSelector)
355 ? groupIdOrSelector
356 : [groupIdOrSelector]
357 updateData = groupIdOrSelectorArray.map((id) => ({
358 id,
359 ...data,
360 }))
361 } else {
362 updateData = {
363 selector: groupIdOrSelector,
364 data: data,
365 }
366 }
367
368 const groups = await this.customerGroupService_.update(
369 updateData,
370 sharedContext
371 )
372
373 if (isString(groupIdOrSelector)) {
374 return groups[0]
375 }
376
377 return groups
378 }
379
380 async addCustomerToGroup(
381 groupCustomerPair: CustomerTypes.GroupCustomerPair,
382 sharedContext?: Context
383 ): Promise<{ id: string }>
384
385 async addCustomerToGroup(
386 groupCustomerPairs: CustomerTypes.GroupCustomerPair[],
387 sharedContext?: Context
388 ): Promise<{ id: string }[]>
389
390 @InjectManager()
391 @EmitEvents()
392 async addCustomerToGroup(
393 data: CustomerTypes.GroupCustomerPair | CustomerTypes.GroupCustomerPair[],
394 @MedusaContext() sharedContext: Context = {}
395 ): Promise<{ id: string } | { id: string }[]> {
396 const groupCustomers = await this.addCustomerToGroup_(data, sharedContext)
397
398 return groupCustomers
399 }
400
401 @InjectTransactionManager()
402 protected async addCustomerToGroup_(
403 data: CustomerTypes.GroupCustomerPair | CustomerTypes.GroupCustomerPair[],
404 @MedusaContext() sharedContext: Context = {}
405 ): Promise<{ id: string } | { id: string }[]> {
406 const groupCustomers = await this.customerGroupCustomerService_.create(
407 data,
408 sharedContext
409 )
410
411 if (Array.isArray(data)) {
412 return (
413 groupCustomers as unknown as InferEntityType<
414 typeof CustomerGroupCustomer
415 >[]
416 ).map((gc) => ({ id: gc.id }))
417 }
418
419 return { id: groupCustomers.id }
420 }
421
422 // @ts-expect-error
423 async createCustomerAddresses(
424 addresses: CustomerTypes.CreateCustomerAddressDTO[],
425 sharedContext?: Context
426 ): Promise<CustomerTypes.CustomerAddressDTO[]>
427 // @ts-expect-error
428 async createCustomerAddresses(
429 address: CustomerTypes.CreateCustomerAddressDTO,
430 sharedContext?: Context
431 ): Promise<CustomerTypes.CustomerAddressDTO>
432
433 @InjectManager()
434 @EmitEvents()
435 // @ts-expect-error
436 async createCustomerAddresses(
437 data:
438 | CustomerTypes.CreateCustomerAddressDTO
439 | CustomerTypes.CreateCustomerAddressDTO[],
440 @MedusaContext() sharedContext: Context = {}
441 ): Promise<
442 CustomerTypes.CustomerAddressDTO | CustomerTypes.CustomerAddressDTO[]
443 > {
444 const addresses = await this.createCustomerAddresses_(data, sharedContext)
445
446 const serialized = await this.baseRepository_.serialize<
447 CustomerTypes.CustomerAddressDTO[]
448 >(addresses, { populate: true })
449
450 if (Array.isArray(data)) {
451 return serialized
452 }
453
454 return serialized[0]
455 }
456
457 @InjectTransactionManager()
458 private async createCustomerAddresses_(
459 data:
460 | CustomerTypes.CreateCustomerAddressDTO
461 | CustomerTypes.CreateCustomerAddressDTO[],
462 @MedusaContext() sharedContext: Context = {}
463 ) {
464 return await this.customerAddressService_.create(
465 Array.isArray(data) ? data : [data],
466 sharedContext
467 )
468 }
469
470 // @ts-expect-error
471 async updateCustomerAddresses(
472 addressId: string,
473 data: CustomerTypes.UpdateCustomerAddressDTO,
474 sharedContext?: Context
475 ): Promise<CustomerTypes.CustomerAddressDTO>
476 // @ts-expect-error
477 async updateCustomerAddresses(
478 addressIds: string[],
479 data: CustomerTypes.UpdateCustomerAddressDTO,
480 sharedContext?: Context
481 ): Promise<CustomerTypes.CustomerAddressDTO[]>
482 // @ts-expect-error
483 async updateCustomerAddresses(
484 selector: CustomerTypes.FilterableCustomerAddressProps,
485 data: CustomerTypes.UpdateCustomerAddressDTO,
486 sharedContext?: Context
487 ): Promise<CustomerTypes.CustomerAddressDTO[]>
488
489 @InjectManager()
490 @EmitEvents()
491 // @ts-expect-error
492 async updateCustomerAddresses(
493 addressIdOrSelector:
494 | string
495 | string[]
496 | CustomerTypes.FilterableCustomerAddressProps,
497 data: CustomerTypes.UpdateCustomerAddressDTO,
498 @MedusaContext() sharedContext: Context = {}
499 ): Promise<
500 CustomerTypes.CustomerAddressDTO | CustomerTypes.CustomerAddressDTO[]
501 > {
502 const addresses = await this.updateCustomerAddresses_(
503 addressIdOrSelector,
504 data,
505 sharedContext
506 )
507
508 return await this.baseRepository_.serialize<
509 CustomerTypes.CustomerAddressDTO | CustomerTypes.CustomerAddressDTO[]
510 >(addresses)
511 }
512
513 @InjectTransactionManager()
514 protected async updateCustomerAddresses_(
515 addressIdOrSelector:
516 | string
517 | string[]
518 | CustomerTypes.FilterableCustomerAddressProps,
519 data: CustomerTypes.UpdateCustomerAddressDTO,
520 @MedusaContext() sharedContext: Context = {}
521 ): Promise<
522 | InferEntityType<typeof CustomerAddress>[]
523 | InferEntityType<typeof CustomerAddress>
524 > {
525 let updateData:
526 | CustomerTypes.UpdateCustomerAddressDTO[]
527 | {
528 selector: CustomerTypes.FilterableCustomerAddressProps
529 data: CustomerTypes.UpdateCustomerAddressDTO
530 }
531 if (isString(addressIdOrSelector)) {
532 updateData = [
533 {
534 id: addressIdOrSelector,
535 ...data,
536 },
537 ]
538 } else if (Array.isArray(addressIdOrSelector)) {
539 updateData = addressIdOrSelector.map((id) => ({
540 id,
541 ...data,
542 }))
543 } else {
544 updateData = {
545 selector: addressIdOrSelector,
546 data,
547 }
548 }
549
550 const addresses = await this.customerAddressService_.update(
551 updateData,
552 sharedContext
553 )
554
555 if (isString(addressIdOrSelector)) {
556 return addresses[0]
557 }
558
559 return addresses
560 }
561
562 async removeCustomerFromGroup(
563 groupCustomerPair: CustomerTypes.GroupCustomerPair,
564 sharedContext?: Context
565 ): Promise<void>
566 async removeCustomerFromGroup(
567 groupCustomerPairs: CustomerTypes.GroupCustomerPair[],
568 sharedContext?: Context
569 ): Promise<void>
570
571 @InjectManager()
572 @EmitEvents()
573 async removeCustomerFromGroup(
574 data: CustomerTypes.GroupCustomerPair | CustomerTypes.GroupCustomerPair[],
575 @MedusaContext() sharedContext: Context = {}
576 ): Promise<void> {
577 await this.removeCustomerFromGroup_(data, sharedContext)
578 }
579
580 @InjectTransactionManager()
581 protected async removeCustomerFromGroup_(
582 data: CustomerTypes.GroupCustomerPair | CustomerTypes.GroupCustomerPair[],
583 @MedusaContext() sharedContext: Context = {}
584 ): Promise<void> {
585 const pairs = Array.isArray(data) ? data : [data]
586 const groupCustomers = await this.customerGroupCustomerService_.list({
587 $or: pairs,
588 })
589 await this.customerGroupCustomerService_.delete(
590 groupCustomers.map((gc) => gc.id),
591 sharedContext
592 )
593 }
594 }