Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Commit

Permalink
Merge article poll fix
Browse files Browse the repository at this point in the history
  • Loading branch information
phgroe committed Mar 19, 2018
2 parents 37a1d00 + 7ad3116 commit 61954d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "reader-critics",
"version": "0.6.0",
"version": "0.6.1",
"license": "GPL-3.0",
"private": false,
"description": "Leserkritikk v2 (Reader Critics)",
Expand Down
2 changes: 1 addition & 1 deletion src/app/db/common/wrapFind.ts
Expand Up @@ -63,7 +63,7 @@ export function wrapFindOne <D extends Document, Z> (
result.exec()
.then((doc : D) => (doc === null) ? resolve(null) : doc.toObject())
.then((doc : null|Object) => {
if (!isObject(doc)) {
if (!(doc === null || isObject(doc))) { // Explicit null-check here
return reject(new Error('result.exec() did not return a single object'));
}

Expand Down
Expand Up @@ -19,7 +19,6 @@
import * as app from 'app/util/applib';

import { isEmpty } from 'lodash';
import { EmptyError } from 'app/util/errors';
import { translate as __ } from 'app/services/localization';

import MailTemplate from 'app/template/MailTemplate';
Expand Down Expand Up @@ -61,23 +60,20 @@ export function notifyEnduserAboutUpdate(
);

if (feedbacksWithUserData.length === 0) {
// Flow control through exception handling is a Bad Thing™ and I have
// to state that here again, because I'm repeating that pattern!
throw new EmptyError(null);
return; // No feedbacks with user data/e-mail addresses -> do nothing
}

// Extract user e-mail addresses
return Promise.all([
feedbacksWithUserData.map((feedback : Feedback) => feedback.enduser.email),
layoutNotification(website, newRevision),
getMailSubject(website, newRevision),
]);
})

// Put everything together und shoot the mail
.spread((recipients : string[], html : string, subject : string) => (
SendGridMailer(recipients, subject, html)
));
])
// Put everything together und shoot the mail
.spread((recipients : string[], html : string, subject : string) => (
SendGridMailer(recipients, subject, html)
));
});
}

// E-mail layout
Expand Down
40 changes: 26 additions & 14 deletions src/app/services/article/ArticleDAO.ts
Expand Up @@ -82,22 +82,34 @@ export function saveNewVersion(
website : Website,
newArticle : Article,
oldID : ObjectID
) : Promise <Article> {
) : PromiseLike <Article> {
emptyCheck(website, newArticle, oldID);

return makeDocument(website, newArticle)
.then(newDocument => save(website, newDocument))
.then(newPersisted => wrapFindOne(ArticleModel.findOneAndUpdate(
{ _id : oldID },
{
'$set': {
newerVersion: newPersisted.ID,
},
},
{
'new': true,
}
)));
// Try to fetch the object with the new version first, there's a chance it
// already exists in the database (for example if the update on the article
// site just happened and someone put in a feedback to that version here)
return get(newArticle.url, newArticle.version, false)
// If the article is not yet in the database, create a new Mongoose document
// from the schema and persists the newly fetched data
.then((existingArticle : Article) => (
(existingArticle === null)
? makeDocument(website, newArticle)
.then(newDocument => save(website, newDocument))
: existingArticle
))
// Now that we definitely have the object of the new revision, save its ID to
// the older version as a pointer to this "updated" revision
.then((newRevision : Article) => (
ArticleModel.findOneAndUpdate(
{ _id: oldID },
{
'$set': {
newerVersion: newRevision.ID,
},
}
)
.then(() => newRevision)
));
}

export function upsert(website : Website, article : Article) : Promise <Article> {
Expand Down

0 comments on commit 61954d7

Please sign in to comment.