Skip to content

Commit 304d2c1

Browse files
committed
add format as an optional param to submitGuestbook for download use cases
1 parent 69b8a3c commit 304d2c1

8 files changed

Lines changed: 145 additions & 23 deletions

File tree

src/access/domain/repositories/IAccessRepository.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@ import { GuestbookResponseDTO } from '../dtos/GuestbookResponseDTO'
33
export interface IAccessRepository {
44
submitGuestbookForDatafileDownload(
55
fileId: number | string,
6-
guestbookResponse: GuestbookResponseDTO
6+
guestbookResponse: GuestbookResponseDTO,
7+
format?: string
78
): Promise<string>
89

910
submitGuestbookForDatafilesDownload(
1011
fileIds: string | Array<number | string>,
11-
guestbookResponse: GuestbookResponseDTO
12+
guestbookResponse: GuestbookResponseDTO,
13+
format?: string
1214
): Promise<string>
1315

1416
submitGuestbookForDatasetDownload(
1517
datasetId: number | string,
16-
guestbookResponse: GuestbookResponseDTO
18+
guestbookResponse: GuestbookResponseDTO,
19+
format?: string
1720
): Promise<string>
1821

1922
submitGuestbookForDatasetVersionDownload(
2023
datasetId: number | string,
2124
versionId: string,
22-
guestbookResponse: GuestbookResponseDTO
25+
guestbookResponse: GuestbookResponseDTO,
26+
format?: string
2327
): Promise<string>
2428
}

src/access/domain/useCases/SubmitGuestbookForDatafileDownload.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ export class SubmitGuestbookForDatafileDownload implements UseCase<string> {
1212
* @param {GuestbookResponseDTO} guestbookResponse - Guestbook response payload.
1313
* @returns {Promise<string>} - Signed URL for the download.
1414
*/
15-
async execute(fileId: number | string, guestbookResponse: GuestbookResponseDTO): Promise<string> {
16-
return await this.accessRepository.submitGuestbookForDatafileDownload(fileId, guestbookResponse)
15+
async execute(
16+
fileId: number | string,
17+
guestbookResponse: GuestbookResponseDTO,
18+
format?: string
19+
): Promise<string> {
20+
return await this.accessRepository.submitGuestbookForDatafileDownload(
21+
fileId,
22+
guestbookResponse,
23+
format
24+
)
1725
}
1826
}

src/access/domain/useCases/SubmitGuestbookForDatafilesDownload.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ export class SubmitGuestbookForDatafilesDownload implements UseCase<string> {
1414
*/
1515
async execute(
1616
fileIds: string | Array<number | string>,
17-
guestbookResponse: GuestbookResponseDTO
17+
guestbookResponse: GuestbookResponseDTO,
18+
format?: string
1819
): Promise<string> {
1920
return await this.accessRepository.submitGuestbookForDatafilesDownload(
2021
fileIds,
21-
guestbookResponse
22+
guestbookResponse,
23+
format
2224
)
2325
}
2426
}

src/access/domain/useCases/SubmitGuestbookForDatasetDownload.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ export class SubmitGuestbookForDatasetDownload implements UseCase<string> {
1414
*/
1515
async execute(
1616
datasetId: number | string,
17-
guestbookResponse: GuestbookResponseDTO
17+
guestbookResponse: GuestbookResponseDTO,
18+
format?: string
1819
): Promise<string> {
1920
return await this.accessRepository.submitGuestbookForDatasetDownload(
2021
datasetId,
21-
guestbookResponse
22+
guestbookResponse,
23+
format
2224
)
2325
}
2426
}

src/access/domain/useCases/SubmitGuestbookForDatasetVersionDownload.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ export class SubmitGuestbookForDatasetVersionDownload implements UseCase<string>
1616
async execute(
1717
datasetId: number | string,
1818
versionId: string,
19-
guestbookResponse: GuestbookResponseDTO
19+
guestbookResponse: GuestbookResponseDTO,
20+
format?: string
2021
): Promise<string> {
2122
return await this.accessRepository.submitGuestbookForDatasetVersionDownload(
2223
datasetId,
2324
versionId,
24-
guestbookResponse
25+
guestbookResponse,
26+
format
2527
)
2628
}
2729
}

src/access/infra/repositories/AccessRepository.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ export class AccessRepository extends ApiRepository implements IAccessRepository
77

88
public async submitGuestbookForDatafileDownload(
99
fileId: number | string,
10-
guestbookResponse: GuestbookResponseDTO
10+
guestbookResponse: GuestbookResponseDTO,
11+
format?: string
1112
): Promise<string> {
1213
const endpoint = this.buildApiEndpoint(`${this.accessResourceName}/datafile`, undefined, fileId)
13-
return this.doPost(endpoint, guestbookResponse, { signed: true })
14+
const queryParams = format ? { signed: true, format } : { signed: true }
15+
16+
return this.doPost(endpoint, guestbookResponse, queryParams)
1417
.then((response) => {
1518
const signedUrl = response.data.data.signedUrl
1619
return signedUrl
@@ -22,15 +25,18 @@ export class AccessRepository extends ApiRepository implements IAccessRepository
2225

2326
public async submitGuestbookForDatafilesDownload(
2427
fileIds: Array<number>,
25-
guestbookResponse: GuestbookResponseDTO
28+
guestbookResponse: GuestbookResponseDTO,
29+
format?: string
2630
): Promise<string> {
31+
const queryParams = format ? { signed: true, format } : { signed: true }
32+
2733
return this.doPost(
2834
this.buildApiEndpoint(
2935
this.accessResourceName,
3036
`datafiles/${Array.isArray(fileIds) ? fileIds.join(',') : fileIds}`
3137
),
3238
guestbookResponse,
33-
{ signed: true }
39+
queryParams
3440
)
3541
.then((response) => {
3642
const signedUrl = response.data.data.signedUrl
@@ -43,14 +49,17 @@ export class AccessRepository extends ApiRepository implements IAccessRepository
4349

4450
public async submitGuestbookForDatasetDownload(
4551
datasetId: number | string,
46-
guestbookResponse: GuestbookResponseDTO
52+
guestbookResponse: GuestbookResponseDTO,
53+
format?: string
4754
): Promise<string> {
4855
const endpoint = this.buildApiEndpoint(
4956
`${this.accessResourceName}/dataset`,
5057
undefined,
5158
datasetId
5259
)
53-
return this.doPost(endpoint, guestbookResponse, { signed: true })
60+
const queryParams = format ? { signed: true, format } : { signed: true }
61+
62+
return this.doPost(endpoint, guestbookResponse, queryParams)
5463
.then((response) => {
5564
const signedUrl = response.data.data.signedUrl
5665
return signedUrl
@@ -63,14 +72,17 @@ export class AccessRepository extends ApiRepository implements IAccessRepository
6372
public async submitGuestbookForDatasetVersionDownload(
6473
datasetId: number | string,
6574
versionId: string,
66-
guestbookResponse: GuestbookResponseDTO
75+
guestbookResponse: GuestbookResponseDTO,
76+
format?: string
6777
): Promise<string> {
6878
const endpoint = this.buildApiEndpoint(
6979
`${this.accessResourceName}/dataset`,
7080
`versions/${versionId}`,
7181
datasetId
7282
)
73-
return this.doPost(endpoint, guestbookResponse, { signed: true })
83+
const queryParams = format ? { signed: true, format } : { signed: true }
84+
85+
return this.doPost(endpoint, guestbookResponse, queryParams)
7486
.then((response) => {
7587
const signedUrl = response.data.data.signedUrl
7688
return signedUrl

test/integration/access/AccessRepository.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ describe('AccessRepository', () => {
9797
expect(() => new URL(actual)).not.toThrow()
9898
})
9999

100+
test('should preserve format=tab in signed url for dataset download', async () => {
101+
const actual = await sut.submitGuestbookForDatasetDownload(
102+
testDatasetIds.numericId,
103+
guestbookResponse,
104+
'original'
105+
)
106+
const signedUrl = new URL(actual)
107+
108+
expect(signedUrl.searchParams.get('format')).toEqual('original')
109+
})
110+
100111
test('should return error when dataset does not exist', async () => {
101112
const nonExistentId = 999999999
102113
await expect(

test/unit/access/SubmitGuestbookDownloads.test.ts

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,31 @@ describe('access download use cases', () => {
2222

2323
const actual = await sut.execute(1, guestbookResponse)
2424

25-
expect(repository.submitGuestbookForDatafileDownload).toHaveBeenCalledWith(1, guestbookResponse)
25+
expect(repository.submitGuestbookForDatafileDownload).toHaveBeenCalledWith(
26+
1,
27+
guestbookResponse,
28+
undefined
29+
)
2630
expect(actual).toEqual('https://signed.datafile')
2731
})
2832

33+
test('should submit datafile download with format and return signed url', async () => {
34+
const repository: IAccessRepository = {} as IAccessRepository
35+
repository.submitGuestbookForDatafileDownload = jest
36+
.fn()
37+
.mockResolvedValue('https://signed.datafile?format=original')
38+
const sut = new SubmitGuestbookForDatafileDownload(repository)
39+
40+
const actual = await sut.execute(1, guestbookResponse, 'original')
41+
42+
expect(repository.submitGuestbookForDatafileDownload).toHaveBeenCalledWith(
43+
1,
44+
guestbookResponse,
45+
'original'
46+
)
47+
expect(actual).toEqual('https://signed.datafile?format=original')
48+
})
49+
2950
test('should submit datafiles download and return signed url', async () => {
3051
const repository: IAccessRepository = {} as IAccessRepository
3152
repository.submitGuestbookForDatafilesDownload = jest
@@ -37,11 +58,29 @@ describe('access download use cases', () => {
3758

3859
expect(repository.submitGuestbookForDatafilesDownload).toHaveBeenCalledWith(
3960
[1, 2],
40-
guestbookResponse
61+
guestbookResponse,
62+
undefined
4163
)
4264
expect(actual).toEqual('https://signed.datafiles')
4365
})
4466

67+
test('should submit datafiles download with format and return signed url', async () => {
68+
const repository: IAccessRepository = {} as IAccessRepository
69+
repository.submitGuestbookForDatafilesDownload = jest
70+
.fn()
71+
.mockResolvedValue('https://signed.datafiles?format=original')
72+
const sut = new SubmitGuestbookForDatafilesDownload(repository)
73+
74+
const actual = await sut.execute([1, 2], guestbookResponse, 'original')
75+
76+
expect(repository.submitGuestbookForDatafilesDownload).toHaveBeenCalledWith(
77+
[1, 2],
78+
guestbookResponse,
79+
'original'
80+
)
81+
expect(actual).toEqual('https://signed.datafiles?format=original')
82+
})
83+
4584
test('should submit dataset download and return signed url', async () => {
4685
const repository: IAccessRepository = {} as IAccessRepository
4786
repository.submitGuestbookForDatasetDownload = jest
@@ -53,11 +92,29 @@ describe('access download use cases', () => {
5392

5493
expect(repository.submitGuestbookForDatasetDownload).toHaveBeenCalledWith(
5594
'doi:10.5072/FK2/TEST',
56-
guestbookResponse
95+
guestbookResponse,
96+
undefined
5797
)
5898
expect(actual).toEqual('https://signed.dataset')
5999
})
60100

101+
test('should submit dataset download with format and return signed url', async () => {
102+
const repository: IAccessRepository = {} as IAccessRepository
103+
repository.submitGuestbookForDatasetDownload = jest
104+
.fn()
105+
.mockResolvedValue('https://signed.dataset?format=original')
106+
const sut = new SubmitGuestbookForDatasetDownload(repository)
107+
108+
const actual = await sut.execute('doi:10.5072/FK2/TEST', guestbookResponse, 'original')
109+
110+
expect(repository.submitGuestbookForDatasetDownload).toHaveBeenCalledWith(
111+
'doi:10.5072/FK2/TEST',
112+
guestbookResponse,
113+
'original'
114+
)
115+
expect(actual).toEqual('https://signed.dataset?format=original')
116+
})
117+
61118
test('should throw WriteError when dataset version download fails', async () => {
62119
const repository: IAccessRepository = {} as IAccessRepository
63120
repository.submitGuestbookForDatasetVersionDownload = jest
@@ -66,5 +123,29 @@ describe('access download use cases', () => {
66123
const sut = new SubmitGuestbookForDatasetVersionDownload(repository)
67124

68125
await expect(sut.execute(10, '2.0', guestbookResponse)).rejects.toThrow(WriteError)
126+
expect(repository.submitGuestbookForDatasetVersionDownload).toHaveBeenCalledWith(
127+
10,
128+
'2.0',
129+
guestbookResponse,
130+
undefined
131+
)
132+
})
133+
134+
test('should submit dataset version download with format and return signed url', async () => {
135+
const repository: IAccessRepository = {} as IAccessRepository
136+
repository.submitGuestbookForDatasetVersionDownload = jest
137+
.fn()
138+
.mockResolvedValue('https://signed.dataset.version?format=original')
139+
const sut = new SubmitGuestbookForDatasetVersionDownload(repository)
140+
141+
const actual = await sut.execute(10, '2.0', guestbookResponse, 'original')
142+
143+
expect(repository.submitGuestbookForDatasetVersionDownload).toHaveBeenCalledWith(
144+
10,
145+
'2.0',
146+
guestbookResponse,
147+
'original'
148+
)
149+
expect(actual).toEqual('https://signed.dataset.version?format=original')
69150
})
70151
})

0 commit comments

Comments
 (0)