mongoose#QueryOptions TypeScript Examples

The following examples show how to use mongoose#QueryOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: group.controller.ts    From EDA with GNU Affero General Public License v3.0 6 votes vote down vote up
static async deleteGroup (req: Request, res: Response, next: NextFunction) {
    try {
      await Dashboard.updateOne({}, { $pull: { group: req.params.id } }).exec()
      await User.updateOne(
        { role: req.params.id },
        { $pull: { role: { $in: [req.params.id] } } }
      ).exec()
      let options: QueryOptions = {}

      Group.findByIdAndDelete(
        req.params.id,
        options,
        async (err, groupDeleted: IGroup) => {
          if (err) {
            return next(new HttpException(500, 'Error removing group'))
          }

          if (!groupDeleted) {
            return next(new HttpException(400, 'Group not exists'))
          }

          return res.status(200).json({ ok: true })
        }
      )
    } catch (err) {
      next(err)
    }
  }
Example #2
Source File: group.controller.ts    From EDA with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
   * Esta función borra un grupo que ya no está en el Active Directory
   */
  static async deleteGroupFromAD ( grupo:string ) {
    
    try {
      let groupId =  await  GroupController.getLocalGroupsIds( [grupo] );
      await Dashboard.updateOne({}, { $pull: { group: groupId[0] } }).exec()
      await User.updateOne(
        { role: groupId[0] },
        { $pull: { role: { $in: [groupId[0]] } } }
      ).exec()
      let options: QueryOptions = {}

      Group.findByIdAndDelete(
        groupId[0] ,
        options,
        async (err, groupDeleted: IGroup) => {
          if (err) {
            console.log( 'Error removing group');
          }

          if (!groupDeleted) {
            console.log( 'Group not exists');
          }

          return grupo;
        }
      )
    } catch (err) {
      console.log( err);
    }
  }
Example #3
Source File: user.controller.ts    From EDA with GNU Affero General Public License v3.0 6 votes vote down vote up
static async getUsers(req: Request, res: Response, next: NextFunction) {

        try {

            let userID = req.user._id;
            const groups = await Group.find({ users: { $in: userID } }).exec();
            const isAdmin = groups.filter(g => g.role === 'EDA_ADMIN_ROLE').length > 0;

            User.find({}, 'name email img role google').exec((err, users: IUser[]) => {
                if (err) {
                    return next(new HttpException(500, 'Error loading users'));
                }
                let options:QueryOptions = {};
                Group.find({}, 'name role', options, (err, groups: IGroup[]) => {
                    if (err) {
                        return next(new HttpException(500, 'Error loading users'));
                    }

                    for (const user of users) {
                        const groupsUsers = [];
                        for (const role of user.role) {
                            groupsUsers.push(groups.find((group: IGroup) => JSON.stringify(role) === JSON.stringify(group._id)));
                        }
                        user.role = groupsUsers;
                    }

                    users = isAdmin ? users : UserController.filterUsersByGroup(req.user, users);

                    return res.status(200).json(users);
                });
            })
        } catch (err) {
            next(err);
        }
    }
Example #4
Source File: user.controller.ts    From EDA with GNU Affero General Public License v3.0 6 votes vote down vote up
static async getUser(req: Request, res: Response, next: NextFunction) {
        try {
            User.findById({ _id: req.params.id }, (err, user) => {

                if (err) {
                    return next(new HttpException(500, 'User not found with this id'));
                }
                let options:QueryOptions = {};
                Group.find({ _id: { $in: user.role } }, 'name role', options, (err, groups) => {
                    if (err) {
                        return next(new HttpException(500, 'Error waiting for user groups'));
                    }

                    // const isAdmin = groups.filter(g => g.role === 'EDA_ADMIN_ROLE').length > 0;

                    user.role = groups;

                    user.password = 'password_protected';
                    return res.status(200).json({ ok: true, user });
                });
            });
        } catch (err) {
            next(err);
        }
    }
Example #5
Source File: user.controller.ts    From EDA with GNU Affero General Public License v3.0 6 votes vote down vote up
static async getIsAdmin(req: Request, res: Response, next: NextFunction) {
        try {
            User.findById({ _id: req.params.id }, (err, user) => {

                if (err) {
                    return next(new HttpException(500, 'User not found with this id'));
                }
                let options:QueryOptions = {};
                Group.find({ _id: { $in: user.role } }, 'name role',options, (err, groups) => {
                    if (err) {
                        return next(new HttpException(500, 'Error waiting for user groups'));
                    }

                    const isAdmin = groups.filter(g => g.role === 'EDA_ADMIN_ROLE').length > 0;

                    return res.status(200).json({ isAdmin });
                });
            });
        } catch (err) {
            next(err);
        }
    }
Example #6
Source File: user.controller.ts    From EDA with GNU Affero General Public License v3.0 6 votes vote down vote up
static async delete(req: Request, res: Response, next: NextFunction) {
        let options:QueryOptions = {};
        try {
            User.findByIdAndDelete(req.params.id, options, (err, userRemoved) => {

                if (err) {
                    return next(new HttpException(500, 'Error removing an user'));
                }

                if (!userRemoved) {
                    return next(new HttpException(400, 'Not exists user with this id'));
                }

                return res.status(200).json({ ok: true, user: userRemoved });
            });
        } catch (err) {
            next(err);
        }
    }
Example #7
Source File: dashboard.controller.ts    From EDA with GNU Affero General Public License v3.0 6 votes vote down vote up
static async delete(req: Request, res: Response, next: NextFunction) {
        let options:QueryOptions = {};
        try {
            Dashboard.findByIdAndDelete(req.params.id, options, (err, dashboard) => {

                if (err) {
                    return next(new HttpException(500, 'Error removing dashboard'));
                }

                if (!dashboard) {
                    return next(new HttpException(400, 'Not exists dahsboard with this id'));
                }

                return res.status(200).json({ ok: true, dashboard });
            });
        } catch (err) {
            next(err);
        }
    }
Example #8
Source File: datasource.controller.ts    From EDA with GNU Affero General Public License v3.0 6 votes vote down vote up
static async GetDataSourcesNames(req: Request, res: Response, next: NextFunction) {
        let options:QueryOptions = {};
        DataSource.find({}, '_id ds.metadata.model_name ds.security', options, (err, ds) => {
            if (!ds) {
                return next(new HttpException(500, 'Error loading DataSources'));
            }

            const names = JSON.parse(JSON.stringify(ds));

            const output = [];

            for (let i = 0, n = names.length; i < n; i += 1) {
                const e = names[i];
                output.push({ _id: e._id, model_name: e.ds.metadata.model_name });
            }
            output.sort((a,b) => (upperCase(a.model_name) > upperCase(b.model_name)) ? 1 : 
            ((upperCase(b.model_name) > upperCase(a.model_name)) ? -1 : 0))
            return res.status(200).json({ ok: true, ds: output });
        });
    }
Example #9
Source File: datasource.controller.ts    From EDA with GNU Affero General Public License v3.0 6 votes vote down vote up
static async DeleteDataSource(req: Request, res: Response, next: NextFunction) {
        try {
            Dashboard.find({}, (err, dashboards) => {
                const dbds = dashboards.filter(d => d.config.ds._id === req.params.id);
                let stopLoop = false;

                for (let i = 0; i < dbds.length; i++) {
                    if (stopLoop) {
                        return false;
                    }
                    let options:QueryOptions = {};
                    Dashboard.findByIdAndDelete(dbds[i]._id, options, (err, dashboard) => {
                        if (err) {
                            stopLoop = true;
                            return next(new HttpException(500, 'Error removing dashboard'));
                        }
                    });
                }
                let options:QueryOptions = {};
                DataSource.findByIdAndDelete(req.params.id, options, (err, dataSource) => {
                    if (err) {
                        return next(new HttpException(500, 'Error removing dataSource'));
                    }

                    return res.status(200).json({ ok: true, dataSource });
                });
            });
        } catch (err) {
            next(err);
        }
    }
Example #10
Source File: datasource.controller.ts    From EDA with GNU Affero General Public License v3.0 5 votes vote down vote up
/* Aquesta funció retorna els datasources disponibles per fer un dashboard.
    Un cop filtrats els permisos de grup i de usuari. */
    static async GetDataSourcesNamesForDashboard(req: Request, res: Response, next: NextFunction) {

        let options:QueryOptions = {};
        DataSource.find({}, '_id ds.metadata.model_name ds.metadata.model_granted_roles',options, (err, ds) => {
            if (!ds) {
                return next(new HttpException(500, 'Error loading DataSources'));
            }

            const names = JSON.parse(JSON.stringify(ds));

            const output = [];

            for (let i = 0, n = names.length; i < n; i += 1) {
                const e = names[i];

                if (e.ds.metadata.model_granted_roles.length > 0) {

                    const userID = req.user._id;
                    const users = [];
                    const roles = [];
                    //Get users with permission
                    e.ds.metadata.model_granted_roles.forEach(permission => {

                        switch(permission.type){
                            case 'users':
                                permission.users.forEach(user => {
                                    if (!users.includes(user)) users.push(user);
                                });
                            break;
                            case 'groups':
                                req.user.role.forEach(role => {
                                    if(permission.groups.includes(role)){
                                        if (!roles.includes(role)) roles.push(role);
                                    }
                                });
                        }
                    });

                    if (users.includes(userID) || roles.length > 0) {
                        output.push({ _id: e._id, model_name: e.ds.metadata.model_name });
                    }

                }
                else {

                    output.push({ _id: e._id, model_name: e.ds.metadata.model_name });

                }
            }
            output.sort((a,b) => (upperCase(a.model_name) > upperCase(b.model_name)) ? 1 : 
                                    ((upperCase(b.model_name) > upperCase(a.model_name)) ? -1 : 0))
            return res.status(200).json({ ok: true, ds: output });
        });
    }