Skip to content

Commit

Permalink
Update changeCommentStatus method
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-nebel committed Dec 2, 2023
1 parent ba02724 commit 5164a13
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/modules/apiRequests/apiRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ const TEACHER = [
exec: [(req, res) => getApiRequest("changeCommentStatus", { req, res })],
},
{
name: "changeCommentStatus",
name: "newPayment",
method: "post",
path: "/changeCommentStatus",
exec: [(req, res) => getApiRequest("changeCommentStatus", { req, res })],
path: "/newPayment",
exec: [(req, res) => getApiRequest("newPayment", { req, res })],
},
]

Expand Down
36 changes: 27 additions & 9 deletions src/modules/apiRequests/changeCommentStatus/changeCommentStatus.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
const { log } = require("../../../services/logger/logger");
const { db } = require("../../dbRequests/mongo");
const { generateMessage } = require("../../../utils/messageGenerator");

function changeCommentStatus({ userId, taskId, status }) {
return db.STATE.findOneAndUpdate(
{ userId, taskId },
{
$set: {
"comments.0.readedByTeacher": status,
async function changeCommentStatus({ req, res }) {

const {userId, taskId, status = false} = req.body

try {
await db.STATE.findOneAndUpdate(
{ userId, taskId },
{
$set: {
"comments.0.readedByTeacher": status,
},
},
},
{ upsert: true, returnDocument: "after", returnNewDocument: true }
);
{ upsert: true, returnDocument: "after", returnNewDocument: true }
);

const data = generateMessage(0);
res.status(200).send(data);

return data;
} catch (e) {
log.warn(`Error with updating comment status for user ${userId} and task ${taskId}`);
log.warn(e);
const error = generateMessage(20115);
res.status(400).send(error);
}


}

module.exports.changeCommentStatus = changeCommentStatus;
4 changes: 2 additions & 2 deletions src/services/express/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { PUBLIC, TEACHER } = require("../../modules/apiRequests/apiRequests");
const { STUDENT } = require("../../API/student/student");
const { responseHandler, pathHandler } = require("./responses");
const prepareRequestData = require("./prepareRequestData");
const { checkAuth } = require("./security");
const { checkAuth, checkAdmin } = require("./security");
const { paramsProcessor } = require("../../utils/validate");

const { SERVER_PORT = 8888, ORIGIN = "*" } = process.env;
Expand Down Expand Up @@ -40,7 +40,7 @@ for (const request of PUBLIC) {

const oldTeacher = express.Router();
app.use("/v2/teacher", oldTeacher);
oldTeacher.use(checkAuth);
oldTeacher.use(checkAdmin);
for (const request of TEACHER) {
const { path, method, exec } = request;
switch (method) {
Expand Down
7 changes: 6 additions & 1 deletion src/utils/messageGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,14 @@ const ERRORS = [
type: "process_failure",
description: "Error with getting counselor content",
},
{
code: 20117,
type: "process_failure",
description: "Error with updating comment status",
},
];

function generateMessage(code, data = {}) {
function generateMessage(code, data) {
if (code === 0) {
return { OK: true, data };
} else {
Expand Down

0 comments on commit 5164a13

Please sign in to comment.