This commit is contained in:
1 parent
5fbce42948
commit
82eee77610
2 files changed
+14
-18
No files matched your search
@@ -8,7 +8,6 @@
|
|||||||
/packages/medusa/src/services/product-variant.js
|
/packages/medusa/src/services/product-variant.js
|
||||||
/packages/medusa/src/services/product.js
|
/packages/medusa/src/services/product.js
|
||||||
/packages/medusa/src/services/shipping-profile.js
|
/packages/medusa/src/services/shipping-profile.js
|
||||||
/packages/medusa/src/services/store.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
|
||||||
/packages/medusa/src/subscribers/product.js
|
/packages/medusa/src/subscribers/product.js
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
import _ from "lodash"
|
import { MedusaError } from "medusa-core-utils"
|
||||||
import { Validator, MedusaError } from "medusa-core-utils"
|
|
||||||
import { BaseService } from "medusa-interfaces"
|
import { BaseService } from "medusa-interfaces"
|
||||||
|
|
||||||
import { currencies } from "../utils/currencies"
|
import { currencies } from "../utils/currencies"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides layer to manipulate store settings.
|
* Provides layer to manipulate store settings.
|
||||||
* @implements BaseService
|
* @extends BaseService
|
||||||
*/
|
*/
|
||||||
class StoreService extends BaseService {
|
class StoreService extends BaseService {
|
||||||
constructor({
|
constructor({
|
||||||
@@ -52,7 +51,7 @@ class StoreService extends BaseService {
|
|||||||
* @return {Promise<Store>} the store.
|
* @return {Promise<Store>} the store.
|
||||||
*/
|
*/
|
||||||
async create() {
|
async create() {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const storeRepository = manager.getCustomRepository(this.storeRepository_)
|
const storeRepository = manager.getCustomRepository(this.storeRepository_)
|
||||||
|
|
||||||
let store = await this.retrieve()
|
let store = await this.retrieve()
|
||||||
@@ -68,6 +67,7 @@ class StoreService extends BaseService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the store settings. There is always a maximum of one store.
|
* Retrieve the store settings. There is always a maximum of one store.
|
||||||
|
* @param {string[]} relations - relations to fetch with store
|
||||||
* @return {Promise<Store>} the store
|
* @return {Promise<Store>} the store
|
||||||
*/
|
*/
|
||||||
async retrieve(relations = []) {
|
async retrieve(relations = []) {
|
||||||
@@ -90,16 +90,12 @@ class StoreService extends BaseService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates a customer. Metadata updates and address updates should
|
* Updates a store
|
||||||
* use dedicated methods, e.g. `setMetadata`, etc. The function
|
|
||||||
* will throw errors if metadata updates and address updates are attempted.
|
|
||||||
* @param {string} variantId - the id of the variant. Must be a string that
|
|
||||||
* can be casted to an ObjectId
|
|
||||||
* @param {object} update - an object with the update values.
|
* @param {object} update - an object with the update values.
|
||||||
* @return {Promise} resolves to the update result.
|
* @return {Promise} resolves to the update result.
|
||||||
*/
|
*/
|
||||||
async update(update) {
|
async update(update) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const storeRepository = manager.getCustomRepository(this.storeRepository_)
|
const storeRepository = manager.getCustomRepository(this.storeRepository_)
|
||||||
const currencyRepository = manager.getCustomRepository(
|
const currencyRepository = manager.getCustomRepository(
|
||||||
this.currencyRepository_
|
this.currencyRepository_
|
||||||
@@ -109,7 +105,6 @@ class StoreService extends BaseService {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
metadata,
|
metadata,
|
||||||
default_currency,
|
|
||||||
default_currency_code,
|
default_currency_code,
|
||||||
currencies: storeCurrencies,
|
currencies: storeCurrencies,
|
||||||
...rest
|
...rest
|
||||||
@@ -137,7 +132,7 @@ class StoreService extends BaseService {
|
|||||||
|
|
||||||
if (storeCurrencies) {
|
if (storeCurrencies) {
|
||||||
store.currencies = await Promise.all(
|
store.currencies = await Promise.all(
|
||||||
storeCurrencies.map(async curr => {
|
storeCurrencies.map(async (curr) => {
|
||||||
const currency = await currencyRepository.findOne({
|
const currency = await currencyRepository.findOne({
|
||||||
where: { code: curr.toLowerCase() },
|
where: { code: curr.toLowerCase() },
|
||||||
})
|
})
|
||||||
@@ -169,7 +164,7 @@ class StoreService extends BaseService {
|
|||||||
* @return {Promise} result after update
|
* @return {Promise} result after update
|
||||||
*/
|
*/
|
||||||
async addCurrency(code) {
|
async addCurrency(code) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const storeRepo = manager.getCustomRepository(this.storeRepository_)
|
const storeRepo = manager.getCustomRepository(this.storeRepository_)
|
||||||
const currencyRepository = manager.getCustomRepository(
|
const currencyRepository = manager.getCustomRepository(
|
||||||
this.currencyRepository_
|
this.currencyRepository_
|
||||||
@@ -187,7 +182,9 @@ class StoreService extends BaseService {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (store.currencies.map(c => c.code).includes(curr.code.toLowerCase())) {
|
if (
|
||||||
|
store.currencies.map((c) => c.code).includes(curr.code.toLowerCase())
|
||||||
|
) {
|
||||||
throw new MedusaError(
|
throw new MedusaError(
|
||||||
MedusaError.Types.DUPLICATE_ERROR,
|
MedusaError.Types.DUPLICATE_ERROR,
|
||||||
`Currency already added`
|
`Currency already added`
|
||||||
@@ -206,17 +203,17 @@ class StoreService extends BaseService {
|
|||||||
* @return {Promise} result after update
|
* @return {Promise} result after update
|
||||||
*/
|
*/
|
||||||
async removeCurrency(code) {
|
async removeCurrency(code) {
|
||||||
return this.atomicPhase_(async manager => {
|
return this.atomicPhase_(async (manager) => {
|
||||||
const storeRepo = manager.getCustomRepository(this.storeRepository_)
|
const storeRepo = manager.getCustomRepository(this.storeRepository_)
|
||||||
const store = await this.retrieve(["currencies"])
|
const store = await this.retrieve(["currencies"])
|
||||||
|
|
||||||
const exists = store.currencies.find(c => c.code === code.toLowerCase())
|
const exists = store.currencies.find((c) => c.code === code.toLowerCase())
|
||||||
// If currency does not exist, return early
|
// If currency does not exist, return early
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
|
|
||||||
store.currencies = store.currencies.filter(c => c.code !== code)
|
store.currencies = store.currencies.filter((c) => c.code !== code)
|
||||||
const updated = await storeRepo.save(store)
|
const updated = await storeRepo.save(store)
|
||||||
return updated
|
return updated
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user