Skip to content

Commit

Permalink
Merge pull request #33 from HDRUK/IG-230
Browse files Browse the repository at this point in the history
IG-230 - Adding discourse discussions to projects
  • Loading branch information
tonyespley-pa authored Jun 11, 2020
2 parents 2b68ead + 4e24119 commit 2e576c9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ [email protected]
DISCOURSE_API_KEY=
DISCOURSE_URL=
DISCOURSE_CATEGORY_TOOLS_ID=
DISCOURSE_CATEGORY_PROJECTS_ID=
DISCOURSE_CATEGORY_DATASETS_ID=
DISCOURSE_CATEGORY_PAPERS_ID=
DISCOURSE_SSO_SECRET=
```

Expand Down
6 changes: 4 additions & 2 deletions src/resources/account/account.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ router.get(
const { id, activeflag } = req.body;

try {
let tool = await Data.findOneAndUpdate({ id: id }, { $set: { activeflag: activeflag }});
if (!tool) {s
await Data.findOneAndUpdate({ id: id }, { $set: { activeflag: activeflag }});
const tool = await Data.findOne({ id: id });

if (!tool) {
return res.status(400).json({ success: false, error: 'Tool not found' });
}

Expand Down
25 changes: 22 additions & 3 deletions src/resources/discourse/discourse.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ export async function findPostsByTopicId(topicId) {
}

export async function createDiscourseTopic(tool) {

if (tool.discourseTopicId || tool.activeflag !== 'active') {
throw new Error('Topic already created or tool is not active');
throw new Error('Topic already created or object is not active');
}

const config = {
Expand All @@ -52,11 +53,29 @@ export async function createDiscourseTopic(tool) {
'Content-Type': 'application/json',
},
};

var rawIs, categoryIs;
if (tool.type === 'tool') {
rawIs = `${tool.description} <br> Original content: ${process.env.homeURL}/tool/${tool.id}`;
categoryIs = process.env.DISCOURSE_CATEGORY_TOOLS_ID;
}
else if (tool.type === 'project') {
rawIs = `${tool.description} <br> Original content: ${process.env.homeURL}/project/${tool.id}`;
categoryIs = process.env.DISCOURSE_CATEGORY_PROJECTS_ID;
}
else if (tool.type === 'dataset') {
rawIs = `${tool.description} <br> Original content: ${process.env.homeURL}/dataset/${tool.id}`;
categoryIs = process.env.DISCOURSE_CATEGORY_DATASETS_ID;
}
else if (tool.type === 'paper') {
rawIs = `${tool.description} <br> Original content: ${process.env.homeURL}/paper/${tool.id}`;
categoryIs = process.env.DISCOURSE_CATEGORY_PAPERS_ID;
}

const payload = {
title: tool.name,
raw: `${tool.description} <br> Original content: ${process.env.homeURL}/tool/${tool.id}`,
category: process.env.DISCOURSE_CATEGORY_TOOLS_ID,
raw: rawIs,
category: categoryIs
};

try {
Expand Down
11 changes: 9 additions & 2 deletions src/resources/project/project.route.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from 'express'
import { Data } from '../tool/data.model'
import { findPostsByTopicId } from "../discourse/discourse.service";

const router = express.Router();

Expand All @@ -13,9 +14,15 @@ router.get('/:projectID', async (req, res) => {
{ $match: { $and: [{ id: parseInt(req.params.projectID) }] } },
{ $lookup: { from: "tools", localField: "authors", foreignField: "id", as: "persons" } }
]);
q.exec((err, data) => {
q.exec(async (err, data) => {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true, data: data });

let discourseTopic = {};
if (data[0].discourseTopicId) {
discourseTopic = await findPostsByTopicId(data[0].discourseTopicId);
}

return res.json({ success: true, data: data, discourseTopic: discourseTopic });
});
});

Expand Down

0 comments on commit 2e576c9

Please sign in to comment.