'use strict' ; import { inject } from '../../bases/Utils' ; import BaseAPI from '../../bases/BaseAPI' ; import { ObjectLinkAPI , LikeAPI , TagsAPI , MemberAPI , TasklistAPI } from '..' ; import DetailModel from '../../models/DetailModel' ; import { IDetailInfos , ILinkedData , ITagsData , ILikeData , IMemberData , ITasklistData } from 'teambition' ; @ inject ( [ 'ObjectLinkAPI' , 'LikeAPI' , 'TagsAPI' , 'MemberAPI' , 'TasklistAPI' ] ) export class DetailAPI extends BaseAPI { private ObjectLinkAPI : ObjectLinkAPI ; private LikeAPI : LikeAPI ; private TagsAPI : TagsAPI ; private MemberAPI : MemberAPI ; private TasklistAPI : TasklistAPI ; private filedsMap = { task : this . fields . taskFileds , post : this . fields . postFileds , event : this . fields . eventFileds , work : this . fields . workFileds } ; public fetch ( _id , type , linkedId ? ) { return this . query ( _id , type , linkedId ) . then ( ( data ) => { let detailInfos ; detailInfos = { } ; let fetchTasks = [ this . ObjectLinkAPI . fetch ( type , _id ) . then ( ( linked ) => { data . linked = linked ; } ) , this . LikeAPI . getLiked ( type , _id ) . then ( ( liked ) => { detailInfos . like = liked ; } ) , this . TagsAPI . fetchByObjectId ( `${ type }s` , _id ) . then ( ( tags ) => { detailInfos . tags = tags ; } ) , this . MemberAPI . fetch ( data . _projectId ) . then ( ( members ) => { detailInfos . members = members ; } ) ] ; if ( type === 'task' ) { fetchTasks . push ( this . TasklistAPI . fetch ( data . _tasklistId ) . then ( ( tasklist ) => { detailInfos . tasklist = tasklist ; detailInfos . stage = this . findElementInArray ( tasklist . hasStages , data . _stageId ) ; } ) ) ; } return this . $q . all ( fetchTasks ) . then ( ( ) => { return this . detailParser ( data , type , detailInfos ) ; } ) ; } ) ; } public update ( _id , type , patch , param ? ) { return this . RestAPI . update ( { Type : `${ type }s` , Id : _id , Path1 : param } , patch ) . $promise . then ( ( detail ) => { DetailModel . updateDetail ( `${ type }:detail:${ _id }` , detail ) ; } ) ; } public create ( type , content ) { return this . RestAPI . save ( { Type : `${ type }s` } , content ) . $promise . then ( ( detail ) => { return DetailModel . setDetail ( `${ type }:detail:${ detail . _id }` , detail ) ; } ) ; } public delete ( type , id ) { return this . RestAPI . delete ( { Type : `${ type }s` , Id : id } ) . $promise . then ( ( ) => { DetailModel . removeObject ( type , id ) ; } ) ; } private query ( _id , type , linkedId ) { let cache = DetailModel . getDetail ( `${ type }:detail:${ _id }` ) ; let deferred = this . $q . defer ( ) ; if ( cache ) { deferred . resolve ( cache ) ; return deferred . promise ; } else { return this . RestAPI . get ( { Type : `${ type }s` , Id : _id , _objectLinkId : linkedId , fields : this . filedsMap [ type ] } ) . $promise . then ( ( data ) => { return data ; } ) ; } } private detailParser ( detail , type , detailInfos ) { detail . like = detailInfos . like ; detail . detailInfos = detailInfos ; return DetailModel . setDetail ( `${ type }:detail:${ detail . _id }` , detail ) ; } private findElementInArray < T extends { _id : string ; } > ( array , id ) { for ( let index = 0 ; index < array . length ; index ++ ) { let element = array [ index ] ; if ( element . _id === id ) { return element ; } } } } angular . module ( 'teambition' ) . service ( 'DetailAPI' , DetailAPI ) ;