-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fair Workflows Feature 4 * Fair Workflows Feature 1 - Related Works List (#292) * Added tabs for new relation types * Added prototype connection type facet * Replaced tabs with left-side facet * Removed unnecessary Tab code * Fixed bug where connection type wasn't selected by default * Removed testing code * Fixed undefined bug * Reorder Conneciton types facets and allow Radio icons * Added OtherRelated query --------- Co-authored-by: jrhoads <[email protected]> * Fair work feature 2 - Downloadable Reports (#303) * Refactored DownloadReports to be more flexible * Restructured API routes * Added download related works for DOI * Added connectionType column --------- Co-authored-by: jrhoads <[email protected]> * Fixed failing test * Fixed failing test * Added related works header (#308) * Fixed small related works header rendering issue --------- Co-authored-by: jrhoads <[email protected]>
- Loading branch information
Showing
9 changed files
with
441 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { NextApiRequest, NextApiResponse } from "next" | ||
import { gql } from '@apollo/client'; | ||
import apolloClient from '../../../../utils/apolloClient' | ||
import { stringify } from 'csv-stringify/sync' | ||
import { WorkQueryData, WorkType } from "src/pages/doi.org/[...doi]"; | ||
|
||
const QUERY = gql` | ||
query getDoiQuery( | ||
$id: ID! | ||
$filterQuery: String | ||
$cursor: String | ||
$published: String | ||
$resourceTypeId: String | ||
$fieldOfScience: String | ||
$language: String | ||
$license: String | ||
$registrationAgency: String | ||
$repositoryId: String | ||
) { | ||
work(id: $id) { | ||
citations( | ||
first: 25 | ||
query: $filterQuery | ||
after: $cursor | ||
published: $published | ||
resourceTypeId: $resourceTypeId | ||
fieldOfScience: $fieldOfScience | ||
language: $language | ||
license: $license | ||
registrationAgency: $registrationAgency | ||
repositoryId: $repositoryId | ||
) { | ||
nodes { | ||
...WorkFragment | ||
} | ||
} | ||
references( | ||
first: 25 | ||
query: $filterQuery | ||
after: $cursor | ||
published: $published | ||
resourceTypeId: $resourceTypeId | ||
fieldOfScience: $fieldOfScience | ||
language: $language | ||
license: $license | ||
registrationAgency: $registrationAgency | ||
repositoryId: $repositoryId | ||
) { | ||
nodes { | ||
...WorkFragment | ||
} | ||
} | ||
parts( | ||
first: 25 | ||
query: $filterQuery | ||
after: $cursor | ||
published: $published | ||
resourceTypeId: $resourceTypeId | ||
fieldOfScience: $fieldOfScience | ||
language: $language | ||
license: $license | ||
registrationAgency: $registrationAgency | ||
repositoryId: $repositoryId | ||
) { | ||
nodes { | ||
...WorkFragment | ||
} | ||
} | ||
partOf( | ||
first: 25 | ||
query: $filterQuery | ||
after: $cursor | ||
published: $published | ||
resourceTypeId: $resourceTypeId | ||
fieldOfScience: $fieldOfScience | ||
language: $language | ||
license: $license | ||
registrationAgency: $registrationAgency | ||
repositoryId: $repositoryId | ||
) { | ||
nodes { | ||
...WorkFragment | ||
} | ||
} | ||
otherRelated( | ||
first: 25 | ||
query: $filterQuery | ||
after: $cursor | ||
published: $published | ||
resourceTypeId: $resourceTypeId | ||
fieldOfScience: $fieldOfScience | ||
language: $language | ||
license: $license | ||
registrationAgency: $registrationAgency | ||
repositoryId: $repositoryId | ||
) { | ||
nodes { | ||
...WorkFragment | ||
} | ||
} | ||
} | ||
} | ||
fragment WorkFragment on Work { | ||
titles { | ||
title | ||
} | ||
descriptions { | ||
description | ||
descriptionType | ||
} | ||
types { | ||
resourceTypeGeneral | ||
resourceType | ||
} | ||
doi | ||
formattedCitation(style: "apa", locale: "en-US", format: text) | ||
publicationYear | ||
} | ||
` | ||
|
||
|
||
function addConnectionType(w: WorkType, connectionType: string) { | ||
return { ...w, connectionType: connectionType } | ||
} | ||
|
||
|
||
|
||
export default async function downloadReportsHandler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const variables = req.query | ||
|
||
const { data } = await apolloClient.query<WorkQueryData, unknown>({ | ||
query: QUERY, | ||
variables: variables | ||
}) | ||
|
||
|
||
const references = data.work.references.nodes.map(w => addConnectionType(w, 'Reference')) | ||
const citations = data.work.citations.nodes.map(w => addConnectionType(w, 'Citation')) | ||
const parts = data.work.parts.nodes.map(w => addConnectionType(w, 'Part')) | ||
const partOf = data.work.partOf.nodes.map(w => addConnectionType(w, 'Is Part Of')) | ||
const otherRelated = data.work.otherRelated.nodes.map(w => addConnectionType(w, 'Other Relation')) | ||
|
||
const works = references.concat(citations, parts, partOf, otherRelated) | ||
const sortedData = works.sort((a, b) => b.publicationYear - a.publicationYear) | ||
|
||
const csv = stringify(sortedData, { | ||
header: true, | ||
columns: [ | ||
{ key: 'titles[0].title', header: 'Title' }, | ||
{ key: 'publicationYear', header: 'Publication Year' }, | ||
{ key: 'doi', header: 'DOI' }, | ||
{ key: 'descriptions[0].description', header: 'Description' }, | ||
{ key: 'formattedCitation', header: 'Formatted Citation' }, | ||
{ key: 'types.resourceTypeGeneral', header: 'Resource Type (General)' }, | ||
{ key: 'types.resourceType', header: 'Resource Type' }, | ||
{ key: 'connectionType', header: 'Connection Type' } | ||
] | ||
}) | ||
|
||
try { | ||
res.status(200) | ||
res.setHeader('Content-Type', 'text/csv') | ||
res.setHeader('Content-Disposition', `attachment; filename="related-works_${variables.id}.csv"`) | ||
res.send(csv) | ||
} catch (error) { | ||
res.status(400).json({ error }) | ||
} | ||
|
||
} |
Oops, something went wrong.