Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OC-722: Update pubrouter lambda to use versioning data structure #514

Merged
merged 1 commit into from
Oct 17, 2023
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
2 changes: 1 addition & 1 deletion api/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ functions:
handler: src/components/publicationVersion/routes.get
events:
- http:
path: ${self:custom.versions.v1}/publications/{id}/publication-versions/{version} # {version} can be a version id, number or "latest"
path: ${self:custom.versions.v1}/publications/{id}/publication-versions/{version} # {version} can be a version id, number, "latest" or "latestLive"
method: GET
cors: true
getIndexedPublicationVersions:
Expand Down
41 changes: 26 additions & 15 deletions infra/modules/s3/pdf-processing-lambda-src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const baseJSONResponse = (statusCode, body) => ({
statusCode,
});

// When a PDF is put into the S3 bucket, take the publication ID from the PDF filename, get the latest live
// version of that publication, and format its metadata into a specific JSON format before sending that to pubrouter.
export const handler = async (event) => {
const bucket = event.Records[0].s3.bucket.name;
const key = event.Records[0].s3.object.key;
Expand All @@ -20,24 +22,30 @@ export const handler = async (event) => {
const publicationId = key.replace(/\.pdf$/, "");

try {
const publicationResponse = await fetch(
`https://${process.env.ENVIRONMENT}.api.octopus.ac/v1/publications/${publicationId}`,
const publicationVersionResponse = await fetch(
`https://${process.env.ENVIRONMENT}.api.octopus.ac/v1/publications/${publicationId}/publication-versions/latestLive`,
{ method: "GET" }
);
const publication = await publicationResponse.json();
const publicationVersion = await publicationVersionResponse.json();

console.log("Fetched publication: ", JSON.stringify(publication));
console.log(
"Fetched publication version: ",
JSON.stringify(publicationVersion)
);

// If publication was written by Science Octopus (seed data), don't send.
if (publication.user && publication.user.id === "octopus") {
if (publicationVersion.user && publicationVersion.user.id === "octopus") {
console.log("Publication author is Octopus user, ignoring");
return baseJSONResponse(
200,
"Publication author is Octopus user, ignoring"
);
}

const pdfMetadata = mapPublicationToMetadata(publication, pdfUrl);
const pdfMetadata = mapPublicationVersionToMetadata(
publicationVersion,
pdfUrl
);

console.log("PDF metadata: ", JSON.stringify(pdfMetadata));

Expand Down Expand Up @@ -127,10 +135,11 @@ const sendFailureEmail = async (error, event, publicationId) => {
return client.send(command);
};

const mapPublicationToMetadata = (publication, pdfUrl) => {
const mapPublicationVersionToMetadata = (publicationVersion, pdfUrl) => {
const formatAuthor = (author) => {
return {
type: author.linkedUser === publication.user.id ? "corresp" : "author",
type:
author.linkedUser === publicationVersion.user.id ? "corresp" : "author",
name: {
firstname: author.user.firstName,
surname: author.user.lastName || "",
Expand Down Expand Up @@ -175,7 +184,7 @@ const mapPublicationToMetadata = (publication, pdfUrl) => {
};
};

const formattedPublicationDate = publication.createdAt.slice(0, 10);
const formattedPublicationDate = publicationVersion.createdAt.slice(0, 10);

return {
provider: {
Expand All @@ -199,18 +208,20 @@ const mapPublicationToMetadata = (publication, pdfUrl) => {
],
},
article: {
title: publication.title,
type: publication.type,
title: publicationVersion.title,
type: publicationVersion.publication.type,
version: "VOR",
language: [publication.language],
language: [publicationVersion.language],
identifier: [
{
type: "doi",
id: publication.doi,
id: publicationVersion.publication.doi,
},
],
},
author: publication.coAuthors?.map((author) => formatAuthor(author)),
author: publicationVersion.coAuthors?.map((author) =>
formatAuthor(author)
),
publication_date: {
date: formattedPublicationDate,
year: formattedPublicationDate.slice(0, 4),
Expand All @@ -219,7 +230,7 @@ const mapPublicationToMetadata = (publication, pdfUrl) => {
},
accepted_date: formattedPublicationDate,
publication_status: "Published",
funding: publication.funders?.map((funder) => ({
funding: publicationVersion.funders?.map((funder) => ({
name: funder.name,
identifier: [
{
Expand Down
Binary file modified infra/modules/s3/pdf-processing-lambda.zip
Binary file not shown.
Loading