Files
cloudron-box/src/routes/subscription.js
T

43 lines
1.9 KiB
JavaScript
Raw Normal View History

2019-05-03 16:27:47 -07:00
'use strict';
exports = module.exports = {
2019-05-03 20:17:40 -07:00
subscribeCloudron: subscribeCloudron,
getSubscription: getSubscription
2019-05-03 16:27:47 -07:00
};
var appstore = require('../appstore.js'),
AppstoreError = appstore.AppstoreError,
assert = require('assert'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess;
function subscribeCloudron(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.email !== 'string' || !req.body.email) return next(new HttpError(400, 'email must be string'));
if (typeof req.body.password !== 'string' || !req.body.password) return next(new HttpError(400, 'password must be string'));
if ('totpToken' in req.body && typeof req.body.totpToken !== 'string') return next(new HttpError(400, 'totpToken must be string'));
if (typeof req.body.signup !== 'boolean') return next(new HttpError(400, 'signup must be a boolean'));
appstore.subscribeCloudron(req.body, function (error) {
2019-05-04 21:42:33 -07:00
if (error && error.reason === AppstoreError.ALREADY_EXISTS) return next(new HttpError(409, error.message));
if (error && error.reason === AppstoreError.ACCESS_DENIED) return next(new HttpError(412, error.message));
2019-05-03 16:27:47 -07:00
if (error && error.reason === AppstoreError.EXTERNAL_ERROR) return next(new HttpError(424, error.message));
if (error) return next(new HttpError(500, error));
2019-05-05 10:52:39 -07:00
next(new HttpSuccess(201, {}));
2019-05-03 16:27:47 -07:00
});
}
2019-05-03 20:17:40 -07:00
function getSubscription(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
appstore.getSubscription(function (error, result) {
2019-05-05 10:31:42 -07:00
if (error && error.reason === AppstoreError.INVALID_TOKEN) return next(new HttpError(402, error.message));
2019-05-03 20:17:40 -07:00
if (error && error.reason === AppstoreError.EXTERNAL_ERROR) return next(new HttpError(424, error.message));
if (error) return next(new HttpError(500, error));
next(new HttpSuccess(200, result)); // { email, subscription }
});
}