This commit is contained in:
Sebastian Rindom
2020-07-06 11:52:16 +02:00
parent 2491bd6779
commit f9b232da54
9 changed files with 49 additions and 10 deletions

View File

@@ -11,9 +11,12 @@ class BaseFulfillmentService extends BaseService {
super()
}
getFulfillmentOptions() {
getIdentifier() {
return this.constructor.identifier
}
getFulfillmentOptions() {}
validateFulfillmentData(data, cart) {
throw Error("validateFulfillmentData must be overridden by the child class")
}

View File

@@ -11,6 +11,10 @@ class BasePaymentService extends BaseService {
super()
}
getIdentifier() {
return this.constructor.identifier
}
/**
* Used to create a payment to be processed with the service's payment gateway.
* @param cart {object} - the cart that the payment should cover.

View File

@@ -5,7 +5,7 @@ export default async (req, res) => {
name: Validator.string().required(),
region_id: Validator.string().required(),
provider_id: Validator.string().required(),
profile_id: Validator.string().required(),
profile_id: Validator.string(),
data: Validator.object().required(),
price: Validator.object()
.keys({

View File

@@ -1,8 +1,11 @@
import _ from "lodash"
export default async (req, res) => {
const { optionId } = req.params
try {
const query = _.pick(req.query, ["region_id", "region_id[]"])
const optionService = req.scope.resolve("shippingOptionService")
const data = await optionService.list()
const data = await optionService.list(query)
res.status(200).json({ shipping_options: data })
} catch (err) {

View File

@@ -2,6 +2,25 @@ export default async ({ container }) => {
const storeService = container.resolve("storeService")
const profileService = container.resolve("shippingProfileService")
await storeService.create()
let payIds
try {
const payProviders = container.resolve("paymentProviders")
payIds = payProviders.map(p => p.getIdentifier())
} catch (e) {
payIds = []
}
let fulfilIds
try {
const fulfilProviders = container.resolve("fulfillmentProviders")
fulfilIds = fulfilProviders.map(p => p.getIdentifier())
} catch (e) {
fulfilIds = []
}
await storeService.create({
fulfillment_providers: fulfilIds,
payment_providers: payIds,
})
await profileService.createDefault()
}

View File

@@ -7,6 +7,8 @@ class StoreModel extends BaseModel {
name: { type: String, required: true, default: "Medusa Store" },
default_currency: { type: String, required: true, default: "USD" },
currencies: { type: [String], default: [] },
payment_providers: { type: [String], default: [] },
fulfillment_providers: { type: [String], default: [] },
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
}
}

View File

@@ -154,7 +154,7 @@ class RegionService extends BaseService {
}
const existing = await this.regionModel_.findOne({ countries: countryCode })
if (existing && existing._id !== id) {
if (existing && !existing._id.equals(id)) {
throw new MedusaError(
MedusaError.Types.NOT_ALLOWED,
`${country.name} already exists in ${existing.name}, delete it in that region before adding it`

View File

@@ -79,7 +79,13 @@ class ShippingOptionService extends BaseService {
* @return {Promise} the result of the find operation
*/
list(selector) {
return this.optionModel_.find(selector)
const query = {}
if (selector.region_id !== undefined) {
query.region_id = selector.region_id
}
return this.optionModel_.find(query)
}
/**

View File

@@ -43,10 +43,12 @@ class StoreService extends BaseService {
* Creates a store if it doesn't already exist.
* @return {Promise<Store>} the store.
*/
async create() {
const store = await this.retrieve()
async create(providers) {
let store = await this.retrieve()
if (!store) {
return this.storeModel_.create({})
return this.storeModel_.create(providers)
} else {
store = await this.update(providers)
}
return store