Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/server/routes/api/document/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO swagger comment & docs

import isWithinInterval from 'date-fns/isWithinInterval';
import Express from 'express';
import _ from 'lodash';
import uuid from 'uuid';
Expand Down Expand Up @@ -1231,6 +1231,7 @@ const getNovelInteractions = async doc => {

const emailRelatedPaperAuthors = async doc => {
if( doc.relatedPapersNotified() ){ return; } // bail out if already notified
const MAX_AGE_PAPER_YEARS = 5;

const getContact = paper => _.get(paper, ['pubmed', 'authors', 'contacts', 0]);

Expand Down Expand Up @@ -1259,12 +1260,30 @@ const emailRelatedPaperAuthors = async doc => {

// TODO RPN use semantic search score etc. in future
// just send to all for now, as we already have a small cutoff at 30
const paperIsGoodFit = async paper => true; // eslint-disable-line
const notReviewPaper = async paper => {
const reviewTypeUI = 'D016454';
const pubTypes = _.get( paper, ['pubmed', 'pubTypes'] );
const notReview = !_.find( pubTypes, ['UI', reviewTypeUI] );
return notReview;
};

const publishedNYearsAgo = ( paper, years = MAX_AGE_PAPER_YEARS ) => {
const dateNow = new Date();
const dateYearsAgo = ( new Date() ).setFullYear( dateNow.getFullYear() - years );
const paperDate = new Date( _.get( paper, ['pubmed', 'ISODate'] ) );
return isWithinInterval( paperDate, { start: dateYearsAgo, end: dateNow } );
};

const paperIsGoodFit = async paper => publishedNYearsAgo( paper ) && notReviewPaper( paper );

const byPaperDate = (a, b) => {
const getISODate = p => _.get( p, ['pubmed', 'ISODate'] );
return new Date( getISODate( a ) ) - new Date( getISODate( b ) );
};

const getSendablePapers = async papers => {
const isGoodFit = await Promise.all(papers.map(paperIsGoodFit));

return papers.filter((paper, i) => isGoodFit[i] && hasContact(paper));
return papers.filter((paper, i) => isGoodFit[i] && hasContact(paper)).sort( byPaperDate );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sort doesn't look strictly necessary, but it's probably useful for debugging.

};

const novelIntns = await getNovelInteractions(doc);
Expand Down
56 changes: 42 additions & 14 deletions src/util/pubmed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import { parse as dateParse } from 'date-fns';

const NUM_AUTHORS_SHOWING = 4;

Expand Down Expand Up @@ -111,28 +112,53 @@ const getJournalNameString = Journal => {
return name;
};

const getPubDateYear = JournalIssue => {
const hasMedlineDate = _.has( JournalIssue, ['PubDate', 'MedlineDate'] );
const hasPubYear = !_.isNil( _.get( JournalIssue, ['PubDate', 'Year'] ) );
let year;
const toISODate = ( year, month, day ) => {
let dateString = `${year}`;
let formatString = 'yyyy';

if( hasMedlineDate ){
year = _.get( JournalIssue, ['PubDate', 'MedlineDate'] );
} else if ( hasPubYear ) {
year = _.get( JournalIssue, ['PubDate', 'Year'] );
} else {
year = '';
if( month ){
dateString += ` ${month}`;

if( /^[0-9]{2}$/.test( month ) ){
formatString += ` MM`;
} else {
formatString += ` LLL`;
}
}

if( day ){
formatString += ` dd`;
dateString += ` ${day}`;
}
const ISODate = dateParse( dateString, formatString, new Date() );
return ISODate;
};

const getPubDate = JournalIssue => {
let year= null,
month = null,
day = null,
ISODate = null;

const PubDate = _.get( JournalIssue, ['PubDate'] );
const hasMedlineDate = _.has( PubDate, ['MedlineDate'] );

if( !hasMedlineDate ){
year = _.get( PubDate, ['Year'], null );
month = _.get( PubDate, ['Month'], null );
day = _.get( PubDate, ['Day'], null );
ISODate = toISODate( year, month, day );
}

return year && `(${year})`;
return { year, month, day, ISODate };
};

const getReferenceString = Journal => {
const journalName = getJournalNameString( Journal );
const JournalIssue = _.get( Journal, ['JournalIssue'] );
const journalVolume = !_.isNil( _.get( JournalIssue, ['Volume'] ) ) ? _.get( JournalIssue, ['Volume'] ): ''; //optional
const pubDateYear = getPubDateYear( JournalIssue );
return _.compact( [ journalName, journalVolume, pubDateYear ] ).join(' ') || null;
const { year } = getPubDate( JournalIssue );
return _.compact( [ journalName, journalVolume, year ] ).join(' ') || null;
};

const getArticleId = ( PubmedArticle, IdType ) => _.get( _.find( _.get( PubmedArticle, ['PubmedData', 'ArticleIdList'], [] ), [ 'IdType', IdType ] ), 'id', null );
Expand All @@ -151,6 +177,7 @@ const getArticleId = ( PubmedArticle, IdType ) => _.get( _.find( _.get( PubmedAr
* @return {String} result.pmid
* @return {String} result.doi
* @return {String} result.pubTypes
* @return {String} result.ISODate ({ year, month, day })
*/
const getPubmedCitation = PubmedArticle => {
const Article = _.get( PubmedArticle, ['MedlineCitation','Article'] ); //required
Expand All @@ -164,8 +191,9 @@ const getPubmedCitation = PubmedArticle => {
const pmid = getArticleId( PubmedArticle, 'pubmed' );
const doi = getArticleId( PubmedArticle, 'doi' );
const pubTypes = _.get( Article, 'PublicationTypeList' ); //required
const { ISODate } = getPubDate( _.get( Article, ['Journal', 'JournalIssue'] ) );

return { title, authors, reference, abstract, pmid, doi, pubTypes };
return { title, authors, reference, abstract, pmid, doi, pubTypes, ISODate };
};

/**
Expand Down