Feat(medusa) refactor claim-item service to typescript (#1640)
* refactor claim-item service to typescript * update pr
This commit is contained in:
@@ -14,12 +14,8 @@ import { MedusaError } from "medusa-core-utils"
|
||||
import { defaultAdminOrdersFields, defaultAdminOrdersRelations } from "."
|
||||
import { AddressPayload } from "../../../../types/common"
|
||||
import { validator } from "../../../../utils/validator"
|
||||
import {
|
||||
ClaimItemReason,
|
||||
ClaimItemReasonValue,
|
||||
ClaimTypeValue,
|
||||
} from "../../../../types/claim"
|
||||
import { ClaimType } from "../../../../models"
|
||||
import { ClaimTypeValue } from "../../../../types/claim"
|
||||
import { ClaimType, ClaimReason } from "../../../../models"
|
||||
|
||||
/**
|
||||
* @oas [post] /order/{id}/claims
|
||||
@@ -422,9 +418,9 @@ class Item {
|
||||
@IsOptional()
|
||||
note?: string
|
||||
|
||||
@IsEnum(ClaimItemReason)
|
||||
@IsEnum(ClaimReason)
|
||||
@IsOptional()
|
||||
reason?: ClaimItemReasonValue
|
||||
reason?: ClaimReason
|
||||
|
||||
@IsArray()
|
||||
@IsOptional()
|
||||
|
||||
@@ -1,13 +1,32 @@
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import { EntityManager } from "typeorm"
|
||||
import { TransactionBaseService as BaseService } from "../interfaces"
|
||||
import { ClaimImage, ClaimItem, ClaimTag } from "../models"
|
||||
import { ClaimImageRepository } from "../repositories/claim-image"
|
||||
import { ClaimItemRepository } from "../repositories/claim-item"
|
||||
import { ClaimTagRepository } from "../repositories/claim-tag"
|
||||
import { CreateClaimItemInput } from "../types/claim"
|
||||
import { FindConfig, Selector } from "../types/common"
|
||||
import { buildQuery, setMetadata } from "../utils"
|
||||
import EventBusService from "./event-bus"
|
||||
import LineItemService from "./line-item"
|
||||
|
||||
class ClaimItemService extends BaseService {
|
||||
class ClaimItemService extends BaseService<ClaimItemService> {
|
||||
static Events = {
|
||||
CREATED: "claim_item.created",
|
||||
UPDATED: "claim_item.updated",
|
||||
CANCELED: "claim_item.canceled",
|
||||
}
|
||||
|
||||
protected readonly lineItemService_: LineItemService
|
||||
protected readonly eventBus_: EventBusService
|
||||
protected readonly claimItemRepository_: typeof ClaimItemRepository
|
||||
protected readonly claimTagRepository_: typeof ClaimTagRepository
|
||||
protected readonly claimImageRepository_: typeof ClaimImageRepository
|
||||
|
||||
protected manager_: EntityManager
|
||||
protected transactionManager_: EntityManager | undefined
|
||||
|
||||
constructor({
|
||||
manager,
|
||||
claimItemRepository,
|
||||
@@ -16,45 +35,22 @@ class ClaimItemService extends BaseService {
|
||||
lineItemService,
|
||||
eventBusService,
|
||||
}) {
|
||||
super()
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(arguments[0])
|
||||
|
||||
/** @private @constant {EntityManager} */
|
||||
this.manager_ = manager
|
||||
|
||||
/** @private @constant {ClaimRepository} */
|
||||
this.claimItemRepository_ = claimItemRepository
|
||||
this.claimTagRepository_ = claimTagRepository
|
||||
this.claimImageRepository_ = claimImageRepository
|
||||
|
||||
/** @private @constant {LineItemService} */
|
||||
this.lineItemService_ = lineItemService
|
||||
|
||||
/** @private @constant {EventBus} */
|
||||
this.eventBus_ = eventBusService
|
||||
}
|
||||
|
||||
withTransaction(manager) {
|
||||
if (!manager) {
|
||||
return this
|
||||
}
|
||||
|
||||
const cloned = new ClaimItemService({
|
||||
manager,
|
||||
claimItemRepository: this.claimItemRepository_,
|
||||
claimTagRepository: this.claimTagRepository_,
|
||||
claimImageRepository: this.claimImageRepository_,
|
||||
lineItemService: this.lineItemService_,
|
||||
eventBusService: this.eventBus_,
|
||||
})
|
||||
|
||||
cloned.transactionManager_ = manager
|
||||
|
||||
return cloned
|
||||
}
|
||||
|
||||
create(data) {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const ciRepo = manager.getCustomRepository(this.claimItemRepository_)
|
||||
async create(data: CreateClaimItemInput): Promise<ClaimItem> {
|
||||
return await this.atomicPhase_(async (manager) => {
|
||||
const ciRepo: ClaimItemRepository = manager.getCustomRepository(
|
||||
this.claimItemRepository_
|
||||
)
|
||||
|
||||
const { item_id, reason, quantity, tags, images, ...rest } = data
|
||||
|
||||
@@ -81,7 +77,7 @@ class ClaimItemService extends BaseService {
|
||||
)
|
||||
}
|
||||
|
||||
let tagsToAdd = []
|
||||
let tagsToAdd: ClaimTag[] = []
|
||||
if (tags && tags.length) {
|
||||
const claimTagRepo = manager.getCustomRepository(
|
||||
this.claimTagRepository_
|
||||
@@ -100,7 +96,7 @@ class ClaimItemService extends BaseService {
|
||||
)
|
||||
}
|
||||
|
||||
let imagesToAdd = []
|
||||
let imagesToAdd: ClaimImage[] = []
|
||||
if (images && images.length) {
|
||||
const claimImgRepo = manager.getCustomRepository(
|
||||
this.claimImageRepository_
|
||||
@@ -110,7 +106,7 @@ class ClaimItemService extends BaseService {
|
||||
})
|
||||
}
|
||||
|
||||
const created = ciRepo.create({
|
||||
const toCreate: Partial<ClaimItem> = {
|
||||
...rest,
|
||||
variant_id: lineItem.variant_id,
|
||||
tags: tagsToAdd,
|
||||
@@ -118,7 +114,8 @@ class ClaimItemService extends BaseService {
|
||||
item_id,
|
||||
reason,
|
||||
quantity,
|
||||
})
|
||||
}
|
||||
const created = ciRepo.create(toCreate)
|
||||
|
||||
const result = await ciRepo.save(created)
|
||||
|
||||
@@ -132,7 +129,7 @@ class ClaimItemService extends BaseService {
|
||||
})
|
||||
}
|
||||
|
||||
update(id, data) {
|
||||
async update(id, data): Promise<ClaimItem> {
|
||||
return this.atomicPhase_(async (manager) => {
|
||||
const ciRepo = manager.getCustomRepository(this.claimItemRepository_)
|
||||
const item = await this.retrieve(id, { relations: ["images", "tags"] })
|
||||
@@ -148,7 +145,7 @@ class ClaimItemService extends BaseService {
|
||||
}
|
||||
|
||||
if (metadata) {
|
||||
item.metadata = this.setMetadata_(item, metadata)
|
||||
item.metadata = setMetadata(item, metadata)
|
||||
}
|
||||
|
||||
if (tags) {
|
||||
@@ -209,19 +206,21 @@ class ClaimItemService extends BaseService {
|
||||
})
|
||||
}
|
||||
|
||||
async cancel(id) {}
|
||||
|
||||
/**
|
||||
* @param {Object} selector - the query object for find
|
||||
* @param {Object} config - the config object for find
|
||||
* @return {Promise} the result of the find operation
|
||||
*/
|
||||
async list(
|
||||
selector,
|
||||
config = { skip: 0, take: 50, order: { created_at: "DESC" } }
|
||||
) {
|
||||
selector: Selector<ClaimItem>,
|
||||
config: FindConfig<ClaimItem> = {
|
||||
skip: 0,
|
||||
take: 50,
|
||||
order: { created_at: "DESC" },
|
||||
}
|
||||
): Promise<ClaimItem[]> {
|
||||
const ciRepo = this.manager_.getCustomRepository(this.claimItemRepository_)
|
||||
const query = this.buildQuery_(selector, config)
|
||||
const query = buildQuery(selector, config)
|
||||
return ciRepo.find(query)
|
||||
}
|
||||
|
||||
@@ -231,13 +230,14 @@ class ClaimItemService extends BaseService {
|
||||
* @param {Object} config - configuration for the find operation
|
||||
* @return {Promise<Order>} the ClaimItem
|
||||
*/
|
||||
async retrieve(id, config = {}) {
|
||||
async retrieve(
|
||||
id: string,
|
||||
config: FindConfig<ClaimItem> = {}
|
||||
): Promise<ClaimItem> {
|
||||
const claimItemRepo = this.manager_.getCustomRepository(
|
||||
this.claimItemRepository_
|
||||
)
|
||||
const validatedId = this.validateId_(id)
|
||||
|
||||
const query = this.buildQuery_({ id: validatedId }, config)
|
||||
const query = buildQuery({ id }, config)
|
||||
const item = await claimItemRepo.findOne(query)
|
||||
|
||||
if (!item) {
|
||||
@@ -249,30 +249,6 @@ class ClaimItemService extends BaseService {
|
||||
|
||||
return item
|
||||
}
|
||||
|
||||
/**
|
||||
* Dedicated method to delete metadata for an order.
|
||||
* @param {string} orderId - the order to delete metadata from.
|
||||
* @param {string} key - key for metadata field
|
||||
* @return {Promise} resolves to the updated result.
|
||||
*/
|
||||
async deleteMetadata(orderId, key) {
|
||||
const validatedId = this.validateId_(orderId)
|
||||
|
||||
if (typeof key !== "string") {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
"Key type is invalid. Metadata keys must be strings"
|
||||
)
|
||||
}
|
||||
|
||||
const keyPath = `metadata.${key}`
|
||||
return this.orderModel_
|
||||
.updateOne({ _id: validatedId }, { $unset: { [keyPath]: "" } })
|
||||
.catch((err) => {
|
||||
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default ClaimItemService
|
||||
@@ -1,17 +1,8 @@
|
||||
import { ClaimType, Order } from "../models"
|
||||
import { ClaimReason, ClaimType, Order } from "../models"
|
||||
import { AddressPayload } from "./common"
|
||||
|
||||
export type ClaimTypeValue = `${ClaimType}`
|
||||
|
||||
export enum ClaimItemReason {
|
||||
missing_item = "missing_item",
|
||||
wrong_item = "wrong_item",
|
||||
production_failure = "production_failure",
|
||||
other = "other",
|
||||
}
|
||||
|
||||
export type ClaimItemReasonValue = `${ClaimItemReason}`
|
||||
|
||||
/* CREATE INPUT */
|
||||
|
||||
export type CreateClaimInput = {
|
||||
@@ -40,11 +31,12 @@ type CreateClaimShippingMethodInput = {
|
||||
price?: number
|
||||
}
|
||||
|
||||
type CreateClaimItemInput = {
|
||||
export type CreateClaimItemInput = {
|
||||
item_id: string
|
||||
quantity: number
|
||||
claim_order_id?: string
|
||||
reason: ClaimReason
|
||||
note?: string
|
||||
reason?: ClaimItemReasonValue
|
||||
tags?: string[]
|
||||
images?: string[]
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { MedusaError } from "medusa-core-utils/dist"
|
||||
|
||||
/**
|
||||
* Dedicated method to set metadata.
|
||||
* @param obj - the entity to apply metadata to.
|
||||
* @param metadata - the metadata to set
|
||||
* @return resolves to the updated result.
|
||||
*/
|
||||
* Dedicated method to set metadata.
|
||||
* @param obj - the entity to apply metadata to.
|
||||
* @param metadata - the metadata to set
|
||||
* @return resolves to the updated result.
|
||||
*/
|
||||
export function setMetadata(
|
||||
obj: { metadata: Record<string, unknown> },
|
||||
metadata: Record<string, unknown>
|
||||
@@ -26,4 +26,4 @@ export function setMetadata(
|
||||
...existing,
|
||||
...newData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user