Skip to content

Commit

Permalink
finished script, API and UI work
Browse files Browse the repository at this point in the history
  • Loading branch information
Florin H committed Oct 30, 2023
1 parent 452a70f commit 2d4bc11
Show file tree
Hide file tree
Showing 12 changed files with 1,400 additions and 105 deletions.
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"convertResearchTopics": "ts-node -r tsconfig-paths/register prisma/convertResearchTopics.ts",
"convertPublicationBookmarks": "ts-node prisma/convertPublicationBookmarks.ts",
"removeResearchTopicProblems": "ts-node -r tsconfig-paths/register prisma/removeResearchTopicProblems.ts",
"createVersionedDOIs": "ts-node -r tsconfig-paths/register prisma/createVersionedDOIs.ts",
"format:check": "npx prettier --check src/",
"format:write": "npx prettier --write src/",
"lint:check": "eslint src/",
Expand Down
120 changes: 120 additions & 0 deletions api/prisma/createVersionedDOIs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import * as client from '../src/lib/client';
import * as helpers from '../src/lib/helpers';
import * as publicationVersionService from '../src/components/publicationVersion/service';

const createVersionedDOIs = async (): Promise<number> => {
// get the latest LIVE version of each publication
const latestLiveVersions = await client.prisma.publicationVersion.findMany({
where: {
isLatestLiveVersion: true,
createdBy: {
not: 'octopus' // ignore seed data publications
},
doi: null // only get versions without DOI
},
include: {
publication: {
select: {
id: true,
type: true,
doi: true,
topics: true,
publicationFlags: true,
url_slug: true
}
},
publicationStatus: {
select: {
status: true,
createdAt: true,
id: true
},
orderBy: {
createdAt: 'desc'
}
},
funders: {
select: {
id: true,
city: true,
country: true,
name: true,
link: true,
ror: true
}
},
coAuthors: {
select: {
id: true,
email: true,
linkedUser: true,
publicationVersionId: true,
confirmedCoAuthor: true,
approvalRequested: true,
createdAt: true,
reminderDate: true,
isIndependent: true,
affiliations: true,
user: {
select: {
firstName: true,
lastName: true,
orcid: true
}
}
},
orderBy: {
position: 'asc'
}
},
user: {
select: {
id: true,
orcid: true,
firstName: true,
lastName: true,
email: true,
createdAt: true,
updatedAt: true
}
}
}
});

console.log(`Found ${latestLiveVersions.length} without a DOI.`);

let createdVersionDOIsCount = 0;

for (const version of latestLiveVersions) {
// create a new DOI for each version
console.log(`Creating version ${version.versionNumber} DOI for publication ${version.versionOf}`);
const versionDOIResponse = await helpers.createPublicationVersionDOI(version);
const versionDOI = versionDOIResponse.data.attributes.doi;

console.log(`Successfully created version ${version.versionNumber} DOI: ${versionDOI}`);

// update publication "HasVersion" with the created DOI
console.log(`Updating canonical DOI: ${version.publication.doi} "HasVersion" with version DOI: ${versionDOI}`);
await helpers.updatePublicationDOI(version.publication.doi, version);

console.log(`Successfully updated canonical DOI: ${version.publication.doi}`);

// update version DOI
console.log(`Updating version DOI: ${versionDOI} in DB`);

await publicationVersionService.update(version.id, {
doi: versionDOI
});

createdVersionDOIsCount += 1;
console.log(); // new line
}

return createdVersionDOIsCount;
};

createVersionedDOIs()
.then((versionedDOIsCount) =>
console.log(`Successfully created ${versionedDOIsCount} versioned DOIs and updated their canonical DOIs`)
)
.catch((err) => console.log(err));
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "PublicationVersion" ADD COLUMN "doi" TEXT;
1 change: 1 addition & 0 deletions api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ model Publication {

model PublicationVersion {
id String @id @default(cuid())
doi String?
versionOf String
versionNumber Int
isLatestVersion Boolean @default(true)
Expand Down
19 changes: 12 additions & 7 deletions api/src/components/publicationVersion/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as response from 'lib/response';
import * as publicationVersionService from 'publicationVersion/service';
import * as publicationService from 'publication/service';
import * as coAuthorService from 'coauthor/service';
import * as referenceService from 'reference/service';
import * as helpers from 'lib/helpers';
import * as sqs from 'lib/sqs';

Expand Down Expand Up @@ -228,7 +227,16 @@ export const updateStatus = async (
}
}

const updatedVersion = await publicationVersionService.updateStatus(publicationVersion.id, newStatus);
// create version DOI
const publicationVersionDOI = await helpers.createPublicationVersionDOI(publicationVersion);

// update version status first so that published date is available for Open Search
await publicationVersionService.updateStatus(publicationVersion.id, 'LIVE');

// update version DOI into DB
const updatedVersion = await publicationVersionService.update(publicationVersion.id, {
doi: publicationVersionDOI.data.attributes.doi
});

// now that the publication version is LIVE, add/update the opensearch record
await publicationService.createOpenSearchRecord({
Expand All @@ -243,11 +251,8 @@ export const updateStatus = async (
cleanContent: htmlToText.convert(updatedVersion.content)
});

const references = await referenceService.getAllByPublicationVersion(updatedVersion.id);
const { linkedTo } = await publicationService.getDirectLinksForPublication(publicationVersion.versionOf, true);

// Publication version is live, so update the DOI
await helpers.updateDOI(publicationVersion.publication.doi, publicationVersion, linkedTo, references);
// Publication version is live, so update the canonical DOI with this version info
await helpers.updatePublicationDOI(publicationVersion.publication.doi, updatedVersion);

// send message to the pdf generation queue
// currently only on deployed instances while a local solution is developed
Expand Down
98 changes: 67 additions & 31 deletions api/src/components/publicationVersion/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,61 +209,97 @@ export const getAllByPublicationIds = async (ids: string[]) => {
return latestVersions;
};

export const update = (id: string, updateContent: I.UpdatePublicationVersionRequestBody) =>
export const update = (id: string, data: Prisma.PublicationVersionUpdateInput) =>
client.prisma.publicationVersion.update({
where: {
id
},
data: updateContent
});

export const updateStatus = async (id: string, status: I.PublicationStatusEnum) => {
const query = {
where: {
id
},
data: {
currentStatus: status,
publicationStatus: {
create: {
status
data,
include: {
publication: {
select: {
id: true,
type: true,
doi: true,
url_slug: true
}
},
...(status === 'LIVE' && {
publishedDate: new Date().toISOString(),
isLatestLiveVersion: true
})
},
include: {
publicationStatus: {
select: {
status: true,
createdAt: true,
id: true
},
orderBy: {
createdAt: Prisma.SortOrder.desc
createdAt: 'desc'
}
},
user: {
funders: {
select: {
id: true,
firstName: true,
lastName: true
city: true,
country: true,
name: true,
link: true,
ror: true
}
},
publication: {
coAuthors: {
select: {
type: true
id: true,
email: true,
linkedUser: true,
publicationVersionId: true,
confirmedCoAuthor: true,
approvalRequested: true,
createdAt: true,
reminderDate: true,
isIndependent: true,
affiliations: true,
user: {
select: {
firstName: true,
lastName: true,
orcid: true
}
}
},
orderBy: {
position: 'asc'
}
},
user: {
select: {
id: true,
orcid: true,
firstName: true,
lastName: true,
email: true,
createdAt: true,
updatedAt: true
}
}
}
};

const updatedPublication = await client.prisma.publicationVersion.update(query);
});

return updatedPublication;
};
export const updateStatus = async (id: string, status: I.PublicationStatusEnum) =>
client.prisma.publicationVersion.update({
where: {
id
},
data: {
currentStatus: status,
publicationStatus: {
create: {
status
}
},
...(status === 'LIVE' && {
publishedDate: new Date().toISOString(),
isLatestLiveVersion: true
})
}
});

export const validateConflictOfInterest = (version: I.PublicationVersion) => {
if (version.conflictOfInterestStatus) {
Expand Down
Loading

0 comments on commit 2d4bc11

Please sign in to comment.