'use strict' ; import BaseAPI from '../../bases/BaseAPI' ; import WorkModel from '../../models/WorkModel' ; import { IStrikerRes , IFileData , ICollectionData } from 'teambition' ; export class WorkAPI extends BaseAPI { public upload ( _parentId , strikerRes ) { let { fileName , fileSize , fileType , fileCategory , fileKey } = strikerRes ; return this . RestAPI . save ( { Type : 'works' } , { _parentId : _parentId , fileName : fileName , fileSize : fileSize , fileType : fileType , fileCategory : fileCategory , fileKey : fileKey } ) . $promise . then ( ( data ) => { return data ; } ) ; } public uploads ( _parentId , _projectId , works ) { return this . RestAPI . post ( { Type : 'works' } , { _parentId : _parentId , _projectId : _projectId , works : works } ) . $promise . then ( ( data ) => { angular . forEach ( data , ( file , index ) => { WorkModel . addFile ( _parentId , file ) ; } ) ; return data ; } ) ; } public fetchWorks ( _projectId , _parentId ) { let cache = WorkModel . getFolderFilesCollection ( _projectId , _parentId ) ; let deferred = this . $q . defer < IFileData [ ] > ( ) ; if ( cache ) { deferred . resolve ( cache ) ; return deferred . promise ; } return this . RestAPI . query ( { Type : 'projects' , Id : _projectId , Path1 : 'collections' , Path2 : _parentId , Path3 : 'works' , fields : this . fields . workFileds } ) . $promise . then ( ( works ) => { let result = this . prepareWorks ( works , _projectId , _parentId ) ; return result ; } ) ; } public fetchCollections ( _projectId , _collectionId ) { let cache = WorkModel . getFoldersCollection ( _projectId , _collectionId ) ; let deferred = this . $q . defer < ICollectionData [ ] > ( ) ; if ( cache ) { deferred . resolve ( cache ) ; return deferred . promise ; } return this . RestAPI . query ( { Type : 'collections' , all : true , _parentId : _collectionId , _projectId : _projectId } ) . $promise . then ( ( collections ) => { let result = this . prepareCollections ( collections , _projectId , _collectionId ) ; return result ; } ) ; } public createCollection ( title , parentId , description ? , color ? ) { return this . RestAPI . save ( { Type : 'collections' } , { title : title , _parentId : parentId , description : description , color : color } ) . $promise . then ( ( collection ) => { WorkModel . addFolder ( collection ) ; } ) ; } public rename ( type , id , name , description ? , color ? ) { let update ; update = { description : description } ; let key ; if ( type === 'collections' ) { key = 'title' ; update . color = color ; } else { key = 'fileName' ; } update [ key ] = name ; return this . RestAPI . update ( { Type : type , Id : id } , update ) . $promise . then ( ( data ) => { let namespace = type . substr ( 0 , type . length - 1 ) + `:detail:${ data . _id }` ; WorkModel . updateDetail ( namespace , data ) ; } ) ; } public delete ( type , id ) { return this . RestAPI . delete ( { Type : type , Id : id } ) . $promise . then ( ( ) => { WorkModel . removeObj ( type , id ) ; } ) ; } private prepareWorks ( works , _projectId , _parentId ) { if ( works ) { return WorkModel . setFolderFilesCollection ( _projectId , _parentId , works ) ; } } private prepareCollections ( collections , _projectId , _collectionId ) { if ( ! collections . length ) { return collections ; } return WorkModel . setFoldersCollection ( _projectId , _collectionId , collections ) ; } } angular . module ( 'teambition' ) . service ( 'WorkAPI' , WorkAPI ) ;