Remove v1-related code from medusa app (#7326)
* chore: Remove unused validations and utilities * chore: Remove all resources that are not being loaded * chore: Remove unused dependencies, typeorm related code and fix tests * chore: Use createAdminUser in all module tests
This commit is contained in:
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
import { EntityManager } from "typeorm"
|
||||
import {
|
||||
BatchJobService,
|
||||
EventBusService,
|
||||
StrategyResolverService,
|
||||
} from "../services"
|
||||
|
||||
type InjectedDependencies = {
|
||||
eventBusService: EventBusService
|
||||
batchJobService: BatchJobService
|
||||
strategyResolverService: StrategyResolverService
|
||||
manager: EntityManager
|
||||
}
|
||||
|
||||
class BatchJobSubscriber {
|
||||
private readonly eventBusService_: EventBusService
|
||||
private readonly batchJobService_: BatchJobService
|
||||
private readonly strategyResolver_: StrategyResolverService
|
||||
private readonly manager_: EntityManager
|
||||
|
||||
constructor({
|
||||
eventBusService,
|
||||
batchJobService,
|
||||
strategyResolverService,
|
||||
manager,
|
||||
}: InjectedDependencies) {
|
||||
this.eventBusService_ = eventBusService
|
||||
this.batchJobService_ = batchJobService
|
||||
this.strategyResolver_ = strategyResolverService
|
||||
this.manager_ = manager
|
||||
|
||||
this.eventBusService_.subscribe(
|
||||
BatchJobService.Events.CREATED,
|
||||
this.preProcessBatchJob
|
||||
) as EventBusService
|
||||
|
||||
this.eventBusService_.subscribe(
|
||||
BatchJobService.Events.CONFIRMED,
|
||||
this.processBatchJob
|
||||
) as EventBusService
|
||||
}
|
||||
|
||||
preProcessBatchJob = async (data): Promise<void> => {
|
||||
try {
|
||||
await this.manager_.transaction(async (manager) => {
|
||||
const batchJobServiceTx = this.batchJobService_.withTransaction(manager)
|
||||
const batchJob = await batchJobServiceTx.retrieve(data.id)
|
||||
|
||||
const batchJobStrategy = this.strategyResolver_.resolveBatchJobByType(
|
||||
batchJob.type
|
||||
)
|
||||
|
||||
await batchJobStrategy
|
||||
.withTransaction(manager)
|
||||
.preProcessBatchJob(batchJob.id)
|
||||
await batchJobServiceTx.setPreProcessingDone(batchJob.id)
|
||||
})
|
||||
} catch (e) {
|
||||
await this.batchJobService_.setFailed(data.id, e.message)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
processBatchJob = async (data): Promise<void> => {
|
||||
try {
|
||||
await this.manager_.transaction(async (manager) => {
|
||||
const batchJobServiceTx = this.batchJobService_.withTransaction(manager)
|
||||
const batchJob = await batchJobServiceTx.retrieve(data.id)
|
||||
|
||||
const batchJobStrategy = this.strategyResolver_.resolveBatchJobByType(
|
||||
batchJob.type
|
||||
)
|
||||
|
||||
await batchJobServiceTx.setProcessing(batchJob.id)
|
||||
await batchJobStrategy.withTransaction(manager).processJob(batchJob.id)
|
||||
await batchJobServiceTx.complete(batchJob.id)
|
||||
})
|
||||
} catch (e) {
|
||||
await this.batchJobService_.setFailed(data.id, e.message)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default BatchJobSubscriber
|
||||
*/
|
||||
@@ -1,52 +0,0 @@
|
||||
// TODO: we need to discuss this
|
||||
/*
|
||||
import { EntityManager } from "typeorm"
|
||||
import { CartService, EventBusService } from "../services"
|
||||
|
||||
type InjectedDependencies = {
|
||||
eventBusService: EventBusService
|
||||
cartService: CartService
|
||||
manager: EntityManager
|
||||
}
|
||||
|
||||
class CartSubscriber {
|
||||
protected readonly manager_: EntityManager
|
||||
protected readonly cartService_: CartService
|
||||
protected readonly eventBus_: EventBusService
|
||||
|
||||
constructor({ manager, cartService, eventBusService }: InjectedDependencies) {
|
||||
this.cartService_ = cartService
|
||||
this.eventBus_ = eventBusService
|
||||
this.manager_ = manager
|
||||
|
||||
this.eventBus_.subscribe(
|
||||
CartService.Events.CUSTOMER_UPDATED,
|
||||
async (cartId) => {
|
||||
await this.onCustomerUpdated(cartId)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
async onCustomerUpdated(cartId) {
|
||||
await this.manager_.transaction(
|
||||
"SERIALIZABLE",
|
||||
async (transactionManager) => {
|
||||
const cartServiceTx =
|
||||
this.cartService_.withTransaction(transactionManager)
|
||||
|
||||
const cart = await cartServiceTx.retrieve(cartId, {
|
||||
relations: ["payment_sessions"],
|
||||
})
|
||||
|
||||
if (!cart.payment_sessions?.length) {
|
||||
return
|
||||
}
|
||||
|
||||
return await cartServiceTx.setPaymentSessions(cart.id)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default CartSubscriber
|
||||
*/
|
||||
@@ -1,15 +0,0 @@
|
||||
class NotificationSubscriber {
|
||||
constructor({ eventBusService, notificationService }) {
|
||||
this.notificationService_ = notificationService
|
||||
|
||||
this.eventBus_ = eventBusService
|
||||
|
||||
this.eventBus_.subscribe("*", this.onEvent)
|
||||
}
|
||||
|
||||
onEvent = (data, eventName) => {
|
||||
return this.notificationService_.handleEvent(eventName, data)
|
||||
}
|
||||
}
|
||||
|
||||
export default NotificationSubscriber
|
||||
@@ -1,67 +0,0 @@
|
||||
// TODO: we need to discuss this
|
||||
/*
|
||||
import { promiseAll } from "@medusajs/utils"
|
||||
|
||||
class OrderSubscriber {
|
||||
constructor({
|
||||
manager,
|
||||
eventBusService,
|
||||
discountService,
|
||||
giftCardService,
|
||||
totalsService,
|
||||
orderService,
|
||||
draftOrderService,
|
||||
regionService,
|
||||
}) {
|
||||
this.manager_ = manager
|
||||
this.totalsService_ = totalsService
|
||||
|
||||
this.discountService_ = discountService
|
||||
|
||||
this.giftCardService_ = giftCardService
|
||||
|
||||
this.orderService_ = orderService
|
||||
|
||||
this.draftOrderService_ = draftOrderService
|
||||
|
||||
this.regionService_ = regionService
|
||||
|
||||
this.eventBus_ = eventBusService
|
||||
|
||||
this.eventBus_.subscribe("order.placed", this.handleOrderPlaced)
|
||||
|
||||
this.eventBus_.subscribe("order.placed", this.updateDraftOrder)
|
||||
}
|
||||
|
||||
handleOrderPlaced = async (data) => {
|
||||
const order = await this.orderService_.retrieveWithTotals(data.id, {
|
||||
relations: ["discounts", "discounts.rule"],
|
||||
})
|
||||
|
||||
await promiseAll(
|
||||
order.discounts.map(async (d) => {
|
||||
const usageCount = d?.usage_count || 0
|
||||
return this.discountService_.update(d.id, {
|
||||
usage_count: usageCount + 1,
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
updateDraftOrder = async (data) => {
|
||||
const order = await this.orderService_.retrieve(data.id)
|
||||
const draftOrder = await this.draftOrderService_
|
||||
.retrieveByCartId(order.cart_id)
|
||||
.catch((_) => null)
|
||||
|
||||
if (draftOrder) {
|
||||
await this.draftOrderService_.registerCartCompletion(
|
||||
draftOrder.id,
|
||||
order.id
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default OrderSubscriber
|
||||
*/
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
ProviderWebhookPayload,
|
||||
Subscriber,
|
||||
} from "@medusajs/types"
|
||||
import { EventBusService } from "../services"
|
||||
|
||||
type SerializedBuffer = {
|
||||
data: ArrayBuffer
|
||||
@@ -15,7 +14,7 @@ type SerializedBuffer = {
|
||||
|
||||
type InjectedDependencies = {
|
||||
paymentModuleService: IPaymentModuleService
|
||||
eventBusService: EventBusService
|
||||
eventBusService: IEventBusService
|
||||
}
|
||||
|
||||
class PaymentWebhookSubscriber {
|
||||
|
||||
@@ -1,122 +0,0 @@
|
||||
// TODO: we need to discuss this
|
||||
/*
|
||||
import { IEventBusService, ISearchService } from "@medusajs/types"
|
||||
import { defaultSearchIndexingProductRelations, FlagRouter } from "@medusajs/utils"
|
||||
import { indexTypes } from "medusa-core-utils"
|
||||
import { isSearchEngineInstalledResolutionKey } from "../loaders/plugins"
|
||||
import ProductService from "../services/product"
|
||||
import ProductVariantService from "../services/product-variant"
|
||||
import ProductCategoryFeatureFlag from "../loaders/feature-flags/product-categories";
|
||||
|
||||
type InjectedDependencies = {
|
||||
eventBusService: IEventBusService
|
||||
searchService: ISearchService
|
||||
productService: ProductService
|
||||
featureFlagRouter: FlagRouter
|
||||
}
|
||||
|
||||
class ProductSearchSubscriber {
|
||||
private readonly eventBusService_: IEventBusService
|
||||
private readonly searchService_: ISearchService
|
||||
private readonly productService_: ProductService
|
||||
private readonly featureFlagRouter_: FlagRouter
|
||||
|
||||
constructor(container: InjectedDependencies) {
|
||||
this.eventBusService_ = container.eventBusService
|
||||
this.searchService_ = container.searchService
|
||||
this.productService_ = container.productService
|
||||
this.featureFlagRouter_ = container.featureFlagRouter
|
||||
|
||||
/!**
|
||||
* Do not subscribe to any event in case no search engine have been installed.
|
||||
* If some events need to be subscribed out of the search engine reason, they can be subscribed above this comment
|
||||
*!/
|
||||
|
||||
try {
|
||||
container[isSearchEngineInstalledResolutionKey]
|
||||
} catch (e) {
|
||||
return this
|
||||
}
|
||||
|
||||
this.eventBusService_
|
||||
.subscribe(ProductService.Events.CREATED, this.handleProductCreation)
|
||||
.subscribe(ProductService.Events.UPDATED, this.handleProductUpdate)
|
||||
.subscribe(ProductService.Events.DELETED, this.handleProductDeletion)
|
||||
.subscribe(
|
||||
ProductVariantService.Events.CREATED,
|
||||
this.handleProductVariantChange
|
||||
)
|
||||
.subscribe(
|
||||
ProductVariantService.Events.UPDATED,
|
||||
this.handleProductVariantChange
|
||||
)
|
||||
.subscribe(
|
||||
ProductVariantService.Events.DELETED,
|
||||
this.handleProductVariantChange
|
||||
)
|
||||
}
|
||||
|
||||
handleProductCreation = async (data) => {
|
||||
const relations = [...defaultSearchIndexingProductRelations]
|
||||
if (
|
||||
this.featureFlagRouter_.isFeatureEnabled(ProductCategoryFeatureFlag.key)
|
||||
) {
|
||||
relations.push("categories")
|
||||
}
|
||||
|
||||
const product = await this.productService_.retrieve(data.id, {
|
||||
relations,
|
||||
})
|
||||
|
||||
await this.searchService_.addDocuments(
|
||||
ProductService.IndexName,
|
||||
[product],
|
||||
indexTypes.products
|
||||
)
|
||||
}
|
||||
|
||||
handleProductUpdate = async (data) => {
|
||||
const relations = [...defaultSearchIndexingProductRelations]
|
||||
if (
|
||||
this.featureFlagRouter_.isFeatureEnabled(ProductCategoryFeatureFlag.key)
|
||||
) {
|
||||
relations.push("categories")
|
||||
}
|
||||
|
||||
const product = await this.productService_.retrieve(data.id, {
|
||||
relations,
|
||||
})
|
||||
|
||||
await this.searchService_.addDocuments(
|
||||
ProductService.IndexName,
|
||||
[product],
|
||||
indexTypes.products
|
||||
)
|
||||
}
|
||||
|
||||
handleProductDeletion = async (data) => {
|
||||
await this.searchService_.deleteDocument(ProductService.IndexName, data.id)
|
||||
}
|
||||
|
||||
handleProductVariantChange = async (data) => {
|
||||
const relations = [...defaultSearchIndexingProductRelations]
|
||||
if (
|
||||
this.featureFlagRouter_.isFeatureEnabled(ProductCategoryFeatureFlag.key)
|
||||
) {
|
||||
relations.push("categories")
|
||||
}
|
||||
|
||||
const product = await this.productService_.retrieve(data.product_id, {
|
||||
relations,
|
||||
})
|
||||
|
||||
await this.searchService_.addDocuments(
|
||||
ProductService.IndexName,
|
||||
[product],
|
||||
indexTypes.products
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default ProductSearchSubscriber
|
||||
*/
|
||||
@@ -1,83 +0,0 @@
|
||||
// TODO: we need to discuss this
|
||||
/*
|
||||
import { IEventBusService, ISearchService } from "@medusajs/types"
|
||||
import { FlagRouter, defaultSearchIndexingProductRelations } from "@medusajs/utils"
|
||||
import { indexTypes } from "medusa-core-utils"
|
||||
import ProductCategoryFeatureFlag from "../loaders/feature-flags/product-categories"
|
||||
import { SEARCH_INDEX_EVENT } from "../loaders/search-index"
|
||||
import { Product } from "../models"
|
||||
import ProductService from "../services/product"
|
||||
|
||||
type InjectedDependencies = {
|
||||
eventBusService: IEventBusService
|
||||
searchService: ISearchService
|
||||
productService: ProductService
|
||||
featureFlagRouter: FlagRouter
|
||||
}
|
||||
|
||||
class SearchIndexingSubscriber {
|
||||
private readonly eventBusService_: IEventBusService
|
||||
private readonly searchService_: ISearchService
|
||||
private readonly productService_: ProductService
|
||||
private readonly featureFlagRouter_: FlagRouter
|
||||
|
||||
constructor({
|
||||
eventBusService,
|
||||
searchService,
|
||||
productService,
|
||||
featureFlagRouter,
|
||||
}: InjectedDependencies) {
|
||||
this.eventBusService_ = eventBusService
|
||||
this.searchService_ = searchService
|
||||
this.productService_ = productService
|
||||
this.featureFlagRouter_ = featureFlagRouter
|
||||
|
||||
this.eventBusService_.subscribe(SEARCH_INDEX_EVENT, this.indexDocuments)
|
||||
}
|
||||
|
||||
indexDocuments = async (): Promise<void> => {
|
||||
const TAKE = (this.searchService_?.options?.batch_size as number) ?? 1000
|
||||
let hasMore = true
|
||||
|
||||
let lastSeenId = ""
|
||||
|
||||
while (hasMore) {
|
||||
const products = await this.retrieveNextProducts(lastSeenId, TAKE)
|
||||
|
||||
if (products.length > 0) {
|
||||
await this.searchService_.addDocuments(
|
||||
ProductService.IndexName,
|
||||
products,
|
||||
indexTypes.products
|
||||
)
|
||||
lastSeenId = products[products.length - 1].id
|
||||
} else {
|
||||
hasMore = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected async retrieveNextProducts(
|
||||
lastSeenId: string,
|
||||
take: number
|
||||
): Promise<Product[]> {
|
||||
const relations = [...defaultSearchIndexingProductRelations]
|
||||
if (
|
||||
this.featureFlagRouter_.isFeatureEnabled(ProductCategoryFeatureFlag.key)
|
||||
) {
|
||||
relations.push("categories")
|
||||
}
|
||||
|
||||
return await this.productService_.list(
|
||||
{ id: { gt: lastSeenId } },
|
||||
{
|
||||
relations,
|
||||
take: take,
|
||||
order: { id: "ASC" },
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default SearchIndexingSubscriber
|
||||
*/
|
||||
Reference in New Issue
Block a user