jamztang of MagicSketch
12/23/2016 - 3:51 AM

Activate License via Hook and Airtable

Activate License via Hook and Airtable

//https://hook.io/jamztang/activate?key=5T7EgN6DVhYDyoYo&ip=127.0.0.2&region=HongKong&os=Mac&deviceId=fheewieghIHO
// activate hook
var Airtable = require('airtable');
var request = require('request');
var async = require('async');
var _ = require('lodash');

function addActivation(base, licenseRecordId, deviceId, osVersion, ip, region, callbackResult){
    async.waterfall([
        // create device record
        function(callback){
            base('Device').create({
                'OS': osVersion,
                'DeviceID': deviceId,
                'IP Address': ip,
                'Region': region,
            }, function(err, record) {
                if (err) { console.log(err); callback(err); return; }
                callback(null, record.id);
            });
        },

        // create activation record
        function(deviceRecordId, callback){
            base('Activation').create({
                'Device': [deviceRecordId],
                'License': [licenseRecordId],
            }, function(err, record) {
                if (err) { console.log(err); callback(err); return; }
                callback(null);
            });
        },

    ], function(error, results){
        if(error){
            callbackResult(false, error.message);
        }else{
            callbackResult(true, 'Finished');
        }
    });

}

module['exports'] = function echoHttp (hook) {
    var airtableKey = hook.env["airtableKey"];
    var airtableBase = hook.env["airtableLicenseBase"];
    var p = hook.params;
    var license = '';

    base = new Airtable({ apiKey: airtableKey }).base(airtableBase);

    if(p['key'] == undefined){
        hook.res.end(JSON.stringify({'success': false, 'message': 'No key'}));
    }else{
        license = p['key'];
        var now = new Date();
        var allLicenses = [];
        var deviceId = p['deviceId'];
        var osVersion = p['os'] || 'Unknown';
        var ip = p['ip'] || 'Unknown';
        var region = p['region'] || 'Unknown';

        base('License').select({
            view: "Main View",
        }).eachPage(function page(records, fetchNextPage) {
            allLicenses = allLicenses.concat(records);

            // To fetch the next page of records, call `fetchNextPage`.
            // If there are more records, `page` will get called again.
            // If there are no more records, `done` will get called.
            fetchNextPage();

        }, function done(error) {
            if (error) {
                console.log(error);
                hook.res.end(JSON.stringify({'success': false, 'message': error}));
                return;
            }

            var found = false;

            _.forEach(allLicenses, function(record) {
                var expiresAt = new Date(record.get('Expire By'));
                
                if(license == record.get('LicenseID') && expiresAt >= now){
                    // ok
                    found = true;
                    addActivation(base, record.id, deviceId, osVersion, ip, region, function(success, message){
                        hook.res.end(JSON.stringify({'success': success, 'message': success?record:message}));
                    });

                    return false;
                }
            });

            if(!found){
                hook.res.end(JSON.stringify({'success': false, 'message': 'Invalid key.'}));
            }
        });

    }
};