import { Injectable } from '@angular/core' ; import { ContextService } from "../context.service" ; import * as _ from "lodash" ; @ Injectable ( ) export class GroupsUtilsService { constructor ( private context ) { } getMyGroups ( ) { let user = this . context . getUser ( ) ; let allGroups = this . context . getAllGroups ( ) ; return _ . filter ( allGroups , ( group ) => { return this . userIsMemOwnAdminOfGroup ( user . id , group ) ; } ) ; } getGroupMembers ( group ) { let allUsers = this . context . getAllUsers ( ) ; let list = _ . filter ( allUsers , ( user ) => { let inGroup = ! ! this . userIsMemOwnAdminOfGroup ( user . id , group ) ; return inGroup ; } ) ; return list ; } userIsMemOwnAdminOfGroup ( userId , group ) { if ( this . userIsMember ( userId , group ) ) { return 'Member' ; } else if ( this . userIsOwner ( userId , group ) ) { return 'Owner' ; } else if ( this . userIsAdmin ( userId , group ) ) { return 'Administrator' ; } else { return false ; } } getGroup ( id ) { let allGroups = this . context . getAllGroups ( ) ; return _ . find ( allGroups , ( group ) => { return group . id == id ; } ) ; } userIsMember ( userId , group ) { let isMember = ! ! _ . find ( group . members , ( i ) => { return i == userId ; } ) ; return isMember ; } userIsOwner ( userId , group ) { let isOwner = ! ! _ . find ( group . owners , ( i ) => { return i == userId ; } ) ; return isOwner ; } userIsAdmin ( userId , group ) { let isAdministrator = ! ! _ . find ( group . administrators , ( i ) => { return i == userId ; } ) ; return isAdministrator ; } }