import { Injectable , Inject } from '@angular/core' ; import { Http } from "@angular/http" ; import { toPromise } from "rxjs/operator/toPromise" ; import { UtilsService } from "../utils.service" ; import { environment } from "../../../environments/environment" ; import { ContextService } from "../context.service" ; @ Injectable ( ) export class GroupService { constructor ( private http , private context , private utils ) { } getGroups ( ) { return this . http . get ( environment . roles , { headers : this . context . httpHeaders ( ) } ) . toPromise ( ) . then ( ( response ) => { console . log ( 'Groups: ' , response . json ( ) . data ) ; return response . json ( ) . data ; } ) . catch ( this . utils . catchError ) ; } addMemberToGroup ( groupId , member ) { let request = environment . add_member ( groupId , member ) ; return this . http . post ( request . url , request . body , this . context . httpOptions ( ) ) . toPromise ( ) . then ( ( response ) => { console . log ( 'Add Member: ' , response . json ( ) ) ; return response . json ( ) ; } ) . catch ( this . utils . catchError ) ; } leaveGroup ( userId , groupId ) { return this . utils . setTimeoutPromise ( 0 ) . then ( ( ) => { return true ; } ) . catch ( this . utils . catchError ) ; } promoteToOwner ( userId , groupId ) { return this . utils . setTimeoutPromise ( 0 ) . then ( ( ) => { return true ; } ) . catch ( this . utils . catchError ) ; } promoteAllToOwner ( selection ) { return this . utils . setTimeoutPromise ( 0 ) . then ( ( ) => { return true ; } ) . catch ( this . utils . catchError ) ; } removeFromGroup ( userId , groupId ) { return this . utils . setTimeoutPromise ( 0 ) . then ( ( ) => { return true ; } ) . catch ( this . utils . catchError ) ; } removeAllFromGroup ( selection ) { return this . utils . setTimeoutPromise ( 0 ) . then ( ( ) => { return true ; } ) . catch ( this . utils . catchError ) ; } getGroup ( groupId ) { return this . http . get ( '' ) . toPromise ( ) . then ( ( response ) => { console . log ( 'response' , response ) ; return response . json ( ) ; } ) . catch ( this . utils . catchError ) ; } createNewGroup ( groupName ) { let request = environment . create_role ( this . context . getUser ( ) . id , groupName ) ; return this . http . post ( request . url , request . body , { headers : this . context . httpHeaders ( ) } ) . toPromise ( ) . then ( ( response ) => { console . log ( 'Create Group Response: ' , response . json ( ) . data ) ; return response . json ( ) . data ; } ) ; } }