Skip to content
This repository has been archived by the owner on May 28, 2023. It is now read-only.

Commit

Permalink
Merge pull request #309 from patzick/bugfix/port-functionality-from-#302
Browse files Browse the repository at this point in the history
Bugfix/port functionality from #302
  • Loading branch information
patzick authored Jul 22, 2019
2 parents ff82335 + 1a35f9b commit 9f44e1b
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 926 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New payload for magento1 stock check endpoint (#261)
- `yarn dev:inspect` command and extract nodemon config to nodemon.json @Tjitse-E, @cewald (#272, #279)
- Include Magento Currency Code in mage2vs import and productsdelta if available @rain2o (#281)
- Better handling of HTTP error codes provided by API client - pkarw (#3151)

## [1.9.5] - 2019.06.17
- Dependencies security update against js-yaml flaws.
Expand Down
26 changes: 13 additions & 13 deletions src/api/cart.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apiStatus } from '../lib/util';
import { apiStatus, apiError } from '../lib/util';
import { Router } from 'express';
import PlatformFactory from '../platform/factory';

Expand All @@ -21,7 +21,7 @@ export default ({ config, db }) => {
cartProxy.create(req.query.token).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -41,7 +41,7 @@ export default ({ config, db }) => {
cartProxy.update(req.query.token, req.query.cartId ? req.query.cartId : null, req.body.cartItem).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -59,7 +59,7 @@ export default ({ config, db }) => {
cartProxy.applyCoupon(req.query.token, req.query.cartId ? req.query.cartId : null, req.query.coupon).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -73,7 +73,7 @@ export default ({ config, db }) => {
cartProxy.deleteCoupon(req.query.token, req.query.cartId ? req.query.cartId : null).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -87,7 +87,7 @@ export default ({ config, db }) => {
cartProxy.getCoupon(req.query.token, req.query.cartId ? req.query.cartId : null).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -107,7 +107,7 @@ export default ({ config, db }) => {
cartProxy.delete(req.query.token, req.query.cartId ? req.query.cartId : null, req.body.cartItem).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -122,7 +122,7 @@ export default ({ config, db }) => {
cartProxy.pull(req.query.token, req.query.cartId ? req.query.cartId : null, req.body).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -137,7 +137,7 @@ export default ({ config, db }) => {
cartProxy.totals(req.query.token, req.query.cartId ? req.query.cartId : null, req.body).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -156,7 +156,7 @@ export default ({ config, db }) => {
cartProxy.getShippingMethods(req.query.token, req.query.cartId ? req.query.cartId : null, req.body.address).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -171,7 +171,7 @@ export default ({ config, db }) => {
cartProxy.getPaymentMethods(req.query.token, req.query.cartId ? req.query.cartId : null).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -190,7 +190,7 @@ export default ({ config, db }) => {
cartProxy.setShippingInformation(req.query.token, req.query.cartId ? req.query.cartId : null, req.body).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -209,7 +209,7 @@ export default ({ config, db }) => {
cartProxy.collectTotals(req.query.token, req.query.cartId ? req.query.cartId : null, req.body.methods).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand Down
7 changes: 3 additions & 4 deletions src/api/order.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import resource from 'resource-router-middleware';
import { apiStatus } from '../lib/util';
import { merge } from 'lodash';
import { apiStatus, apiError } from '../lib/util';import { merge } from 'lodash';
import PlatformFactory from '../platform/factory';

const Ajv = require('ajv'); // json validator
Expand Down Expand Up @@ -68,7 +67,7 @@ export default ({ config, db }) => resource({
const job = queue.create('order', incomingOrder).save( function(err){
if(err) {
console.error(err)
apiStatus(res, err, 500);
apiError(res, err);
} else {
apiStatus(res, job.id, 200);
}
Expand All @@ -81,7 +80,7 @@ export default ({ config, db }) => resource({
orderProxy.create(req.body).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err.toString(), 500);
apiError(res, err);
})
}
},
Expand Down
6 changes: 3 additions & 3 deletions src/api/product.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apiStatus, sgnSrc } from '../lib/util';
import { apiStatus, sgnSrc, apiError } from '../lib/util';
import { Router } from 'express';
import PlatformFactory from '../platform/factory';

Expand Down Expand Up @@ -28,7 +28,7 @@ export default ({ config, db }) => {
productProxy.list(req.query.skus.split(',')).then((result) => {
apiStatus(res, result, 200);
}).catch(err=> {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand Down Expand Up @@ -56,7 +56,7 @@ export default ({ config, db }) => {
})
apiStatus(res, result, 200);
}).catch(err=> {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand Down
5 changes: 2 additions & 3 deletions src/api/review.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { apiStatus } from '../lib/util';
import { Router } from 'express';
import { apiStatus, apiError } from '../lib/util';import { Router } from 'express';
import PlatformFactory from '../platform/factory'

const Ajv = require('ajv'); // json validator
Expand Down Expand Up @@ -28,7 +27,7 @@ export default ({config, db}) => {
reviewProxy.create(req.body.review).then((result) => {
apiStatus(res, result, 200);
}).catch(err=> {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand Down
5 changes: 2 additions & 3 deletions src/api/stock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { apiStatus } from '../lib/util';
import { Router } from 'express';
import { apiStatus, apiError } from '../lib/util';import { Router } from 'express';
import PlatformFactory from '../platform/factory'

export default ({ config, db }) => {
Expand Down Expand Up @@ -78,7 +77,7 @@ export default ({ config, db }) => {
Promise.all(promisesList).then((results) => {
apiStatus(res, results, 200);
}).catch(err=> {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand Down
5 changes: 2 additions & 3 deletions src/api/sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { apiStatus } from '../lib/util';
import { Router } from 'express';
import { apiStatus, apiError } from '../lib/util';import { Router } from 'express';

export default ({ config, db }) => {

Expand All @@ -24,7 +23,7 @@ export default ({ config, db }) => {
if (orderMetaData) {
orderMetaData.order = null // for security reasons we're just clearing out the real order data as it's set by `order_2_magento2.js`
}
apiStatus(res, err ? err : orderMetaData, err ? 500 :200);
apiStatus(res, err ? err : orderMetaData, err ? 500 : 200);
})
})

Expand Down
25 changes: 12 additions & 13 deletions src/api/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apiStatus, encryptToken, decryptToken } from '../lib/util';
import { apiStatus, encryptToken, decryptToken, apiError } from '../lib/util';
import { Router } from 'express';
import PlatformFactory from '../platform/factory';
import jwt from 'jwt-simple';
Expand Down Expand Up @@ -46,8 +46,7 @@ export default ({config, db}) => {
const validate = ajv.compile(merge(userRegisterSchema, userRegisterSchemaExtension))

if (!validate(req.body)) { // schema validation of upcoming order
console.dir(validate.errors);
apiStatus(res, validate.errors, 200);
apiStatus(res, validate.errors, 400);
return;
}

Expand All @@ -56,7 +55,7 @@ export default ({config, db}) => {
userProxy.register(req.body).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
})

Expand All @@ -74,13 +73,13 @@ export default ({config, db}) => {
userProxy.me(result).then((resultMe) => {
apiStatus(res, result, 200, {refreshToken: encryptToken(jwt.encode(req.body, config.authHashSecret ? config.authHashSecret : config.objHashSecret), config.authHashSecret ? config.authHashSecret : config.objHashSecret)});
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
} else {
apiStatus(res, result, 200, {refreshToken: encryptToken(jwt.encode(req.body, config.authHashSecret ? config.authHashSecret : config.objHashSecret), config.authHashSecret ? config.authHashSecret : config.objHashSecret)});
apiStatus(res, result, 200, {refreshToken: encryptToken(jwt.encode(req.body, config.authHashSecret ? config.authHashSecret : config.objHashSecret), config.authHashSecret ? config.authHashSecret : config.objHashSecret)});
}
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
});

Expand All @@ -103,10 +102,10 @@ export default ({config, db}) => {
userProxy.login(decodedToken).then((result) => {
apiStatus(res, result, 200, {refreshToken: encryptToken(jwt.encode(decodedToken, config.authHashSecret ? config.authHashSecret : config.objHashSecret), config.authHashSecret ? config.authHashSecret : config.objHashSecret)});
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
} catch (err) {
return apiStatus(res, err.message, 500);
apiError(res, err);
}
});

Expand All @@ -123,7 +122,7 @@ export default ({config, db}) => {
userProxy.resetPassword({ email: req.body.email, template: "email_reset", websiteId: 1 }).then((result) => {
apiStatus(res, result, 200);
}).catch(err=> {
apiStatus(res, err, 500);
apiError(res, err);
})
});

Expand All @@ -140,7 +139,7 @@ export default ({config, db}) => {
userProxy.resetPassword({ email: req.body.email, template: "email_reset", websiteId: 1 }).then((result) => {
apiStatus(res, result, 200);
}).catch(err=> {
apiStatus(res, err, 500);
apiError(res, err);
})
});

Expand All @@ -153,7 +152,7 @@ export default ({config, db}) => {
addUserGroupToken(config, result)
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
});

Expand All @@ -165,7 +164,7 @@ export default ({config, db}) => {
userProxy.orderHistory(req.query.token).then((result) => {
apiStatus(res, result, 200);
}).catch(err => {
apiStatus(res, err, 500);
apiError(res, err);
})
});

Expand Down
10 changes: 10 additions & 0 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ export function apiStatus(res, result = 'OK', code = 200, meta = null) {
return result;
}


/** Creates a api error status Express Response object.
* @param {express.Response} res Express HTTP Response
* @param {number} [code=200] Status code to send on success
* @param {json} [result='OK'] Text message or result information object
*/
export function apiError(res, errorObj, code = 500) {
return apiStatus(res, errorObj.errorMessage ? errorObj.errorMessage : errorObj, errorObj.code ? errorObj.code : 500)
}

export function encryptToken(textToken, secret) {
const cipher = crypto.createCipher(algorithm, secret)
let crypted = cipher.update(textToken, 'utf8', 'hex')
Expand Down
Loading

0 comments on commit 9f44e1b

Please sign in to comment.