Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async "connectors/shopify/import/customers"(options) {
check(options, Match.Maybe(Object));
if (!Reaction.hasPermission(connectorsRoles)) {
throw new Meteor.Error(403, "Access Denied");
}
const apiCreds = getApiInfo();
const shopify = new Shopify(apiCreds);
const shopId = Reaction.getShopId();
const limit = 250; // Shopify returns a maximum of 250 results per request
const ids = [];
const opts = Object.assign({}, {
published_status: "published",
limit
}, { ...options });
try {
const customerCount = await shopify.customer.count();
const numPages = Math.ceil(customerCount / limit);
const pages = [...Array(numPages).keys()];
Logger.info(`Shopify Connector is preparing to import ${customerCount} customers`);
for (const page of pages) {
Logger.debug(`Importing page ${page + 1} of ${numPages} - each page has ${limit} products`);
async "connectors/shopify/api/products/count"(options) {
check(options, Match.Maybe(Object));
if (!Reaction.hasPermission(connectorsRoles)) {
throw new Meteor.Error("access-denied", "Access denied");
}
const apiCreds = getApiInfo();
const shopify = new Shopify(apiCreds);
const opts = Object.assign({}, { published_status: "published" }, { ...options }); // eslint-disable-line camelcase
try {
const count = await shopify.product.count(opts);
return count;
} catch (err) {
Logger.error("Something went wrong during Shopify products count");
}
}
};
throw new Meteor.Error("access-denied", "Access denied");
}
// This code is duplicated in `../api/api`
// But is left here intentionally as we are updating the specific shopifyPkg that this returns later in this method
const shopifyPkg = Reaction.getPackageSettingsWithOptions({
shopId: Reaction.getShopId(),
name: "reaction-connectors-shopify"
});
if (!shopifyPkg) {
throw new Meteor.Error("server-error", `No shopify package found for shop ${Reaction.getShopId()}`);
}
const { settings } = shopifyPkg;
const shopify = new Shopify({
apiKey: settings.apiKey,
password: settings.password,
shopName: settings.shopName
});
const host = options.webhooksDomain || Meteor.absoluteUrl();
const webhookAddress = `${host}webhooks/shopify/${options.topic.replace(/\//g, "-")}?shopId=${Reaction.getShopId()}`;
try {
let shopifyId;
// Create webhook on Shopify if it isn't installed yet
const webhooks = await shopify.webhook.list({
address: webhookAddress
});
if (webhooks.length === 0) {
const webhookResponse = await shopify.webhook.create({
export async function exportToShopify(doc) {
const numShopOrders = doc.billing.length; // if we have multiple billing, we have multiple shops
Logger.debug(`Exporting ${numShopOrders} order(s) to Shopify`);
const shopifyOrders = [];
for (let index = 0; index < numShopOrders; index += 1) {
// send a shopify order once for each merchant order
const { shopId } = doc.billing[index];
const apiCreds = getApiInfo(shopId);
const shopify = new Shopify(apiCreds);
const existingCustomerQuery = await isExistingCustomer(doc.billing[index].address, doc.email, shopify); // eslint-disable-line no-await-in-loop
// this should never happen but I want a meaningful error here in case it does
if (existingCustomerQuery.length > 1) {
throw new Meteor.Error("duplicate-customer", "Discovered more than one customer in Shopify. Cannot continue");
}
const existingCustomer = existingCustomerQuery[0];
const shopifyOrder = convertOrderToShopifyOrder(doc, index, shopId, existingCustomer);
Logger.debug("sending shopify order", shopifyOrder, doc._id);
const newShopifyOrder = await shopify.order.create(shopifyOrder); // eslint-disable-line no-await-in-loop
markExported(newShopifyOrder, shopId, doc);
shopifyOrders.push(newShopifyOrder);
}
return shopifyOrders;
}
const getShopifyApi = session => {
const { shopify: { shop: shopUrl, token } } = session;
return new ShopifyApi({
shopName: shopUrl.split('.')[0],
accessToken: token
});
};
async "connectors/shopify/api/credentials/test"() {
if (!Reaction.hasPermission(connectorsRoles)) {
throw new Meteor.Error("access-denied", "Access denied");
}
const apiCreds = getApiInfo();
try {
const shopify = new Shopify(apiCreds);
await shopify.product.count();
} catch (err) {
return false;
}
return true;
}
};
export function loadShopify(options) {
return new Shopify(options.shopName, options.apiKey, options.password);
}