forked from IQSS/dataverse-client-javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreateDataset.ts
59 lines (53 loc) · 2.75 KB
/
CreateDataset.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { UseCase } from '../../../core/domain/useCases/UseCase'
import { IDatasetsRepository } from '../repositories/IDatasetsRepository'
import { NewDatasetDTO, NewDatasetMetadataBlockValuesDTO } from '../dtos/NewDatasetDTO'
import { NewResourceValidator } from '../../../core/domain/useCases/validators/NewResourceValidator'
import { IMetadataBlocksRepository } from '../../../metadataBlocks/domain/repositories/IMetadataBlocksRepository'
import { MetadataBlock } from '../../../metadataBlocks'
import { CreatedDatasetIdentifiers } from '../models/CreatedDatasetIdentifiers'
import { ROOT_COLLECTION_ALIAS } from '../../../collections/domain/models/Collection'
export class CreateDataset implements UseCase<CreatedDatasetIdentifiers> {
private datasetsRepository: IDatasetsRepository
private metadataBlocksRepository: IMetadataBlocksRepository
private newDatasetValidator: NewResourceValidator
constructor(
datasetsRepository: IDatasetsRepository,
metadataBlocksRepository: IMetadataBlocksRepository,
newDatasetValidator: NewResourceValidator
) {
this.datasetsRepository = datasetsRepository
this.metadataBlocksRepository = metadataBlocksRepository
this.newDatasetValidator = newDatasetValidator
}
/**
* Creates a new Dataset in a collection, given a NewDatasetDTO object and an optional collection identifier, which defaults to root.
*
* @param {NewDatasetDTO} [newDataset] - NewDatasetDTO object including the new dataset metadata field values for each metadata block.
* @param {string} [collectionId] - Specifies the collection identifier where the new dataset should be created (optional, defaults to root).
* @returns {Promise<CreatedDatasetIdentifiers>}
* @throws {ResourceValidationError} - If there are validation errors related to the provided information.
* @throws {ReadError} - If there are errors while reading data.
* @throws {WriteError} - If there are errors while writing data.
*/
async execute(
newDataset: NewDatasetDTO,
collectionId = ROOT_COLLECTION_ALIAS
): Promise<CreatedDatasetIdentifiers> {
const metadataBlocks = await this.getNewDatasetMetadataBlocks(newDataset)
this.newDatasetValidator.validate(newDataset, metadataBlocks)
return this.datasetsRepository.createDataset(newDataset, metadataBlocks, collectionId)
}
async getNewDatasetMetadataBlocks(newDataset: NewDatasetDTO): Promise<MetadataBlock[]> {
const metadataBlocks: MetadataBlock[] = []
await Promise.all(
newDataset.metadataBlockValues.map(
async (metadataBlockValue: NewDatasetMetadataBlockValuesDTO) => {
metadataBlocks.push(
await this.metadataBlocksRepository.getMetadataBlockByName(metadataBlockValue.name)
)
}
)
)
return metadataBlocks
}
}