Skip to content

Commit

Permalink
finished creating & publishing versions logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Florin H committed Nov 8, 2023
1 parent 1b4fddc commit 65f3924
Show file tree
Hide file tree
Showing 25 changed files with 816 additions and 375 deletions.
2 changes: 0 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
"reindex": "ts-node prisma/reindex.ts",
"convertAffiliations": "ts-node prisma/convertAffiliations.ts",
"insertTestTopics": "ts-node prisma/insertTestTopics.ts",
"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",
"updateLinksStatus": "ts-node -r tsconfig-paths/register prisma/updateLinksStatus.ts",
"format:check": "npx prettier --check src/",
Expand Down
7 changes: 7 additions & 0 deletions api/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ functions:
cors: true

# Publication Versions
createPublicationVersion:
handler: src/components/publicationVersion/routes.create
events:
- http:
path: ${self:custom.versions.v1}/publications/{id}/publication-versions
method: POST
cors: true
getPublicationVersion:
handler: src/components/publicationVersion/routes.get
events:
Expand Down
8 changes: 8 additions & 0 deletions api/src/components/funder/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export const create = async (
});
}

if (
publicationVersion.funders.some(
(funder) => funder.ror === event.body.ror || funder.link === event.body.link
)
) {
return response.json(400, { message: 'This funder already exists on this publication version.' });
}

const funder = await funderService.create(publicationVersion.id, event.body);

return response.json(200, funder);
Expand Down
4 changes: 4 additions & 0 deletions api/src/components/link/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export const deleteLink = async (
return response.json(404, { message: 'Link not found' });
}

if (!link.draft) {
return response.json(403, { message: 'You are not allowed to delete inherited Links.' });
}

const fromCurrentVersion = link.publicationFromRef.versions.find((version) => version.isLatestVersion);

if (
Expand Down
28 changes: 13 additions & 15 deletions api/src/components/publication/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,25 @@ export const getLinksForPublication = async (
const publicationId = event.pathParameters.id;
const directLinks = event.queryStringParameters?.direct === 'true';
const user = event.user;
let includeDraft = false;
let includeDraftVersion = false;

try {
if (directLinks) {
if (user) {
const latestVersion = await publicationVersionService.get(publicationId, 'latest');

// if latest version is a DRAFT, check if user can see it
if (
latestVersion?.currentStatus === 'DRAFT' &&
(user.id === latestVersion?.createdBy ||
latestVersion?.coAuthors.some((coAuthor) => coAuthor.linkedUser === user.id))
) {
includeDraft = true;
}
if (user) {
const latestVersion = await publicationVersionService.get(publicationId, 'latest');

// if latest version is a DRAFT, check if user can see it
if (
latestVersion?.currentStatus !== 'LIVE' &&
(user.id === latestVersion?.createdBy ||
latestVersion?.coAuthors.some((coAuthor) => coAuthor.linkedUser === user.id))
) {
includeDraftVersion = true;
}
}

const { publication, linkedFrom, linkedTo } = directLinks
? await publicationService.getDirectLinksForPublication(publicationId, includeDraft)
: await publicationService.getLinksForPublication(publicationId);
? await publicationService.getDirectLinksForPublication(publicationId, includeDraftVersion)
: await publicationService.getLinksForPublication(publicationId, includeDraftVersion);

if (!publication) {
return response.json(404, { message: 'Not found.' });
Expand Down
50 changes: 35 additions & 15 deletions api/src/components/publication/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as s3 from 'lib/s3';
import chromium from '@sparticuz/chromium';
import * as s3 from 'lib/s3';
import * as I from 'interface';
import * as client from 'lib/client';
import * as referenceService from 'reference/service';
Expand Down Expand Up @@ -88,6 +88,9 @@ export const get = async (id: string) => {
createdAt: true
}
}
},
orderBy: {
versionNumber: 'asc'
}
},
publicationFlags: {
Expand Down Expand Up @@ -227,6 +230,9 @@ export const createOpenSearchRecord = async (data: I.OpenSearchPublication) => {
return publication;
};

export const deleteOpenSearchRecord = (publicationId: string) =>
client.search.delete({ index: 'publications', id: publicationId });

export const getOpenSearchPublications = async (filters: I.OpenSearchPublicationFilters) => {
const orderBy = filters.orderBy
? {
Expand Down Expand Up @@ -318,6 +324,7 @@ export const create = async (e: I.CreatePublicationRequestBody, user: I.User, do
// Create first version when publication is created
versions: {
create: {
id: doiResponse.data.attributes.suffix + '-v1',
versionNumber: 1,
title: e.title,
licence: e.licence,
Expand Down Expand Up @@ -390,7 +397,10 @@ export const doesDuplicateFlagExist = async (publication, category, user) => {
return flag;
};

export const getLinksForPublication = async (id: string): Promise<I.PublicationWithLinks> => {
export const getLinksForPublication = async (
id: string,
includeDraftVersion = false
): Promise<I.PublicationWithLinks> => {
const publication = await get(id);

if (!publication) {
Expand All @@ -401,9 +411,11 @@ export const getLinksForPublication = async (id: string): Promise<I.PublicationW
};
}

const latestLiveVersion = publication?.versions.find((version) => version.isLatestLiveVersion);
const latestVersion = publication?.versions.find((version) =>
includeDraftVersion ? version.isLatestVersion : version.isLatestLiveVersion
);

if (!latestLiveVersion) {
if (!latestVersion) {
return {
publication: null,
linkedFrom: [],
Expand Down Expand Up @@ -520,7 +532,7 @@ export const getLinksForPublication = async (id: string): Promise<I.PublicationW
LEFT JOIN "PublicationVersion" AS pfrom_version
ON "pfrom".id = "pfrom_version"."versionOf"
AND "pfrom_version"."isLatestVersion" = TRUE
AND "pfrom_version"."isLatestLiveVersion" = TRUE
LEFT JOIN "Publication" AS pto
ON "pto".id = "Links"."publicationTo"
Expand Down Expand Up @@ -554,7 +566,7 @@ export const getLinksForPublication = async (id: string): Promise<I.PublicationW
LEFT JOIN "PublicationVersion" AS pfrom_version
ON "pfrom".id = "pfrom_version"."versionOf"
AND "pfrom_version"."isLatestVersion" = TRUE
AND "pfrom_version"."isLatestLiveVersion" = TRUE
LEFT JOIN "Publication" AS pto
ON "pto".id = "l"."publicationTo"
Expand Down Expand Up @@ -625,13 +637,13 @@ export const getLinksForPublication = async (id: string): Promise<I.PublicationW
id: publication.id,
type: publication.type,
doi: publication.doi,
title: latestLiveVersion.title || '',
createdBy: latestLiveVersion.createdBy,
currentStatus: latestLiveVersion.currentStatus,
publishedDate: latestLiveVersion.publishedDate?.toISOString() || '',
authorFirstName: latestLiveVersion.user.firstName,
authorLastName: latestLiveVersion.user.lastName || '',
authors: latestLiveVersion.coAuthors.map((author) => ({
title: latestVersion.title || '',
createdBy: latestVersion.createdBy,
currentStatus: latestVersion.currentStatus,
publishedDate: latestVersion.publishedDate?.toISOString() || '',
authorFirstName: latestVersion.user.firstName,
authorLastName: latestVersion.user.lastName || '',
authors: latestVersion.coAuthors.map((author) => ({
id: author.id,
linkedUser: author.linkedUser,
user: {
Expand All @@ -648,9 +660,9 @@ export const getLinksForPublication = async (id: string): Promise<I.PublicationW

export const getDirectLinksForPublication = async (
id: string,
includeDraft = false
includeDraftVersion = false
): Promise<I.PublicationWithLinks> => {
const publicationFilter: Prisma.PublicationVersionWhereInput = includeDraft
const publicationFilter: Prisma.PublicationVersionWhereInput = includeDraftVersion
? { isLatestVersion: true }
: { isLatestLiveVersion: true };

Expand All @@ -674,6 +686,7 @@ export const getDirectLinksForPublication = async (
},
linkedTo: {
where: {
draft: includeDraftVersion ? undefined : includeDraftVersion,
publicationToRef: {
versions: {
some: {
Expand All @@ -691,6 +704,9 @@ export const getDirectLinksForPublication = async (
doi: true,
type: true,
versions: {
where: {
isLatestLiveVersion: true
},
include: {
user: true
}
Expand All @@ -701,6 +717,7 @@ export const getDirectLinksForPublication = async (
},
linkedFrom: {
where: {
draft: includeDraftVersion ? undefined : includeDraftVersion,
publicationFromRef: {
versions: {
some: {
Expand All @@ -718,6 +735,9 @@ export const getDirectLinksForPublication = async (
doi: true,
type: true,
versions: {
where: {
isLatestLiveVersion: true
},
include: {
user: true
}
Expand Down
Loading

0 comments on commit 65f3924

Please sign in to comment.