App-Dochazka-WWW

 view release on metacpan or  search on metacpan

share/js/dochazka-www/emp-lib.js  view on Meta::CPAN

            var cu = currentUser('obj'),
                cup = appCaches.getProfileByEID(cu.eid),
                priv = currentUser('priv');
            console.log("Entering currentEmpHasReports(), current employee profile", cup);
            if (priv === 'admin') {
                // not applicable to admins
                return true;
            }
            if (typeof cup !== 'object' || ! 'hasReports' in cup || typeof cup.hasReports !== 'function') {
                throw "Profile of current user has not been loaded into the cache";
            }
            if (typeof cup.hasReports === 'function') {
                return cup.hasReports();
            }
            console.log("CRITICAL ERROR: Bad current user profile object", cup);
            throw "Bad current user profile object";
        },

        empProfileEditSave = function (emp) {
                // protoEmp = Object.create(prototypes.empProfile),
            var empObj,
                parentTarget,
                protoEmp = $.extend(Object.create(prototypes.empObject), emp);
            console.log("Entering empProfileEditSave with object", emp);
            // protoEmp = {
            //     'emp': { 'eid': emp.eid,
            //              'email': coreLib.nullify(emp.email),
            //              'fullname': coreLib.nullify(emp.fullname),
            //              'nick': coreLib.nullify(emp.nick),
            //              'remark': coreLib.nullify(emp.remark),
            //              'sec_id': coreLib.nullify(emp.sec_id), },
            //     'has_reports': emp.has_reports,
            //     'priv': emp.priv,
            //     'privhistory': { 'effective': emp.privEffective },
            //     'schedhistory': { 'effective': emp.schedEffective,
            //                       'scode': emp.scode,
            //                       'sid': emp.sid },
            //     'schedule': { 'scode': emp.scode, 'sid': emp.sid },
            // };
            var rest = {
                    "method": 'POST',
                    "path": 'employee/nick',
                    "body": protoEmp.sanitize(),
                },
                sc = function (st) {
                    console.log("POST employee/nick returned status", st);
                    // what we do now depends on what targets are on the stack
                    // the target on the top of the stack will be "empProfileEdit"
                    // but the one below that can be either "empProfile" or
                    // "simpleEmployeeBrowser"
                    parentTarget = stack.getTarget(-1);
                    console.log("parentTarget", parentTarget);
                    empObj = Object.create(prototypes.empObject);
                    $.extend(empObj, st.payload);
                    if (parentTarget.name === 'empProfile') {
                        console.log("Employee object is", empObj);
                        currentUser('obj', empObj);
                        appCaches.setProfileCache({"emp": empObj});
                        stack.unwindToTarget(
                            'myProfileAction', undefined,
                            {"resultLine": "Employee profile updated"}
                        );
                    } else if (parentTarget.name === 'simpleEmployeeBrowser') {
                        console.log("Parent target is " + parentTarget.name);
                        console.log("current object in dbrowerState set",
                                    coreLib.dbrowserState.set[coreLib.dbrowserState.pos]);
                        $.extend(
                            coreLib.dbrowserState.set[coreLib.dbrowserState.pos],
                            empObj,
                        );
                        stack.pop(undefined, {"resultLine": "Employee profile updated"});
                    } else {
                        console.log("FATAL ERROR: unexpected parent target", parentTarget);
                    }
                },
                fc = function (st) {
                    console.log("AJAX: " + rest["path"] + " failed with", st);
                    coreLib.displayError(st.payload.message);
                };
            ajax(rest, sc, fc);
        },

        empProfileSetSuperDelete = function () {
            stack.push('empProfileSetSuperChoose', { "eid": null, "nick": null });
        },

        empProfileSetSuperChoose = function (superEmp) {
            var cu = currentUser('obj'),
                obj = {
                    "ePsetsuperofEID": cu.eid,
                    "ePsetsupertoEID": superEmp.eid,
                    "ePsetsuperof": cu.nick,
                    "ePsetsuperto": superEmp.nick,
                };
            console.log("Entering empSetSupervisor() with superEmp", superEmp);
            console.log("Will set superEmp as the supervisor of " + cu.nick);
            console.log("Pushing empProfileSetSuperConfirm onto stack with obj", obj);
            stack.push('empProfileSetSuperConfirm', obj);
        },

        empProfileSetSuperCommit = function (obj) {
            var cu = currentUser('obj'),
                empProfile,
                rest = {
                    "method": 'PUT',
                    "path": 'employee/eid/' + obj.ePsetsuperofEID,
                    "body": {
                        "supervisor": obj.ePsetsupertoEID,
                    }
                },
                sc = function (st) {
                    if (st.code === 'DOCHAZKA_CUD_OK' || st.code === 'DISPATCH_UPDATE_NO_CHANGE_OK' ) {
                        cu.supervisor = obj.ePsetsupertoEID;
                        empProfile = appCaches.getProfileByEID(obj.ePsetsuperofEID);
                        if (empProfile) {
                             empProfile.supervisor = obj.ePsetsupertoEID;
                             appCaches.setProfileCache(empProfile);
                        }
                        stack.unwindToType('dmenu', {
                            "_start": false
                        });
                        stack.push('myProfileAction', {
                            "resultLine": "Commit OK"
                        });
                    } else {
                        coreLib.displayError("CRITICAL ERROR THIS IS A BUG: " + st.code);
                        throw st.code;
                    }
                };
            console.log("Entered empProfileSetSuperCommit() with obj", obj);
            ajax(rest, sc);



( run in 1.368 second using v1.01-cache-2.11-cpan-5b529ec07f3 )