clean up draft order service

This commit is contained in:
olivermrbl committed 2021-02-18 16:37:25 +01:00
1 parent 3ac71e4a4c
commit cfcc6f3fe0
1 file changed
+32 -125
+32 -125
View File
@@ -1,10 +1,10 @@
import _ from "lodash" import _ from "lodash"
import { BaseService } from "medusa-interfaces" import { BaseService } from "medusa-interfaces"
import { MedusaError, Validator } from "medusa-core-utils" import { MedusaError } from "medusa-core-utils"
import { Brackets } from "typeorm" import { Brackets } from "typeorm"
/** /**
* Handles swaps * Handles draft orders
* @implements BaseService * @implements BaseService
*/ */
class DraftOrderService extends BaseService { class DraftOrderService extends BaseService {
@@ -81,17 +81,18 @@ class DraftOrderService extends BaseService {
const cloned = new DraftOrderService({ const cloned = new DraftOrderService({
manager: transactionManager, manager: transactionManager,
draftOrderRepository: this.draftOrderRepository_, draftOrderRepository: this.draftOrderRepository_,
orderRepository: this.orderRepository_,
paymentRepository: this.paymentRepository_, paymentRepository: this.paymentRepository_,
paymentProviderService: this.paymentProviderService_, orderRepository: this.orderRepository_,
regionService: this.regionService_,
eventBusService: this.eventBus_,
cartService: this.cartService_,
totalsService: this.totalsService_, totalsService: this.totalsService_,
productVariantService: this.productVariantService_,
addressRepository: this.addressRepository_, addressRepository: this.addressRepository_,
shippingOptionService: this.shippingOptionService_,
lineItemService: this.lineItemService_, lineItemService: this.lineItemService_,
cartService: this.cartService_,
customerService: this.customerService_,
regionService: this.regionService_,
paymentProviderService: this.paymentProviderService_,
productVariantService: this.productVariantService_,
shippingOptionService: this.shippingOptionService_,
eventBusService: this.eventBus_,
}) })
cloned.transactionManager_ = transactionManager cloned.transactionManager_ = transactionManager
@@ -99,68 +100,6 @@ class DraftOrderService extends BaseService {
return cloned return cloned
} }
transformQueryForTotals_(config) {
let { select, relations } = config
if (!select) {
return {
select,
relations,
totalsToSelect: [],
}
}
const totalFields = [
"subtotal",
"tax_total",
"shipping_total",
"discount_total",
"total",
]
const totalsToSelect = select.filter(v => totalFields.includes(v))
if (totalsToSelect.length > 0) {
const relationSet = new Set(relations)
relationSet.add("items")
relationSet.add("discounts")
relationSet.add("shipping_methods")
relationSet.add("region")
relations = [...relationSet]
select = select.filter(v => !totalFields.includes(v))
}
return {
relations,
select,
totalsToSelect,
}
}
decorateTotals_(draftOrder, totalsFields = []) {
if (totalsFields.includes("shipping_total")) {
draftOrder.shipping_total = this.totalsService_.getShippingTotal(
draftOrder
)
}
if (totalsFields.includes("discount_total")) {
draftOrder.discount_total = this.totalsService_.getDiscountTotal(
draftOrder
)
}
if (totalsFields.includes("tax_total")) {
draftOrder.tax_total = this.totalsService_.getTaxTotal(draftOrder)
}
if (totalsFields.includes("subtotal")) {
draftOrder.subtotal = this.totalsService_.getSubtotal(draftOrder)
}
if (totalsFields.includes("total")) {
draftOrder.total = this.totalsService_.getTotal(draftOrder)
}
return draftOrder
}
/** /**
* Retrieves a draft order with the given id. * Retrieves a draft order with the given id.
* @param {string} id - id of the draft order to retrieve * @param {string} id - id of the draft order to retrieve
@@ -173,32 +112,17 @@ class DraftOrderService extends BaseService {
const validatedId = this.validateId_(id) const validatedId = this.validateId_(id)
const { select, relations, totalsToSelect } = this.transformQueryForTotals_( const query = this.buildQuery_({ id: validatedId }, config)
config
)
const query = { const draftOrder = await draftOrderRepo.findOne(query)
where: { id: validatedId },
}
if (relations && relations.length > 0) { if (!draftOrder) {
query.relations = relations
}
if (select && select.length > 0) {
query.select = select
}
const raw = await draftOrderRepo.findOne(query)
if (!raw) {
throw new MedusaError( throw new MedusaError(
MedusaError.Types.NOT_FOUND, MedusaError.Types.NOT_FOUND,
`Draft order with ${id} was not found` `Draft order with ${id} was not found`
) )
} }
const draftOrder = this.decorateTotals_(raw, totalsToSelect)
return draftOrder return draftOrder
} }
@@ -252,6 +176,12 @@ class DraftOrderService extends BaseService {
}) })
} }
/**
* Lists draft orders alongside the count
* @param {object} selector - query selector to filter draft orders
* @param {object} config - query config
* @return {Promise<DraftOrder[]>} draft orders
*/
async listAndCount( async listAndCount(
selector, selector,
config = { skip: 0, take: 50, order: { created_at: "DESC" } } config = { skip: 0, take: 50, order: { created_at: "DESC" } }
@@ -295,20 +225,7 @@ class DraftOrderService extends BaseService {
} }
} }
const { select, relations, totalsToSelect } = this.transformQueryForTotals_( const [draftOrders, count] = await draftOrderRepository.findAndCount(query)
config
)
if (select && select.length) {
query.select = select
}
if (relations && relations.length) {
query.relations = relations
}
const [raw, count] = await draftOrderRepository.findAndCount(query)
const draftOrders = raw.map(r => this.decorateTotals_(r, totalsToSelect))
return [draftOrders, count] return [draftOrders, count]
} }
@@ -317,7 +234,7 @@ class DraftOrderService extends BaseService {
* Lists draft orders * Lists draft orders
* @param {Object} selector - query object for find * @param {Object} selector - query object for find
* @param {Object} config - configurable attributes for find * @param {Object} config - configurable attributes for find
* @return {Promise<Array<DraftOrder>>} list of draft orders * @return {Promise<DraftOrder>} list of draft orders
*/ */
async list( async list(
selector, selector,
@@ -332,27 +249,6 @@ class DraftOrderService extends BaseService {
return draftOrderRepo.find(query) return draftOrderRepo.find(query)
} }
/**
* Confirms if the contents of a line item is covered by the inventory.
* To be covered a variant must either not have its inventory managed or it
* must allow backorders or it must have enough inventory to cover the request.
* If the content is made up of multiple variants it will return true if all
* variants can be covered. If the content consists of a single variant it will
* return true if the variant is covered.
* @param {(LineItemContent | LineItemContentArray)} - the content of the line
* item
* @param {number} - the quantity of the line item
* @return {boolean} true if the inventory covers the line item.
*/
async confirmInventory_(variantId, quantity) {
// If the line item is not stock tracked we don't have double check it
if (!variantId) {
return true
}
return this.productVariantService_.canCoverQuantity(variantId, quantity)
}
/** /**
* Creates a draft order. * Creates a draft order.
* @param {object} data - data to create draft order from * @param {object} data - data to create draft order from
@@ -457,6 +353,11 @@ class DraftOrderService extends BaseService {
}) })
} }
/**
* Registers a system payment and creates an order
* @param {string} doId - id of draft order to register payment for
* @return {Promise<Order>} the created order
*/
async registerSystemPayment(doId) { async registerSystemPayment(doId) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
const draftOrder = await this.retrieve(doId) const draftOrder = await this.retrieve(doId)
@@ -534,6 +435,12 @@ class DraftOrderService extends BaseService {
}) })
} }
/**
* Registers a draft order as completed, when an order has been completed.
* @param {string} doId - id of draft order to complete
* @param {string} orderId - id of order completed from draft order cart
* @return {Promise} the created order
*/
async registerCartCompletion(doId, orderId) { async registerCartCompletion(doId, orderId) {
return this.atomicPhase_(async manager => { return this.atomicPhase_(async manager => {
const draftOrderRepo = manager.getCustomRepository( const draftOrderRepo = manager.getCustomRepository(