Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
9 changes: 9 additions & 0 deletions imports/collections/schemas/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ registerSchema("OrderTransaction", OrderTransaction);
* @property {String} shopId The shop that fulfills this group
* @property {Number} totalItemQuantity The total item quantity, sum of all quantities
* @property {String} tracking Tracking reference ID
* @property {String} trackingUrl Tracking URL
* @property {String} type Fulfillment type
* @property {Object} workflow Current status and past statuses for this fulfillment
*/
Expand Down Expand Up @@ -355,10 +356,18 @@ export const OrderFulfillmentGroup = new SimpleSchema({
type: String,
optional: true
},
"trackingUrl": {
type: String,
optional: true
},
"type": {
type: String,
allowedValues: ["shipping"]
},
"updatedAt": {
type: Date,
optional: true
},
"workflow": Workflow
});

Expand Down
28 changes: 28 additions & 0 deletions imports/plugins/core/orders/client/graphql/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import simpleGraphQLClient from "/imports/plugins/core/graphql/lib/helpers/simpleClient";
import getOpaqueIds from "/imports/plugins/core/core/client/util/getOpaqueIds";
import captureOrderPaymentsMutation from "./captureOrderPayments.graphql";
import updateOrderFulfillmentGroupMutation from "./updateOrderFulfillmentGroup.graphql";

const captureOrderPaymentsMutate = simpleGraphQLClient.createMutationFunction(captureOrderPaymentsMutation);
const updateOrderFulfillmentGroupMutate = simpleGraphQLClient.createMutationFunction(updateOrderFulfillmentGroupMutation);

export const captureOrderPayments = async ({ orderId, paymentIds, shopId }) => {
// Convert MongoDB IDs to GraphQL IDs
const conversionRequests = [
Expand All @@ -28,3 +31,28 @@ export const captureOrderPayments = async ({ orderId, paymentIds, shopId }) => {
}
});
};

export const updateOrderFulfillmentGroup = async ({
orderId,
orderFulfillmentGroupId,
status,
tracking
Copy link
Member

Choose a reason for hiding this comment

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

See that you noted trackingUrl is not updated here yet. Should we go ahead and add it already to keep this in sync with what the other mutation can do?

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'd rather wait until we actually add a field in UI for it and need it here.

}) => {
// Convert MongoDB IDs to GraphQL IDs
const conversionRequests = [
{ namespace: "Order", id: orderId },
{ namespace: "OrderFulfillmentGroup", id: orderFulfillmentGroupId }
];

const [
opaqueOrderId,
opaqueOrderFulfillmentGroupId
] = await getOpaqueIds(conversionRequests);

return updateOrderFulfillmentGroupMutate({
orderFulfillmentGroupId: opaqueOrderFulfillmentGroupId,
orderId: opaqueOrderId,
status,
tracking
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
updateOrderFulfillmentGroup($orderFulfillmentGroupId: ID!, $orderId: ID!, $status: String, $tracking: String) {
updateOrderFulfillmentGroup(input: {
orderFulfillmentGroupId: $orderFulfillmentGroupId,
orderId: $orderId,
status: $status,
tracking: $tracking
}) {
order {
fulfillmentGroups {
status
tracking
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { Tracker } from "meteor/tracker";
import { ReactiveVar } from "meteor/reactive-var";
import { Template } from "meteor/templating";
import { i18next, Reaction } from "/client/api";
import Logger from "/client/modules/logger";
import { Orders } from "/lib/collections";
import { getShippingInfo } from "../../helpers";
import { updateOrderFulfillmentGroup } from "../../graphql";

Template.coreOrderShippingTracking.onCreated(() => {
const template = Template.instance();
Expand Down Expand Up @@ -86,12 +88,19 @@ Template.coreOrderShippingTracking.events({
const shipment = currentData.fulfillment;
const tracking = event.target.trackingNumber.value;

Meteor.call("orders/updateShipmentTracking", order, shipment, tracking, (error) => {
if (!error) {
updateOrderFulfillmentGroup({
orderFulfillmentGroupId: shipment._id,
orderId: order._id,
tracking
})
.then(() => {
template.orderDep.changed();
template.showTrackingEditForm.set(false);
}
});
return null;
})
.catch((error) => {
Logger.error(error);
});
},
"click [data-event-action=editTracking]": (event, template) => {
template.showTrackingEditForm.set(true);
Expand Down
4 changes: 1 addition & 3 deletions imports/plugins/core/orders/server/methods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import shipmentPacked from "./shipmentPacked";
import shipmentPicked from "./shipmentPicked";
import shipmentShipped from "./shipmentShipped";
import updateHistory from "./updateHistory";
import updateShipmentTracking from "./updateShipmentTracking";

/**
* @file Methods for Orders.
Expand All @@ -35,6 +34,5 @@ export default {
"orders/shipmentPacked": shipmentPacked,
"orders/shipmentPicked": shipmentPicked,
"orders/shipmentShipped": shipmentShipped,
"orders/updateHistory": updateHistory,
"orders/updateShipmentTracking": updateShipmentTracking
"orders/updateHistory": updateHistory
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`throws if orderFulfillmentGroupId isn't supplied 1`] = `"Order fulfillment group ID is required"`;

exports[`throws if orderId isn't supplied 1`] = `"Order ID is required"`;

exports[`throws if permission check fails 1`] = `"Access Denied"`;

exports[`throws if the order doesn't exist 1`] = `"Order not found"`;

exports[`throws if the order fulfillment group doesn't exist 1`] = `"Order fulfillment group not found"`;
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import cancelOrderItem from "./cancelOrderItem";
import placeOrder from "./placeOrder";
import updateOrder from "./updateOrder";
import updateOrderFulfillmentGroup from "./updateOrderFulfillmentGroup";

export default {
cancelOrderItem,
placeOrder,
updateOrder
updateOrder,
updateOrderFulfillmentGroup
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import SimpleSchema from "simpl-schema";
import ReactionError from "@reactioncommerce/reaction-error";
import { Order as OrderSchema } from "/imports/collections/schemas";

const inputSchema = new SimpleSchema({
tracking: {
type: String,
optional: true
},
trackingUrl: {
type: String,
optional: true
},
orderFulfillmentGroupId: String,
orderId: String,
status: {
type: String,
optional: true
}
});

/**
* @method updateOrderFulfillmentGroup
* @summary Use this mutation to update an order fulfillment group status and tracking information
* @param {Object} context - an object containing the per-request state
* @param {Object} input - Necessary input. See SimpleSchema
* @return {Promise<Object>} Object with `order` property containing the updated order
*/
export default async function updateOrderFulfillmentGroup(context, input) {
inputSchema.validate(input);

const {
tracking,
trackingUrl,
orderFulfillmentGroupId,
orderId,
status
} = input;

const { appEvents, collections, isInternalCall, userHasPermission, userId } = context;
const { Orders } = collections;

// First verify that this order actually exists
const order = await Orders.findOne({ _id: orderId });
if (!order) throw new ReactionError("not-found", "Order not found");

// Allow update if the account has "orders" permission. When called internally by another
// plugin, context.isInternalCall can be set to `true` to disable this check.
if (!isInternalCall && !userHasPermission(["orders"], order.shopId)) {
throw new ReactionError("access-denied", "Access Denied");
}

// Verify that there is a group with the ID
const orderFulfillmentGroup = order.shipping.find((group) => group._id === orderFulfillmentGroupId);
if (!orderFulfillmentGroup) throw new ReactionError("not-found", "Order fulfillment group not found");

const modifier = {
$set: {
"shipping.$.updatedAt": new Date(),
"updatedAt": new Date()
}
};

if (tracking) modifier.$set["shipping.$.tracking"] = tracking;
if (trackingUrl) modifier.$set["shipping.$.trackingUrl"] = trackingUrl;

if (status && orderFulfillmentGroup.workflow.status !== status) {
modifier.$set["shipping.$.workflow.status"] = status;
modifier.$push = {
"shipping.$.workflow.workflow": status
};
}

// Skip updating if we have no updates to make
if (Object.keys(modifier.$set).length === 2) return { order };

OrderSchema.validate(modifier, { modifier: true });

const { modifiedCount, value: updatedOrder } = await Orders.findOneAndUpdate(
{
"_id": orderId,
"shipping._id": orderFulfillmentGroupId
},
modifier,
{ returnOriginal: false }
);
if (modifiedCount === 0 || !updatedOrder) throw new ReactionError("server-error", "Unable to update order");

await appEvents.emit("afterOrderUpdate", {
order: updatedOrder,
updatedBy: userId
});

return { order: updatedOrder };
}
Loading