Skip to content

Commit

Permalink
Saving manuscript count (#4425)
Browse files Browse the repository at this point in the history
* Saving manuscript count

* linting

* add test

* simplify code
  • Loading branch information
lctrt authored Oct 22, 2024
1 parent 5b7050e commit 8d26292
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
Environment,
FetchManuscriptByIdQuery,
FetchManuscriptByIdQueryVariables,
FetchManuscriptsByTeamIdQuery,
FETCH_MANUSCRIPTS_BY_TEAM_ID,
FETCH_MANUSCRIPT_BY_ID,
getLinkAsset,
getLinkAssets,
Expand Down Expand Up @@ -46,6 +48,19 @@ export class ManuscriptContentfulDataProvider
throw new Error('Method not implemented.');
}

async fetchCountByTeamId(id: string) {
const { teams } = await this.contentfulClient.request<
FetchManuscriptsByTeamIdQuery,
FetchManuscriptByIdQueryVariables
>(FETCH_MANUSCRIPTS_BY_TEAM_ID, { id });

return (
teams?.linkedFrom?.manuscriptsCollection?.items.filter(
(item) => item?.teamsCollection?.items[0]?.sys.id === id,
).length || 0
);
}

async fetchById(id: string): Promise<ManuscriptDataObject | null> {
const { manuscripts } = await this.contentfulClient.request<
FetchManuscriptByIdQuery,
Expand Down Expand Up @@ -95,6 +110,7 @@ export class ManuscriptContentfulDataProvider
{
fields: addLocaleToFields({
...version,
count: 1,
teams: getLinkEntities(version.teams),
firstAuthors: getLinkEntities(version.firstAuthors),
correspondingAuthor: getLinkEntities(version.correspondingAuthor),
Expand All @@ -120,9 +136,12 @@ export class ManuscriptContentfulDataProvider

const { id: manuscriptVersionId } = manuscriptVersionEntry.sys;

const currentCount = (await this.fetchCountByTeamId(teamId)) || 0;

const manuscriptEntry = await environment.createEntry('manuscripts', {
fields: addLocaleToFields({
...plainFields,
count: currentCount + 1,
teams: getLinkEntities([teamId]),
versions: getLinkEntities([manuscriptVersionId]),
}),
Expand All @@ -138,6 +157,7 @@ const parseGraphQLManuscript = (
manuscripts: ManuscriptItem,
): ManuscriptDataObject => ({
id: manuscripts.sys.id,
count: manuscripts.count || 0,
title: manuscripts.title || '',
teamId: manuscripts.teamsCollection?.items[0]?.sys.id || '',
status: manuscriptMapStatus(manuscripts.status) || undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import {
getManuscriptCreateDataObject,
getManuscriptDataObject,
} from '../../fixtures/manuscript.fixtures';
import { getUsersTeamsCollection } from '../../fixtures/teams.fixtures';
import {
getContentfulGraphql,
getUsersTeamsCollection,
} from '../../fixtures/teams.fixtures';
import { getContentfulGraphqlClientMock } from '../../mocks/contentful-graphql-client.mock';
import { getContentfulEnvironmentMock } from '../../mocks/contentful-rest-client.mock';

Expand All @@ -32,6 +35,7 @@ describe('Manuscripts Contentful Data Provider', () => {

const contentfulGraphqlClientMockServer =
getContentfulGraphqlClientMockServer({
...getContentfulGraphql(),
UsersTeamsCollection: () => getUsersTeamsCollection(),
Manuscripts: () => getContentfulGraphqlManuscript(),
ManuscriptsVersionsCollection: () =>
Expand Down Expand Up @@ -60,6 +64,22 @@ describe('Manuscripts Contentful Data Provider', () => {
});
});

describe('FetchCountByTeamId', () => {
it('Should return 0 when team does not exist', async () => {
const teamId = 'team-id-2';
const result =
await manuscriptDataProviderMockGraphql.fetchCountByTeamId(teamId);
expect(result).toBe(0);
});

it('Should fetch the count of existing manuscripts for a given team', async () => {
const teamId = 'team-id-1';
const result =
await manuscriptDataProviderMockGraphql.fetchCountByTeamId(teamId);
expect(result).toBe(0);
});
});

describe('Fetch-by-id', () => {
test('Should fetch the manuscript from Contentful GraphQl', async () => {
const manuscriptId = 'manuscript-id-1';
Expand Down Expand Up @@ -148,6 +168,7 @@ describe('Manuscripts Contentful Data Provider', () => {
const result = await manuscriptDataProvider.fetchById('1');
expect(result).toEqual({
id: 'manuscript-id-1',
count: 1,
teamId: '',
title: '',
versions: [],
Expand Down Expand Up @@ -237,7 +258,7 @@ describe('Manuscripts Contentful Data Provider', () => {
.calledWith(manuscriptCreateDataObject.versions[0]!.manuscriptFile.id)
.mockResolvedValue(assetMock);

const result = await manuscriptDataProvider.create({
const result = await manuscriptDataProviderMockGraphql.create({
...manuscriptCreateDataObject,
userId: 'user-id-0',
});
Expand Down Expand Up @@ -284,6 +305,7 @@ describe('Manuscripts Contentful Data Provider', () => {
},
submissionDate: { 'en-US': undefined },
correspondingAuthor: { 'en-US': [] },
count: { 'en-US': 1 },
additionalAuthors: { 'en-US': [] },
teams: {
'en-US': [
Expand Down Expand Up @@ -328,6 +350,9 @@ describe('Manuscripts Contentful Data Provider', () => {
eligibilityReasons: {
'en-US': [],
},
count: {
'en-US': 3,
},
versions: {
'en-US': [
{
Expand Down Expand Up @@ -384,7 +409,7 @@ describe('Manuscripts Contentful Data Provider', () => {
)
.mockResolvedValue(assetMock);

const result = await manuscriptDataProvider.create({
const result = await manuscriptDataProviderMockGraphql.create({
...manuscriptCreateDataObject,
userId: 'user-id-0',
});
Expand Down Expand Up @@ -445,6 +470,7 @@ describe('Manuscripts Contentful Data Provider', () => {
},
submissionDate: { 'en-US': undefined },
correspondingAuthor: { 'en-US': [] },
count: { 'en-US': 1 },
additionalAuthors: { 'en-US': [] },
teams: {
'en-US': [
Expand Down Expand Up @@ -475,6 +501,9 @@ describe('Manuscripts Contentful Data Provider', () => {
title: {
'en-US': 'Manuscript Title',
},
count: {
'en-US': 3,
},
teams: {
'en-US': [
{
Expand Down
2 changes: 2 additions & 0 deletions apps/crn-server/test/fixtures/manuscript.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const getManuscriptDataObject = (
id: 'manuscript-id-1',
title: 'Manuscript Title',
teamId: 'team-1',
count: 1,
versions: [
{
id: 'version-1',
Expand Down Expand Up @@ -62,6 +63,7 @@ export const getContentfulGraphqlManuscript = (
id: 'manuscript-id-1',
},
title: 'Manuscript Title',
count: 1,
teamsCollection: {
items: [{ sys: { id: 'team-1' } }],
},
Expand Down
Loading

0 comments on commit 8d26292

Please sign in to comment.