import { Injectable } from '@angular/core' ; import { ContextService } from "../context.service" ; import * as _ from "lodash" ; import { GroupsUtilsService } from "../groups/groups-utils.service" ; import { getUser } from "../../mock-data/user" ; import { UsersService } from "./users.service" ; import { RequestsService } from "../requests/requests.service" ; import { RequestsUtilsService } from "../requests/requests-utils.service" ; @ Injectable ( ) export class UsersUtilsService { constructor ( private groupUtils , private usersService , private requestsService , private requestsUtils , private context ) { } getSelf ( ) { let userId = this . context . getUserId ( ) ; let allUsers = this . context . getAllUsers ( ) ; return _ . find ( allUsers , ( user ) => { return user . id == userId ; } ) ; } getUser ( userId ) { let allUsers = this . context . getAllUsers ( ) ; let found = _ . find ( allUsers , ( user ) => { return user . id == userId ; } ) ; return found ; } displayOwners ( element , user , collectionName = 'owners' ) { if ( element [ collectionName ] . length > 1 ) { let count = element [ collectionName ] . length ; if ( ! ! _ . find ( element [ collectionName ] , ( el ) => { return el . id == user . id ; } ) ) { return 'You and ' + ( count - 1 ) + ' others' ; } else { return count + ' Owners' ; } } else if ( element [ collectionName ] . length === 1 ) { if ( element [ collectionName ] [ 0 ] === user . id ) { return 'You' ; } else { return this . getUser ( element [ collectionName ] [ 0 ] ) . name ; } } else { console . log ( 'Group has no members' ) ; } } getRequestsPendingApproval ( ) { let userId = this . context . getUser ( ) . id ; return this . usersService . getUser ( userId ) . then ( ( user ) => { return this . requestsUtils . getAllRequests ( user . proposals ) ; } ) . then ( ( requests ) => { console . log ( 'All User Requests: ' , requests ) ; return requests ; } ) ; } }