Moves Medusa interfaces defines model schemas for Customer, Cart, Order

This commit is contained in:
Sebastian Rindom
2020-01-30 09:38:08 +01:00
parent 751adec7f7
commit edce88c34b
26 changed files with 147 additions and 19 deletions
@@ -1,121 +0,0 @@
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
@@ -1,6 +0,0 @@
/**
* Common functionality for Services
* @interface
*/
class BaseService {}
export default BaseService
-2
View File
@@ -1,2 +0,0 @@
export { default as BaseService } from "./base-service"
export { default as BaseModel } from "./base-model"
@@ -1,62 +0,0 @@
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
+1 -3
View File
@@ -1,13 +1,11 @@
import glob from "glob"
import { BaseModel, BaseService, PaymentService } from "medusa-interfaces"
import _ from "lodash"
import path from "path"
import fs from "fs"
import { asFunction } from "awilix"
import { sync as existsSync } from "fs-exists-cached"
import { plugins } from "../../medusa-config.js"
import PaymentService from "../interfaces/payment-service"
import BaseModel from "../interfaces/base-model"
import BaseService from "../interfaces/base-service"
/**
* Registers all services in the services directory
+10 -6
View File
@@ -1,11 +1,12 @@
/*******************************************************************************
* models/product-variant.js
*
******************************************************************************/
import mongoose from "mongoose"
import { BaseModel } from "../interfaces"
import { BaseModel } from "medusa-interfaces"
import LineItemSchema from "./schemas/line-item"
import PaymentMethodSchema from "./schemas/payment-method"
import ShippingMethodSchema from "./schemas/shipping-method"
import AddressSchema from "./schemas/address"
class CartModel extends BaseModel {
@@ -13,12 +14,15 @@ class CartModel extends BaseModel {
static schema = {
email: { type: String },
billingAddress: { type: AddressSchema },
shippingAddress: { type: AddressSchema },
items: { type: [LinetItemSchema], default: [] },
billing_address: { type: AddressSchema },
shipping_address: { type: AddressSchema },
items: { type: [LineItemSchema], default: [] },
region: { type: String, required: true },
discounts: { type: [String], default: true },
discounts: { type: [String], default: [] },
customer_id: { type: String },
payment_method: { type: PaymentMethodSchema },
shipping_methods: { type: [ShippingMethodSchema] },
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
}
}
+22
View File
@@ -0,0 +1,22 @@
/*******************************************************************************
*
******************************************************************************/
import mongoose from "mongoose"
import { BaseModel } from "medusa-interfaces"
import AddressSchema from "./schemas/address"
class CustomerModel extends BaseModel {
static modelName = "Customer"
static schema = {
email: { type: String, required: true, unique: true },
first_name: { type: String, required: true },
last_name: { type: String, required: true },
billingAddress: { type: AddressSchema },
password_hash: { type: String },
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
}
}
export default CustomerModel
+31
View File
@@ -0,0 +1,31 @@
/*******************************************************************************
*
******************************************************************************/
import mongoose from "mongoose"
import { BaseModel } from "medusa-interfaces"
import LineItemSchema from "./schemas/line-item"
import PaymentMethodSchema from "./schemas/payment-method"
import ShippingMethodSchema from "./schemas/shipping-method"
import AddressSchema from "./schemas/address"
class OrderModel extends BaseModel {
static modelName = "Order"
static schema = {
canceled: { type: Boolean, default: false },
archived: { type: Boolean, default: false },
email: { type: String, required: true },
billing_address: { type: AddressSchema, required: true },
shipping_address: { type: AddressSchema, required: true },
items: { type: [LineItemSchema], required: true },
region: { type: String, required: true },
discounts: { type: [String], default: [] },
customer_id: { type: String, required: true },
payment_method: { type: PaymentMethodSchema, required: true },
shipping_methods: { type: [ShippingMethodSchema], required: true },
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
}
}
export default OrderModel
@@ -3,7 +3,7 @@
*
******************************************************************************/
import mongoose from "mongoose"
import { BaseModel } from "../interfaces"
import { BaseModel } from "medusa-interfaces"
import MoneyAmountSchema from "./schemas/money-amount"
import OptionValueSchema from "./schemas/option-value"
+1 -1
View File
@@ -3,7 +3,7 @@
*
******************************************************************************/
import mongoose from "mongoose"
import { BaseModel } from "../interfaces"
import { BaseModel } from "medusa-interfaces"
import OptionSchema from "./schemas/option"
@@ -11,6 +11,7 @@ export default new mongoose.Schema({
// mongoose doesn't allow multi-type validation but this field allows both
// an object containing:
// {
// unit_price: (MoneyAmount),
// variant: (ProductVariantSchema),
// product: (ProductSchema)
// }
@@ -18,11 +19,17 @@ export default new mongoose.Schema({
// and and array containing:
// [
// {
// unit_price: (MoneyAmount),
// variant: (ProductVariantSchema),
// product: (ProductSchema)
// }
// ]
// validation is done in the cart service.
//
// The unit_price field can be used to override the default pricing mechanism.
// By default the price will be set based on the variant(s) in content,
// however, to allow line items with variable pricing e.g. limited sales, gift
// cards etc. the unit_price field is provided to give more granular control.
content: { type: mongoose.Schema.Types.Mixed, required: true },
quantity: { type: Number, required: true },
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
@@ -0,0 +1,9 @@
/*******************************************************************************
*
******************************************************************************/
import mongoose from "mongoose"
export default new mongoose.Schema({
provider_id: { type: String, required: true },
data: { type: mongoose.Schema.Types.Mixed, default: {} },
})
@@ -0,0 +1,9 @@
/*******************************************************************************
*
******************************************************************************/
import mongoose from "mongoose"
export default new mongoose.Schema({
provider_id: { type: String, required: true },
data: { type: mongoose.Schema.Types.Mixed, default: {} },
})
+1 -1
View File
@@ -2,7 +2,7 @@
* models/user.js
*
******************************************************************************/
import { BaseModel } from "../interfaces"
import { BaseModel } from "medusa-interfaces"
class UserModel extends BaseModel {
static modelName = "User"
+1 -1
View File
@@ -1,5 +1,5 @@
import bcrypt from "bcrypt"
import { BaseService } from "../interfaces"
import { BaseService } from "medusa-interfaces"
/**
* Can authenticate a user based on email password combination
+1 -1
View File
@@ -1,7 +1,7 @@
import mongoose from "mongoose"
import _ from "lodash"
import { Validator, MedusaError } from "medusa-core-utils"
import { BaseService } from "../interfaces"
import { BaseService } from "medusa-interfaces"
/**
* Provides layer to manipulate carts.
@@ -1,5 +1,5 @@
import _ from "lodash"
import { BaseService } from "../interfaces"
import { BaseService } from "medusa-interfaces"
import { Validator, MedusaError } from "medusa-core-utils"
/**
+1 -1
View File
@@ -1,7 +1,7 @@
import mongoose from "mongoose"
import _ from "lodash"
import { Validator, MedusaError } from "medusa-core-utils"
import { BaseService } from "../interfaces"
import { BaseService } from "medusa-interfaces"
/**
* Provides layer to manipulate products.