import { Context, FindConfig } from "@medusajs/types" import { InjectManager, InjectTransactionManager, MedusaContext, ModulesSdkUtils, } from "@medusajs/utils" import { doNotForceTransaction } from "../utils" type InjectedDependencies = { linkRepository: any } export default class LinkService { protected readonly linkRepository_: any constructor({ linkRepository }: InjectedDependencies) { this.linkRepository_ = linkRepository } @InjectManager("linkRepository_") async list( filters: unknown = {}, config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} ): Promise { const queryOptions = ModulesSdkUtils.buildQuery( filters as any, config ) return await this.linkRepository_.find(queryOptions, sharedContext) } @InjectManager("linkRepository_") async listAndCount( filters = {}, config: FindConfig = {}, @MedusaContext() sharedContext: Context = {} ): Promise<[TEntity[], number]> { const queryOptions = ModulesSdkUtils.buildQuery(filters, config) return await this.linkRepository_.findAndCount(queryOptions, sharedContext) } @InjectTransactionManager(doNotForceTransaction, "linkRepository_") async create( data: unknown[], @MedusaContext() sharedContext: Context = {} ): Promise { return await this.linkRepository_.create(data, { transactionManager: sharedContext.transactionManager, }) } @InjectTransactionManager(doNotForceTransaction, "linkRepository_") async dismiss( data: unknown[], @MedusaContext() sharedContext: Context = {} ): Promise { const filter: any = [] for (const pair of data) { filter.push({ $and: Object.entries(pair as object).map(([key, value]) => ({ [key]: value, })), }) } const [rows] = await this.linkRepository_.softDelete( { $or: filter }, { transactionManager: sharedContext.transactionManager, } ) return rows } @InjectTransactionManager(doNotForceTransaction, "linkRepository_") async delete( data: unknown, @MedusaContext() sharedContext: Context = {} ): Promise { await this.linkRepository_.delete(data, { transactionManager: sharedContext.transactionManager, }) } @InjectTransactionManager(doNotForceTransaction, "linkRepository_") async softDelete( data: any[], @MedusaContext() sharedContext: Context = {} ): Promise<[object[], Record]> { const deleteFilters = { $or: data.map((dataEntry) => { const filter = {} for (const key in dataEntry) { filter[key] = { $in: Array.isArray(dataEntry[key]) ? dataEntry[key] : [dataEntry[key]], } } return filter }), } return await this.linkRepository_.softDelete(deleteFilters, { transactionManager: sharedContext.transactionManager, }) } @InjectTransactionManager(doNotForceTransaction, "linkRepository_") async restore( data: any, @MedusaContext() sharedContext: Context = {} ): Promise<[object[], Record]> { const restoreFilters = { $or: data.map((dataEntry) => { const filter = {} for (const key in dataEntry) { filter[key] = { $in: Array.isArray(dataEntry[key]) ? dataEntry[key] : [dataEntry[key]], } } return filter }), } return await this.linkRepository_.restore(restoreFilters, { transactionManager: sharedContext.transactionManager, }) } }