Adds OrderService and /orders endpoints

Adds OrderService

Adds endpoints for orders - both store and admin
This commit is contained in:
Oliver Windall Juhl authored and GitHub committed 2020-05-11 13:54:09 +02:00
1 parent 7ca03579db
commit 8255a839f6
39 files changed
+2648 -47

No files matched your search

@@ -0,0 +1,242 @@
import { IdMap } from "medusa-test-utils"
export const orders = {
testOrder: {
_id: IdMap.getId("test-order"),
email: "oliver@test.dk",
billing_address: {
first_name: "Oli",
last_name: "Medusa",
address_1: "testaddress",
city: "LA",
country_code: "US",
postal_code: "90002",
},
shipping_address: {
first_name: "Oli",
last_name: "Medusa",
address_1: "testaddress",
city: "LA",
country_code: "US",
postal_code: "90002",
},
items: [
{
_id: IdMap.getId("existingLine"),
title: "merge line",
description: "This is a new line",
thumbnail: "test-img-yeah.com/thumb",
content: {
unit_price: 123,
variant: {
_id: IdMap.getId("can-cover"),
},
product: {
_id: IdMap.getId("validId"),
},
quantity: 1,
},
quantity: 10,
},
],
region: IdMap.getId("region-france"),
customer_id: IdMap.getId("test-customer"),
payment_method: {
provider_id: "default_provider",
},
shipping_methods: [
{
_id: IdMap.getId("expensiveShipping"),
name: "Expensive Shipping",
price: 100,
provider_id: "default_provider",
profile_id: IdMap.getId("default"),
},
{
_id: IdMap.getId("freeShipping"),
name: "Free Shipping",
price: 10,
provider_id: "default_provider",
profile_id: IdMap.getId("profile1"),
},
],
fulfillment_status: "not_fulfilled",
payment_status: "awaiting",
status: "pending",
},
processedOrder: {
_id: IdMap.getId("processed-order"),
email: "oliver@test.dk",
billing_address: {
first_name: "Oli",
last_name: "Medusa",
address_1: "testaddress",
city: "LA",
country_code: "US",
postal_code: "90002",
},
shipping_address: {
first_name: "Oli",
last_name: "Medusa",
address_1: "testaddress",
city: "LA",
country_code: "US",
postal_code: "90002",
},
items: [
{
_id: IdMap.getId("existingLine"),
title: "merge line",
description: "This is a new line",
thumbnail: "test-img-yeah.com/thumb",
content: {
unit_price: 123,
variant: {
_id: IdMap.getId("can-cover"),
},
product: {
_id: IdMap.getId("validId"),
},
quantity: 1,
},
quantity: 10,
},
],
region: IdMap.getId("region-france"),
customer_id: IdMap.getId("test-customer"),
payment_method: {
provider_id: "default_provider",
},
shipping_methods: [
{
_id: IdMap.getId("expensiveShipping"),
name: "Expensive Shipping",
price: 100,
provider_id: "default_provider",
profile_id: IdMap.getId("default"),
},
{
_id: IdMap.getId("freeShipping"),
name: "Free Shipping",
price: 10,
provider_id: "default_provider",
profile_id: IdMap.getId("profile1"),
},
],
fulfillment_status: "fulfilled",
payment_status: "captured",
status: "completed",
},
orderToRefund: {
_id: IdMap.getId("refund-order"),
email: "oliver@test.dk",
billing_address: {
first_name: "Oli",
last_name: "Medusa",
address_1: "testaddress",
city: "LA",
country_code: "US",
postal_code: "90002",
},
shipping_address: {
first_name: "Oli",
last_name: "Medusa",
address_1: "testaddress",
city: "LA",
country_code: "US",
postal_code: "90002",
},
items: [
{
_id: IdMap.getId("existingLine"),
title: "merge line",
description: "This is a new line",
thumbnail: "test-img-yeah.com/thumb",
content: {
unit_price: 100,
variant: {
_id: IdMap.getId("eur-8-us-10"),
},
product: {
_id: IdMap.getId("product"),
},
quantity: 1,
},
quantity: 10,
},
{
_id: IdMap.getId("existingLine2"),
title: "merge line",
description: "This is a new line",
thumbnail: "test-img-yeah.com/thumb",
content: {
unit_price: 100,
variant: {
_id: IdMap.getId("can-cover"),
},
product: {
_id: IdMap.getId("product"),
},
quantity: 1,
},
quantity: 10,
},
],
region: IdMap.getId("region-france"),
customer_id: IdMap.getId("test-customer"),
payment_method: {
provider_id: "default_provider",
},
shipping_methods: [
{
provider_id: "default_provider",
profile_id: IdMap.getId("default"),
data: {},
items: {},
},
{
provider_id: "default_provider",
profile_id: IdMap.getId("default"),
data: {},
items: {},
},
],
discounts: [],
},
}
export const OrderModelMock = {
create: jest.fn().mockReturnValue(Promise.resolve()),
updateOne: jest.fn().mockImplementation((query, update) => {
return Promise.resolve()
}),
deleteOne: jest.fn().mockReturnValue(Promise.resolve()),
findOne: jest.fn().mockImplementation(query => {
if (query._id === IdMap.getId("test-order")) {
orders.testOrder.payment_status = "awaiting"
return Promise.resolve(orders.testOrder)
}
if (query._id === IdMap.getId("not-fulfilled-order")) {
orders.testOrder.fulfillment_status = "not_fulfilled"
orders.testOrder.payment_status = "awaiting"
return Promise.resolve(orders.testOrder)
}
if (query._id === IdMap.getId("fulfilled-order")) {
orders.testOrder.fulfillment_status = "fulfilled"
return Promise.resolve(orders.testOrder)
}
if (query._id === IdMap.getId("payed-order")) {
orders.testOrder.fulfillment_status = "not_fulfilled"
orders.testOrder.payment_status = "captured"
return Promise.resolve(orders.testOrder)
}
if (query._id === IdMap.getId("processed-order")) {
return Promise.resolve(orders.processedOrder)
}
if (query._id === IdMap.getId("order-refund")) {
orders.orderToRefund.payment_status = "captured"
return Promise.resolve(orders.orderToRefund)
}
return Promise.resolve(undefined)
}),
}
+8 -6
View File
@@ -1,6 +1,3 @@
/*******************************************************************************
*
******************************************************************************/
import mongoose from "mongoose"
import { BaseModel } from "medusa-interfaces"
@@ -8,19 +5,24 @@ import LineItemSchema from "./schemas/line-item"
import PaymentMethodSchema from "./schemas/payment-method"
import ShippingMethodSchema from "./schemas/shipping-method"
import AddressSchema from "./schemas/address"
import DiscountModel from "./discount"
class OrderModel extends BaseModel {
static modelName = "Order"
static schema = {
canceled: { type: Boolean, default: false },
archived: { type: Boolean, default: false },
// pending, completed, archived, cancelled
status: { type: String, default: "pending" },
// not_fulfilled, partially_fulfilled (some line items have been returned), fulfilled, returned,
fulfillment_status: { type: String, default: "not_fulfilled" },
// awaiting, captured, refunded
payment_status: { type: String, default: "awaiting" },
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: [] },
discounts: { type: [DiscountModel.schema], default: [] },
customer_id: { type: String, required: true },
payment_method: { type: PaymentMethodSchema, required: true },
shipping_methods: { type: [ShippingMethodSchema], required: true },
@@ -33,5 +33,6 @@ export default new mongoose.Schema({
// 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 },
returned: { type: Boolean, default: false },
metadata: { type: mongoose.Schema.Types.Mixed, default: {} },
})
@@ -2,6 +2,8 @@ import mongoose from "mongoose"
export default new mongoose.Schema({
provider_id: { type: String, required: true },
profile_id: { type: String, required: true },
price: { type: Number, required: true },
data: { type: mongoose.Schema.Types.Mixed, default: {} },
items: { type: [mongoose.Schema.Types.Mixed], default: [] },
})
@@ -10,6 +10,7 @@ class ShippingOptionModel extends BaseModel {
name: { type: String, required: true },
region_id: { type: String, required: true },
provider_id: { type: String, required: true },
profile_id: { type: String, required: true },
data: { type: mongoose.Schema.Types.Mixed, default: {} },
price: { type: ShippingOptionPrice, required: true },
requirements: { type: [ShippingOptionRequirement], default: [] },