'use strict' ; import { inject } from '../../bases/Utils' ; import BaseAPI from '../../bases/BaseAPI' ; import { DingRestAPI } from '..' ; import MemberModel from '../../models/MemberModel' ; import { IMemberData } from 'teambition' ; let hasResolved = { } ; @ inject ( [ 'DingRestAPI' ] ) export class MemberAPI extends BaseAPI { private DingRestAPI : DingRestAPI ; public fetch ( _id ) { let members = MemberModel . getMemberCollection ( _id ) ; if ( members ) { let deferred = this . $q . defer < { [ index ] : IMemberData ; } > ( ) ; deferred . resolve ( members ) ; return deferred . promise ; } else { return this . RestAPI . query ( { V2 : 'v2' , Type : 'projects' , Id : _id , Path1 : 'members' , fields : this . fields . memberFileds } ) . $promise . then ( ( data ) => { return MemberModel . setMemberCollection ( _id , data ) ; } ) ; } } public getOrganizationMembers ( organizationId ) { let cache = MemberModel . getOrganizationMembers ( organizationId ) ; if ( cache ) { let defer = this . $q . defer ( ) ; defer . resolve ( cache ) ; return defer . promise ; } return this . RestAPI . query ( { V2 : 'v2' , Type : 'organizations' , Id : organizationId , Path1 : 'members' } ) . $promise . then ( ( members ) => { return MemberModel . setOrganizationMembers ( organizationId , members ) ; } ) ; } public getDingId ( userIds , projectId , corpId ) { let cache = hasResolved [ projectId ] ; if ( ! cache ) { return this . DingRestAPI . query ( { Type : 'userIds' , type : '_userId' , corpId : corpId , conditions : userIds . join ( ',' ) } , ( data ) => { hasResolved [ projectId ] = data ; } ) . $promise ; } else { let defer = this . $q . defer ( ) ; defer . resolve ( cache ) ; return defer . promise ; } } } angular . module ( 'teambition' ) . service ( 'MemberAPI' , MemberAPI ) ;