|
@@ -41,7 +41,7 @@ CacheModule.runJob("SUB", {
|
|
|
|
|
|
export default {
|
|
export default {
|
|
/**
|
|
/**
|
|
- * Gets all news items
|
|
|
|
|
|
+ * Gets all news items that are published
|
|
*
|
|
*
|
|
* @param {object} session - the session object automatically added by the websocket
|
|
* @param {object} session - the session object automatically added by the websocket
|
|
* @param {Function} cb - gets called with the result
|
|
* @param {Function} cb - gets called with the result
|
|
@@ -51,7 +51,7 @@ export default {
|
|
async.waterfall(
|
|
async.waterfall(
|
|
[
|
|
[
|
|
next => {
|
|
next => {
|
|
- newsModel.find({}).sort({ createdAt: "desc" }).exec(next);
|
|
|
|
|
|
+ newsModel.find({ status: "published" }).sort({ createdAt: "desc" }).exec(next);
|
|
}
|
|
}
|
|
],
|
|
],
|
|
async (err, news) => {
|
|
async (err, news) => {
|
|
@@ -72,7 +72,7 @@ export default {
|
|
* Gets a news item by id
|
|
* Gets a news item by id
|
|
*
|
|
*
|
|
* @param {object} session - the session object automatically added by the websocket
|
|
* @param {object} session - the session object automatically added by the websocket
|
|
- * @param {string} newsId - the news id
|
|
|
|
|
|
+ * @param {string} newsId - the news item id
|
|
* @param {Function} cb - gets called with the result
|
|
* @param {Function} cb - gets called with the result
|
|
*/
|
|
*/
|
|
async getNewsFromId(session, newsId, cb) {
|
|
async getNewsFromId(session, newsId, cb) {
|
|
@@ -87,11 +87,11 @@ export default {
|
|
async (err, news) => {
|
|
async (err, news) => {
|
|
if (err) {
|
|
if (err) {
|
|
err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
- this.log("ERROR", "GET_NEWS_FROM_ID", `Getting news failed. "${err}"`);
|
|
|
|
|
|
+ this.log("ERROR", "GET_NEWS_FROM_ID", `Getting news item ${newsId} failed. "${err}"`);
|
|
return cb({ status: "error", message: err });
|
|
return cb({ status: "error", message: err });
|
|
}
|
|
}
|
|
|
|
|
|
- this.log("SUCCESS", "GET_NEWS_FROM_ID", `Got news successful.`, false);
|
|
|
|
|
|
+ this.log("SUCCESS", "GET_NEWS_FROM_ID", `Got news item ${newsId} successfully.`, false);
|
|
|
|
|
|
return cb({ status: "success", data: { news } });
|
|
return cb({ status: "success", data: { news } });
|
|
}
|
|
}
|
|
@@ -120,8 +120,11 @@ export default {
|
|
this.log("ERROR", "NEWS_CREATE", `Creating news failed. "${err}"`);
|
|
this.log("ERROR", "NEWS_CREATE", `Creating news failed. "${err}"`);
|
|
return cb({ status: "error", message: err });
|
|
return cb({ status: "error", message: err });
|
|
}
|
|
}
|
|
|
|
+
|
|
CacheModule.runJob("PUB", { channel: "news.create", value: news });
|
|
CacheModule.runJob("PUB", { channel: "news.create", value: news });
|
|
|
|
+
|
|
this.log("SUCCESS", "NEWS_CREATE", `Creating news successful.`);
|
|
this.log("SUCCESS", "NEWS_CREATE", `Creating news successful.`);
|
|
|
|
+
|
|
return cb({
|
|
return cb({
|
|
status: "success",
|
|
status: "success",
|
|
message: "Successfully created News"
|
|
message: "Successfully created News"
|
|
@@ -138,84 +141,110 @@ export default {
|
|
*/
|
|
*/
|
|
async newest(session, cb) {
|
|
async newest(session, cb) {
|
|
const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
|
|
const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
|
|
- async.waterfall(
|
|
|
|
- [
|
|
|
|
- next => {
|
|
|
|
- newsModel.findOne({}).sort({ createdAt: "desc" }).exec(next);
|
|
|
|
- }
|
|
|
|
- ],
|
|
|
|
- async (err, news) => {
|
|
|
|
- if (err) {
|
|
|
|
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
|
|
- this.log("ERROR", "NEWS_NEWEST", `Getting the latest news failed. "${err}"`);
|
|
|
|
- return cb({ status: "error", message: err });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- this.log("SUCCESS", "NEWS_NEWEST", `Successfully got the latest news.`, false);
|
|
|
|
- return cb({ status: "success", data: { news } });
|
|
|
|
|
|
+ async.waterfall([next => newsModel.findOne({}).sort({ createdAt: "desc" }).exec(next)], async (err, news) => {
|
|
|
|
+ if (err) {
|
|
|
|
+ err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
|
|
+ this.log("ERROR", "NEWS_NEWEST", `Getting the latest news failed. "${err}"`);
|
|
|
|
+ return cb({ status: "error", message: err });
|
|
}
|
|
}
|
|
- );
|
|
|
|
|
|
+
|
|
|
|
+ this.log("SUCCESS", "NEWS_NEWEST", `Successfully got the latest news.`, false);
|
|
|
|
+ return cb({ status: "success", data: { news } });
|
|
|
|
+ });
|
|
},
|
|
},
|
|
|
|
|
|
/**
|
|
/**
|
|
* Removes a news item
|
|
* Removes a news item
|
|
*
|
|
*
|
|
* @param {object} session - the session object automatically added by the websocket
|
|
* @param {object} session - the session object automatically added by the websocket
|
|
- * @param {object} newsId - the id of the news object we want to remove
|
|
|
|
|
|
+ * @param {object} newsId - the id of the news item we want to remove
|
|
* @param {Function} cb - gets called with the result
|
|
* @param {Function} cb - gets called with the result
|
|
*/
|
|
*/
|
|
remove: isAdminRequired(async function remove(session, newsId, cb) {
|
|
remove: isAdminRequired(async function remove(session, newsId, cb) {
|
|
const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
|
|
const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
|
|
|
|
|
|
- newsModel.deleteOne({ _id: newsId }, async err => {
|
|
|
|
- if (err) {
|
|
|
|
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
|
|
- this.log(
|
|
|
|
- "ERROR",
|
|
|
|
- "NEWS_REMOVE",
|
|
|
|
- `Removing news "${newsId}" failed for user "${session.userId}". "${err}"`
|
|
|
|
- );
|
|
|
|
- return cb({ status: "error", message: err });
|
|
|
|
- }
|
|
|
|
|
|
+ async.waterfall(
|
|
|
|
+ [
|
|
|
|
+ next => {
|
|
|
|
+ if (!newsId) return next("Please provide a news item id to update.");
|
|
|
|
+ return next();
|
|
|
|
+ },
|
|
|
|
|
|
- CacheModule.runJob("PUB", { channel: "news.remove", value: newsId });
|
|
|
|
|
|
+ next => {
|
|
|
|
+ newsModel.deleteOne({ _id: newsId }, err => next(err));
|
|
|
|
+ }
|
|
|
|
+ ],
|
|
|
|
+ async err => {
|
|
|
|
+ if (err) {
|
|
|
|
+ err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
|
|
+ this.log(
|
|
|
|
+ "ERROR",
|
|
|
|
+ "NEWS_REMOVE",
|
|
|
|
+ `Removing news "${newsId}" failed for user "${session.userId}". "${err}"`
|
|
|
|
+ );
|
|
|
|
+ return cb({ status: "error", message: err });
|
|
|
|
+ }
|
|
|
|
|
|
- this.log("SUCCESS", "NEWS_REMOVE", `Removing news "${newsId}" successful by user "${session.userId}".`);
|
|
|
|
|
|
+ CacheModule.runJob("PUB", { channel: "news.remove", value: newsId });
|
|
|
|
|
|
- return cb({
|
|
|
|
- status: "success",
|
|
|
|
- message: "Successfully removed News"
|
|
|
|
- });
|
|
|
|
- });
|
|
|
|
|
|
+ this.log("SUCCESS", "NEWS_REMOVE", `Removing news "${newsId}" successful by user "${session.userId}".`);
|
|
|
|
+
|
|
|
|
+ return cb({
|
|
|
|
+ status: "success",
|
|
|
|
+ message: "Successfully removed News"
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ );
|
|
}),
|
|
}),
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Removes a news item
|
|
|
|
|
|
+ * Updates a news item
|
|
*
|
|
*
|
|
* @param {object} session - the session object automatically added by the websocket
|
|
* @param {object} session - the session object automatically added by the websocket
|
|
- * @param {string} _id - the news id
|
|
|
|
- * @param {object} news - the news object
|
|
|
|
|
|
+ * @param {string} newsId - the id of the news item
|
|
|
|
+ * @param {object} item - the news item object
|
|
|
|
+ * @param {string} item.status - the status of the news e.g. published
|
|
|
|
+ * @param {string} item.title - taken from a level-1 heading at the top of the markdown
|
|
|
|
+ * @param {string} item.markdown - the markdown that forms the content of the news
|
|
* @param {Function} cb - gets called with the result
|
|
* @param {Function} cb - gets called with the result
|
|
*/
|
|
*/
|
|
- // TODO Fix this
|
|
|
|
- update: isAdminRequired(async function update(session, _id, news, cb) {
|
|
|
|
|
|
+ update: isAdminRequired(async function update(session, newsId, item, cb) {
|
|
const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
|
|
const newsModel = await DBModule.runJob("GET_MODEL", { modelName: "news" }, this);
|
|
- newsModel.updateOne({ _id }, news, { upsert: true }, async err => {
|
|
|
|
- if (err) {
|
|
|
|
- err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
|
|
|
|
+
|
|
|
|
+ async.waterfall(
|
|
|
|
+ [
|
|
|
|
+ next => {
|
|
|
|
+ if (!newsId) return next("Please provide a news item id to update.");
|
|
|
|
+ return next();
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ next => {
|
|
|
|
+ newsModel.updateOne({ _id: newsId }, item, { upsert: true }, err => next(err));
|
|
|
|
+ }
|
|
|
|
+ ],
|
|
|
|
+ async err => {
|
|
|
|
+ if (err) {
|
|
|
|
+ err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
|
|
|
|
+ this.log(
|
|
|
|
+ "ERROR",
|
|
|
|
+ "NEWS_UPDATE",
|
|
|
|
+ `Updating news item "${newsId}" failed for user "${session.userId}". "${err}"`
|
|
|
|
+ );
|
|
|
|
+ return cb({ status: "error", message: err });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ CacheModule.runJob("PUB", { channel: "news.update", value: { ...item, _id: newsId } });
|
|
|
|
+
|
|
this.log(
|
|
this.log(
|
|
- "ERROR",
|
|
|
|
|
|
+ "SUCCESS",
|
|
"NEWS_UPDATE",
|
|
"NEWS_UPDATE",
|
|
- `Updating news "${_id}" failed for user "${session.userId}". "${err}"`
|
|
|
|
|
|
+ `Updating news item "${newsId}" successful for user "${session.userId}".`
|
|
);
|
|
);
|
|
- return cb({ status: "error", message: err });
|
|
|
|
|
|
+ return cb({
|
|
|
|
+ status: "success",
|
|
|
|
+ message: "Successfully updated news item"
|
|
|
|
+ });
|
|
}
|
|
}
|
|
- CacheModule.runJob("PUB", { channel: "news.update", value: news });
|
|
|
|
- this.log("SUCCESS", "NEWS_UPDATE", `Updating news "${_id}" successful for user "${session.userId}".`);
|
|
|
|
- return cb({
|
|
|
|
- status: "success",
|
|
|
|
- message: "Successfully updated News"
|
|
|
|
- });
|
|
|
|
- });
|
|
|
|
|
|
+ );
|
|
})
|
|
})
|
|
};
|
|
};
|