Skip to content

Commit

Permalink
fix: use PrivateUrl object in Dataset domain object
Browse files Browse the repository at this point in the history
  • Loading branch information
ekraffmiller committed Oct 26, 2023
1 parent fccce0e commit a441c83
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 78 deletions.
32 changes: 20 additions & 12 deletions src/dataset/domain/models/Dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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))
}
}
}

Expand Down
29 changes: 3 additions & 26 deletions src/dataset/infrastructure/mappers/JSDatasetMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion src/stories/dataset/WithDatasetPrivateUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ export const WithDatasetPrivateUrl = (Story: StoryFn) => {
): Promise<Dataset | undefined> => {
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)
})
}
Expand Down
37 changes: 20 additions & 17 deletions src/stories/dataset/dataset-alerts/DatasetAlert.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 (
<div>
Expand Down
9 changes: 2 additions & 7 deletions tests/component/dataset/domain/models/DatasetMother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,7 @@ export class DatasetMother {
return undefined
}

static createWithPrivateUrlToken(privateUrlToken: string, props?: Partial<Dataset>) {
return this.create(props, privateUrlToken)
}

static create(props?: Partial<Dataset>, privateUrlToken?: string): Dataset {
static create(props?: Partial<Dataset>): Dataset {
const dataset = {
persistentId: faker.datatype.uuid(),
title: faker.lorem.sentence(),
Expand Down Expand Up @@ -306,8 +302,7 @@ export class DatasetMother {
dataset.hasValidTermsOfAccess,
dataset.isValid,
dataset.isReleased,
dataset.privateUrl,
privateUrlToken
dataset.privateUrl
).build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(<DatasetAlerts alerts={dataset.alerts} />)
const expectedMessageKeys = [
Expand All @@ -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(<DatasetAlerts alerts={dataset.alerts} />)
const expectedMessageKey = DatasetAlertMessageKey.UNPUBLISHED_DATASET
Expand Down

0 comments on commit a441c83

Please sign in to comment.