Want to fetch data from roles, in which given accountId is present. (Javasscript, MongoDb)

34 Views Asked by At

Roles data model:-

Roles : {
    id: string,
    scopes: [
        {
         isOwnes: bool, 
         account: string(id), 
         privileges: array_of_string
        }
    ]
}

In the below function I am storing all users, which is a lot of space and time-consuming.

User.subUsers = async function(accessToken, cb) {
    const accountId = accessToken.accountId;
    let userData = [];
    const user = await User.find();
    for (let it = 0; it < user.length; it++) {
      const roleData = await User.app.models.roles.findById(user[it].roleId);
      for (let iterator = 0; iterator < roleData.scopes.length; iterator++) {
        const scopeAccountId = roleData.scopes[iterator].account;
        if (scopeAccountId == accountId) {
          userData.push({
            email: user[it].email,
            id: user[it].id,
          });
          break;
        }
      }
    }
    return cb(null, userData);
  };

I want to change this program in a way such that I can fetch all the roles in which the given account id is present, but I can't find a way about how to apply that condition.

Can anyone help me?

0

There are 0 best solutions below