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

Special Parser Restock Mode #433

Merged
merged 11 commits into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from 10 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
8 changes: 3 additions & 5 deletions packages/frontend/lib/_electron/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,11 @@ class App {
if (!nebulaEnv.isDevelopment()) {
// attach an interval to check for any logging applications
setInterval(async () => {
const isRunning = await this._securityManager.isRunning();
console.log(`[DEBUG]: Logger running?: %j`, isRunning);
const isRunning = await this._securityManager.isHTTPLoggerRunning();
if (isRunning) {
await this.onBeforeQuit();
Electron.app.quit();
this.onWindowAllClosed();
}
}, 1000); // TODO: is 1 second too short/long?
}, 1500);
}

// create the window
Expand Down
6 changes: 5 additions & 1 deletion packages/frontend/lib/_electron/rpc.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
const {
app: { getVersion },
} = require('electron');
const DiscordRPC = require('discord-rpc');

class RPC {
constructor(context) {
this._context = context;
this.clientId = '571372290994864146';
this.startTimestamp = new Date();
this.version = getVersion();

try {
DiscordRPC.register(this.clientId);
Expand All @@ -21,7 +25,7 @@ class RPC {
}

this.rpc.setActivity({
state: `v1.0.0`,
state: `v${this.version}`,
startTimestamp: this.startTimestamp,
largeImageKey: 'logo',
largeImageText: 'Nebula Orion',
Expand Down
9 changes: 3 additions & 6 deletions packages/frontend/lib/_electron/securityManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
const psList = require('ps-list');

class SecurityManager {
async isRunning() {
return (
JSON.stringify(await psList())
.toUpperCase()
.indexOf('CHARLES') > -1
);
async isHTTPLoggerRunning() {
const processList = JSON.stringify(await psList());
return processList.test(/charles|proxyman|fiddler|httpfox/i);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nebula/orion",
"version": "1.0.0",
"version": "1.0.1",
"private": true,
"main": "./dist/_electron/electron.js",
"homepage": "./",
Expand Down
35 changes: 23 additions & 12 deletions packages/task-runner/src/shopify/classes/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
waitForDelay,
} = require('./utils');
const { isSpecialSite } = require('./utils/siteOptions');
const { States, Types } = require('./utils/constants').TaskRunner;
const { States, Types, CheckoutTypes } = require('./utils/constants').TaskRunner;

class Checkout {
get context() {
Expand All @@ -21,6 +21,7 @@ class Checkout {
this._context = context;
this._logger = this._context.logger;
this._request = this._context.request;
this._checkoutType = this._context.checkoutType;

this.shippingMethods = [];
const preFetchedShippingRates = this._context.task.profile.rates.find(
Expand All @@ -32,9 +33,10 @@ class Checkout {
preFetchedShippingRates &&
preFetchedShippingRates.selectedRate
) {
const { name, rate } = preFetchedShippingRates.selectedRate;
const { name, price, rate } = preFetchedShippingRates.selectedRate;
this.chosenShippingMethod = {
name,
price,
id: rate,
};
} else {
Expand Down Expand Up @@ -337,8 +339,6 @@ class Checkout {
return { message: 'Creating checkout', nextState: States.CreateCheckout };
}

// TODO - test this more.
// On stores with password page it makes sense, but idk if it will mess with anything else..
if (statusCode === 401) {
await waitForDelay(monitorDelay);
return { message: 'Password page', nextState: States.CreateCheckout };
Expand All @@ -347,6 +347,7 @@ class Checkout {
if (!location) {
return { message: `(${statusCode}) Failed: Creating checkout`, nextState: States.Errored };
}

const [redirectUrl, qs] = location.split('?');
this._logger.silly('CHECKOUT: Create checkout redirect url: %s', redirectUrl);
if (!redirectUrl) {
Expand All @@ -369,10 +370,6 @@ class Checkout {
return { message: 'Account required', nextState: States.Errored };
}

if (redirectUrl.indexOf('stock_problems') > -1) {
return { message: 'Running for restocks', nextState: States.Restocking };
}

if (redirectUrl.indexOf('password') > -1) {
await waitForDelay(monitorDelay);
return { message: 'Password page', nextState: States.CreateCheckout };
Expand Down Expand Up @@ -563,6 +560,10 @@ class Checkout {
if (redirectUrl.indexOf('password') > -1) {
return { message: 'Password page', nextState: States.CreateCheckout };
}

if (redirectUrl.indexOf('throttle') > -1) {
return { message: 'Waiting in queue', nextState: States.PollQueue };
}
}

// start the monitor timer again...
Expand Down Expand Up @@ -593,6 +594,7 @@ class Checkout {
const {
task: {
site: { url, apiKey, localCheckout = false },
size,
},
timers: { checkout },
proxy,
Expand Down Expand Up @@ -656,7 +658,12 @@ class Checkout {
}

if (redirectUrl.indexOf('stock_problems') > -1) {
return { message: 'Running for restocks', nextState: States.Restocking };
if (this._checkoutType === CheckoutTypes.fe) {
const nextState = size.includes('Random') ? States.Monitor : States.GetCheckout;
return { message: 'Running for restocks', nextState };
}
const nextState = size.includes('Random') ? States.Restocking : States.PostPayment;
return { message: 'Running for restocks', nextState };
}
}

Expand Down Expand Up @@ -703,7 +710,7 @@ class Checkout {
const {
task: {
site: { url, apiKey, localCheckout = false },
monitorDelay,
size,
username,
password,
},
Expand Down Expand Up @@ -765,8 +772,12 @@ class Checkout {

// out of stock
if (redirectUrl.indexOf('stock_problems') > -1) {
await waitForDelay(monitorDelay);
return { message: 'Running for restocks', nextState: States.Restocking };
if (this._checkoutType === CheckoutTypes.fe) {
const nextState = size.includes('Random') ? States.Monitor : States.GetCheckout;
return { message: 'Running for restocks', nextState };
}
const nextState = size.includes('Random') ? States.Restocking : States.PostPayment;
return { message: 'Running for restocks', nextState };
}

// login needed
Expand Down
25 changes: 15 additions & 10 deletions packages/task-runner/src/shopify/classes/checkouts/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class APICheckout extends Checkout {
const {
task: {
site: { url, apiKey },
size,
product: { variants },
monitorDelay,
username,
Expand Down Expand Up @@ -265,17 +266,17 @@ class APICheckout extends Checkout {
}
}

if (errors && errors.line_items[0]) {
const error = errors.line_items[0];
if (errors && errors.line_items) {
const [error] = errors.line_items;
this._logger.silly('Error adding to cart: %j', error);
if (error.quantity) {
if (error && error.quantity) {
if (monitor.getRunTime() > CheckoutRefresh) {
return { message: 'Pinging checkout', nextState: States.PingCheckout };
}
await waitForDelay(monitorDelay);
return { message: 'Running for restocks', nextState: States.Restocking };
const nextState = size.includes('Random') ? States.Monitor : States.AddToCart;
return { message: 'Running for restocks', nextState };
}
if (error.variant_id && error.variant_id[0]) {
if (error && error.variant_id && error.variant_id[0]) {
if (type === Types.ShippingRates) {
return { message: 'Invalid variant', nextState: States.Errored };
}
Expand All @@ -288,7 +289,7 @@ class APICheckout extends Checkout {
return { message: `(${statusCode}) Failed: Add to cart`, nextState: States.Errored };
}

if (checkout && checkout.line_items.length > 0) {
if (checkout && checkout.line_items && checkout.line_items.length) {
const { total_price: totalPrice } = checkout;

this._context.task.product.name = checkout.line_items[0].title;
Expand All @@ -300,11 +301,12 @@ class APICheckout extends Checkout {
checkoutTimer.start();

this.prices.item = parseFloat(totalPrice).toFixed(2);
this.prices.total = (
parseFloat(this.prices.item) + parseFloat(this.prices.shipping)
).toFixed(2);

if (this.chosenShippingMethod.id) {
this._logger.silly('API CHECKOUT: Shipping total: %s', this.prices.shipping);
this.prices.total = (
parseFloat(this.prices.item) + parseFloat(this.chosenShippingMethod.price)
).toFixed(2);
return { message: `Posting payment`, nextState: States.PostPayment };
}
return { message: 'Fetching shipping rates', nextState: States.ShippingRates };
Expand Down Expand Up @@ -409,6 +411,9 @@ class APICheckout extends Checkout {

// set shipping price for cart
this.prices.shipping = parseFloat(cheapest.price).toFixed(2);
this.prices.total = (
parseFloat(this.prices.item) + parseFloat(this.prices.shipping)
).toFixed(2);
this._logger.silly('API CHECKOUT: Shipping total: %s', this.prices.shipping);
return { message: `Posting payment`, nextState: States.PostPayment };
}
Expand Down
33 changes: 26 additions & 7 deletions packages/task-runner/src/shopify/classes/checkouts/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const Checkout = require('../checkout');
* 7. POST CHECKOUT
*/
class FrontendCheckout extends Checkout {
constructor(context) {
super(context);
this._hasPatched = false;
}

async getPaymentToken() {
const {
task: {
Expand Down Expand Up @@ -145,7 +150,7 @@ class FrontendCheckout extends Checkout {

if (redirectUrl) {
if (redirectUrl.indexOf('stock_problems') > -1) {
return { message: 'Running for restocks', nextState: States.Restocking };
return { message: 'Running for restocks', nextState: States.AddToCart };
}

if (redirectUrl.indexOf('account') > -1) {
Expand Down Expand Up @@ -173,9 +178,12 @@ class FrontendCheckout extends Checkout {
}

if (body && body.status === 404) {
return { message: 'Running for restocks', nextState: States.Restocking };
return { message: 'Running for restocks', nextState: States.Monitor };
}

if (this.chosenShippingMethod.id && this._hasPatched) {
return { message: 'Posting payment', nextState: States.PostPayment };
}
return { message: 'Creating checkout', nextState: States.CreateCheckout };
} catch (err) {
this._logger.error(
Expand Down Expand Up @@ -211,8 +219,10 @@ class FrontendCheckout extends Checkout {
const {
task: {
site: { url, apiKey },
monitorDelay,
username,
password,
size,
},
proxy,
} = this._context;
Expand Down Expand Up @@ -262,7 +272,9 @@ class FrontendCheckout extends Checkout {
}

if (redirectUrl.indexOf('stock_problems') > -1) {
return { message: 'Running for restocks', nextState: States.Restocking };
const nextState = size.includes('Random') ? States.Monitor : States.GetCheckout;
await waitForDelay(monitorDelay);
return { message: 'Running for restocks', nextState };
}

if (redirectUrl.indexOf('password') > -1) {
Expand Down Expand Up @@ -355,6 +367,10 @@ class FrontendCheckout extends Checkout {
this._logger.silly('FRONTEND CHECKOUT: Patch checkout redirect url: %s', redirectUrl);
if (!redirectUrl) {
if (statusCode >= 200 && statusCode < 310) {
this._hasPatched = true;
if (this.chosenShippingMethod.id) {
return { message: 'Posting payment', nextState: States.PostPayment };
}
return { message: 'Fetching shipping rates', nextState: States.ShippingRates };
}
return { message: 'Failed: Submitting information', nextState: States.Errored };
Expand All @@ -373,6 +389,11 @@ class FrontendCheckout extends Checkout {
return { message: 'Password page', nextState: States.CreateCheckout };
}

if (redirectUrl.indexOf('stock_problems') > -1) {
// we can maybe find shipping rates meanwhile (if not found already) here
return { message: 'Running for restocks', nextState: States.GetCheckout };
}

if (redirectUrl.indexOf('throttle') > -1) {
return { message: 'Waiting in queue', nextState: States.PollQueue };
}
Expand Down Expand Up @@ -453,6 +474,7 @@ class FrontendCheckout extends Checkout {

if (errors) {
this._logger.silly('FRONTEND CHECKOUT: Error getting shipping rates: %j', errors);
await waitForDelay(2000);
return { message: 'Polling for shipping rates', nextState: States.ShippingRates };
}

Expand All @@ -471,10 +493,7 @@ class FrontendCheckout extends Checkout {
this.prices.shipping = price;
this._logger.silly('FRONTEND CHECKOUT: Shipping cost: %s', this.prices.shipping);

return {
message: 'Posting payment',
nextState: States.PostPayment,
};
return { message: 'Posting payment', nextState: States.PostPayment };
}
this._logger.silly('No shipping rates available, polling %d ms', monitorDelay);
await waitForDelay(monitorDelay);
Expand Down
Loading