Extend Profile model in SCA

We extend the profile model to sign out and sign in a user when they’re first registering to the website. This is done in order to change the subsidiary to a new one

define('ProfileModel.Ext', [
    'SC.Model', 'Application', 'Models.Init', 'Profile.Model', 'Utils'
], function(
    SCModel, Application, ModelsInit, Profile, Utils
) {
    'use strict';
    _.extend(Profile, {
        get: function() {
            var profile = {};

            //You can get the profile information only if you are logged in.
            if (ModelsInit.session.isLoggedIn2() && this.isSecure) {

                //Define the fields to be returned
                this.fields = this.fields || ['isperson', 'email', 'internalid', 'name', 'overduebalance', 'phoneinfo', 'companyname', 'firstname', 'lastname', 'middlename', 'emailsubscribe', 'campaignsubscriptions', 'paymentterms', 'creditlimit', 'balance', 'creditholdoverride'];

                profile = ModelsInit.customer.getFieldValues(this.fields);

                //Make some attributes more friendly to the response
                profile.phone = profile.phoneinfo.phone;
                profile.altphone = profile.phoneinfo.altphone;
                profile.fax = profile.phoneinfo.fax;
                profile.type = profile.isperson ? 'INDIVIDUAL' : 'COMPANY';

                profile.creditlimit = parseFloat(profile.creditlimit || 0);
                profile.creditlimit_formatted = Utils.formatCurrency(profile.creditlimit);

                profile.balance = parseFloat(profile.balance || 0);
                profile.balance_formatted = Utils.formatCurrency(profile.balance);

                profile.balance_available = profile.creditlimit - profile.balance;
                profile.balance_available_formatted = Utils.formatCurrency(profile.balance_available);
            } else {
                profile = ModelsInit.customer.getFieldValues(['addressbook', 'balance', 'campaignsubscriptions', 'companyname', 'creditcards', 'creditholdoverride', 'creditlimit', 'email', 'emailsubscribe', 'firstname', 'internalid', 'isperson', 'lastname', 'middlename', 'name', 'paymentterms', 'phoneinfo', 'vatregistration']);

                profile.isLoggedIn = ModelsInit.session.isLoggedIn2() ? 'T' : 'F';
                profile.isRecognized = ModelsInit.session.isRecognized() ? 'T' : 'F';
                profile.isGuest = ModelsInit.customer.isGuest() ? 'T' : 'F';
                profile.priceLevel = ModelsInit.session.getShopperPriceLevel().internalid ? ModelsInit.session.getShopperPriceLevel().internalid : ModelsInit.session.getSiteSettings('defaultpricelevel');

                profile.internalid = nlapiGetUser() + '';
            }


            var customFields = nlapiGetWebContainer()
                .getShoppingSession()
                .getCustomer()
                .getCustomFieldValues();
            var string_customFields = JSON.stringify(customFields, null, 2); //To create an array of object
            var customfieldsJSON = JSON.parse(string_customFields);
            var state = customfieldsJSON[2].value;

            profile.subsidiary = ModelsInit.session.getShopperSubsidiary();
            if (state == "") {
                profile.subsidiary = "1";
            } else if (state == "5" || state == "27" || state == "16" || state == "51" || state == "26" || state == "42" || state == "34") {
                profile.subsidiary = "9"; //CO
            } else if (state == "14" || state == "35" || state == "17" || state == "22") {
                profile.subsidiary = "4"; //IN
            } else if (state == "44" || state == "18" || state == "36" || state == "31" || state == "3") {
                profile.subsidiary = "6"; //TX
            } else if (state == "9") {
                profile.subsidiary = "10"; //FL
            } else if (state == "50" || state == "13" || state == "23" || state == "15" || state == "25") {
                profile.subsidiary = "3"; //TestGauge Inc
            } else if (state == "48" || state == "37" || state == "12" || state == "28" || state == "4" || state == "45" || state == "2" || state == "1" || state == "11" || state == "19" || state == "29" || state == "46" || state == "21" || state == "6" || state == "40" || state == "32" || state == "38" || state == "30" || state == "7" || state == "20" || state == "49" || state == "47" || state == "33" || state == "41" || state == "43" || state == "24" || state == "0" || state == "10") {
                profile.subsidiary = "2"; //Test Gauge & Backflow Supply, Inc.
            }

            profile.isGuest = ModelsInit.customer.isGuest() ? 'T' : 'F';
            // profile.subsidiary = ModelsInit.session.getShopperSubsidiary();
            profile.language = ModelsInit.session.getShopperLanguageLocale();
            profile.currency = ModelsInit.session.getShopperCurrency();
            profile.priceLevel = ModelsInit.session.getShopperPriceLevel().internalid ? ModelsInit.session.getShopperPriceLevel().internalid : ModelsInit.session.getSiteSettings(['defaultpricelevel']).defaultpricelevel;

            console.log('profile', JSON.stringify(profile))
            return profile;
        }
    });
});

Leave a comment

Your email address will not be published. Required fields are marked *