Moves Medusa interfaces defines model schemas for Customer, Cart, Order
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
import mongoose from "mongoose"
|
||||
|
||||
/**
|
||||
* Interface for data models. The default data layer uses an internal mongoose
|
||||
* model and is as such compatible with MongoDB.
|
||||
* @interface
|
||||
*/
|
||||
class BaseModel {
|
||||
constructor() {
|
||||
/** @const the underlying mongoose model used for queries */
|
||||
this.mongooseModel_ = this.createMongooseModel_(this.schema)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the model schema. The child class must implement the static schema
|
||||
* property.
|
||||
* @return {string} the models schema
|
||||
*/
|
||||
getSchema() {
|
||||
if (!this.constructor.schema) {
|
||||
throw new Error("Schema not defined")
|
||||
}
|
||||
return this.constructor.schema
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the model name. The child class must implement the static modelName
|
||||
* property.
|
||||
* @return {string} the name of the model
|
||||
*/
|
||||
getModelName() {
|
||||
if (!this.constructor.modelName) {
|
||||
throw new Error("Every model must have a static modelName property")
|
||||
}
|
||||
return this.constructor.modelName
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
* Creates a mongoose model based on schema and model name.
|
||||
* @return {Mongooose.Model} the mongoose model
|
||||
*/
|
||||
createMongooseModel_() {
|
||||
return mongoose.model(this.getModelName(), this.getSchema())
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries the mongoose model via the mongoose's findOne.
|
||||
* @param query {object} a mongoose selector query
|
||||
* @param options {?object=} mongoose options
|
||||
* @return {?mongoose.Document} the retreived mongoose document or null.
|
||||
*/
|
||||
findOne(query, options = {}) {
|
||||
return this.mongooseModel_.findOne(query, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries the mongoose model via the mongoose's find.
|
||||
* @param query {object} a mongoose selector query
|
||||
* @param options {?object=} mongoose options
|
||||
* @return {Array<mongoose.Document>} the retreived mongoose documents or
|
||||
* an empty array
|
||||
*/
|
||||
find(query, options) {
|
||||
return this.mongooseModel_.find(query, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a model via the mongoose model's updateOne.
|
||||
* @param query {object} a mongoose selector query
|
||||
* @param update {object} mongoose update object
|
||||
* @param options {?object=} mongoose options
|
||||
* @return {object} mongoose result
|
||||
*/
|
||||
updateOne(query, update, options) {
|
||||
return this.mongooseModel_.updateOne(query, update, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a model via the mongoose model's update.
|
||||
* @param query {object} a mongoose selector query
|
||||
* @param update {object} mongoose update object
|
||||
* @param options {?object=} mongoose options
|
||||
* @return {object} mongoose result
|
||||
*/
|
||||
update(query, update, options) {
|
||||
return this.mongooseModel_.update(query, update, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a document in the mongoose model's collection via create.
|
||||
* @param object {object} the value of the document to be created
|
||||
* @param options {?object=} mongoose options
|
||||
* @return {object} mongoose result
|
||||
*/
|
||||
create(object, options) {
|
||||
return this.mongooseModel_.create(object, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a document in the mongoose model's collection
|
||||
* @param query {object} the value of the document to be created
|
||||
* @param options {?object=} mongoose options
|
||||
* @return {object} mongoose result
|
||||
*/
|
||||
deleteOne(query, options) {
|
||||
return this.mongooseModel_.deleteOne(query, options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes many document in the mongoose model's collection
|
||||
* @param query {object} the value of the document to be created
|
||||
* @param options {?object=} mongoose options
|
||||
* @return {object} mongoose result
|
||||
*/
|
||||
delete(query, options) {
|
||||
return this.mongooseModel_.deleteMany(query, options)
|
||||
}
|
||||
}
|
||||
|
||||
export default BaseModel
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Common functionality for Services
|
||||
* @interface
|
||||
*/
|
||||
class BaseService {}
|
||||
export default BaseService
|
||||
@@ -0,0 +1,3 @@
|
||||
export { default as BaseService } from "./base-service"
|
||||
export { default as BaseModel } from "./base-model"
|
||||
export { default as PaymentService } from "./payment-service"
|
||||
@@ -0,0 +1,62 @@
|
||||
import BaseService from "./base-service"
|
||||
|
||||
/**
|
||||
* The interface that all payment services must inherit from. The intercace
|
||||
* provides the necessary methods for creating, authorizing and managing
|
||||
* payments.
|
||||
* @interface
|
||||
*/
|
||||
class BasePaymentService extends BaseService {
|
||||
constructor() {
|
||||
super()
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to create a payment to be processed with the service's payment gateway.
|
||||
* @param cart {object} - the cart that the payment should cover.
|
||||
* @return {Promise<{object}>} - returns a promise that resolves to an object
|
||||
* containing the payment data. This data will be saved to the cart for later
|
||||
* use.
|
||||
*/
|
||||
createPayment(cart) {
|
||||
throw Error("createPayment must be overridden by the child class")
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to retrieve a payment.
|
||||
* @param cart {object} - the cart whose payment should be retrieved.
|
||||
* @return {Promise<{object}>} - returns a promise that resolves to the
|
||||
* payment object as stored with the provider.
|
||||
*/
|
||||
retrievePayment(cart) {
|
||||
throw Error("getPayment must be overridden by the child class")
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to update a payment. This method is called when the cart is updated.
|
||||
* @param cart {object} - the cart whose payment should be updated.
|
||||
* @return {Promise<{object}>} - returns a promise that resolves to the
|
||||
* payment object as stored with the provider.
|
||||
*/
|
||||
updatePayment(cart) {
|
||||
throw Error("updatePayment must be overridden by the child class")
|
||||
}
|
||||
|
||||
authorizePayment() {
|
||||
throw Error("authorizePayment must be overridden by the child class")
|
||||
}
|
||||
|
||||
capturePayment() {
|
||||
throw Error("capturePayment must be overridden by the child class")
|
||||
}
|
||||
|
||||
refundPayment() {
|
||||
throw Error("refundPayment must be overridden by the child class")
|
||||
}
|
||||
|
||||
deletePayment() {
|
||||
throw Error("deletePayment must be overridden by the child class")
|
||||
}
|
||||
}
|
||||
|
||||
export default BasePaymentService
|
||||
Reference in New Issue
Block a user