Merge branch 'master' of github.com:medusajs/medusa
This commit is contained in:
@@ -3,8 +3,9 @@ import { MedusaError, Validator } from "medusa-core-utils"
|
||||
export default async (req, res) => {
|
||||
const { discount_id } = req.params
|
||||
const schema = Validator.object().keys({
|
||||
code: Validator.string().required(),
|
||||
code: Validator.string().optional(),
|
||||
is_dynamic: Validator.boolean().default(false),
|
||||
is_giftcard: Validator.boolean().optional(),
|
||||
discount_rule: Validator.object()
|
||||
.keys({
|
||||
description: Validator.string().optional(),
|
||||
@@ -14,7 +15,7 @@ export default async (req, res) => {
|
||||
valid_for: Validator.array().items(Validator.string()),
|
||||
usage_limit: Validator.number().optional(),
|
||||
})
|
||||
.required(),
|
||||
.optional(),
|
||||
usage_count: Validator.number().optional(),
|
||||
disabled: Validator.boolean().optional(),
|
||||
starts_at: Validator.date().optional(),
|
||||
|
||||
@@ -6,19 +6,25 @@ export default async (req, res) => {
|
||||
const queryBuilderService = req.scope.resolve("queryBuilderService")
|
||||
|
||||
const query = queryBuilderService.buildQuery(req.query, [
|
||||
"display_id",
|
||||
"email",
|
||||
"status",
|
||||
"fulfillment_status",
|
||||
"payment_status",
|
||||
])
|
||||
|
||||
let orders = await orderService.list(query)
|
||||
const limit = parseInt(req.query.limit) || 0
|
||||
const offset = parseInt(req.query.offset) || 0
|
||||
|
||||
let orders = await orderService.list(query, offset, limit)
|
||||
|
||||
orders = await Promise.all(
|
||||
orders.map(order => orderService.decorate(order))
|
||||
)
|
||||
|
||||
res.json({ orders })
|
||||
let numOrders = await orderService.count()
|
||||
|
||||
res.json({ orders, total_count: numOrders })
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
|
||||
@@ -10,7 +10,10 @@ export default async (req, res) => {
|
||||
"description",
|
||||
])
|
||||
|
||||
let products = await productService.list(query)
|
||||
const limit = parseInt(req.query.limit) || 0
|
||||
const offset = parseInt(req.query.offset) || 0
|
||||
|
||||
let products = await productService.list(query, offset, limit)
|
||||
|
||||
products = await Promise.all(
|
||||
products.map(
|
||||
@@ -32,7 +35,10 @@ export default async (req, res) => {
|
||||
)
|
||||
)
|
||||
)
|
||||
res.json({ products })
|
||||
|
||||
const numProducts = await productService.count()
|
||||
|
||||
res.json({ products, total_count: numProducts })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
throw error
|
||||
|
||||
@@ -10,14 +10,7 @@ describe("Get product by id", () => {
|
||||
beforeAll(async () => {
|
||||
subject = await request(
|
||||
"GET",
|
||||
`/admin/products/${IdMap.getId("product1")}`,
|
||||
{
|
||||
adminSession: {
|
||||
jwt: {
|
||||
userId: IdMap.getId("admin_user"),
|
||||
},
|
||||
},
|
||||
}
|
||||
`/store/products/${IdMap.getId("product1")}`
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
export const MiddlewareServiceMock = {
|
||||
usePostAuthentication: jest.fn(),
|
||||
usePreAuthentication: jest.fn(),
|
||||
usePreCartCreation: jest.fn().mockReturnValue([]),
|
||||
getRouters: jest.fn().mockReturnValue([]),
|
||||
}
|
||||
|
||||
|
||||
@@ -145,8 +145,18 @@ class OrderService extends BaseService {
|
||||
* @param {Object} selector - the query object for find
|
||||
* @return {Promise} the result of the find operation
|
||||
*/
|
||||
list(selector) {
|
||||
return this.orderModel_.find(selector)
|
||||
list(selector, offset, limit) {
|
||||
return this.orderModel_
|
||||
.find(selector, {}, offset, limit)
|
||||
.sort({ created: -1 })
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the total number of documents in database
|
||||
* @return {Promise} the result of the count operation
|
||||
*/
|
||||
count() {
|
||||
return this.orderModel_.count()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -232,14 +242,6 @@ class OrderService extends BaseService {
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} selector - the query object for find
|
||||
* @return {Promise} the result of the find operation
|
||||
*/
|
||||
list(selector) {
|
||||
return this.orderModel_.find(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} orderId - id of the order to complete
|
||||
* @return {Promise} the result of the find operation
|
||||
@@ -247,9 +249,6 @@ class OrderService extends BaseService {
|
||||
async completeOrder(orderId) {
|
||||
const order = await this.retrieve(orderId)
|
||||
|
||||
// Capture the payment
|
||||
await this.capturePayment(orderId)
|
||||
|
||||
// Run all other registered events
|
||||
const completeOrderJob = await this.eventBus_.emit(
|
||||
OrderService.Events.COMPLETED,
|
||||
@@ -593,12 +592,7 @@ class OrderService extends BaseService {
|
||||
)
|
||||
}
|
||||
|
||||
// prepare update object
|
||||
const updateFields = { payment_status: "captured" }
|
||||
const completed = order.fulfillment_status !== "not_fulfilled"
|
||||
if (completed) {
|
||||
updateFields.status = "completed"
|
||||
}
|
||||
|
||||
const { provider_id, data } = order.payment_method
|
||||
const paymentProvider = await this.paymentProviderService_.retrieveProvider(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import mongoose from "mongoose"
|
||||
import _ from "lodash"
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
import { Validator, MedusaError, compareObjectsByProp } from "medusa-core-utils"
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
|
||||
/**
|
||||
@@ -49,8 +49,16 @@ class ProductService extends BaseService {
|
||||
* @param {Object} selector - the query object for find
|
||||
* @return {Promise} the result of the find operation
|
||||
*/
|
||||
list(selector) {
|
||||
return this.productModel_.find(selector)
|
||||
list(selector, offset, limit) {
|
||||
return this.productModel_.find(selector, {}, offset, limit)
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the total number of documents in database
|
||||
* @return {Promise} the result of the count operation
|
||||
*/
|
||||
count() {
|
||||
return this.productModel_.count()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,32 +161,53 @@ class ProductService extends BaseService {
|
||||
await Promise.all(
|
||||
update.variants.map(async variant => {
|
||||
if (variant._id) {
|
||||
const variantFromDb = existingVariants.find(v =>
|
||||
v._id.equals(variant._id)
|
||||
)
|
||||
if (variant.prices && variant.prices.length) {
|
||||
for (const price of variant.prices) {
|
||||
if (price.region_id) {
|
||||
await this.productVariantService_.setRegionPrice(
|
||||
variant._id,
|
||||
price.region_id,
|
||||
price.amount
|
||||
)
|
||||
} else {
|
||||
await this.productVariantService_.setCurrencyPrice(
|
||||
variant._id,
|
||||
price.currency_code,
|
||||
price.amount
|
||||
)
|
||||
// if equal we dont want to update
|
||||
const isPricesEqual = compareObjectsByProp(
|
||||
variant,
|
||||
variantFromDb,
|
||||
"prices"
|
||||
)
|
||||
|
||||
if (!isPricesEqual) {
|
||||
for (const price of variant.prices) {
|
||||
if (price.region_id) {
|
||||
await this.productVariantService_.setRegionPrice(
|
||||
variant._id,
|
||||
price.region_id,
|
||||
price.amount
|
||||
)
|
||||
} else {
|
||||
await this.productVariantService_.setCurrencyPrice(
|
||||
variant._id,
|
||||
price.currency_code,
|
||||
price.amount
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (variant.options && variant.options.length) {
|
||||
for (const option of variant.options) {
|
||||
await this.updateOptionValue(
|
||||
productId,
|
||||
variant._id,
|
||||
option.option_id,
|
||||
option.value
|
||||
)
|
||||
// if equal we dont want to update
|
||||
const isOptionsEqual = compareObjectsByProp(
|
||||
variant,
|
||||
variantFromDb,
|
||||
"options"
|
||||
)
|
||||
|
||||
if (!isOptionsEqual) {
|
||||
for (const option of variant.options) {
|
||||
await this.updateOptionValue(
|
||||
productId,
|
||||
variant._id,
|
||||
option.option_id,
|
||||
option.value
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user