Skip to content

Commit

Permalink
Merge pull request #102 from drodil/delete_events
Browse files Browse the repository at this point in the history
feat: add events from deleting questions and answers
  • Loading branch information
drodil authored Oct 16, 2023
2 parents 0acc1e7 + fdd5d01 commit 72f690b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ Events are published in topic `qeta`. The following events are published:
- Vote answer
- payload: question, answer, author, score
- metadata.action = `vote_answer`
- Delete question
- payload: question, author
- metadata.action = `delete_question`
- Delete answer
- payload: question, answer, author
- metadata.action = `delete_answer`
26 changes: 24 additions & 2 deletions plugins/qeta-backend/src/service/routes/answers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const answersRoutes = (router: Router, options: RouterOptions) => {
// Validation
const username = await getUsername(request, options);
const moderator = await isModerator(request, options);

// Act
const answer = await database.deleteAnswerComment(
Number.parseInt(request.params.answerId, 10),
Expand Down Expand Up @@ -220,11 +221,32 @@ export const answersRoutes = (router: Router, options: RouterOptions) => {
async (request, response) => {
// Validation
const moderator = await isModerator(request, options);
const username = await getUsername(request, options);
const answerId = Number.parseInt(request.params.answerId, 10);

if (eventBroker) {
const questionId = Number.parseInt(request.params.id, 10);
const question = await database.getQuestion(
username,
questionId,
false,
);
const answer = await database.getAnswer(answerId, username);
await eventBroker.publish({
topic: 'qeta',
eventPayload: {
question,
answer,
author: username,
},
metadata: { action: 'delete_answer' },
});
}

// Act
const deleted = await database.deleteAnswer(
await getUsername(request, options),
Number.parseInt(request.params.answerId, 10),
username,
answerId,
moderator,
);

Expand Down
29 changes: 27 additions & 2 deletions plugins/qeta-backend/src/service/routes/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,17 @@ export const questionsRoutes = (router: Router, options: RouterOptions) => {
return;
}

if (eventBroker) {
await eventBroker.publish({
topic: 'qeta',
eventPayload: {
question,
author: username,
},
metadata: { action: 'update_question' },
});
}

// Response
response.status(200);
response.send(question);
Expand All @@ -301,11 +312,25 @@ export const questionsRoutes = (router: Router, options: RouterOptions) => {
router.delete('/questions/:id', async (request, response) => {
// Validation
const moderator = await isModerator(request, options);
const username = await getUsername(request, options);
const questionId = Number.parseInt(request.params.id, 10);

if (eventBroker) {
const question = database.getQuestion(username, questionId, false);
await eventBroker.publish({
topic: 'qeta',
eventPayload: {
question,
author: username,
},
metadata: { action: 'delete_question' },
});
}

// Act
const deleted = await database.deleteQuestion(
await getUsername(request, options),
Number.parseInt(request.params.id, 10),
username,
questionId,
moderator,
);

Expand Down

0 comments on commit 72f690b

Please sign in to comment.