From a441c832aa7dbed4d650030b91c5229d9b91bf92 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Thu, 26 Oct 2023 09:14:19 -0400 Subject: [PATCH] fix: use PrivateUrl object in Dataset domain object --- src/dataset/domain/models/Dataset.ts | 32 ++++++++++------ .../infrastructure/mappers/JSDatasetMapper.ts | 29 ++------------- .../DatasetJSDataverseRepository.ts | 8 +--- src/stories/dataset/WithDatasetPrivateUrl.tsx | 9 ++++- .../dataset-alerts/DatasetAlert.stories.tsx | 37 ++++++++++--------- .../dataset/domain/models/DatasetMother.ts | 9 +---- .../dataset-alerts/DatasetAlerts.spec.tsx | 22 +++++++---- 7 files changed, 68 insertions(+), 78 deletions(-) diff --git a/src/dataset/domain/models/Dataset.ts b/src/dataset/domain/models/Dataset.ts index a8c5384ec..a0d96e6c7 100644 --- a/src/dataset/domain/models/Dataset.ts +++ b/src/dataset/domain/models/Dataset.ts @@ -275,6 +275,11 @@ export enum DatasetLockReason { FILE_VALIDATION_FAILED = 'fileValidationFailed' } +export interface PrivateUrl { + token: string + urlSnippet: string +} + export class Dataset { constructor( public readonly persistentId: string, @@ -290,7 +295,7 @@ export class Dataset { public readonly hasValidTermsOfAccess: boolean, public readonly isValid: boolean, public readonly isReleased: boolean, - public readonly privateUrl?: string + public readonly privateUrl?: PrivateUrl ) {} public getTitle(): string { @@ -335,8 +340,7 @@ export class Dataset { public readonly hasValidTermsOfAccess: boolean, public readonly isValid: boolean, public readonly isReleased: boolean, - public readonly privateUrl?: string, - public readonly privateUrlToken?: string + public readonly privateUrl?: PrivateUrl ) { this.withLabels() this.withAlerts() @@ -420,15 +424,19 @@ export class Dataset { ) } } - if (this.privateUrl && !this.privateUrlToken) { - const dynamicFields = { privateUrl: this.privateUrl } - this.alerts.push( - new DatasetAlert('info', DatasetAlertMessageKey.SHARE_UNPUBLISHED_DATASET, dynamicFields) - ) - } - - if (this.privateUrlToken) { - this.alerts.push(new DatasetAlert('warning', DatasetAlertMessageKey.UNPUBLISHED_DATASET)) + if (this.privateUrl) { + if (this.permissions.canPublishDataset) { + const dynamicFields = { privateUrl: this.privateUrl.urlSnippet + this.privateUrl.token } + this.alerts.push( + new DatasetAlert( + 'info', + DatasetAlertMessageKey.SHARE_UNPUBLISHED_DATASET, + dynamicFields + ) + ) + } else { + this.alerts.push(new DatasetAlert('warning', DatasetAlertMessageKey.UNPUBLISHED_DATASET)) + } } } diff --git a/src/dataset/infrastructure/mappers/JSDatasetMapper.ts b/src/dataset/infrastructure/mappers/JSDatasetMapper.ts index 7b7bc47f8..fdf543694 100644 --- a/src/dataset/infrastructure/mappers/JSDatasetMapper.ts +++ b/src/dataset/infrastructure/mappers/JSDatasetMapper.ts @@ -13,40 +13,17 @@ import { DatasetMetadataBlocks, DatasetMetadataFields, DatasetVersion, - MetadataBlockName + MetadataBlockName, + PrivateUrl } from '../../domain/models/Dataset' -/* - - static Builder = class { - public readonly labels: DatasetLabel[] = [] - public readonly alerts: DatasetAlert[] = [] - - constructor( - public readonly persistentId: string, - public readonly version: DatasetVersion, - public readonly citation: string, - public readonly summaryFields: DatasetMetadataBlock[], - public readonly license: DatasetLicense = defaultLicense, - public readonly metadataBlocks: DatasetMetadataBlocks, - public readonly permissions: DatasetPermissions, - public readonly locks: DatasetLock[], - public readonly hasValidTermsOfAccess: boolean, - public readonly isValid: boolean, - public readonly isReleased: boolean, - public readonly privateUrl?: string - ) { - this.withLabels() - this.withAlerts() - } - */ export class JSDatasetMapper { static toDataset( jsDataset: JSDataset, citation: string, summaryFieldsNames: string[], requestedVersion?: string, - privateUrl?: string + privateUrl?: PrivateUrl ): Dataset { return new Dataset.Builder( jsDataset.persistentId, diff --git a/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts b/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts index 1464847ea..317254b9b 100644 --- a/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts +++ b/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts @@ -44,13 +44,7 @@ export class DatasetJSDataverseRepository implements DatasetRepository { getPrivateUrlDatasetCitation.execute(privateUrlToken) ]) .then(([jsDataset, summaryFieldsNames, citation]: [JSDataset, string[], string]) => - JSDatasetMapper.toDataset( - jsDataset, - citation, - summaryFieldsNames, - undefined, - privateUrlToken - ) + JSDatasetMapper.toDataset(jsDataset, citation, summaryFieldsNames, undefined) ) .catch((error: WriteError) => { throw new Error(error.message) diff --git a/src/stories/dataset/WithDatasetPrivateUrl.tsx b/src/stories/dataset/WithDatasetPrivateUrl.tsx index 5aa2e6dec..1cc892f75 100644 --- a/src/stories/dataset/WithDatasetPrivateUrl.tsx +++ b/src/stories/dataset/WithDatasetPrivateUrl.tsx @@ -12,7 +12,14 @@ export const WithDatasetPrivateUrl = (Story: StoryFn) => { ): Promise => { return new Promise((resolve) => { setTimeout(() => { - resolve(DatasetMother.createWithPrivateUrlToken(privateUrlToken)) + resolve( + DatasetMother.createRealistic({ + privateUrl: { + urlSnippet: 'http://localhost:8080/privateurl.xhtml?token=', + token: 'cd943c75-1cc7-4c1d-9717-98141d65d5cb' + } + }) + ) }, 1000) }) } diff --git a/src/stories/dataset/dataset-alerts/DatasetAlert.stories.tsx b/src/stories/dataset/dataset-alerts/DatasetAlert.stories.tsx index 56aacf802..072994ad5 100644 --- a/src/stories/dataset/dataset-alerts/DatasetAlert.stories.tsx +++ b/src/stories/dataset/dataset-alerts/DatasetAlert.stories.tsx @@ -94,9 +94,11 @@ export const SharePrivateUrl: Story = { 1, 0 ), - permissions: DatasetPermissionsMother.createWithUpdateDatasetAllowed(), - privateUrl: - 'http://localhost:8080/privateurl.xhtml?token=cd943c75-1cc7-4c1d-9717-98141d65d5cb' + permissions: DatasetPermissionsMother.createWithAllAllowed(), + privateUrl: { + urlSnippet: 'http://localhost:8080/privateurl.xhtml?token=', + token: 'cd943c75-1cc7-4c1d-9717-98141d65d5cb' + } }) return ( @@ -108,21 +110,22 @@ export const SharePrivateUrl: Story = { } export const UsePrivateUrl: Story = { render: () => { - const dataset = DatasetMother.createWithPrivateUrlToken( - 'cd943c75-1cc7-4c1d-9717-98141d65d5cb', - { - version: new DatasetVersion( - 1, - DatasetPublishingStatus.RELEASED, - true, - false, - DatasetPublishingStatus.DRAFT, - 1, - 0 - ), - permissions: DatasetPermissionsMother.createWithNoneAllowed() + const dataset = DatasetMother.createRealistic({ + version: new DatasetVersion( + 1, + DatasetPublishingStatus.RELEASED, + true, + false, + DatasetPublishingStatus.DRAFT, + 1, + 0 + ), + permissions: DatasetPermissionsMother.createWithNoneAllowed(), + privateUrl: { + urlSnippet: 'http://localhost:8080/privateurl.xhtml?token=', + token: 'cd943c75-1cc7-4c1d-9717-98141d65d5cb' } - ) + }) return (
diff --git a/tests/component/dataset/domain/models/DatasetMother.ts b/tests/component/dataset/domain/models/DatasetMother.ts index 9a1fc483a..c96e0a5f8 100644 --- a/tests/component/dataset/domain/models/DatasetMother.ts +++ b/tests/component/dataset/domain/models/DatasetMother.ts @@ -190,11 +190,7 @@ export class DatasetMother { return undefined } - static createWithPrivateUrlToken(privateUrlToken: string, props?: Partial) { - return this.create(props, privateUrlToken) - } - - static create(props?: Partial, privateUrlToken?: string): Dataset { + static create(props?: Partial): Dataset { const dataset = { persistentId: faker.datatype.uuid(), title: faker.lorem.sentence(), @@ -306,8 +302,7 @@ export class DatasetMother { dataset.hasValidTermsOfAccess, dataset.isValid, dataset.isReleased, - dataset.privateUrl, - privateUrlToken + dataset.privateUrl ).build() } diff --git a/tests/component/sections/dataset/dataset-alerts/DatasetAlerts.spec.tsx b/tests/component/sections/dataset/dataset-alerts/DatasetAlerts.spec.tsx index 698b94b1c..d70fd4977 100644 --- a/tests/component/sections/dataset/dataset-alerts/DatasetAlerts.spec.tsx +++ b/tests/component/sections/dataset/dataset-alerts/DatasetAlerts.spec.tsx @@ -107,12 +107,15 @@ it('does not show draft alert if version is RELEASED', () => { cy.findByRole('alert').should('not.exist') }) -it('shows draft & share private url message if privateUrlToken exists', () => { +it('shows draft & share private url message if privateUrl exists and user can edit', () => { cy.fixture('../../../public/locales/en/dataset.json').then((datasetText: DatasetTranslation) => { - const privateUrlToken = '12345' - const dataset = DatasetMother.createWithPrivateUrlToken(privateUrlToken, { + const dataset = DatasetMother.createRealistic({ version: DatasetVersionMother.createDraftAsLatestVersion(), - permissions: DatasetPermissionsMother.createWithAllAllowed() + permissions: DatasetPermissionsMother.createWithAllAllowed(), + privateUrl: { + urlSnippet: 'http://localhost:8080/privateurl.xhtml?token=', + token: 'cd943c75-1cc7-4c1d-9717-98141d65d5cb' + } }) cy.customMount() const expectedMessageKeys = [ @@ -127,12 +130,15 @@ it('shows draft & share private url message if privateUrlToken exists', () => { }) }) }) -it('shows private url message only if privateUrlToken exists and user cannot edit', () => { +it('shows private url message only if privateUrl exists and user cannot edit', () => { cy.fixture('../../../public/locales/en/dataset.json').then((datasetText: DatasetTranslation) => { - const privateUrlToken = '12345' - const dataset = DatasetMother.createWithPrivateUrlToken(privateUrlToken, { + const dataset = DatasetMother.createRealistic({ version: DatasetVersionMother.createDraftAsLatestVersion(), - permissions: DatasetPermissionsMother.createWithNoneAllowed() + permissions: DatasetPermissionsMother.createWithNoneAllowed(), + privateUrl: { + urlSnippet: 'http://localhost:8080/privateurl.xhtml?token=', + token: 'cd943c75-1cc7-4c1d-9717-98141d65d5cb' + } }) cy.customMount() const expectedMessageKey = DatasetAlertMessageKey.UNPUBLISHED_DATASET