@@ -27,7 +27,7 @@ const opts = require('nomnom')
27
27
. parse ( ) ;
28
28
29
29
const fetch = function ( result ) {
30
- return result . fetch ( ) ;
30
+ return _ . isFunction ( result . fetch ) ? result . fetch ( ) : [ result ] ;
31
31
} ;
32
32
33
33
const fetchImage = function ( url ) {
@@ -119,13 +119,68 @@ fs.readJson(opts.config).then(config => {
119
119
} ) ;
120
120
}
121
121
122
- return client . tasks . findByProject ( opts . from ) . then ( fetch ) . then ( result => {
123
- return Promise . mapSeries ( _ . first ( result , 2 ) , item => {
124
- return client . tasks . addProject ( item . id , {
125
- project : opts . to
126
- } ) . then ( ( ) => {
127
- console . log ( item ) ;
122
+ // test 579942885784874
123
+ // return client.tasks.findByProject(opts.from, {
124
+ // opt_fields: ['attachments', 'assignee', 'followers', 'created_at', 'completed', 'notes', 'parent', 'tags'].join(',')
125
+ // }).then(fetch).then(result => {
126
+ return client . tasks . findById ( 579942885784874 , {
127
+ opt_fields : [ 'attachments' , 'assignee' , 'followers' , 'created_at' , 'completed' , 'notes' , 'parent' , 'tags' , 'stories' ] . join ( ',' )
128
+ } ) . then ( fetch ) . then ( result => {
129
+ const map = _ . indexBy ( result , 'id' ) ;
130
+
131
+ // Grouping by parent
132
+ result = _ . compact ( _ . map ( result , item => {
133
+ if ( item . parent === null ) {
134
+ return item ;
135
+ } else if ( map [ item . parent . id ] ) {
136
+ map [ item . parent . id ] . children = map [ item . parent . id ] . children || [ ] ;
137
+ map [ item . parent . id ] . children . push ( item ) ;
138
+ return null ;
139
+ }
140
+ } ) ) ;
141
+
142
+ const createTask = function ( data ) {
143
+ return client . tasks . create ( {
144
+ assignee : ! _ . isEmpty ( data . assignee ) ? data . assignee . id : null ,
145
+ followers : ! _ . isEmpty ( data . followers ) ? _ . pluck ( data . followers , 'id' ) : null ,
146
+ name : data . name ,
147
+ notes : data . notes ,
148
+ memberships : [ {
149
+ project : opts . to
150
+ } ] ,
151
+ tags : ! _ . isEmpty ( data . tags ) ? _ . pluck ( data . tags , 'id' ) : null
152
+ } ) . then ( result => {
153
+ const task = result ;
154
+ const promises = [ ] ;
155
+
156
+ if ( ! _ . isEmpty ( data . attachments ) ) {
157
+ promises . push ( client . attachments . findByTask ( data . id ) . then ( result => {
158
+ return Promise . mapSeries ( result , attachement => {
159
+ return fetchImage ( attachement . view_url ) . then ( image => {
160
+ return uploadImageToAsana ( task . id , image , attachment . name ) ;
161
+ } ) . catch ( reason => {
162
+ console . log ( 'Failed to upload attachment' , reason ) ;
163
+ } )
164
+ } ) ;
165
+ } ) ) ;
166
+ }
167
+
168
+ if ( ! _ . isEmpty ( data . stories ) ) {
169
+ promises . push ( client . stories . findByTask ( data . id ) . then ( result => {
170
+ return Promise . mapSeries ( result , story => {
171
+ return client . tasks . addComment ( task . id , {
172
+ text : `${ story . created_by . name } : ${ story . text } `
173
+ } ) ;
174
+ } ) ;
175
+ } ) ) ;
176
+ }
177
+
178
+ return Promise . all ( promises ) ;
128
179
} ) ;
180
+ } ;
181
+
182
+ return Promise . mapSeries ( result , item => {
183
+ console . log ( item ) ;
129
184
} ) ;
130
185
} ) ;
131
186
0 commit comments