Skip to content

Commit c2287e9

Browse files
committed
refactor code
1 parent a474cd0 commit c2287e9

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

controllers/tasks.js

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,15 @@ const addNewTask = async (req, res) => {
2828
taskId,
2929
dependsOn,
3030
};
31-
if (data.dependsOn) {
32-
const taskDependency = await dependencyModel.addDependency(data);
33-
return res.json({
34-
message: "Task created successfully!",
35-
task: {
36-
...taskDetails,
37-
dependsOn: taskDependency,
38-
id: taskId,
39-
},
40-
});
41-
} else {
42-
return res.json({
43-
message: "Task created successfully!",
44-
task: {
45-
...taskDetails,
46-
id: taskId,
47-
},
48-
});
49-
}
31+
const taskDependency = dependsOn && (await dependencyModel.addDependency(data));
32+
return res.json({
33+
message: "Task created successfully!",
34+
task: {
35+
...taskDetails,
36+
...(taskDependency && { dependsOn: taskDependency }),
37+
id: taskId,
38+
},
39+
});
5040
} catch (err) {
5141
logger.error(`Error while creating new task: ${err}`);
5242
return res.boom.badImplementation(INTERNAL_SERVER_ERROR);

models/tasks.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const addDependency = async (data) => {
4343
try {
4444
const { taskId, dependsOn } = data;
4545
const batch = firestore.batch();
46+
if (dependsOn.length > 500) {
47+
throw new Error("Error cannot add more than 500 taskId");
48+
}
4649
for (const dependsId of dependsOn) {
4750
const taskDependOn = {
4851
taskId,

test/integration/tasks.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ describe("Tasks", function () {
106106
expect(res).to.have.status(200);
107107
expect(res.body).to.be.a("object");
108108
expect(res.body.message).to.equal("Task created successfully!");
109-
expect(res.body.task.id).to.be.a("string");
110109
expect(res.body.task).to.be.a("object");
110+
expect(res.body.task.id).to.be.a("string");
111111
expect(res.body.task.createdBy).to.equal(appOwner.username);
112112
expect(res.body.task.assignee).to.equal(appOwner.username);
113113
expect(res.body.task.participants).to.be.a("array");

0 commit comments

Comments
 (0)