Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create migration files for upgrade from 1.0 to 1.5 #2962

Merged
merged 23 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
49aef2f
Check for null on object references before using
impactmass Sep 25, 2017
902124f
Migration - Set a primary shop needed in a multi-shop world
impactmass Sep 26, 2017
e5567cf
Migration - add / remove shopId on billing objects on orders and cart
impactmass Sep 26, 2017
e542836
Migration - add / remove shopId on shipping objects on orders and cart
impactmass Sep 26, 2017
5eb7b01
Migration to rebuild search collections
impactmass Sep 26, 2017
8419cec
Merge branch 'marketplace' into seun-migration-1.0-1.5
impactmass Sep 27, 2017
52c95ae
Migration - Update cart schema to include all product data #2610
impactmass Sep 27, 2017
7094c44
Reset changes on 1_rebuild_account_and_order_search_collections
impactmass Sep 27, 2017
4fc866a
Check field references before using
impactmass Sep 27, 2017
948074a
Add down migration for step 9
impactmass Sep 27, 2017
547cdaf
Direct update on down to enable restore to previous data state
impactmass Sep 27, 2017
0b062fb
Merge branch 'marketplace' into seun-migration-1.0-1.5
impactmass Sep 27, 2017
b0b4998
Add Array.isArray checks
impactmass Sep 28, 2017
0c99835
Merge branch 'marketplace' into seun-migration-1.0-1.5
impactmass Sep 29, 2017
5236509
Merge branch 'seun-migration-1.0-1.5' of github.com:reactioncommerce/…
impactmass Sep 29, 2017
a7dfb46
Check currency before using
impactmass Sep 29, 2017
843f824
Add up migration to update shipping status to workflow
impactmass Sep 29, 2017
5da6373
Add down() for shipping status and fix typo on new status
impactmass Sep 29, 2017
0b27b43
Merge branch 'marketplace' into seun-migration-1.0-1.5
impactmass Sep 29, 2017
2fc56b0
Add Array.isArray check before calling reduce
impactmass Sep 29, 2017
585abd6
Merge branch 'marketplace' into seun-migration-1.0-1.5
spencern Sep 29, 2017
7cdc7c9
Merge branch 'marketplace' into seun-migration-1.0-1.5
spencern Sep 29, 2017
6c5d774
Merge branch 'marketplace' into seun-migration-1.0-1.5
spencern Sep 29, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be the hard-coded first shipping object or the shipping object associated with the current active shop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that to staying consistent with 1.0 (or below 1.5) codebase. It's all x[0] in there. Since this migration file is interacting with data created from such code, I referenced it that way.
I think it's more recently that we started referencing shipping it by shopId (i.e marketplace).

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