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

1780 product archive #1816

Merged
merged 16 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -1,9 +1,8 @@
import React from "react";
import { Meteor } from "meteor/meteor";
import { Tags } from "/lib/collections";
import { composeWithTracker } from "/lib/api/compose";
import { Reaction, i18next } from "/client/api";

import { Tags } from "/lib/collections";
import { TranslationProvider, AdminContextProvider } from "/imports/plugins/core/ui/client/providers";
import { isRevisionControlEnabled } from "/imports/plugins/core/revisions/lib/api";

Expand Down
55 changes: 55 additions & 0 deletions imports/plugins/core/revisions/server/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,33 @@ Products.before.insert((userId, product) => {
}
});

// Prevent this product beign created if a parent product / varaint ancestor is deleted.
//
// This will prevent cases where a parent variant hase been deleted and a user tries to create a
// child variant. You cannot create the child variant becuase the parent will no longer exist when
// changes have been published; resulting in a broken inheretence and UI
const productHasAncestors = Array.isArray(product.ancestors);

if (productHasAncestors) {
// Verify there are no deleted ancestors,
// Variants cannot be restored if their parent product / variant is deleted
const archivedCount = Revisions.find({
"documentId": { $in: product.ancestors },
"documentData.isDeleted": true,
"workflow.status": {
$nin: [
"revision/published"
]
}
}).count();

if (archivedCount > 0) {
Logger.debug(`Cannot create product ${product._id} as a product/variant higher in it's ancestors tree is marked as 'isDeleted'.`);
throw new Meteor.Error(403, "Unable to create product variant");
}
}


if (!productRevision) {
Logger.debug(`No revision found for product ${product._id}. Creating new revision`);

Expand All @@ -300,6 +327,34 @@ Products.before.update(function (userId, product, fieldNames, modifier, options)
}
});

// Prevent this product revision from beign restored from isDeleted state if a product / varaint
// ancestor is also deleted.
//
// This will prevent cases where a parent variant hase been deleted and a user tries to undeleted a
// child variant. You cannot undeleted the child variant, becuase the parent will no longer exist when
// changes have been published; resulting in a broken inheretence and UI
const revisionHasAncestors = productRevision && productRevision.documentData && Array.isArray(productRevision.documentData.ancestors);
const modiferContainsIsDeleted = modifier.$set && modifier.$set.isDeleted === false;

if (revisionHasAncestors && modiferContainsIsDeleted) {
// Verify there are no deleted ancestors,
// Variants cannot be restored if their parent product / variant is deleted
const archivedCount = Revisions.find({
"documentId": { $in: productRevision.documentData.ancestors },
"documentData.isDeleted": true,
"workflow.status": {
$nin: [
"revision/published"
]
}
}).count();

if (archivedCount > 0) {
Logger.debug(`Cannot restore product ${product._id} as a product/variant higher in it's ancestors tree is marked as 'isDeleted'.`);
throw new Meteor.Error(403, "Unable to delete product variant");
}
}

const originalSelector = this.args[0];

if (!productRevision) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ class Variant extends Component {
"variant-deleted": this.props.variant.isDeleted
});

let variantTitleElement;

if (typeof variant.title === "string" && variant.title.length) {
variantTitleElement = (
<span className="variant-title">{variant.title}</span>
);
} else {
variantTitleElement = (
<Translation defaultValue="Label" i18nKey="productVariant.title" />
);
}

const variantElement = (
<li
className="variant-list-item"
Expand All @@ -87,7 +99,7 @@ class Variant extends Component {
>
<div className={classes}>
<div className="title">
<span className="variant-title">{variant.title}</span>
{variantTitleElement}
</div>

<div className="actions">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Component, PropTypes } from "react";
import { composeWithTracker } from "/lib/api/compose";
import { ReactionProduct } from "/lib/api";
import { Reaction } from "/client/api";
import { Reaction, i18next } from "/client/api";
import { VariantList } from "../components";
import { getChildVariants } from "../selectors/variants";
import { Products, Media } from "/lib/collections";
Expand Down Expand Up @@ -84,7 +84,14 @@ class VariantListContainer extends Component {
handleCreateVariant = () => {
const selectedProduct = ReactionProduct.selectedProduct();

Meteor.call("products/createVariant", selectedProduct._id);
Meteor.call("products/createVariant", selectedProduct._id, (error) => {
if (error) {
Alerts.alert({
text: i18next.t("productDetailEdit.addVariantFail", { title: selectedProduct.title }),
confirmButtonText: i18next.t("app.close", { defaultValue: "Close" })
});
}
});
}

handleVariantClick = (event, variant, ancestors = -1) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import PublishContainer from "/imports/plugins/core/revisions/client/containers/

class GridProductPublishContainer extends Component {
static propTypes = {
documentIds: PropTypes.arrayOf(PropTypes.string)
documentIds: PropTypes.arrayOf(PropTypes.string),
documents: PropTypes.arrayOf(PropTypes.object)
};

handleVisibilityChange = (event, isProductVisible) => {
Expand All @@ -17,9 +18,9 @@ class GridProductPublishContainer extends Component {
}
}

handlePublishActions = (event, action, documentIds) => {
handlePublishActions = (event, action) => {
if (action === "archive") {
ReactionProduct.maybeDeleteProduct(documentIds);
ReactionProduct.maybeDeleteProduct(this.props.documents);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,14 @@ Template.childVariantForm.events({
}, (isConfirm) => {
if (isConfirm) {
const id = instance.data._id;
Meteor.call("products/updateProductField", id, "isDeleted", false);
Meteor.call("products/updateProductField", id, "isDeleted", false, (error) => {
if (error) {
Alerts.alert({
text: i18next.t("productDetailEdit.restoreVariantFail", { title }),
confirmButtonText: i18next.t("app.close", { defaultValue: "Close" })
});
}
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,14 @@ Template.variantForm.helpers({
}, (isConfirm) => {
if (isConfirm) {
const id = variant._id;
Meteor.call("products/updateProductField", id, "isDeleted", false);
Meteor.call("products/updateProductField", id, "isDeleted", false, (error) => {
if (error) {
Alerts.alert({
text: i18next.t("productDetailEdit.restoreVariantFail", { title }),
confirmButtonText: i18next.t("app.close", { defaultValue: "Close" })
});
}
});
}
});
};
Expand Down Expand Up @@ -141,11 +148,18 @@ Template.variantForm.events({
event.stopPropagation();
event.preventDefault();
const productId = ReactionProduct.selectedProductId();

if (!productId) {
return;
}

Meteor.call("products/createVariant", template.data._id, function (error, result) {
if (result) {
if (error) {
Alerts.alert({
text: i18next.t("productDetailEdit.addVariantFail", { title: template.data.title }),
confirmButtonText: i18next.t("app.close", { defaultValue: "Close" })
});
} else if (result) {
const newVariantId = result;
const selectedProduct = ReactionProduct.selectedProduct();
ReactionProduct.setCurrentVariant(newVariantId);
Expand All @@ -167,7 +181,12 @@ Template.variantForm.events({
}
Meteor.call("products/cloneVariant", productId, template.data._id,
function (error, result) {
if (result) {
if (error) {
Alerts.alert({
text: i18next.t("productDetailEdit.cloneVariantFail", { title: template.data.title }),
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect that "productDetailEdit": { could all be moved into package server/i18n.

confirmButtonText: i18next.t("app.close", { defaultValue: "Close" })
});
} else if (result) {
const variantId = result[0];

ReactionProduct.setCurrentVariant(variantId);
Expand Down
31 changes: 29 additions & 2 deletions lib/api/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Meteor } from "meteor/meteor";
import { ReactiveDict } from "meteor/reactive-dict";
import { FlowRouter } from "meteor/kadira:flow-router-ssr";
import { getCurrentTag, getShopName } from "/lib/api";
import { Products } from "/lib/collections";
import { Products, Revisions } from "/lib/collections";
import Catalog from "./catalog";
import { MetaData } from "/lib/api/router/metadata";

Expand Down Expand Up @@ -443,7 +443,7 @@ ReactionProduct.maybeDeleteProduct = function (productOrArray) {
if (result) {
FlowRouter.go("/");
if (products.length === 1) {
title = products[0].title || "productDetail.";
title = products[0].title || i18next.t("productDetail.theProduct");
Alerts.toast(i18next.t("productDetail.archivedAlert", { product: title }), "info");
} else {
title = i18next.t("productDetail.theSelectedProducts");
Expand All @@ -455,5 +455,32 @@ ReactionProduct.maybeDeleteProduct = function (productOrArray) {
});
};

ReactionProduct.isAncestorDeleted = function (product, includeSelf) {
const productIds = [
...product.ancestors // Avoid mutations
];

if (includeSelf) {
productIds.push(product._id);
}

// Verify there are no deleted ancestors,
// Variants cannot be restored if their parent product / variant is deleted
const archivedCount = Revisions.find({
"documentId": { $in: productIds },
"documentData.isDeleted": true,
"workflow.status": {
$nin: [
"revision/published"
]
}
}).count();

if (archivedCount > 0) {
return true;
}

return false;
};

export default ReactionProduct;
3 changes: 3 additions & 0 deletions private/data/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,12 @@
"variantDetails": "Variant Details",
"duplicateVariant": "Duplicate",
"addVariantOption": "Add Option",
"addVariantFail": "Unable to create variant for {{title}}.",
"cloneVariantFail": "Unable to clone variant for {{title}}.",
"removeVariant": "Remove",
"removeVariantConfirm": "Are you sure you want to delete {{title}}",
"restoreVariantConfirm": "Are you sure you want to restore {{title}} from the trash?",
"restoreVariantFail": "Unable to restore {{title}}.",
"archiveVariantConfirm": "Are you sure you want to archive {{title}}",
"thisVariant": "this variant",
"thisOption": "this option",
Expand Down
30 changes: 27 additions & 3 deletions server/methods/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,15 @@ Meteor.methods({
throw new Meteor.Error(403, "Access Denied");
}

const variant = Products.findOne(variantId);

// Verify that this variant and any ancestors are not deleted.
// Child variants cannot be added if a parent product or product revision
// is marked as `{ isDeleted: true }`
if (ReactionProduct.isAncestorDeleted(variant, true)) {
throw new Meteor.Error(403, "Unable to create product variant");
}

const variants = Products.find({
$or: [{
_id: variantId
Expand Down Expand Up @@ -406,9 +415,16 @@ Meteor.methods({

const newVariantId = Random.id();
// get parent ancestors to build new ancestors array
const {
ancestors
} = Products.findOne(parentId);
const product = Products.findOne(parentId);
const { ancestors } = product;

// Verify that the parent variant and any ancestors are not deleted.
// Child variants cannot be added if a parent product or product revision
// is marked as `{ isDeleted: true }`
if (ReactionProduct.isAncestorDeleted(product, true)) {
throw new Meteor.Error(403, "Unable to create product variant");
}

Array.isArray(ancestors) && ancestors.push(parentId);
const assembledVariant = Object.assign(newVariant || {}, {
_id: newVariantId,
Expand Down Expand Up @@ -505,6 +521,10 @@ Meteor.methods({
}

const selector = {
// Don't "archive" variants that are already marked deleted.
isDeleted: {
$in: [false, undefined]
},
$or: [{
_id: variantId
}, {
Expand Down Expand Up @@ -700,6 +720,10 @@ Meteor.methods({
productIds = productId;
}
const productsWithVariants = Products.find({
// Don't "archive" products that are already marked deleted.
isDeleted: {
$in: [false, undefined]
},
$or: [{
_id: {
$in: productIds
Expand Down