DSSUserUtility d365fo x++

DSSUserUtility d365fo x++

Code:

internal final class DSSUserUtility
{
    private static DSSUserUtility userUtilityObj;

    private void new()
    {

    }

    public static DSSUserUtility construct()
    {
        return (userUtilityObj) ? userUtilityObj : new DSSUserUtility();
    }

    public HcmWorker getWorkerFromPrimaryEmail(Email _email)
    {
        HcmWorker worker;
        DirPartyTable dirPartyTable;
        LogisticsElectronicAddress  logisticsElectronicAddress;

        select firstonly worker
            exists join dirPartyTable
            exists join logisticsElectronicAddress
            where worker.Person == dirPartyTable.RecId
            && dirPartyTable.PrimaryContactEmail == logisticsElectronicAddress.RecId
            && logisticsElectronicAddress.Locator == _email;

        return worker;
    }

    public UserId getSystemUserFromEmail(Email _email)
    {
        UserInfo userInfo;

        select firstonly1 userInfo
            where userInfo.networkAlias == _email;

        return userInfo.Id;
    }

    public UserId getUserFromWorker(HcmWorker _worker)
    {
        return DirPersonUser::findParty(_worker.Person).User;
    }

    public boolean isActiveWorker(HcmWorker _worker)
    {
        boolean isActive;
        ;

        HcmEmployment employment = HcmEmployment::getActiveEmploymentsByWorker(_worker.RecId);
        isActive = (employment) ? true : false;

        if(!isActive)
        {
            throw error("@DSSLabel:DSS000285");
        }

        return isActive;
    }

    public boolean isUserHavePersonAttached(UserId _userId)
    {
        boolean isPersonAttached;

        isPersonAttached = DirPersonUser::anyExist(_userId);

        if(!isPersonAttached)
        {
            throw error("@DSSLabel:DSS000286");
        }

        return isPersonAttached;
    }

    public DSSUsers getSelfServiceUserFromEmail(Email _email)
    {
        DSSUsers selfSeriveUser;
        HcmWorker worker;
        UserId userId;
        ;

        //userId = this.getSystemUserFromEmail(_email);

        //if(!userId)
        //{
        //    worker = this.getWorkerFromPrimaryEmail(_email);
        //    userId = (worker) ? this.getUserFromWorker(worker) : userId;
        //}

        if(DSSLicenseInfo::find().isValidateForNewUserCreation())
        {
            worker = this.getWorkerFromPrimaryEmail(_email);
            userId = (worker) ? this.getUserFromWorker(worker) : userId;
        

            if(userId)
            {
                selfSeriveUser = DSSUsers::createUser(userId, _email, NoYes::Yes, '' , true);
            }
            else if(worker)
            {
                selfSeriveUser = DSSUsers::createUser(worker.PersonnelNumber, _email, NoYes::No, '' , true);
            }
            else
            {
                throw Error("@DSSLabel:DSS000284");
            }
        }
        else
        {
            throw Error("Registration is currently unavailable as all user licenses are fully occupied.\nPlease contact your administrator for further assistance.");
        }
        

        return selfSeriveUser;
    }

    // <Sceanario>
    // There are two types of users in ESS
    // Worker User  ESS User Id will be employeeId/PersonnelNumber from HcmWorker and can use ess
    // Dynamics User ESS User Id will be userId/Id  from UserInfo and can use ESS and also take actions on assigned workflows
    // Now there is sceanrio when user is opened as Worker user but later it is opened in the system/UserInfo table
    // So in the above case we have to convert Worker user to Dynamcis user auto
    // </Sceanario>

    // <Method Functionality>
    // validate if user is worker and its user is also opened in the system/UserInfo
    // Convert the worker user to Dynamics User
    // </Method Functionality>
    public DSSUsers validateAndConvertESSUserToDynamicUser(DSSUsers _essUser)
    {
        DSSUsers essUser = _essUser;

        if(!essUser.IsDynamicsUser)
        {
            UserId userId = this.getUserFromWorker(essUser.getWorker());

            if(userId)
            {
                ttsbegin;
                essUser.selectForUpdate(true);
                essUser.UserId = userId;
                essUser.IsDynamicsUser = NoYes::Yes;
                essUser.update();
                ttscommit;
            }
        }

        return essUser;
    }

    public DSSFileStringData getUserImage(DSSUsers _dssUser)
    {
        HcmWorker worker;
        HcmPersonImage personImage;
        Bitmap image;
        DSSFileStringData imageString;
        ;

        select firstonly Image
            from personImage
            exists join worker
            where personImage.Person == worker.Person
            && worker.PersonnelNumber == _dssUser.displayEmployeeId();

        image = (personImage.Image) ? personImage.Image : personImage.displayImageAlternate();

        imageString = DSSUtil::construct().getImageAsBase64(image);

        return imageString;
    }

}

Comments

Popular posts from this blog

D365FO – AX – X++ –Refresh, Reread, Research, and ExecuteQuery

SalesLine Reservation in D365fo x++

Create Inventory Journal through Code in D365FO X++