Adds default shipping profile
This commit is contained in:
@@ -43,6 +43,8 @@ export default async (req, res) => {
|
||||
delete value.variants
|
||||
|
||||
const productService = req.scope.resolve("productService")
|
||||
const shippingProfileService = req.scope.resolve("shippingProfileService")
|
||||
|
||||
let newProduct = await productService.createDraft(value)
|
||||
|
||||
if (variants) {
|
||||
@@ -64,6 +66,10 @@ export default async (req, res) => {
|
||||
)
|
||||
}
|
||||
|
||||
// Add to default shipping profile
|
||||
const { _id } = await shippingProfileService.retrieveDefault()
|
||||
await shippingProfileService.addProduct(_id, newProduct._id)
|
||||
|
||||
newProduct = await productService.decorate(newProduct, [
|
||||
"title",
|
||||
"description",
|
||||
|
||||
@@ -27,10 +27,17 @@ export default async (req, res) => {
|
||||
|
||||
try {
|
||||
const optionService = req.scope.resolve("shippingOptionService")
|
||||
const shippingProfileService = req.scope.resolve("shippingProfileService")
|
||||
|
||||
const data = await optionService.create(value)
|
||||
|
||||
// Add to default shipping profile
|
||||
const { _id } = await shippingProfileService.retrieveDefault()
|
||||
await shippingProfileService.addProduct(_id, data._id)
|
||||
|
||||
res.status(200).json({ shipping_option: data })
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
||||
7
packages/medusa/src/loaders/defaults.js
Normal file
7
packages/medusa/src/loaders/defaults.js
Normal file
@@ -0,0 +1,7 @@
|
||||
export default async ({ container }) => {
|
||||
const storeService = container.resolve("storeService")
|
||||
const profileService = container.resolve("shippingProfileService")
|
||||
|
||||
await storeService.create()
|
||||
await profileService.createDefault()
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import modelsLoader from "./models"
|
||||
import servicesLoader from "./services"
|
||||
import passportLoader from "./passport"
|
||||
import pluginsLoader from "./plugins"
|
||||
import storeLoader from "./store"
|
||||
import defaultsLoader from "./defaults"
|
||||
import Logger from "./logger"
|
||||
|
||||
export default async ({ directory: rootDirectory, expressApp }) => {
|
||||
@@ -58,8 +58,8 @@ export default async ({ directory: rootDirectory, expressApp }) => {
|
||||
await apiLoader({ container, rootDirectory, app: expressApp })
|
||||
Logger.info("API initialized")
|
||||
|
||||
await storeLoader({ container })
|
||||
Logger.info("Store initialized")
|
||||
await defaultsLoader({ container })
|
||||
Logger.info("Defaults initialized")
|
||||
|
||||
return { container, dbConnection, app: expressApp }
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
/**
|
||||
* Registers all services in the services directory
|
||||
*/
|
||||
export default ({ container }) => {
|
||||
const storeService = container.resolve("storeService")
|
||||
return storeService.create()
|
||||
}
|
||||
@@ -31,6 +31,9 @@ export const ShippingProfileServiceMock = {
|
||||
}
|
||||
return Promise.resolve()
|
||||
}),
|
||||
retrieveDefault: jest.fn().mockImplementation(data => {
|
||||
return Promise.resolve({ _id: IdMap.getId("default_shipping_profile") })
|
||||
}),
|
||||
list: jest.fn().mockImplementation(selector => {
|
||||
if (!selector) {
|
||||
return Promise.resolve([])
|
||||
|
||||
@@ -33,7 +33,7 @@ class ShippingProfileService extends BaseService {
|
||||
*/
|
||||
validateId_(rawId) {
|
||||
const schema = Validator.objectId()
|
||||
const { value, error } = schema.validate(rawId)
|
||||
const { value, error } = schema.validate(rawId.toString())
|
||||
if (error) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.INVALID_ARGUMENT,
|
||||
@@ -76,6 +76,27 @@ class ShippingProfileService extends BaseService {
|
||||
return profile
|
||||
}
|
||||
|
||||
async retrieveDefault() {
|
||||
return await this.profileModel_
|
||||
.findOne({ name: "default_shipping_profile" })
|
||||
.catch(err => {
|
||||
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a default shipping profile, if this does not already exist.
|
||||
* @return {Promise<ShippingProfile>} the shipping profile
|
||||
*/
|
||||
async createDefault() {
|
||||
const profile = await this.retrieveDefault()
|
||||
if (!profile) {
|
||||
return this.profileModel_.create({ name: "default_shipping_profile" })
|
||||
}
|
||||
|
||||
return profile
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new shipping profile.
|
||||
* @param {ShippingProfile} profile - the shipping profile to create from
|
||||
|
||||
Reference in New Issue
Block a user