JOSIMODDIN SHAIKH
JOSIMODDIN SHAIKH

Reputation: 1

error due to dividing number multiple time in nodejs

I have to set a target for a comapny's user and those user are formed in tree hierarchy, so here the target set for one person will be divided to child in tree structure. Now the problem is whenever dividing it is getting divided multiple times and i am storing the user target in database using tofixed(4) tiil 4 digit, and if i sum up all user target it is not same as the total target.

Here is my code for dividing:

async function insertUserTarget(connection, tempData) {
    try {
        let childDesignation = []
        childDesignation = await dao.getDesignationIdsByParent(tempData.clientId, tempData.designationId);
        if (childDesignation.length > 0) {
            let idValues = childDesignation.map(item => item.designationId.toString());
            let childDesignationIds = idValues.join(',');
            let users
            users = await dao.getUserByParents(tempData.clientId, childDesignationIds, tempData.targetUserId.toString());
            if (users.length > 0) {
                tempData.percentage = (tempData.percentage / users.length).toFixed(toFixedLength)
                tempData.value = (tempData.value / users.length).toFixed(toFixedLength)
                // from upper code getting the user from tree level
                // below code is setting target for itself and its child user recursively
                for (let l = 0; l < users.length; l++) {
                    temp = {}
                    temp.clientId = tempData.clientId
                    temp.targetUserId = users[l].userId
                    temp.designationId = users[l].designationId
                    temp.financialYearId = tempData.financialYearId
                    temp.finQuarterId = tempData.finQuarterId
                    temp.monthNumber = tempData.monthNumber
                    temp.year = tempData.year
                    temp.value = tempData.value
                    temp.percentage = tempData.percentage
                    temp.userId = tempData.userId
                    temp.currentDateTime = tempData.currentDateTime

                    temp.approvedStatus = tempData.approvedStatus
                    temp.approvedBy = tempData.approvedBy
                    temp.approvedRemarks = tempData.approvedRemarks
                    temp.approvedDatetime = tempData.approvedDatetime
                  
                    await dao.insertClientUserTarget(connection, temp, temp.approvedStatus, temp.approvedBy, temp.approvedRemarks, temp.approvedDatetime);
                   
                    await insertUserTarget(connection, temp)
                }
            } else {
                return
            }
        } else {
            return
        }
    } catch (e) {
        util.createLog(e);
        throw "error in designation"
    }
}

What is the correct strategy to divide and store and edit user target so that the sum of all child will be always same as the target set?

Upvotes: 0

Views: 26

Answers (0)

Related Questions