{"size": 1445, "ext": "js", "max_stars_repo_path": "graphql/resolvers/group-resolver.js", "max_stars_repo_name": "kryptin/krypton", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "graphql/resolvers/group-resolver.js", "max_issues_repo_name": "kryptin/krypton", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "graphql/resolvers/group-resolver.js", "max_forks_repo_name": "kryptin/krypton", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.4915254237, "max_line_length": 82, "alphanum_fraction": 0.508650519}
import Group from '../../models/group';
import GroupMember from '../../models/group-member';
import { requireAuth } from '../../services/auth';

export default {

  getGroups: async (_, args, { user }) => {
    try {
      //await requireAuth(user);
        const duserid = user? user._id: user;
        return Group.find({user:duserid}).sort({ createdAt: -1 })
    } catch (error) {
      throw error;
    }
  },
  getGroup: async (_, { _id }, { user }) => {
    try {
     // await requireAuth(user);
      return Group.findById(_id);
    } catch (error) {
      throw error;
    }
  },

  addGroup: async (_, args, { user }) => {
    try {
      await requireAuth(user);
      const duserid = user? user._id: user;

      var group = new Group({ ...args, user: duserid  });
      group.save(function (err) {
        if (err) return err;
        // saved!
      })
      GroupMember.create({ group: group._id, user: duserid, user_type:"Admin" });

      return group;
    } catch (error) {
      throw error;
    }
  },
 
  deleteGroup: async (_, { _id }, { user }) => {
    try {
      await requireAuth(user);
      const group = await Group.findOne({ _id, user: user._id });

      if (!group) {
        throw new Error('Not found!');
      }
      await group.remove();
      return {
        message: 'Delete Success!'
      }
    } catch (error) {
      throw error;
    }
  }
};