1 import { Context, FindConfig } from "@medusajs/framework/types"
2 import {
3 InjectManager,
4 InjectTransactionManager,
5 MedusaContext,
6 MikroOrmBaseRepository,
7 ModulesSdkUtils,
8 } from "@medusajs/framework/utils"
9
10 type InjectedDependencies = {
11 linkRepository: MikroOrmBaseRepository
12 }
13
14 export default class LinkService<TEntity> {
15 protected readonly linkRepository_: any
16
17 constructor({ linkRepository }: InjectedDependencies) {
18 this.linkRepository_ = linkRepository
19 }
20
21 @InjectManager("linkRepository_")
22 async list(
23 filters: unknown = {},
24 config: FindConfig<unknown> = {},
25 @MedusaContext() sharedContext: Context = {}
26 ): Promise<TEntity[]> {
27 const queryOptions = ModulesSdkUtils.buildQuery<unknown>(
28 filters as any,
29 config
30 )
31 return await this.linkRepository_.find(queryOptions, sharedContext)
32 }
33
34 @InjectManager("linkRepository_")
35 async listAndCount(
36 filters = {},
37 config: FindConfig<unknown> = {},
38 @MedusaContext() sharedContext: Context = {}
39 ): Promise<[TEntity[], number]> {
40 const queryOptions = ModulesSdkUtils.buildQuery<unknown>(filters, config)
41 return await this.linkRepository_.findAndCount(queryOptions, sharedContext)
42 }
43
44 @InjectTransactionManager("linkRepository_")
45 async create(
46 data: unknown[],
47 @MedusaContext() sharedContext: Context = {}
48 ): Promise<TEntity[]> {
49 return await this.linkRepository_.create(data, {
50 transactionManager: sharedContext.transactionManager,
51 })
52 }
53
54 @InjectTransactionManager("linkRepository_")
55 async dismiss(
56 data: unknown[],
57 @MedusaContext() sharedContext: Context = {}
58 ): Promise<TEntity[]> {
59 const filter: any = []
60 for (const pair of data) {
61 filter.push({
62 $and: Object.entries(pair as object).map(([key, value]) => ({
63 [key]: value,
64 })),
65 })
66 }
67
68 const [rows] = await this.linkRepository_.softDelete(
69 { $or: filter },
70 {
71 transactionManager: sharedContext.transactionManager,
72 }
73 )
74
75 return rows
76 }
77
78 @InjectTransactionManager("linkRepository_")
79 async delete(
80 data: unknown,
81 @MedusaContext() sharedContext: Context = {}
82 ): Promise<void> {
83 await this.linkRepository_.delete(data, {
84 transactionManager: sharedContext.transactionManager,
85 })
86 }
87
88 @InjectTransactionManager("linkRepository_")
89 async softDelete(
90 data: any[],
91 @MedusaContext() sharedContext: Context = {}
92 ): Promise<[object[], Record<string, string[]>]> {
93 const deleteFilters = {
94 $or: data.map((dataEntry) => {
95 const filter = {}
96 for (const key in dataEntry) {
97 filter[key] = {
98 $in: Array.isArray(dataEntry[key])
99 ? dataEntry[key]
100 : [dataEntry[key]],
101 }
102 }
103 return filter
104 }),
105 }
106
107 return await this.linkRepository_.softDelete(deleteFilters, {
108 transactionManager: sharedContext.transactionManager,
109 })
110 }
111
112 @InjectTransactionManager("linkRepository_")
113 async restore(
114 data: any,
115 @MedusaContext() sharedContext: Context = {}
116 ): Promise<[object[], Record<string, string[]>]> {
117 const restoreFilters = {
118 $or: data.map((dataEntry) => {
119 const filter = {}
120 for (const key in dataEntry) {
121 filter[key] = {
122 $in: Array.isArray(dataEntry[key])
123 ? dataEntry[key]
124 : [dataEntry[key]],
125 }
126 }
127 return filter
128 }),
129 }
130
131 return await this.linkRepository_.restore(restoreFilters, {
132 transactionManager: sharedContext.transactionManager,
133 })
134 }
135 }