{"size": 2651, "ext": "js", "max_stars_repo_path": "src/gateway/skus-connector/apiproxy/resources/node/libs/skus.js", "max_stars_repo_name": "apigee/opencommerce", "max_stars_repo_stars_event_min_datetime": "2016-08-02T11:48:08.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-14T23:04:55.000Z", "max_issues_repo_path": "src/gateway/skus-connector/apiproxy/resources/node/libs/skus.js", "max_issues_repo_name": "apigee/opencommerce", "max_issues_repo_issues_event_min_datetime": "2017-08-15T13:58:04.000Z", "max_issues_repo_issues_event_max_datetime": "2017-09-28T06:39:02.000Z", "max_forks_repo_path": "src/gateway/skus-connector/apiproxy/resources/node/libs/skus.js", "max_forks_repo_name": "apigee/opencommerce", "max_forks_repo_forks_event_min_datetime": "2016-11-15T06:22:23.000Z", "max_forks_repo_forks_event_max_datetime": "2020-12-01T16:34:58.000Z"}
{"max_stars_count": 22, "max_issues_count": 4, "max_forks_count": 11, "avg_line_length": 29.1318681319, "max_line_length": 134, "alphanum_fraction": 0.6439079593}
/**
 * Created by rrai on 23/06/16.
 */
var request = require('request');
var pkginfo = require('../package');

skus = {};
module.exports = skus;

var basePath = pkginfo.baasBasePath;
function assignResponse(body)
{
    var json=JSON.parse(body);
    var formattedJson={};


    formattedJson.id=json.entities[0].name;
    formattedJson.name=json.entities[0].sku_name;
    formattedJson.product_id=json.entities[0].product_id;
    formattedJson.sku_id=json.entities[0].name;
    formattedJson.sku_description=json.entities[0].sku_description;
    formattedJson.inventory_type=json.entities[0].inventory_type;
    formattedJson.available_quantity=json.entities[0].available_quantity;
    formattedJson.barcode=json.entities[0].barcode || "";
    formattedJson.price=json.entities[0].price;
    formattedJson.currency=json.entities[0].currency || "";
    formattedJson.discount=json.entities[0].discount;
    formattedJson.total_price=json.entities[0].total_price || "";
    formattedJson.attributes=json.entities[0].attributes || "";
    formattedJson.image=json.entities[0].images;
    formattedJson.created_date=json.entities[0].created_date;
    formattedJson.expiry_date=json.entities[0].expiry_date;
    formattedJson.product_url="/products/" + json.entities[0].product_id;

    return formattedJson;

}


skus.get = function(params, callback)
{
    //form the url
    var url=pkginfo.baasBasePath + "/skus/"+params.skuid;
    //create request object
    request({
        method:'GET',
        uri:url,
        //headers:{"Authorization":"Bearer YWMt6hC8gDkpEeahpY8g1ZSFHgAAAVWgxJgOagbavdxfXXw80gKiAyVToqfIWkc"}//set authorization header
    },function (error, response, body)
    {
        errobj={};
        //if no error and the response is a success, return the json
        if (!error && response.statusCode == 200)
        {
            var sku = assignResponse(body);

            callback(null,sku);
        }

        else if(response.statusCode==400)
        {
            errobj.code=response.statusCode;
            errobj.msg="Bad Request";
            callback(errobj,null);
        }
        else if(response.statusCode==401)
        {
            errobj.code=response.statusCode;
            errobj.msg="UnAuthorised Access";
            callback(errobj,null);
        }
        else if(response.statusCode==404)
        {
            errobj.code=response.statusCode;
            errobj.msg="Skus Not Found";
            callback(errobj,null);
        }
        else
        {
            errobj.code=response.statusCode;
            errobj.msg="Something went wrong";
            callback(errobj,null);

        }
    });


};


