Skip to content

Commit

Permalink
feat: connect discord-slash-command /task/update api (#2016)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Lakshay Manchanda <[email protected]>
  • Loading branch information
3 people authored Apr 23, 2024
1 parent 1f4d942 commit 4e3a6d4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
4 changes: 3 additions & 1 deletion controllers/progresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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}`,
Expand Down
9 changes: 9 additions & 0 deletions test/integration/progressesTasks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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`)
Expand Down
9 changes: 9 additions & 0 deletions test/integration/progressesUsers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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`)
Expand Down
33 changes: 33 additions & 0 deletions test/unit/utils/sendTaskUpdate.test.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
23 changes: 23 additions & 0 deletions utils/sendTaskUpdate.js
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit 4e3a6d4

Please sign in to comment.