chore: Make packages/medusa/src/services/product.js (#585)
This commit is contained in:
@@ -6,7 +6,6 @@
|
|||||||
/packages/medusa/src/services/middleware.js
|
/packages/medusa/src/services/middleware.js
|
||||||
/packages/medusa/src/services/payment-provider.js
|
/packages/medusa/src/services/payment-provider.js
|
||||||
/packages/medusa/src/services/product-variant.js
|
/packages/medusa/src/services/product-variant.js
|
||||||
/packages/medusa/src/services/product.js
|
|
||||||
/packages/medusa/src/services/shipping-profile.js
|
/packages/medusa/src/services/shipping-profile.js
|
||||||
/packages/medusa/src/subscribers/notification.js
|
/packages/medusa/src/subscribers/notification.js
|
||||||
/packages/medusa/src/subscribers/order.js
|
/packages/medusa/src/subscribers/order.js
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import _ from "lodash"
|
|
||||||
import { MedusaError } from "medusa-core-utils"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
import { Brackets } from "typeorm"
|
import { Brackets } from "typeorm"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides layer to manipulate products.
|
* Provides layer to manipulate products.
|
||||||
* @implements BaseService
|
* @extends BaseService
|
||||||
*/
|
*/
|
||||||
class ProductService extends BaseService {
|
class ProductService extends BaseService {
|
||||||
static IndexName = `products`
|
static IndexName = `products`
|
||||||
@@ -88,7 +87,8 @@ class ProductService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Object} listOptions - the query object for find
|
* @param {object} selector - selector for query
|
||||||
|
* @param {Object} config - config for query object for find
|
||||||
* @return {Promise} the result of the find operation
|
* @return {Promise} the result of the find operation
|
||||||
*/
|
*/
|
||||||
async list(selector = {}, config = { relations: [], skip: 0, take: 20 }) {
|
async list(selector = {}, config = { relations: [], skip: 0, take: 20 }) {
|
||||||
@@ -112,7 +112,7 @@ class ProductService extends BaseService {
|
|||||||
query.select = config.select
|
query.select = config.select
|
||||||
}
|
}
|
||||||
|
|
||||||
let rels = query.relations
|
const rels = query.relations
|
||||||
delete query.relations
|
delete query.relations
|
||||||
|
|
||||||
if (q) {
|
if (q) {
|
||||||
@@ -162,6 +162,7 @@ class ProductService extends BaseService {
|
|||||||
* Gets a product by id.
|
* Gets a product by id.
|
||||||
* Throws in case of DB Error and if product was not found.
|
* Throws in case of DB Error and if product was not found.
|
||||||
* @param {string} productId - id of the product to get.
|
* @param {string} productId - id of the product to get.
|
||||||
|
* @param {object} config - config of the product to get.
|
||||||
* @return {Promise<Product>} the result of the find one operation.
|
* @return {Promise<Product>} the result of the find one operation.
|
||||||
*/
|
*/
|
||||||
async retrieve(productId, config = {}) {
|
async retrieve(productId, config = {}) {
|
||||||
@@ -258,7 +259,7 @@ class ProductService extends BaseService {
|
|||||||
this.productTagRepository_
|
this.productTagRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
let newTags = []
|
const newTags = []
|
||||||
for (const tag of tags) {
|
for (const tag of tags) {
|
||||||
const existing = await productTagRepository.findOne({
|
const existing = await productTagRepository.findOne({
|
||||||
where: { value: tag.value },
|
where: { value: tag.value },
|
||||||
@@ -339,7 +340,7 @@ class ProductService extends BaseService {
|
|||||||
this.imageRepository_
|
this.imageRepository_
|
||||||
)
|
)
|
||||||
|
|
||||||
let productImages = []
|
const productImages = []
|
||||||
for (const img of images) {
|
for (const img of images) {
|
||||||
const existing = await imageRepository.findOne({
|
const existing = await imageRepository.findOne({
|
||||||
where: { url: img },
|
where: { url: img },
|
||||||
@@ -376,8 +377,7 @@ class ProductService extends BaseService {
|
|||||||
relations: ["variants", "tags", "images"],
|
relations: ["variants", "tags", "images"],
|
||||||
})
|
})
|
||||||
|
|
||||||
const { variants, metadata, options, images, tags, type, ...rest } =
|
const { variants, metadata, images, tags, type, ...rest } = update
|
||||||
update
|
|
||||||
|
|
||||||
if (!product.thumbnail && !update.thumbnail && images?.length) {
|
if (!product.thumbnail && !update.thumbnail && images?.length) {
|
||||||
product.thumbnail = images[0]
|
product.thumbnail = images[0]
|
||||||
@@ -473,7 +473,9 @@ class ProductService extends BaseService {
|
|||||||
{ relations: ["variants"] }
|
{ relations: ["variants"] }
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!product) return Promise.resolve()
|
if (!product) {
|
||||||
|
return Promise.resolve()
|
||||||
|
}
|
||||||
|
|
||||||
await productRepo.softRemove(product)
|
await productRepo.softRemove(product)
|
||||||
|
|
||||||
@@ -574,7 +576,7 @@ class ProductService extends BaseService {
|
|||||||
* optionOrder and the length of the product's options are different. Will
|
* optionOrder and the length of the product's options are different. Will
|
||||||
* throw optionOrder contains an id not associated with the product.
|
* throw optionOrder contains an id not associated with the product.
|
||||||
* @param {string} productId - the product whose options we are reordering
|
* @param {string} productId - the product whose options we are reordering
|
||||||
* @param {[ObjectId]} optionId - the ids of the product's options in the
|
* @param {string[]} optionOrder - the ids of the product's options in the
|
||||||
* new order
|
* new order
|
||||||
* @return {Promise} the result of the update operation
|
* @return {Promise} the result of the update operation
|
||||||
*/
|
*/
|
||||||
@@ -727,7 +729,7 @@ class ProductService extends BaseService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Decorates a product with product variants.
|
* Decorates a product with product variants.
|
||||||
* @param {Product} product - the product to decorate.
|
* @param {string} productId - the productId to decorate.
|
||||||
* @param {string[]} fields - the fields to include.
|
* @param {string[]} fields - the fields to include.
|
||||||
* @param {string[]} expandFields - fields to expand.
|
* @param {string[]} expandFields - fields to expand.
|
||||||
* @return {Product} return the decorated product.
|
* @return {Product} return the decorated product.
|
||||||
|
|||||||
Reference in New Issue
Block a user