Skip to content

Commit c3fe4ec

Browse files
committed
Improve volunteering for a task
1 parent b652823 commit c3fe4ec

File tree

5 files changed

+96
-22
lines changed

5 files changed

+96
-22
lines changed

.vscode/launch.json

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "attach",
10+
"name": "Attach by Process ID",
11+
"processId": "${command:PickProcess}"
12+
},
713
{
814
"type": "node",
915
"request": "launch",

client/src/components/Gigs/API/RocketChat/RocketChat.jsx

+18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ export const publishMessage = function(payload) {
2121
});
2222
};
2323

24+
export const publishMessageWithReplyOptions = async function(payload) {
25+
const authSet = UserProfile.getAuthSet();
26+
const response = await fetch(`/admin-ui/api/rc/publish-replyable-message`, {
27+
method: "POST",
28+
headers: {
29+
"Content-Type": "application/json",
30+
"x-auth-token": authSet.token,
31+
"x-user-id": authSet.userId
32+
},
33+
body: JSON.stringify(payload)
34+
});
35+
36+
if (response.status !== 200) {
37+
const json = await response.json();
38+
NotificationManager.error(json.error.errmsg);
39+
}
40+
};
41+
2442
export const setRoomToReadOnly = function(gigRoomId) {
2543
const authSet = UserProfile.getAuthSet();
2644
const setReadOnlyPayload = {

client/src/components/Gigs/API/Tasks/index.js

+31-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NotificationManager } from "react-notifications";
2-
import { publishMessage } from "components/Gigs/API/RocketChat/RocketChat";
2+
import { publishMessageWithReplyOptions } from "components/Gigs/API/RocketChat/RocketChat";
33

44
const fetchOptions = method => {
55
return {
@@ -50,26 +50,27 @@ export const update = function(taskId, payload, statusCallback) {
5050
});
5151
};
5252

53-
export const add = function(gigRoomId, gigId, state, statusCallback) {
53+
export const add = async function(gigRoomId, gigId, state, statusCallback) {
5454
if (statusCallback) statusCallback("loading");
55-
fetch("/admin-ui/api/tasks/addTask", {
55+
const data = await fetch("/admin-ui/api/tasks/addTask", {
5656
method: "POST",
5757
headers: { "Content-Type": "application/json" },
5858
body: JSON.stringify(buildTaskPayload(gigId, state))
59-
}).then(data => {
60-
if (data.status !== 200) {
61-
data.json().then(json => {
62-
NotificationManager.error(json.error.errmsg);
63-
});
64-
if (statusCallback) statusCallback("working");
65-
} else {
66-
data.json().then(json => {
67-
const task = json.task;
68-
publishMessage(buildTaskPublishMessage(gigRoomId, task));
69-
});
70-
if (statusCallback) statusCallback("success");
71-
}
7259
});
60+
61+
const json = await data.json();
62+
if (data.status !== 200) {
63+
NotificationManager.error(json.error.errmsg);
64+
//TODO: should this be error?
65+
if (statusCallback) statusCallback("working");
66+
return;
67+
}
68+
69+
const task = json.task;
70+
publishMessageWithReplyOptions(
71+
buildTaskPublishMessageWithReply(gigRoomId, task)
72+
);
73+
if (statusCallback) statusCallback("success");
7374
};
7475

7576
export const remove = function(gigRoomId, taskId, taskName, statusCallback) {
@@ -91,13 +92,21 @@ export const remove = function(gigRoomId, taskId, taskName, statusCallback) {
9192
});
9293
};
9394

94-
function buildTaskPublishMessage(roomId, task) {
95-
const message = `New Task!
96-
**${task.task_name}** (${task._id})
97-
Description: ${task.task_description}
98-
Reply "volunteer" to volunteer for this task.`;
95+
function buildTaskPublishMessageWithReply(roomId, task) {
96+
const message = `A new task **${task.task_name}** in category *${
97+
task.task_category
98+
}* has been created.
99+
_${task.task_description}_`;
100+
101+
const optionsTitle = "Please click volunteer if you can help";
102+
const replyOptions = [
103+
{
104+
label: "Volunteer",
105+
reply: `I would like to volunteer for **${task.task_name}** (${task._id})`
106+
}
107+
];
99108

100-
return { message, roomId };
109+
return { message, roomId, optionsTitle, replyOptions };
101110
}
102111

103112
function buildTaskPayload(gigId, state) {

server/controllers/rc.controller.js

+37
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,43 @@ exports.publish_message = async function(req, res) {
115115
});
116116
};
117117

118+
exports.publishMessageWithReplyOptions = asyncMiddleware(async function(
119+
req,
120+
res
121+
) {
122+
const authSetBot = getCachedApiAuth(req);
123+
const data = await api.post(
124+
"chat.postMessage",
125+
authSetBot.authToken,
126+
authSetBot.userId,
127+
{
128+
roomId: req.body.roomId,
129+
text: req.body.message,
130+
attachments: [
131+
{
132+
title: req.body.optionsTitle,
133+
collapsed: false,
134+
actions: req.body.replyOptions.map(x => ({
135+
type: "button",
136+
text: x.label,
137+
msg: x.reply,
138+
msg_in_chat_window: true
139+
}))
140+
}
141+
]
142+
}
143+
);
144+
145+
if (!data.success) {
146+
return res.status(500).send({
147+
error: "Unable to publish message to " + req.body.roomId
148+
});
149+
}
150+
res.status(200).send({
151+
message: data.message
152+
});
153+
});
154+
118155
exports.publishBroadcastMessage = async function(authSetBot, roomId, text) {
119156
const result = await api.post(
120157
"chat.postMessage",

server/routes/rc.route.js

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ const rc_controller = require("../controllers/rc.controller");
66
router.post("/set_read_only_channel", rc_controller.set_read_only_channel);
77
router.post("/set_group_type", rc_controller.set_group_type);
88
router.post("/publish_message", rc_controller.publish_message);
9+
router.post(
10+
"/publish-replyable-message",
11+
rc_controller.publishMessageWithReplyOptions
12+
);
913
router.post("/kick_user", rc_controller.kick_user);
1014
router.post("/remove_owner_from_group", rc_controller.remove_owner_from_group);
1115

0 commit comments

Comments
 (0)