1 import {
2 Context,
3 DAL,
4 FindConfig,
5 InferEntityType,
6 OrderTypes,
7 RepositoryService,
8 } from "@medusajs/framework/types"
9 import {
10 InjectManager,
11 MedusaContext,
12 MedusaError,
13 ModulesSdkUtils,
14 } from "@medusajs/framework/utils"
15 import { Order } from "@models"
16
17 type InjectedDependencies = {
18 orderRepository: DAL.RepositoryService
19 }
20
21 export default class OrderService extends ModulesSdkUtils.MedusaInternalService<
22 InjectedDependencies,
23 InferEntityType<typeof Order>
24 >(Order) {
25 protected readonly orderRepository_: RepositoryService<
26 InferEntityType<typeof Order>
27 >
28
29 constructor(container: InjectedDependencies) {
30 // @ts-ignore
31 super(...arguments)
32 this.orderRepository_ = container.orderRepository
33 }
34
35 @InjectManager("orderRepository_")
36 async retrieveOrderVersion<TEntityMethod = OrderTypes.OrderDTO>(
37 id: string,
38 version: number,
39 config: FindConfig<TEntityMethod> = {},
40 @MedusaContext() sharedContext: Context = {}
41 ): Promise<typeof Order> {
42 const queryConfig = ModulesSdkUtils.buildQuery<typeof Order>(
43 { id, items: { version } },
44 { ...config, take: 1 } as any
45 )
46 const [result] = await this.orderRepository_.find(
47 queryConfig,
48 sharedContext
49 )
50
51 if (!result) {
52 throw new MedusaError(
53 MedusaError.Types.NOT_FOUND,
54 `Order with id: "${id}" and version: "${version}" not found`
55 )
56 }
57
58 return result
59 }
60 }