diff --git a/controllers/progresses.js b/controllers/progresses.js index 68584f2e1..7dd658d45 100644 --- a/controllers/progresses.js +++ b/controllers/progresses.js @@ -6,6 +6,7 @@ const { getProgressByDate, } = require("../models/progresses"); const { PROGRESSES_RESPONSE_MESSAGES, INTERNAL_SERVER_ERROR_MESSAGE } = require("../constants/progresses"); +const { sendTaskUpdate } = require("../utils/sendTaskUpdate"); const { PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED, PROGRESS_DOCUMENT_CREATED_SUCCEEDED } = PROGRESSES_RESPONSE_MESSAGES; /** @@ -45,10 +46,11 @@ const { PROGRESS_DOCUMENT_RETRIEVAL_SUCCEEDED, PROGRESS_DOCUMENT_CREATED_SUCCEED const createProgress = async (req, res) => { const { - body: { type }, + body: { type, completed, planned, blockers }, } = req; try { const data = await createProgressDocument({ ...req.body, userId: req.userData.id }); + await sendTaskUpdate(completed, blockers, planned); return res.status(201).json({ data, message: `${type.charAt(0).toUpperCase() + type.slice(1)} ${PROGRESS_DOCUMENT_CREATED_SUCCEEDED}`, diff --git a/test/integration/progressesTasks.test.js b/test/integration/progressesTasks.test.js index cc09e5499..704c534db 100644 --- a/test/integration/progressesTasks.test.js +++ b/test/integration/progressesTasks.test.js @@ -31,8 +31,10 @@ describe("Test Progress Updates API for Tasks", function () { let userToken; let taskId1; let taskId2; + let fetchMock; beforeEach(async function () { + fetchMock = sinon.stub(global, "fetch"); clock = sinon.useFakeTimers({ now: new Date(Date.UTC(2023, 4, 2, 0, 25)).getTime(), // UTC time equivalent to 5:55 AM IST toFake: ["Date"], @@ -49,10 +51,17 @@ describe("Test Progress Updates API for Tasks", function () { }); afterEach(function () { + sinon.restore(); clock.restore(); }); it("Stores the task progress entry", function (done) { + fetchMock.returns( + Promise.resolve({ + status: 200, + json: () => Promise.resolve({}), + }) + ); chai .request(app) .post(`/progresses`) diff --git a/test/integration/progressesUsers.test.js b/test/integration/progressesUsers.test.js index 9d1025678..e70d5d317 100644 --- a/test/integration/progressesUsers.test.js +++ b/test/integration/progressesUsers.test.js @@ -29,8 +29,10 @@ describe("Test Progress Updates API for Users", function () { let userToken; let anotherUserId; let anotherUserToken; + let fetchMock; beforeEach(async function () { + fetchMock = sinon.stub(global, "fetch"); clock = sinon.useFakeTimers({ now: new Date(Date.UTC(2023, 4, 2, 0, 25)).getTime(), // UTC time equivalent to 5:55 AM IST toFake: ["Date"], @@ -44,10 +46,17 @@ describe("Test Progress Updates API for Users", function () { }); afterEach(function () { + sinon.restore(); clock.restore(); }); it("stores the user progress document", function (done) { + fetchMock.returns( + Promise.resolve({ + status: 200, + json: () => Promise.resolve({}), + }) + ); chai .request(app) .post(`/progresses`) diff --git a/test/unit/utils/sendTaskUpdate.test.js b/test/unit/utils/sendTaskUpdate.test.js new file mode 100644 index 000000000..5302fa86c --- /dev/null +++ b/test/unit/utils/sendTaskUpdate.test.js @@ -0,0 +1,33 @@ +import chai from "chai"; +import sinon from "sinon"; +import { sendTaskUpdate } from "../../../utils/sendTaskUpdate"; +const { expect } = chai; + +describe("sendTaskUpdate function", function () { + let fetchMock; + + beforeEach(function () { + fetchMock = sinon.stub(global, "fetch"); + }); + + afterEach(function () { + fetchMock.restore(); + }); + + it("should send task update successfully", async function () { + fetchMock.resolves({ ok: true }); + + const result = await sendTaskUpdate("Task completed", "No blockers", "Plan for the next phase"); + expect(result).to.equal(undefined); + }); + + it("should throw an error if fails", async function () { + const error = new Error("Error"); + fetchMock.rejects(error); + try { + await sendTaskUpdate("Task completed", "No blockers", "Plan for the next phase"); + } catch (err) { + expect(err).to.be.equal(error); + } + }); +}); diff --git a/utils/sendTaskUpdate.js b/utils/sendTaskUpdate.js new file mode 100644 index 000000000..d990725c3 --- /dev/null +++ b/utils/sendTaskUpdate.js @@ -0,0 +1,23 @@ +import { generateCloudFlareHeaders } from "../utils/discord-actions.js"; +const DISCORD_BASE_URL = config.get("services.discordBot.baseUrl"); + +export const sendTaskUpdate = async (completed, blockers, planned) => { + try { + const headers = generateCloudFlareHeaders(); + const body = { + content: { + completed, + blockers, + planned, + }, + }; + await fetch(`${DISCORD_BASE_URL}/task/update`, { + method: "POST", + headers, + body: JSON.stringify(body), + }); + } catch (error) { + logger.error("Something went wrong", error); + throw error; + } +};