Example #1
0
exports.calculatePrice = function(req: express.Request, res: express.Response) {
    var check = require('validator').check; // Validation
    var pages = req.query.pages,
        destination = req.query.destination,
        preferredCurrency = req.query.preferred_currency;

    if (["AUD", "EUR", "GBP", "USD"].indexOf(preferredCurrency) < 0) {
        res.send(502, {'error': "Your preferred currency is not supported"});
        return;
    }

    // Check the input
    check(pages).notNull().isInt();
    check(destination).notNull();
    check(preferredCurrency).notNull().len(1,6);

    var mailClient = new MailClient();
    mailClient.calculatePrice(pages, destination, function (error : Error, digest?: CalculatePriceDigest) {
        if (error) {
            res.send(502, {'error': error.message});
        } else {
            var finalPrice : number = (digest.priceInEur + 0.20 + BraintreeClient.guessTransactionCost(digest.priceInEur + 0.20)) * 1.27;
            var finalPriceShorted : string = finalPrice.toFixed(2);

            CurrencyConverter.convert(CurrencyConverter.convertStringToCurrencyType("EUR"), CurrencyConverter.convertStringToCurrencyType(preferredCurrency), finalPrice, function (result: number) {
                var preferredPriceShorted : string = result.toFixed(2);
                res.send({'preferredCurrency': preferredCurrency, 'priceInEur': finalPriceShorted, 'priceInPreferredCurrency': preferredPriceShorted, 'printingCity': digest.city, 'printingCountry': digest.country, 'courier': digest.courier});
            });
        }
    });
};
Example #2
0
    public static validate(req: express.Request) {
        var check = require('validator').check,
            sanitize = require('validator').sanitize;

        check(req.body.pdf).notNull();
        check(req.body.recipientName).notNull();
        check(req.body.recipientAddress1).notNull();
        check(req.body.recipientCity).notNull();
        check(req.body.recipientPostalCode).notNull();
        var iso = req.body.recipientCountryIso;
        var name = req.body.recipientName;
        var company = req.body.recipientCompany;
        var address1 = req.body.recipientAddress1;
        var address2 = req.body.recipientAddress2;
        var city = req.body.recipientCity;
        var state = req.body.recipientState;
        var zip = req.body.recipientPostalCode;

        if (name.length > 50) throw  new Error("The FaxRecipient Name may not be longer than 50 characters");
        if (company != null && typeof company !== 'undefined' && company.length > 50) throw  new Error("The FaxRecipient Company may not be longer than 50 characters");
        if (address1.length > 50) throw  new Error("The FaxRecipient Address Line 1 may not be longer than 50 characters");
        if (address2 != null && typeof address2 !== 'undefined' && address2.length > 50) throw  new Error("The FaxRecipient Address Line 2 may not be longer than 50 characters");
        if (city.length > 50) throw  new Error("The FaxRecipient City may not be longer than 50 characters");
        if (state != null && typeof state !== 'undefined' && state.length > 50) throw  new Error("The FaxRecipient State may not be longer than 50 characters");
        if (zip.length > 50) throw  new Error("The FaxRecipient Zip may not be longer than 50 characters");

        // Sanitize
        req.body.recipientName = sanitize(req.body.recipientName).trim();
        req.body.recipientName = sanitize(req.body.recipientName).escape();
        req.body.recipientName = req.body.recipientName.replace("'", '').replace('"', '');

        req.body.recipientCompany = (typeof req.body.recipientCompany === 'undefined' || req.body.recipientCompany == null) ? undefined : sanitize(req.body.recipientCompany).trim();
        req.body.recipientCompany = (typeof req.body.recipientCompany === 'undefined' || req.body.recipientCompany == null) ? undefined : sanitize(req.body.recipientCompany).escape();
        req.body.recipientCompany = (typeof req.body.recipientCompany === 'undefined' || req.body.recipientCompany == null) ? undefined : req.body.recipientCompany.replace("'", '').replace('"', '');

        req.body.recipientAddress1 = sanitize(req.body.recipientAddress1).trim();
        req.body.recipientAddress1 = sanitize(req.body.recipientAddress1).escape();
        req.body.recipientAddress1 = req.body.recipientAddress1.replace("'", '').replace('"', '');

        req.body.recipientAddress2 = (typeof req.body.recipientAddress2 === 'undefined' || req.body.recipientAddress2 == null) ? undefined : sanitize(req.body.recipientAddress2).trim();
        req.body.recipientAddress2 = (typeof req.body.recipientAddress2 === 'undefined' || req.body.recipientAddress2 == null) ? undefined : sanitize(req.body.recipientAddress2).escape();
        req.body.recipientAddress2 = (typeof req.body.recipientAddress2 === 'undefined' || req.body.recipientAddress2 == null) ? undefined : req.body.recipientAddress2.replace("'", '').replace('"', '');

        req.body.recipientPostalCode = sanitize(req.body.recipientPostalCode).trim();
        req.body.recipientPostalCode = sanitize(req.body.recipientPostalCode).escape();
        req.body.recipientPostalCode = req.body.recipientPostalCode.replace("'", '').replace('"', '');

        req.body.recipientCity = sanitize(req.body.recipientCity).trim();
        req.body.recipientCity = sanitize(req.body.recipientCity).escape();
        req.body.recipientCity = req.body.recipientCity.replace("'", '').replace('"', '');

        req.body.recipientState = (typeof req.body.recipientState === 'undefined' || req.body.recipientState == null) ? undefined : req.body.recipientState;

        req.body.recipientCountryIso = (typeof req.body.recipientCountryIso === 'undefined') ? undefined : sanitize(req.body.recipientCountryIso).escape();
    }
Example #3
0
exports.pushNotification = function(req :express.Request, res :express.Response) {
    var check = require('validator').check; // Validation
    check(req.query.device).notNull();
    check(req.params.id).notNull();
    check(req.query.uri).notNull();

    MongoManager.getDb(function (db : mongo.Db) {
        db.collection('letter', function (err:Error, collection:mongo.Collection) {
            collection.findOne({'_id': new mongo.ObjectID(req.params.id)}, function (err:Error, letter:Letter) {
                if (err) {
                    res.json(404, "The letter could not be found");
                    return;
                }

                var client;
                switch (req.query.device) {
                    case "OSX1010":
                        client = new Client(ClientType.ClientType.MacOS1010, req.query.uri);
                        letter.devices.push(client);
                        break;
                    default :
                        client = new Client(ClientType.ClientType.Windows81, req.query.uri);
                        letter.devices.push(client);
                        break;
                }

                letter.updatedAt = new Date(); // Update the date
                collection.update({'_id': letter._id}, letter, {safe: true}, function (err:Error, result:number) {
                    if (err) {
                        res.json(500, {'error': 'An error has occurred'});
                    } else {
                        res.json("Device added");
                    }
                });
            });
        });
    });
};
Example #4
0
exports.geocode = function(req :express.Request, res :express.Response) {
    try {
        var check = require('validator').check; // Validation
        check(req.body.address).notNull();
    } catch (e) {
        res.send(400, {'error': e.message});
        return;
    }

    var address = req.body.address;
    Geocoder.geocode(address, function(error: Error, location?: Location) {
        if (typeof error != "undefined") {
            res.send(500, {'error': error.message});
            return;
        }

        res.send(location);
    });
};
Example #5
0
    public static validate(req: express.Request) {
        var check = require('validator').check,
            sanitize = require('validator').sanitize;

        check(req.params.id).notNull();
        if (req.params.id.length !== 24) throw new Error("The ID Provided is not Correct");
        check(req.body).notNull();
        check(req.body.emailAddress).notNull().isEmail();
        check(req.body.address).notNull();
        check(req.body.creditCard).notNull();
        check(req.body.creditCard.number).notNull();
        req.body.creditCard.type = undefined; // we do not need this input
        check(req.body.creditCard.cvv).notNull();
        check(req.body.creditCard.date).notNull();
        check(req.body.address.name).notNull();
        check(req.body.address.line1).notNull();
        check(req.body.address.postalCode).notNull();
        check(req.body.address.city).notNull();
        check(req.body.address.country).notNull();
    }