Skip to content

Commit

Permalink
update dataset schema and module to allow custom type names
Browse files Browse the repository at this point in the history
  • Loading branch information
HayenNico committed Nov 26, 2024
1 parent 01e9f09 commit 2467577
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 12 additions & 5 deletions src/datasets/datasets.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { forwardRef, Module } from "@nestjs/common";
import { BadRequestException, forwardRef, Module } from "@nestjs/common";
import { MongooseModule } from "@nestjs/mongoose";
import { DatasetClass, DatasetSchema } from "./schemas/dataset.schema";
import { DatasetsController } from "./datasets.controller";
import { DatasetsService } from "./datasets.service";
import { CaslAbilityFactory } from "src/casl/casl-ability.factory";
import { AttachmentsModule } from "src/attachments/attachments.module";
import { ConfigModule } from "@nestjs/config";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { OrigDatablocksModule } from "src/origdatablocks/origdatablocks.module";
import { DatablocksModule } from "src/datablocks/datablocks.module";
import { InitialDatasetsModule } from "src/initial-datasets/initial-datasets.module";
Expand All @@ -26,8 +26,11 @@ import { ElasticSearchModule } from "src/elastic-search/elastic-search.module";
MongooseModule.forFeatureAsync([
{
name: DatasetClass.name,
imports: [PoliciesModule],
useFactory: (policyService: PoliciesService) => {
imports: [PoliciesModule, ConfigModule], //TEST: remove ConfigModule import here
inject: [PoliciesService, ConfigService],
useFactory: (policyService: PoliciesService, configService: ConfigService) => {
const datasetTypes = configService.get("datasetTypes") || "{}";
const datasetTypesArray: string[] = Object.values(datasetTypes);
const schema = DatasetSchema;

schema.pre<DatasetClass>("save", async function (next) {
Expand All @@ -36,6 +39,11 @@ import { ElasticSearchModule } from "src/elastic-search/elastic-search.module";
if (!this._id) {
this._id = this.pid;
}
if (this.type && !datasetTypesArray.includes(this.type)) {
throw new BadRequestException(
`type must be one of the following values: ${datasetTypesArray.join(", ")}`,
);
}
const policy = await policyService.findOne({
ownerGroup: this.ownerGroup,
});
Expand All @@ -59,7 +67,6 @@ import { ElasticSearchModule } from "src/elastic-search/elastic-search.module";

return schema;
},
inject: [PoliciesService],
},
]),
],
Expand Down
4 changes: 1 addition & 3 deletions src/datasets/schemas/dataset.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,12 @@ export class DatasetClass extends OwnableClass {
@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.",
"Characterize type of dataset. Either 'raw' or 'derived' by default, additional custom options are defined in datasetTypes.json.",
})
@Prop({
type: String,
required: true,
enum: [DatasetType.Raw, DatasetType.Derived],
index: true,
})
type: string;
Expand Down

0 comments on commit 2467577

Please sign in to comment.