Skip to content

Commit

Permalink
Merge pull request #2962 from reactioncommerce/seun-migration-1.0-1.5
Browse files Browse the repository at this point in the history
Create migration files for upgrade from 1.0 to 1.5
  • Loading branch information
spencern authored Sep 29, 2017
2 parents eada374 + 6c5d774 commit 782ce04
Show file tree
Hide file tree
Showing 15 changed files with 279 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const composer = (props, onData) => {
const localStorageCurrency = localStorage.getItem("currency");
const locale = Reaction.Locale.get();

if (localStorageCurrency) {
if (localStorageCurrency && shop.currencies[localStorageCurrency] && shop.currencies[localStorageCurrency].symbol) {
currentCurrency = localStorageCurrency + " " + shop.currencies[localStorageCurrency].symbol;
} else if (locale && locale.currency && locale.currency.enabled) {
currentCurrency = locale.locale.currency + " " + locale.currency.symbol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class InvoiceContainer extends Component {
const discounts = paymentMethod && paymentMethod.discounts;
const refund = value;
const refunds = this.state.refunds;
const refundTotal = refunds && refunds.reduce((acc, item) => acc + parseFloat(item.amount), 0);
const refundTotal = refunds && Array.isArray(refunds) && refunds.reduce((acc, item) => acc + parseFloat(item.amount), 0);

let adjustedTotal;

Expand Down Expand Up @@ -626,7 +626,7 @@ const composer = (props, onData) => {

// get adjusted Total
let adjustedTotal;
const refundTotal = refunds && refunds.reduce((acc, item) => acc + parseFloat(item.amount), 0);
const refundTotal = refunds && Array.isArray(refunds) && refunds.reduce((acc, item) => acc + parseFloat(item.amount), 0);

if (paymentMethod && paymentMethod.processor === "Stripe") {
adjustedTotal = Math.abs(paymentMethod.amount + orderDiscounts - refundTotal);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ Template.coreOrderShippingTracking.helpers({
}
});

return fullItem.workflow.workflow.includes("coreOrderItemWorkflow/completed");
if (Array.isArray(fullItem.workflow.workflow)) {
return fullItem.workflow.workflow.includes("coreOrderItemWorkflow/completed");
}
});

return completedItems;
Expand Down Expand Up @@ -203,7 +205,7 @@ Template.coreOrderShippingTracking.helpers({
const shipment = getShippingInfo(order);
const shipmentWorkflow = shipment.workflow;

return shipmentWorkflow && shipmentWorkflow.workflow.includes("coreOrderWorkflow/packed") && shipment.tracking
|| shipmentWorkflow && shipmentWorkflow.workflow.includes("coreOrderWorkflow/packed");
return shipmentWorkflow && Array.isArray(shipmentWorkflow.workflow) && shipmentWorkflow.workflow.includes("coreOrderWorkflow/packed") && shipment.tracking
|| shipmentWorkflow && Array.isArray(shipmentWorkflow.workflow) && shipmentWorkflow.workflow.includes("coreOrderWorkflow/packed");
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Migrations } from "meteor/percolate:migrations";
import { Reaction } from "/server/api/";
import { Shops } from "/lib/collections";

Migrations.add({
// moving to multi-shop setup requries a primary shop to be set.
// Updates a shop marked active, that has associated email for domain as the primary shop
version: 10,
up() {
Shops.update({
"status": "active",
"domains": Reaction.getDomain(),
"emails.0.address": { $exists: true }
}, {
$set: { shopType: "primary" }
});
},
down() {
Shops._collection.update({ shopType: "primary" }, {
$unset: { shopType: "" }
});
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Migrations } from "meteor/percolate:migrations";
import { Cart, Orders, Products } from "/lib/collections";

Migrations.add({
version: 11,
up() {
// Add whole product object to all cart items in all Cart documents if the cart has an item added
Cart.find().forEach((cart) => {
if (Array.isArray(cart.items) && cart.items.length) {
cart.items.forEach((item) => {
item.product = Products.findOne({ _id: item.productId });
});
Cart.update({ _id: cart._id }, {
$set: { items: cart.items }
});
}
});

// Add whole product object to all order items in all order documents
Orders.find().forEach((order) => {
order.items.forEach((item) => {
item.product = Products.findOne({ _id: item.productId });
});
Orders.update({ _id: order._id }, {
$set: { items: order.items }
});
});
},
// Going down, we remove the product object on each item in cart and order
down() {
Cart.find().forEach((cart) => {
if (Array.isArray(cart.items) && cart.items.length) {
cart.items.forEach((item) => {
delete item.product;
});
Cart._collection.update({ _id: cart._id }, {
$set: { items: cart.items }
});
}
});

Orders.find().forEach((order) => {
order.items.forEach((item) => {
delete item.product;
});
Orders._collection.update({ _id: order._id }, {
$set: { items: order.items }
});
});
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Migrations } from "meteor/percolate:migrations";
import { Cart, Orders } from "/lib/collections";
import { Reaction } from "/server/api/";

Migrations.add({
version: 12,
up() {
// moving to multi-shop setup requires each billing objects to be marked by shopId
// This adds shopId field to each billing object in orders and carts.
const shopId = Reaction.getShopId();

Orders.update({}, {
$set: { "billing.0.shopId": shopId }
}, {
multi: true
});

Cart.update({}, {
$set: { "billing.0.shopId": shopId }
}, {
multi: true
});
},
down() {
Orders.update({}, {
$unset: { "billing.0.shopId": "" }
}, {
multi: true
});

Cart.update({}, {
$set: { "billing.0.shopId": "" }
}, {
multi: true
});
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Migrations } from "meteor/percolate:migrations";
import { Cart, Orders } from "/lib/collections";
import { Reaction } from "/server/api/";

Migrations.add({
version: 13,
up() {
// moving to multi-shop setup requires each shipping objects to be marked by shopId
// This adds shopId field to each shipping object in orders and carts.
const shopId = Reaction.getShopId();

Orders.update({}, {
$set: { "shipping.0.shopId": shopId }
}, {
multi: true
});

Cart.update({}, {
$set: { "shipping.0.shopId": shopId }
}, {
multi: true
});
},
down() {
Orders.update({}, {
$unset: { "shipping.0.shopId": "" }
}, {
multi: true
});

Cart.update({}, {
$set: { "shipping.0.shopId": "" }
}, {
multi: true
});
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Migrations } from "meteor/percolate:migrations";
import { OrderSearch } from "/lib/collections";
import { buildOrderSearch } from "/imports/plugins/included/search-mongo/server/methods/searchcollections";

Migrations.add({
// Migrations 12 and 13 introduced changes on Orders, so we need to rebuild the search collection
version: 14,
up: function () {
OrderSearch.remove({});
buildOrderSearch();
},
down: function () {
// whether we are going up or down we just want to update the search collections
// to match whatever the current code in the build methods are.
OrderSearch.remove({});
buildOrderSearch();
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Migrations } from "meteor/percolate:migrations";
import { Orders } from "/lib/collections";

Migrations.add({
version: 15,
// Reaction v1.0 had 3 shipping boolean states (packed, shipped, delivered). Shipping workflow is not managed with a
// workflow object that keeps track of previous state.
up: function () {
Orders.find().forEach((order) => {
const currentShipping = order.shipping[0];
currentShipping.workflow = {};

if (currentShipping.packed) {
currentShipping.workflow.status = "coreOrderWorkflow/packed";
currentShipping.workflow.workflow = ["coreOrderWorkflow/notStarted", "coreOrderWorkflow/packed"];
}

if (currentShipping.shipped) {
currentShipping.workflow.status = "coreOrderWorkflow/shipped";
currentShipping.workflow.workflow = [
"coreOrderWorkflow/notStarted", "coreOrderWorkflow/packed", "coreOrderWorkflow/shipped"
];
}

if (currentShipping.delivered) {
currentShipping.workflow.status = "coreOrderWorkflow/delivered";
currentShipping.workflow.workflow = [
"coreOrderWorkflow/notStarted",
"coreOrderWorkflow/packed",
"coreOrderWorkflow/shipped",
"coreOrderWorkflow/delivered"
];
}

// If none of the 3 v1.0 states is true, set as unstarted.
// Note: In case of customized workflow status, modify here to capture the added status(es) before running the migration
currentShipping.workflow.status = "new";
currentShipping.workflow.workflow = ["coreOrderWorkflow/notStarted"];

delete currentShipping.packed;
delete currentShipping.shipped;
delete currentShipping.delivered;

Orders.update({ _id: order._id }, {
$set: { "shipping.0": currentShipping }
});
});
},
down: function () {
Orders.find().forEach((order) => {
const currentShipping = order.shipping[0];
const workflow = currentShipping.workflow;

currentShipping.packed = false;
currentShipping.shipped = false;
currentShipping.delivered = false;

if (workflow && workflow.status === "coreOrderWorkflow/packed") {
currentShipping.packed = true;
}

if (workflow && workflow.status === "coreOrderWorkflow/shipped") {
currentShipping.shipped = true;
}

if (workflow && workflow.status === "coreOrderWorkflow/delivered") {
currentShipping.delivered = true;
}

delete currentShipping.workflow.workflow;

Orders.update({ _id: order._id }, {
$set: { "shipping.0": currentShipping }
});
});
}
});

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Migrations } from "/imports/plugins/core/versions";
import { Migrations } from "meteor/percolate:migrations";
import { Shops } from "/lib/collections";

Migrations.add({
Expand Down Expand Up @@ -38,5 +38,15 @@ Migrations.add({
});
}
);
},
down() {
Shops.find().forEach((shop) => {
shop.baseUOM = shop.baseUOM && shop.baseUOM.toUpperCase();
Shops.update({ _id: shop._id }, {
$set: {
baseUOM: shop.baseUOM
}
});
});
}
});
7 changes: 7 additions & 0 deletions imports/plugins/core/versions/server/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ import "./5_update_defaultRoles_to_groups";
import "./6_update_tags_is_visible";
import "./7_add_shop_slugs_to_schema";
import "./8_update_registry_provides_to_array";
import "./9_update_metrics";
import "./10_set_primary_shop";
import "./11_add_product_to_cart_items";
import "./12_add_shopId_on_billing";
import "./13_add_shopId_on_shipping";
import "./14_rebuild_order_search_collection";
import "./15_update_shipping_status_to_workflow";
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ export function buildOrderSearchRecord(orderId) {
}
orderSearch.product = {};
orderSearch.variants = {};
orderSearch.product.title = order.items.map(item => item.product.title);
orderSearch.variants.title = order.items.map(item => item.variants.title);
orderSearch.variants.optionTitle = order.items.map(item => item.variants.optionTitle);
orderSearch.product.title = order.items.map(item => item.product && item.product.title);
orderSearch.variants.title = order.items.map(item => item.variants && item.variants.title);
orderSearch.variants.optionTitle = order.items.map(item => item.variants && item.variants.optionTitle);

OrderSearch.insert(orderSearch);
}
Expand Down
2 changes: 0 additions & 2 deletions lib/collections/schemas/payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ export const Payment = new SimpleSchema({
type: Currency,
optional: true
},
// TODO: REVIEW Not sure if shopId should be optional on the Payment Schema
// TODO: If we make shopId on the payment schema required, we need to build this into the migration for old orders
shopId: {
type: String,
optional: true
Expand Down
6 changes: 5 additions & 1 deletion lib/collections/schemas/shops.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,22 @@ export const Shop = new SimpleSchema({
label: "Base Unit of Measure"
},
"unitsOfMeasure": {
type: [Object]
type: [Object],
optional: true
},
"unitsOfMeasure.$.uom": {
type: String,
optional: true,
defaultValue: "oz"
},
"unitsOfMeasure.$.label": {
type: String,
optional: true,
defaultValue: "Ounces"
},
"unitsOfMeasure.$.default": {
type: Boolean,
optional: true,
defaultValue: false
},
"metafields": {
Expand Down
2 changes: 1 addition & 1 deletion server/methods/core/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ export const methods = {
const shippingRecord = order.shipping.find(shipping => shipping.shopId === Reaction.getShopId());
// TODO: Update */refunds/list for marketplace
const refundResult = Meteor.call("orders/refunds/list", order);
const refundTotal = refundResult.reduce((acc, refund) => acc + refund.amount, 0);
const refundTotal = Array.isArray(refundResult) && refundResult.reduce((acc, refund) => acc + refund.amount, 0);

// Get user currency formatting from shops collection, remove saved rate
const userCurrencyFormatting = _.omit(shop.currencies[billing.currency.userCurrency], ["enabled", "rate"]);
Expand Down

0 comments on commit 782ce04

Please sign in to comment.