'use strict';

module.exports = function(app) {
  var Role = app.models.Role;

  Role.registerResolver('groupMember', (role, context, cb) => {
    if (context.modelName !== 'group') {
      return process.nextTick(() => cb(null, false));
    }

    var userId = context.accessToken.userId;
    if (!userId) {
      return process.nextTick(() => cb(null, false));
    }

    context.model.findById(context.modelId, (err, group) => {
      if (err) return cb(err);
      if (!group) return cb(new Error('Group not found'));

      var Group = app.models.Group;
      Group.count(
        {
          ownerId: group.ownerId,
          memberId: userId,
        },
        (err, count) => {
          if (err) return cb(err);
          return count > 0 ? cb(null, true) : cb(null, false);
        }
      );
    });
  });

  Role.registerResolver('groupAdmin', (role, context, cb) => {
    if (context.modelName !== 'group') {
      return process.nextTick(() => cb(null, false));
    }

    var userId = context.accessToken.userId;
    if (!userId) {
      return process.nextTick(() => cb(null, false));
    }

    context.model.findById(context.modelId, (err, group) => {
      if (err) return cb(err);
      if (!group) return cb(new Error('Group not found'));

      var Group = app.models.Group;
      Group.count(
        {
          ownerId: group.ownerId,
          memberId: userId,
        },
        (err, count) => {
          if (err) return cb(err);
          return count > 0 ? cb(null, true) : cb(null, false);
        }
      );
    });
  });
};

@@ -526,280 +526,39 @@
     
-var Group = app.models.Group;%0A      Group.count(%0A        %7B%0A          ownerId: group.ownerId,%0A          memberId: userId,%0A        %7D,%0A        (err, count) =%3E %7B%0A          if (err) return cb(err);%0A          return count %3E 0 ? cb(null, true) : cb(null, false);%0A        %7D%0A      
+group.members.exists(userId, cb
 );%0A 
@@ -1024,280 +1024,38 @@
     
-var Group = app.models.Group;%0A      Group.count(%0A        %7B%0A          ownerId: group.ownerId,%0A          memberId: userId,%0A        %7D,%0A        (err, count) =%3E %7B%0A          if (err) return cb(err);%0A          return count %3E 0 ? cb(null, true) : cb(null, false);%0A        %7D%0A      
+group.admins.exists(userId, cb
 );%0A 

Use simpler approach