Skip to content

Commit 26672d9

Browse files
Add createdAt and updatedAt fields for task collection (#1710)
* refactor: add createdAt and updatedAt for task add/update * refactor: add createdAt and updatedAt * test: add tests
1 parent 3c7c582 commit 26672d9

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

controllers/tasks.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ const addNewTask = async (req, res) => {
2424
const { id: createdBy } = req.userData;
2525
const dependsOn = req.body.dependsOn;
2626
let userStatusUpdate;
27+
const timeStamp = Math.round(Date.now() / 1000);
2728
const body = {
2829
...req.body,
2930
createdBy,
31+
createdAt: timeStamp,
32+
updatedAt: timeStamp,
3033
};
3134
delete body.dependsOn;
3235
const { taskId, taskDetails } = await tasks.updateTask(body);
@@ -270,7 +273,7 @@ const updateTask = async (req, res) => {
270273
if (!task.taskData) {
271274
return res.boom.notFound("Task not found");
272275
}
273-
const requestData = { ...req.body };
276+
const requestData = { ...req.body, updatedAt: Math.round(Date.now() / 1000) };
274277
if (requestData?.assignee) {
275278
const user = await dataAccess.retrieveUsers({ username: requestData.assignee });
276279
if (!user.userExists) {
@@ -310,6 +313,7 @@ const updateTask = async (req, res) => {
310313
*/
311314
const updateTaskStatus = async (req, res, next) => {
312315
try {
316+
req.body.updatedAt = Math.round(new Date().getTime() / 1000);
313317
let userStatusUpdate;
314318
const taskId = req.params.id;
315319
const { id: userId, username } = req.userData;

test/fixtures/tasks/tasks.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ module.exports = () => {
1414
type: "feature",
1515
assignee: "akshay",
1616
createdBy: "ankush",
17+
createdAt: 1644753600,
18+
updatedAt: 1644753600,
1719
status: "IN_PROGRESS",
1820
percentCompleted: 50,
1921
endsOn: 1647172800, // 13 march
@@ -25,6 +27,8 @@ module.exports = () => {
2527
type: "feature",
2628
assignee: "ankur",
2729
createdBy: "ankush",
30+
createdAt: 1644753600,
31+
updatedAt: 1644753600,
2832
status: "ASSIGNED",
2933
percentCompleted: 50,
3034
endsOn: 1647172800, // 13 march
@@ -36,6 +40,8 @@ module.exports = () => {
3640
type: "feature",
3741
assignee: "ankur",
3842
createdBy: "ankush",
43+
createdAt: 1644753600,
44+
updatedAt: 1644753600,
3945
status: "BLOCKED",
4046
percentCompleted: 50,
4147
endsOn: 1647172800,
@@ -51,6 +57,8 @@ module.exports = () => {
5157
percentCompleted: 50,
5258
endsOn: 1650032259,
5359
startedOn: 1644753600,
60+
createdAt: 1644753600,
61+
updatedAt: 1644753600,
5462
},
5563
{
5664
title: "Test task",
@@ -67,6 +75,8 @@ module.exports = () => {
6775
priority: "HIGH",
6876
isNoteworthy: true,
6977
assignee: false,
78+
createdAt: 1644753600,
79+
updatedAt: 1644753600,
7080
},
7181
{
7282
title: "Test task-dependency",
@@ -80,6 +90,8 @@ module.exports = () => {
8090
lossRate: { [DINERO]: 1 },
8191
isNoteworthy: true,
8292
assignee: false,
93+
createdAt: 1644753600,
94+
updatedAt: 1644753600,
8395
},
8496
{
8597
id: "P86Y1hVrS0zR5ZcVPwLZ",
@@ -102,6 +114,8 @@ module.exports = () => {
102114
status: "IN_PROGRESS",
103115
assigneeId: "WbceXEPcohuJ5IxHHecf",
104116
dependsOn: [],
117+
createdAt: 1644753600,
118+
updatedAt: 1644753600,
105119
},
106120
{
107121
id: "P86Y1fsvS0zR5ZcVPwLZ",
@@ -123,6 +137,8 @@ module.exports = () => {
123137
title: "Undefined status",
124138
assigneeId: "WbceXEPcdsuJ5IxHHecf",
125139
dependsOn: [],
140+
createdAt: 1644753600,
141+
updatedAt: 1644753600,
126142
},
127143
];
128144
};

test/integration/tasks.test.js

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,48 @@ describe("Tasks", function () {
112112
expect(res.body.message).to.equal("Task created successfully!");
113113
expect(res.body.task).to.be.a("object");
114114
expect(res.body.task.id).to.be.a("string");
115+
expect(res.body.task.createdAt).to.be.a("number");
116+
expect(res.body.task.updatedAt).to.be.a("number");
115117
expect(res.body.task.createdBy).to.equal(appOwner.username);
116118
expect(res.body.task.assignee).to.equal(appOwner.username);
117119
expect(res.body.task.participants).to.be.a("array");
118120
expect(res.body.task.dependsOn).to.be.a("array");
119121
return done();
120122
});
121123
});
124+
it("Should have same time for createdAt and updatedAt for new tasks", function (done) {
125+
chai
126+
.request(app)
127+
.post("/tasks")
128+
.set("cookie", `${cookieName}=${jwt}`)
129+
.send({
130+
title: "Test task - Create",
131+
type: "feature",
132+
endsOn: 123,
133+
startedOn: 456,
134+
status: "AVAILABLE",
135+
percentCompleted: 10,
136+
priority: "HIGH",
137+
completionAward: { [DINERO]: 3, [NEELAM]: 300 },
138+
lossRate: { [DINERO]: 1 },
139+
assignee: appOwner.username,
140+
participants: [],
141+
dependsOn: [],
142+
})
143+
.end((err, res) => {
144+
if (err) {
145+
return done(err);
146+
}
147+
expect(res).to.have.status(200);
148+
expect(res.body).to.be.a("object");
149+
expect(res.body.message).to.equal("Task created successfully!");
150+
expect(res.body.task).to.be.a("object");
151+
expect(res.body.task.createdAt).to.be.a("number");
152+
expect(res.body.task.updatedAt).to.be.a("number");
153+
expect(res.body.task.createdAt).to.be.eq(res.body.task.updatedAt);
154+
return done();
155+
});
156+
});
122157
it("should return fail response if task has a non-acceptable status value", function (done) {
123158
chai
124159
.request(app)
@@ -374,7 +409,7 @@ describe("Tasks", function () {
374409
matchingTasks.forEach((task) => {
375410
expect(task.title.toLowerCase()).to.include(searchTerm.toLowerCase());
376411
});
377-
expect(matchingTasks).to.have.length(3);
412+
expect(matchingTasks).to.have.length(4);
378413

379414
return done();
380415
});
@@ -535,7 +570,7 @@ describe("Tasks", function () {
535570
});
536571

537572
describe("PATCH /tasks", function () {
538-
it("Should update the task for the given taskid", function (done) {
573+
it("Should update the task for the given taskId", function (done) {
539574
chai
540575
.request(app)
541576
.patch("/tasks/" + taskId1)
@@ -551,6 +586,41 @@ describe("Tasks", function () {
551586
return done();
552587
});
553588
});
589+
590+
it("should update updatedAt field when patch request is made", function (done) {
591+
chai
592+
.request(app)
593+
.patch("/tasks/" + taskId1)
594+
.set("cookie", `${cookieName}=${jwt}`)
595+
.send({
596+
title: "new-title",
597+
})
598+
.end((err, res) => {
599+
if (err) {
600+
return done(err);
601+
}
602+
expect(res).to.have.status(204);
603+
return done();
604+
});
605+
});
606+
607+
it("should update updatedAt field", function (done) {
608+
chai
609+
.request(app)
610+
.get(`/tasks/${taskId1}/details`)
611+
.end((err, res) => {
612+
if (err) {
613+
return done(err);
614+
}
615+
expect(res).to.have.status(200);
616+
expect(res.body).to.be.a("object");
617+
expect(res.body.taskData.updatedAt).to.be.a("number");
618+
expect(res.body.taskData.updatedAt).to.be.not.eq(tasksData[0].updatedAt);
619+
expect(res.body.taskData.updatedAt).to.be.not.eq(res.body.taskData.createdAt);
620+
return done();
621+
});
622+
});
623+
554624
it("Should update dependency", async function () {
555625
taskId = (await tasks.updateTask(tasksData[5])).taskId;
556626
const taskId1 = (await tasks.updateTask(tasksData[5])).taskId;

0 commit comments

Comments
 (0)