@@ -119,44 +119,57 @@ fs.readJson(opts.config).then(config => {
119
119
} ) ;
120
120
}
121
121
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
- } ) ) ;
122
+ const taskOptions = {
123
+ opt_fields : [ 'name' , 'attachments' , 'assignee' , 'followers' , 'created_at' , 'completed' , 'notes' , 'parent' , 'subtasks' , 'tags' ] . join ( ',' )
124
+ } ;
141
125
142
- const createTask = function ( data ) {
143
- return client . tasks . create ( {
126
+ // test 579942885784874
127
+ // return client.tasks.findById(579942885784874, taskOptions).then(fetch).then(result => {
128
+ return client . tasks . findByProject ( opts . from , taskOptions ) . then ( fetch ) . then ( result => {
129
+ const createTask = function createTask ( data , parentTask ) {
130
+ const insertData = {
144
131
assignee : ! _ . isEmpty ( data . assignee ) ? data . assignee . id : null ,
145
132
followers : ! _ . isEmpty ( data . followers ) ? _ . pluck ( data . followers , 'id' ) : null ,
146
133
name : data . name ,
147
134
notes : data . notes ,
148
- memberships : [ {
149
- project : opts . to
150
- } ] ,
151
- tags : ! _ . isEmpty ( data . tags ) ? _ . pluck ( data . tags , 'id' ) : null
152
- } ) . then ( result => {
135
+ tags : ! _ . isEmpty ( data . tags ) ? _ . pluck ( data . tags , 'id' ) : [ ]
136
+ } ;
137
+
138
+ let promise = Promise . resolve ( ) ;
139
+
140
+ if ( parentTask ) {
141
+ insertData . workspace = opts . workspace ;
142
+
143
+ promise = client . tasks . addSubtask ( parentTask . id , insertData ) ;
144
+ } else {
145
+ _ . extend ( insertData , {
146
+ memberships : [ {
147
+ project : opts . to
148
+ } ] ,
149
+ projects : [ opts . to ] // projects 혹은 workspace를 명시해줘야 한다
150
+ } ) ;
151
+
152
+ promise = client . tasks . create ( insertData ) ;
153
+ }
154
+
155
+ return promise . then ( result => {
153
156
const task = result ;
154
157
const promises = [ ] ;
155
158
159
+ if ( ! _ . isEmpty ( data . subtasks ) ) {
160
+ promises . push ( client . tasks . subtasks ( data . id , taskOptions ) . then ( fetch ) . then ( result => {
161
+ return Promise . mapSeries ( result , data => {
162
+ return createTask ( data , task ) ;
163
+ } ) ;
164
+ } ) ) ;
165
+ }
166
+
156
167
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 => {
168
+ promises . push ( client . attachments . findByTask ( data . id , {
169
+ opt_fields : [ 'name' , 'view_url' ] . join ( ',' )
170
+ } ) . then ( fetch ) . then ( result => {
171
+ return Promise . mapSeries ( result , attachment => {
172
+ return fetchImage ( attachment . view_url ) . then ( image => {
160
173
return uploadImageToAsana ( task . id , image , attachment . name ) ;
161
174
} ) . catch ( reason => {
162
175
console . log ( 'Failed to upload attachment' , reason ) ;
@@ -166,7 +179,7 @@ fs.readJson(opts.config).then(config => {
166
179
}
167
180
168
181
if ( ! _ . isEmpty ( data . stories ) ) {
169
- promises . push ( client . stories . findByTask ( data . id ) . then ( result => {
182
+ promises . push ( client . stories . findByTask ( data . id ) . then ( fetch ) . then ( result => {
170
183
return Promise . mapSeries ( result , story => {
171
184
return client . tasks . addComment ( task . id , {
172
185
text : `${ story . created_by . name } : ${ story . text } `
@@ -175,12 +188,18 @@ fs.readJson(opts.config).then(config => {
175
188
} ) ) ;
176
189
}
177
190
178
- return Promise . all ( promises ) ;
191
+ if ( ! _ . isEmpty ( promises ) ) {
192
+ return Promise . all ( promises ) ;
193
+ } else {
194
+ return ;
195
+ }
179
196
} ) ;
180
197
} ;
181
198
182
199
return Promise . mapSeries ( result , item => {
183
- console . log ( item ) ;
200
+ return createTask ( item ) . then ( ( ) => {
201
+ console . log ( `${ item . id } task copied.` ) ;
202
+ } ) ;
184
203
} ) ;
185
204
} ) ;
186
205
@@ -268,7 +287,7 @@ fs.readJson(opts.config).then(config => {
268
287
// });
269
288
// });
270
289
} ) . catch ( reason => {
271
- console . error ( reason ) ;
290
+ console . error ( util . inspect ( reason , null , 4 ) ) ;
272
291
} ) . catch ( Promise . CancellationError , function ( reason ) {
273
292
// nothing to do
274
293
} ) ;
0 commit comments