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

Skip to content
Merged
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
47 changes: 8 additions & 39 deletions imports/plugins/included/jobcontrol/server/jobs/cart.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import Hooks from "@reactioncommerce/hooks";
import Logger from "@reactioncommerce/logger";
import { Meteor } from "meteor/meteor";
import { Accounts, Cart, Jobs } from "/lib/collections";
import { Cart, Jobs } from "/lib/collections";
import Reaction from "/imports/plugins/core/core/server/Reaction";
import { Job } from "/imports/plugins/core/job-collection/lib";

let moment;
async function lazyLoadMoment() {
if (moment) return;
moment = await import("moment").default;
}

/**
* @param {Object} olderThan older than date
* @return {Object} stale carts
* @private
*/
const getStaleCarts = (olderThan) => Cart.find({ updatedAt: { $lte: olderThan } }).fetch();
import moment from "moment";

/**
* @summary Adds an afterCoreInit hook for removing stale carts
Expand Down Expand Up @@ -55,34 +42,16 @@ export function cartCleanupJob() {
pollInterval: 60 * 60 * 1000, // backup polling, see observer below
workTimeout: 180 * 1000
}, (job, callback) => {
Logger.debug("Processing cart/removeFromCart");
Logger.info("Processing cart/removeFromCart");
const settings = Reaction.getShopSettings();
if (settings.cart) {
Promise.await(lazyLoadMoment());
const schedule = (settings.cart.cleanupDurationDays).match(/\d/);// configurable in shop settings
const olderThan = moment().subtract(Number(schedule[0]), "days")._d;
const carts = getStaleCarts(olderThan);
carts.forEach((cart) => {
const account = Accounts.findOne({ _id: cart.accountId });
const removeCart = Cart.remove({ accountId: account._id });
if (!account.emails.length) {
const removeAccount = Accounts.remove({
_id: account._id,
emails: []
});
Hooks.Events.run("afterAccountsRemove", null, account._id);
Meteor.users.remove({ _id: account.userId, emails: [] }); // clears out anonymous user
if (removeCart && removeAccount) {
const success = "Stale anonymous user cart and account successfully cleaned";
Logger.debug(success);
job.done(success, { repeatId: true });
}
} else {
const success = "Stale user cart successfully cleaned";
Logger.debug(success);
job.done(success, { repeatId: true });
}
});
Logger.info("removing carts older than", olderThan);
Cart.remove({ updatedAt: { $lte: olderThan } }, { multi: true });
const success = "Cart cleanup job completed";
Logger.info(success);
job.done(success);
} else {
Logger.debug("No cart cleanup schedule");
}
Expand Down