Skip to content

Commit 4e3a6d4

Browse files
tejaskh3lakshayman
andauthored
feat: connect discord-slash-command /task/update api (#2016)
* feat: connect discord-slash-command /task/update api * remove: failing test cases * Tests fixed * Tests fixed * feat: add test cases for sendTaskUpdate * refactor * fix parameteres * fix: test case coverage * refactor: fetchStub to fetchMock --------- Co-authored-by: Lakshay Manchanda <lakshaymanchanda73@gmail.com> Co-authored-by: Lakshay Manchanda <45519620+lakshayman@users.noreply.github.com>
1 parent 1f4d942 commit 4e3a6d4

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

controllers/progresses.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
getProgressByDate,
77
} = require("../models/progresses");
88
const { PROGRESSES_RESPONSE_MESSAGES, INTERNAL_SERVER_ERROR_MESSAGE } = require("../constants/progresses");
9+
const { sendTaskUpdate } = require("../utils/sendTaskUpdate");
910
const { PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED, PROGRESS_DOCUMENT_CREATED_SUCCEEDED } = PROGRESSES_RESPONSE_MESSAGES;
1011

1112
/**
@@ -45,10 +46,11 @@ const { PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED, PROGRESS_DOCUMENT_CREATED_SUCCEED
4546

4647
const createProgress = async (req, res) => {
4748
const {
48-
body: { type },
49+
body: { type, completed, planned, blockers },
4950
} = req;
5051
try {
5152
const data = await createProgressDocument({ ...req.body, userId: req.userData.id });
53+
await sendTaskUpdate(completed, blockers, planned);
5254
return res.status(201).json({
5355
data,
5456
message: `${type.charAt(0).toUpperCase() + type.slice(1)} ${PROGRESS_DOCUMENT_CREATED_SUCCEEDED}`,

test/integration/progressesTasks.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ describe("Test Progress Updates API for Tasks", function () {
3131
let userToken;
3232
let taskId1;
3333
let taskId2;
34+
let fetchMock;
3435

3536
beforeEach(async function () {
37+
fetchMock = sinon.stub(global, "fetch");
3638
clock = sinon.useFakeTimers({
3739
now: new Date(Date.UTC(2023, 4, 2, 0, 25)).getTime(), // UTC time equivalent to 5:55 AM IST
3840
toFake: ["Date"],
@@ -49,10 +51,17 @@ describe("Test Progress Updates API for Tasks", function () {
4951
});
5052

5153
afterEach(function () {
54+
sinon.restore();
5255
clock.restore();
5356
});
5457

5558
it("Stores the task progress entry", function (done) {
59+
fetchMock.returns(
60+
Promise.resolve({
61+
status: 200,
62+
json: () => Promise.resolve({}),
63+
})
64+
);
5665
chai
5766
.request(app)
5867
.post(`/progresses`)

test/integration/progressesUsers.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ describe("Test Progress Updates API for Users", function () {
2929
let userToken;
3030
let anotherUserId;
3131
let anotherUserToken;
32+
let fetchMock;
3233

3334
beforeEach(async function () {
35+
fetchMock = sinon.stub(global, "fetch");
3436
clock = sinon.useFakeTimers({
3537
now: new Date(Date.UTC(2023, 4, 2, 0, 25)).getTime(), // UTC time equivalent to 5:55 AM IST
3638
toFake: ["Date"],
@@ -44,10 +46,17 @@ describe("Test Progress Updates API for Users", function () {
4446
});
4547

4648
afterEach(function () {
49+
sinon.restore();
4750
clock.restore();
4851
});
4952

5053
it("stores the user progress document", function (done) {
54+
fetchMock.returns(
55+
Promise.resolve({
56+
status: 200,
57+
json: () => Promise.resolve({}),
58+
})
59+
);
5160
chai
5261
.request(app)
5362
.post(`/progresses`)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import chai from "chai";
2+
import sinon from "sinon";
3+
import { sendTaskUpdate } from "../../../utils/sendTaskUpdate";
4+
const { expect } = chai;
5+
6+
describe("sendTaskUpdate function", function () {
7+
let fetchMock;
8+
9+
beforeEach(function () {
10+
fetchMock = sinon.stub(global, "fetch");
11+
});
12+
13+
afterEach(function () {
14+
fetchMock.restore();
15+
});
16+
17+
it("should send task update successfully", async function () {
18+
fetchMock.resolves({ ok: true });
19+
20+
const result = await sendTaskUpdate("Task completed", "No blockers", "Plan for the next phase");
21+
expect(result).to.equal(undefined);
22+
});
23+
24+
it("should throw an error if fails", async function () {
25+
const error = new Error("Error");
26+
fetchMock.rejects(error);
27+
try {
28+
await sendTaskUpdate("Task completed", "No blockers", "Plan for the next phase");
29+
} catch (err) {
30+
expect(err).to.be.equal(error);
31+
}
32+
});
33+
});

utils/sendTaskUpdate.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { generateCloudFlareHeaders } from "../utils/discord-actions.js";
2+
const DISCORD_BASE_URL = config.get("services.discordBot.baseUrl");
3+
4+
export const sendTaskUpdate = async (completed, blockers, planned) => {
5+
try {
6+
const headers = generateCloudFlareHeaders();
7+
const body = {
8+
content: {
9+
completed,
10+
blockers,
11+
planned,
12+
},
13+
};
14+
await fetch(`${DISCORD_BASE_URL}/task/update`, {
15+
method: "POST",
16+
headers,
17+
body: JSON.stringify(body),
18+
});
19+
} catch (error) {
20+
logger.error("Something went wrong", error);
21+
throw error;
22+
}
23+
};

0 commit comments

Comments
 (0)