Skip to content

Commit 824c5d1

Browse files
committed
Completed
1 parent 1c1c6bd commit 824c5d1

File tree

1 file changed

+52
-33
lines changed

1 file changed

+52
-33
lines changed

asana.js

+52-33
Original file line numberDiff line numberDiff line change
@@ -119,44 +119,57 @@ fs.readJson(opts.config).then(config => {
119119
});
120120
}
121121

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+
};
141125

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 = {
144131
assignee: !_.isEmpty(data.assignee) ? data.assignee.id : null,
145132
followers: !_.isEmpty(data.followers) ? _.pluck(data.followers, 'id') : null,
146133
name: data.name,
147134
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 => {
153156
const task = result;
154157
const promises = [];
155158

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+
156167
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 => {
160173
return uploadImageToAsana(task.id, image, attachment.name);
161174
}).catch(reason => {
162175
console.log('Failed to upload attachment', reason);
@@ -166,7 +179,7 @@ fs.readJson(opts.config).then(config => {
166179
}
167180

168181
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 => {
170183
return Promise.mapSeries(result, story => {
171184
return client.tasks.addComment(task.id, {
172185
text: `${story.created_by.name}: ${story.text}`
@@ -175,12 +188,18 @@ fs.readJson(opts.config).then(config => {
175188
}));
176189
}
177190

178-
return Promise.all(promises);
191+
if (!_.isEmpty(promises)) {
192+
return Promise.all(promises);
193+
} else {
194+
return;
195+
}
179196
});
180197
};
181198

182199
return Promise.mapSeries(result, item => {
183-
console.log(item);
200+
return createTask(item).then(() => {
201+
console.log(`${item.id} task copied.`);
202+
});
184203
});
185204
});
186205

@@ -268,7 +287,7 @@ fs.readJson(opts.config).then(config => {
268287
// });
269288
// });
270289
}).catch(reason => {
271-
console.error(reason);
290+
console.error(util.inspect(reason, null, 4));
272291
}).catch(Promise.CancellationError, function (reason) {
273292
// nothing to do
274293
});

0 commit comments

Comments
 (0)