Skip to content

Commit

Permalink
update dataset create endpoint to accept custom typed datasets
Browse files Browse the repository at this point in the history
  • Loading branch information
HayenNico committed Nov 26, 2024
1 parent 2467577 commit f3777c0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
57 changes: 43 additions & 14 deletions src/datasets/datasets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ export class DatasetsController {
dataset:
| CreateRawDatasetObsoleteDto
| CreateDerivedDatasetObsoleteDto
| CreateDatasetDto
| DatasetClass,
): Promise<DatasetClass> {
const datasetInstance = new DatasetClass();
Expand All @@ -355,7 +356,7 @@ export class DatasetsController {

async checkPermissionsForObsoleteDatasetCreate(
request: Request,
dataset: CreateRawDatasetObsoleteDto | CreateDerivedDatasetObsoleteDto,
dataset: CreateRawDatasetObsoleteDto | CreateDerivedDatasetObsoleteDto | CreateDatasetDto,
) {
const user: JWTUser = request.user as JWTUser;

Expand Down Expand Up @@ -400,10 +401,13 @@ export class DatasetsController {
inputObsoleteDataset:
| CreateRawDatasetObsoleteDto
| CreateDerivedDatasetObsoleteDto
| CreateDatasetDto
| UpdateRawDatasetObsoleteDto
| UpdateDerivedDatasetObsoleteDto
| UpdateDatasetDto
| PartialUpdateRawDatasetObsoleteDto
| PartialUpdateDerivedDatasetObsoleteDto,
| PartialUpdateDerivedDatasetObsoleteDto
| PartialUpdateDatasetDto,
): CreateDatasetDto | UpdateDatasetDto | PartialUpdateDatasetDto {
const propertiesModifier: Record<string, unknown> = {
version: "v3",
Expand All @@ -429,7 +433,11 @@ export class DatasetsController {
(inputObsoleteDataset as CreateRawDatasetObsoleteDto).instrumentId,
];
}
} else {
} else if (
inputObsoleteDataset instanceof CreateDerivedDatasetObsoleteDto ||
inputObsoleteDataset instanceof UpdateDerivedDatasetObsoleteDto ||
inputObsoleteDataset instanceof PartialUpdateDerivedDatasetObsoleteDto
) {
if ("investigator" in inputObsoleteDataset) {
propertiesModifier.principalInvestigator = (
inputObsoleteDataset as CreateDerivedDatasetObsoleteDto
Expand All @@ -443,23 +451,26 @@ export class DatasetsController {
| PartialUpdateDatasetDto = {};
if (
inputObsoleteDataset instanceof CreateRawDatasetObsoleteDto ||
inputObsoleteDataset instanceof CreateDerivedDatasetObsoleteDto
inputObsoleteDataset instanceof CreateDerivedDatasetObsoleteDto ||
inputObsoleteDataset instanceof CreateDatasetDto
) {
outputDataset = {
...(inputObsoleteDataset as CreateDatasetDto),
...propertiesModifier,
} as CreateDatasetDto;
} else if (
inputObsoleteDataset instanceof UpdateRawDatasetObsoleteDto ||
inputObsoleteDataset instanceof UpdateDerivedDatasetObsoleteDto
inputObsoleteDataset instanceof UpdateDerivedDatasetObsoleteDto ||
inputObsoleteDataset instanceof UpdateDatasetDto
) {
outputDataset = {
...(inputObsoleteDataset as UpdateDatasetDto),
...propertiesModifier,
} as UpdateDatasetDto;
} else if (
inputObsoleteDataset instanceof PartialUpdateRawDatasetObsoleteDto ||
inputObsoleteDataset instanceof PartialUpdateDerivedDatasetObsoleteDto
inputObsoleteDataset instanceof PartialUpdateDerivedDatasetObsoleteDto ||
inputObsoleteDataset instanceof PartialUpdateDatasetDto
) {
outputDataset = {
...(inputObsoleteDataset as PartialUpdateDatasetDto),
Expand Down Expand Up @@ -515,14 +526,15 @@ export class DatasetsController {
description:
"It creates a new dataset and returns it completed with systems fields.",
})
@ApiExtraModels(CreateRawDatasetObsoleteDto, CreateDerivedDatasetObsoleteDto)
@ApiExtraModels(CreateRawDatasetObsoleteDto, CreateDerivedDatasetObsoleteDto, CreateDatasetDto)
@ApiBody({
description: "Input fields for the dataset to be created",
required: true,
schema: {
oneOf: [
{ $ref: getSchemaPath(CreateRawDatasetObsoleteDto) },
{ $ref: getSchemaPath(CreateDerivedDatasetObsoleteDto) },
{ $ref: getSchemaPath(CreateDatasetDto) },
],
},
})
Expand All @@ -536,15 +548,26 @@ export class DatasetsController {
@Body()
createDatasetObsoleteDto:
| CreateRawDatasetObsoleteDto
| CreateDerivedDatasetObsoleteDto,
| CreateDerivedDatasetObsoleteDto
| CreateDatasetDto,
): Promise<OutputDatasetObsoleteDto> {
// validate dataset
let dtoType;
switch (createDatasetObsoleteDto.type) {
case DatasetType.Raw:
dtoType = CreateRawDatasetObsoleteDto;
break;
case DatasetType.Derived:
dtoType = CreateDerivedDatasetObsoleteDto;
break;
default:
dtoType = CreateDatasetDto;
break;
}
const validatedDatasetObsoleteDto = (await this.validateDatasetObsolete(
createDatasetObsoleteDto,
createDatasetObsoleteDto.type === "raw"
? CreateRawDatasetObsoleteDto
: CreateDerivedDatasetObsoleteDto,
)) as CreateRawDatasetObsoleteDto | CreateDerivedDatasetObsoleteDto;
dtoType
)) as CreateRawDatasetObsoleteDto | CreateDerivedDatasetObsoleteDto | CreateDatasetDto;

const obsoleteDatasetDto =
await this.checkPermissionsForObsoleteDatasetCreate(
Expand Down Expand Up @@ -576,17 +599,23 @@ export class DatasetsController {
inputDatasetDto:
| CreateRawDatasetObsoleteDto
| CreateDerivedDatasetObsoleteDto
| CreateDatasetDto
| PartialUpdateRawDatasetObsoleteDto
| PartialUpdateDerivedDatasetObsoleteDto
| PartialUpdateDatasetDto
| UpdateRawDatasetObsoleteDto
| UpdateDerivedDatasetObsoleteDto,
| UpdateDerivedDatasetObsoleteDto
| UpdateDatasetDto,
dto: ClassConstructor<
| CreateRawDatasetObsoleteDto
| CreateDerivedDatasetObsoleteDto
| CreateDatasetDto
| PartialUpdateRawDatasetObsoleteDto
| PartialUpdateDerivedDatasetObsoleteDto
| PartialUpdateDatasetDto
| UpdateRawDatasetObsoleteDto
| UpdateDerivedDatasetObsoleteDto
| UpdateDatasetDto
>,
) {
const validateOptions: ValidatorOptions = {
Expand All @@ -607,7 +636,7 @@ export class DatasetsController {
(CreateRawDatasetObsoleteDto || CreateDerivedDatasetObsoleteDto)
) {
if (
!(Object.values(DatasetType) as string[]).includes(
!(Object.values(configuration().datasetTypes) as string[]).includes(
outputDatasetDto.type,
)
) {
Expand Down
6 changes: 2 additions & 4 deletions src/datasets/dto/create-dataset.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { IsEnum, IsOptional, IsString } from "class-validator";
import { IsOptional, IsString } from "class-validator";
import { ApiProperty } from "@nestjs/swagger";
import { DatasetType } from "../dataset-type.enum";
import { UpdateDatasetDto } from "./update-dataset.dto";

export class CreateDatasetDto extends UpdateDatasetDto {
Expand All @@ -16,10 +15,9 @@ export class CreateDatasetDto extends UpdateDatasetDto {
@ApiProperty({
type: String,
required: true,
enum: [DatasetType.Raw, DatasetType.Derived],
description:
"Characterize type of dataset, either 'raw' or 'derived'. Autofilled when choosing the proper inherited models.",
})
@IsEnum(DatasetType)
@IsString()
readonly type: string;
}

0 comments on commit f3777c0

Please sign in to comment.