Skip to content

Commit

Permalink
Merge pull request #243 from SciCatProject/fix-linting-issues
Browse files Browse the repository at this point in the history
fix linting issues and add back the datasetlifecycle
  • Loading branch information
nitrosx authored Dec 13, 2022
2 parents 971e844 + c8abaf3 commit b11d900
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 75 deletions.
35 changes: 19 additions & 16 deletions src/datasets/datasets.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class DatasetsController {
@ApiOperation({
summary: "It creates a new dataset which can be a raw or derived one.",
description:
"It creates a new proposal and returnes it completed with systems fields.",
"It creates a new dataset and returns it completed with systems fields.",
})
@ApiExtraModels(CreateRawDatasetDto, CreateDerivedDatasetDto)
@ApiBody({
Expand All @@ -127,8 +127,7 @@ export class DatasetsController {
@ApiResponse({
status: 201,
type: DatasetClass,
description:
"Create a new proposal and return its representation in SciCat",
description: "Create a new dataset and return its representation in SciCat",
})
async create(
@Body() createDatasetDto: CreateRawDatasetDto | CreateDerivedDatasetDto,
Expand Down Expand Up @@ -214,7 +213,7 @@ export class DatasetsController {
status: 200,
type: Boolean,
description:
"Check if the proposal provided pass validation. It return true if the validation is passed",
"Check if the dataset provided pass validation. It return true if the validation is passed",
})
async isValid(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -389,7 +388,7 @@ export class DatasetsController {
status: 200,
type: DatasetClass,
isArray: true,
description: "Return proposals requested",
description: "Return datasets requested",
})
async fullfacet(
@Query() filters: { fields?: string; facets?: string },
Expand Down Expand Up @@ -586,7 +585,7 @@ export class DatasetsController {
@ApiExtraModels(UpdateRawDatasetDto, UpdateDerivedDatasetDto)
@ApiBody({
description:
"Fields that needs to be updated in the dataset. Only the fields that needs to be updated have to passed in.",
"Fields that needs to be updated in the dataset. Only the fields that needs to be updated have to be passed in.",
required: true,
schema: {
oneOf: [
Expand All @@ -609,6 +608,12 @@ export class DatasetsController {
return this.datasetsService.findByIdAndUpdate(pid, updateDatasetDto);
}

/**
* NOTE: PUT and PATCH functionality is exactly the same and behaves as a PATCH. They update only the fields that are passed in.
* In literature, the PUT method should replace completely the object requested with the values passed in.
* Here is an example of a proper implementation: https://wanago.io/2021/09/27/api-nestjs-put-patch-mongodb-mongoose/
* There is a ticket open for discussion where we decide how to move forward: https://jira.esss.lu.se/browse/SWAP-2942
*/
// PUT /datasets/:id
@UseGuards(PoliciesGuard)
@CheckPolicies((ability: AppAbility) =>
Expand All @@ -624,38 +629,36 @@ export class DatasetsController {
@ApiOperation({
summary: "It updates the dataset.",
description:
"It updates the dataset specified through the pid specified. The full dataset is updated.",
"It updates the dataset specified through the pid provided. Only the specified fields are updated(at the moment put and patch behavior is completely the same).",
})
@ApiParam({
name: "pid",
description: "Id of the dataset to modify",
type: String,
})
@ApiExtraModels(CreateRawDatasetDto, CreateDerivedDatasetDto)
@ApiExtraModels(UpdateRawDatasetDto, UpdateDerivedDatasetDto)
@ApiBody({
description:
"Complete dataset definition with new values to replace current ones. The complete dataset structure needs to be specified.",
"Fields that needs to be updated in the dataset. Only the fields that needs to be updated have to be passed in.",
required: true,
schema: {
oneOf: [
{ $ref: getSchemaPath(CreateRawDatasetDto) },
{ $ref: getSchemaPath(CreateDerivedDatasetDto) },
{ $ref: getSchemaPath(UpdateRawDatasetDto) },
{ $ref: getSchemaPath(UpdateDerivedDatasetDto) },
],
},
})
@ApiResponse({
status: 200,
type: DatasetClass,
description:
"Completely update an existing dataset and return its representation in SciCat",
"Update an existing dataset and return its representation in SciCat",
})
async findByIdReplaceOrCreate(
@Param("pid") id: string,
@Body() replaceDatasetDto: CreateRawDatasetDto | CreateDerivedDatasetDto,
@Body() updateDatasetDto: UpdateRawDatasetDto | UpdateDerivedDatasetDto,
): Promise<DatasetClass | null> {
// validate dataset
const validatedDatasetDto = await this.validateDataset(replaceDatasetDto);
return this.datasetsService.findByIdAndUpdate(id, validatedDatasetDto);
return this.datasetsService.findByIdAndUpdate(id, updateDatasetDto);
}

// DELETE /datasets/:id
Expand Down
2 changes: 1 addition & 1 deletion src/datasets/datasets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export class DatasetsService {
previousValue: dataset[updatedField as keyof UpdateDatasetDto],
};
});
dataset.history = dataset.history ?? []
dataset.history = dataset.history ?? [];
dataset.history.push({
updatedBy: (req.user as JWTUser).username,
...JSON.parse(JSON.stringify(historyItem).replace(/\$/g, "")),
Expand Down
20 changes: 15 additions & 5 deletions src/datasets/dto/create-dataset.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { ApiProperty, ApiTags, getSchemaPath } from "@nestjs/swagger";
import { DatasetType } from "../dataset-type.enum";
import { Type } from "class-transformer";
import { CreateRelationshipDto } from "./create-relationship.dto";
import { CreateTechniqueDto } from "./create-technique.dto"
import { CreateTechniqueDto } from "./create-technique.dto";
import { LifecycleClass } from "../schemas/lifecycle.schema";

@ApiTags("datasets")
export class CreateDatasetDto extends OwnableDto {
Expand Down Expand Up @@ -82,14 +83,14 @@ export class CreateDatasetDto extends OwnableDto {
required: false,
description:
"DNS host name of file server hosting sourceFolder, optionally including protocol e.g. [protocol://]fileserver1.example.com",
})
})
@IsOptional()
@IsFQDN()
readonly sourceFolderHost?: string;

/*
* size and number of files fields should be managed by the system
*/
*/
@ApiProperty({
type: Number,
default: 0,
Expand All @@ -107,7 +108,7 @@ export class CreateDatasetDto extends OwnableDto {
required: false,
description:
"Total size of all datablock package files created for this dataset",
})
})
@IsOptional()
@IsInt()
readonly packedSize?: number = 0;
Expand Down Expand Up @@ -273,6 +274,16 @@ export class CreateDatasetDto extends OwnableDto {
@Type(() => CreateRelationshipDto)
readonly relationships?: RelationshipClass[];

@ApiProperty({
type: LifecycleClass,
required: false,
default: {},
description:
"For each dataset there exists an embedded dataset lifecycle document which describes the current status of the dataset during its lifetime with respect to the storage handling systems",
})
@IsOptional()
readonly datasetlifecycle: LifecycleClass;

@ApiProperty({
type: Object,
required: false,
Expand All @@ -283,4 +294,3 @@ export class CreateDatasetDto extends OwnableDto {
@IsObject()
readonly scientificMetadata?: Record<string, unknown>;
}

5 changes: 3 additions & 2 deletions src/datasets/dto/create-raw-dataset.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export class CreateRawDatasetDto extends CreateDatasetDto {
@ApiProperty({
type: String,
required: true,
description: "Email of principal investigator. This field is required if the dataset is a Raw dataset.",
description:
"Email of principal investigator. This field is required if the dataset is a Raw dataset.",
})
@IsString()
readonly principalInvestigator: string;
Expand All @@ -17,7 +18,7 @@ export class CreateRawDatasetDto extends CreateDatasetDto {
required: false,
description:
"Time of end of data taking for this dataset, format according to chapter 5.6 internet date/time format in RFC 3339. Local times without timezone/offset info are automatically transformed to UTC using the timezone of the API server. This field is required if the dataset is a Raw dataset",
})
})
@IsOptional()
@IsDateString()
readonly endTime?: Date;
Expand Down
3 changes: 2 additions & 1 deletion src/datasets/dto/create-technique.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export class CreateTechniqueDto {
@ApiProperty({
type: String,
required: true,
description: "Persistent Identifier of the technique. Usually it is a UUIDv4",
description:
"Persistent Identifier of the technique. Usually it is a UUIDv4",
})
@IsString()
readonly pid: string;
Expand Down
11 changes: 8 additions & 3 deletions src/datasets/interceptors/fullquery.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,21 @@ export class FullQueryInterceptor implements NestInterceptor {
scientificMetadata,
`${lhs}.value`,
) as number;
if (unit && currentUnit && currentUnit !== unit) {
if (
unit &&
currentUnit &&
currentUnit !== unit &&
scientificMetadata
) {
const { valueRequested, unitRequested } =
convertToRequestedUnit(currentValue, currentUnit, unit);
lodash.update(
scientificMetadata as Object,
scientificMetadata,
`${lhs}.unit`,
() => unitRequested,
);
lodash.update(
scientificMetadata as Object,
scientificMetadata,
`${lhs}.value`,
() => valueRequested,
);
Expand Down
38 changes: 17 additions & 21 deletions src/datasets/schemas/dataset.schema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty, getSchemaPath } from "@nestjs/swagger";
import { Document } from "mongoose";
import { Attachment, AttachmentSchema } from "src/attachments/schemas/attachment.schema";
/*
import {
Attachment,
AttachmentSchema,
} from "src/attachments/schemas/attachment.schema";
import { OwnableClass } from "src/common/schemas/ownable.schema";
import {
Datablock,
DatablockSchema,
Expand All @@ -15,10 +14,6 @@ import {
OrigDatablock,
OrigDatablockSchema,
} from "src/origdatablocks/schemas/origdatablock.schema";
*/
import { OwnableClass } from "src/common/schemas/ownable.schema";
import { Datablock, DatablockSchema } from "src/datablocks/schemas/datablock.schema";
import { OrigDatablock, OrigDatablockSchema } from "src/origdatablocks/schemas/origdatablock.schema";
import { v4 as uuidv4 } from "uuid";
import { DatasetType } from "../dataset-type.enum";
import { HistoryClass, HistorySchema } from "./history.schema";
Expand Down Expand Up @@ -104,10 +99,10 @@ export class DatasetClass extends OwnableClass {
description:
"Email of contact person for this dataset. The string may contain a list of emails, which should then be seperated by semicolons.",
})
@Prop({
type: String,
required: true,
index: true
@Prop({
type: String,
required: true,
index: true,
})
contactEmail: string;

Expand All @@ -134,10 +129,10 @@ export class DatasetClass extends OwnableClass {
description:
"DNS host name of file server hosting sourceFolder, optionally including protocol e.g. [protocol://]fileserver1.example.com",
})
@Prop({
type: String,
@Prop({
type: String,
required: false,
index: true
index: true,
})
sourceFolderHost?: string;

Expand All @@ -148,11 +143,11 @@ export class DatasetClass extends OwnableClass {
description:
"Total size of all source files contained in source folder on disk when unpacked",
})
@Prop({
@Prop({
type: Number,
required: true,
index: true,
default: 0
required: true,
index: true,
default: 0,
})
size: number;

Expand Down Expand Up @@ -333,10 +328,10 @@ export class DatasetClass extends OwnableClass {
default: [],
description: "List of users that the dataset has been shared with",
})
@Prop({
@Prop({
type: [String],
required: false,
default: []
default: [],
})
sharedWith?: string[];

Expand Down Expand Up @@ -371,7 +366,7 @@ export class DatasetClass extends OwnableClass {
})
@Prop({ type: [DatablockSchema], default: [] })
datablocks: Datablock[];

@ApiProperty({
type: Object,
required: false,
Expand All @@ -387,7 +382,8 @@ export class DatasetClass extends OwnableClass {
@ApiProperty({
type: String,
required: false,
description: "Email of principal investigator. This field is required if the dataset is a Raw dataset.",
description:
"Email of principal investigator. This field is required if the dataset is a Raw dataset.",
})
@Prop({ type: String, required: false })
principalInvestigator?: string;
Expand Down
39 changes: 18 additions & 21 deletions src/datasets/schemas/history.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import { v4 as uuidv4 } from "uuid";

export type HistoryDocument = HistoryClass & Document;

@Schema({
strict: false,
timestamps: {
createdAt: false,
updatedAt: true
}
@Schema({
strict: false,
timestamps: {
createdAt: false,
updatedAt: true,
},
})
export class HistoryClass {

@ApiProperty({
type: String,
required: true,
Expand All @@ -25,7 +24,7 @@ export class HistoryClass {
type: String,
//unique: true,
required: true,
default: uuidv4()
default: uuidv4(),
})
/*
id: string;
Expand All @@ -51,26 +50,24 @@ export class HistoryClass {
type: Date,
required: true,
default: Date.now(),
description:
"Time when update was performed.",
})
@Prop({
type: Date,
required: true,
default: Date.now()
description: "Time when update was performed.",
})
@Prop({
type: Date,
required: true,
default: Date.now(),
})
updatedAt: Date;

@ApiProperty({
type: Date,
required: true,
default: Date.now(),
description:
"Username of the user that performed the update.",
})
@Prop({
type: String,
required: true
description: "Username of the user that performed the update.",
})
@Prop({
type: String,
required: true,
})
updatedBy: string;
}
Expand Down
Loading

0 comments on commit b11d900

Please sign in to comment.