Feat(medusa) refactor claim-item service to typescript (#1640)
* refactor claim-item service to typescript * update pr
This commit is contained in:
1 parent
a605622bcb
commit
1fe518b4af
4 files changed
+61
-97
No files matched your search
@@ -14,12 +14,8 @@ import { MedusaError } from "medusa-core-utils"
|
|||||||
import { defaultAdminOrdersFields, defaultAdminOrdersRelations } from "."
|
import { defaultAdminOrdersFields, defaultAdminOrdersRelations } from "."
|
||||||
import { AddressPayload } from "../../../../types/common"
|
import { AddressPayload } from "../../../../types/common"
|
||||||
import { validator } from "../../../../utils/validator"
|
import { validator } from "../../../../utils/validator"
|
||||||
import {
|
import { ClaimTypeValue } from "../../../../types/claim"
|
||||||
ClaimItemReason,
|
import { ClaimType, ClaimReason } from "../../../../models"
|
||||||
ClaimItemReasonValue,
|
|
||||||
ClaimTypeValue,
|
|
||||||
} from "../../../../types/claim"
|
|
||||||
import { ClaimType } from "../../../../models"
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @oas [post] /order/{id}/claims
|
* @oas [post] /order/{id}/claims
|
||||||
@@ -422,9 +418,9 @@ class Item {
|
|||||||
@IsOptional()
|
@IsOptional()
|
||||||
note?: string
|
note?: string
|
||||||
|
|
||||||
@IsEnum(ClaimItemReason)
|
@IsEnum(ClaimReason)
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
reason?: ClaimItemReasonValue
|
reason?: ClaimReason
|
||||||
|
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
|
|||||||
+48
-72
@@ -1,13 +1,32 @@
|
|||||||
import { MedusaError } from "medusa-core-utils"
|
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 = {
|
static Events = {
|
||||||
CREATED: "claim_item.created",
|
CREATED: "claim_item.created",
|
||||||
UPDATED: "claim_item.updated",
|
UPDATED: "claim_item.updated",
|
||||||
CANCELED: "claim_item.canceled",
|
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({
|
constructor({
|
||||||
manager,
|
manager,
|
||||||
claimItemRepository,
|
claimItemRepository,
|
||||||
@@ -16,45 +35,22 @@ class ClaimItemService extends BaseService {
|
|||||||
lineItemService,
|
lineItemService,
|
||||||
eventBusService,
|
eventBusService,
|
||||||
}) {
|
}) {
|
||||||
super()
|
// eslint-disable-next-line prefer-rest-params
|
||||||
|
super(arguments[0])
|
||||||
|
|
||||||
/** @private @constant {EntityManager} */
|
|
||||||
this.manager_ = manager
|
this.manager_ = manager
|
||||||
|
|
||||||
/** @private @constant {ClaimRepository} */
|
|
||||||
this.claimItemRepository_ = claimItemRepository
|
this.claimItemRepository_ = claimItemRepository
|
||||||
this.claimTagRepository_ = claimTagRepository
|
this.claimTagRepository_ = claimTagRepository
|
||||||
this.claimImageRepository_ = claimImageRepository
|
this.claimImageRepository_ = claimImageRepository
|
||||||
|
|
||||||
/** @private @constant {LineItemService} */
|
|
||||||
this.lineItemService_ = lineItemService
|
this.lineItemService_ = lineItemService
|
||||||
|
|
||||||
/** @private @constant {EventBus} */
|
|
||||||
this.eventBus_ = eventBusService
|
this.eventBus_ = eventBusService
|
||||||
}
|
}
|
||||||
|
|
||||||
withTransaction(manager) {
|
async create(data: CreateClaimItemInput): Promise<ClaimItem> {
|
||||||
if (!manager) {
|
return await this.atomicPhase_(async (manager) => {
|
||||||
return this
|
const ciRepo: ClaimItemRepository = manager.getCustomRepository(
|
||||||
}
|
this.claimItemRepository_
|
||||||
|
)
|
||||||
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_)
|
|
||||||
|
|
||||||
const { item_id, reason, quantity, tags, images, ...rest } = data
|
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) {
|
if (tags && tags.length) {
|
||||||
const claimTagRepo = manager.getCustomRepository(
|
const claimTagRepo = manager.getCustomRepository(
|
||||||
this.claimTagRepository_
|
this.claimTagRepository_
|
||||||
@@ -100,7 +96,7 @@ class ClaimItemService extends BaseService {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
let imagesToAdd = []
|
let imagesToAdd: ClaimImage[] = []
|
||||||
if (images && images.length) {
|
if (images && images.length) {
|
||||||
const claimImgRepo = manager.getCustomRepository(
|
const claimImgRepo = manager.getCustomRepository(
|
||||||
this.claimImageRepository_
|
this.claimImageRepository_
|
||||||
@@ -110,7 +106,7 @@ class ClaimItemService extends BaseService {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const created = ciRepo.create({
|
const toCreate: Partial<ClaimItem> = {
|
||||||
...rest,
|
...rest,
|
||||||
variant_id: lineItem.variant_id,
|
variant_id: lineItem.variant_id,
|
||||||
tags: tagsToAdd,
|
tags: tagsToAdd,
|
||||||
@@ -118,7 +114,8 @@ class ClaimItemService extends BaseService {
|
|||||||
item_id,
|
item_id,
|
||||||
reason,
|
reason,
|
||||||
quantity,
|
quantity,
|
||||||
})
|
}
|
||||||
|
const created = ciRepo.create(toCreate)
|
||||||
|
|
||||||
const result = await ciRepo.save(created)
|
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) => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const ciRepo = manager.getCustomRepository(this.claimItemRepository_)
|
const ciRepo = manager.getCustomRepository(this.claimItemRepository_)
|
||||||
const item = await this.retrieve(id, { relations: ["images", "tags"] })
|
const item = await this.retrieve(id, { relations: ["images", "tags"] })
|
||||||
@@ -148,7 +145,7 @@ class ClaimItemService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (metadata) {
|
if (metadata) {
|
||||||
item.metadata = this.setMetadata_(item, metadata)
|
item.metadata = setMetadata(item, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tags) {
|
if (tags) {
|
||||||
@@ -209,19 +206,21 @@ class ClaimItemService extends BaseService {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async cancel(id) {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object} selector - the query object for find
|
* @param {Object} selector - the query object for find
|
||||||
* @param {Object} config - the config object for find
|
* @param {Object} config - the config object for find
|
||||||
* @return {Promise} the result of the find operation
|
* @return {Promise} the result of the find operation
|
||||||
*/
|
*/
|
||||||
async list(
|
async list(
|
||||||
selector,
|
selector: Selector<ClaimItem>,
|
||||||
config = { skip: 0, take: 50, order: { created_at: "DESC" } }
|
config: FindConfig<ClaimItem> = {
|
||||||
) {
|
skip: 0,
|
||||||
|
take: 50,
|
||||||
|
order: { created_at: "DESC" },
|
||||||
|
}
|
||||||
|
): Promise<ClaimItem[]> {
|
||||||
const ciRepo = this.manager_.getCustomRepository(this.claimItemRepository_)
|
const ciRepo = this.manager_.getCustomRepository(this.claimItemRepository_)
|
||||||
const query = this.buildQuery_(selector, config)
|
const query = buildQuery(selector, config)
|
||||||
return ciRepo.find(query)
|
return ciRepo.find(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,13 +230,14 @@ class ClaimItemService extends BaseService {
|
|||||||
* @param {Object} config - configuration for the find operation
|
* @param {Object} config - configuration for the find operation
|
||||||
* @return {Promise<Order>} the ClaimItem
|
* @return {Promise<Order>} the ClaimItem
|
||||||
*/
|
*/
|
||||||
async retrieve(id, config = {}) {
|
async retrieve(
|
||||||
|
id: string,
|
||||||
|
config: FindConfig<ClaimItem> = {}
|
||||||
|
): Promise<ClaimItem> {
|
||||||
const claimItemRepo = this.manager_.getCustomRepository(
|
const claimItemRepo = this.manager_.getCustomRepository(
|
||||||
this.claimItemRepository_
|
this.claimItemRepository_
|
||||||
)
|
)
|
||||||
const validatedId = this.validateId_(id)
|
const query = buildQuery({ id }, config)
|
||||||
|
|
||||||
const query = this.buildQuery_({ id: validatedId }, config)
|
|
||||||
const item = await claimItemRepo.findOne(query)
|
const item = await claimItemRepo.findOne(query)
|
||||||
|
|
||||||
if (!item) {
|
if (!item) {
|
||||||
@@ -249,30 +249,6 @@ class ClaimItemService extends BaseService {
|
|||||||
|
|
||||||
return item
|
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
|
export default ClaimItemService
|
||||||
@@ -1,17 +1,8 @@
|
|||||||
import { ClaimType, Order } from "../models"
|
import { ClaimReason, ClaimType, Order } from "../models"
|
||||||
import { AddressPayload } from "./common"
|
import { AddressPayload } from "./common"
|
||||||
|
|
||||||
export type ClaimTypeValue = `${ClaimType}`
|
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 */
|
/* CREATE INPUT */
|
||||||
|
|
||||||
export type CreateClaimInput = {
|
export type CreateClaimInput = {
|
||||||
@@ -40,11 +31,12 @@ type CreateClaimShippingMethodInput = {
|
|||||||
price?: number
|
price?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateClaimItemInput = {
|
export type CreateClaimItemInput = {
|
||||||
item_id: string
|
item_id: string
|
||||||
quantity: number
|
quantity: number
|
||||||
|
claim_order_id?: string
|
||||||
|
reason: ClaimReason
|
||||||
note?: string
|
note?: string
|
||||||
reason?: ClaimItemReasonValue
|
|
||||||
tags?: string[]
|
tags?: string[]
|
||||||
images?: string[]
|
images?: string[]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import { MedusaError } from "medusa-core-utils/dist"
|
import { MedusaError } from "medusa-core-utils/dist"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dedicated method to set metadata.
|
* Dedicated method to set metadata.
|
||||||
* @param obj - the entity to apply metadata to.
|
* @param obj - the entity to apply metadata to.
|
||||||
* @param metadata - the metadata to set
|
* @param metadata - the metadata to set
|
||||||
* @return resolves to the updated result.
|
* @return resolves to the updated result.
|
||||||
*/
|
*/
|
||||||
export function setMetadata(
|
export function setMetadata(
|
||||||
obj: { metadata: Record<string, unknown> },
|
obj: { metadata: Record<string, unknown> },
|
||||||
metadata: Record<string, unknown>
|
metadata: Record<string, unknown>
|
||||||
|
|||||||
Reference in new issue
Block a user