Skip to content

Commit

Permalink
Merge branch 'main' into mpi-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-krenz committed Sep 13, 2024
2 parents f849e19 + cd943e8 commit 2c72c24
Show file tree
Hide file tree
Showing 28 changed files with 290 additions and 71 deletions.
35 changes: 0 additions & 35 deletions .github/workflows/on-push-main.yaml

This file was deleted.

28 changes: 0 additions & 28 deletions .github/workflows/on-release.yaml

This file was deleted.

1 change: 0 additions & 1 deletion scripts/src/commands/load/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export default class LoadDatabase extends Command {
async insertServices(services: any[]): Promise<Map<string, ObjectId>> {
/** Maps the ID as defined in the JSON file to the ID in the database */
const serviceMap = new Map<string, ObjectId>();

// For each of the services, save the info not including
// the allowed connections
for (const service of services) {
Expand Down
1 change: 1 addition & 0 deletions scripts/src/commands/reset/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class Reset extends Command {
const { flags } = await this.parse(Reset);

// Connect to the database
console.log(`${flags.db} connection url`);
const client = await MongoClient.connect(flags.db);
await client.connect();

Expand Down
3 changes: 2 additions & 1 deletion src/bundles/bundles.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { DampLabServicesModule } from '../services/damplab-services.module';
import { Bundle, BundleSchema } from './bundles.model';
import { BundlesResolver } from './bundles.resolver';
import { BundlesService } from './bundles.service';
import { BundleUpdatePipe } from './update.pipe';

@Module({
imports: [MongooseModule.forFeature([{ name: Bundle.name, schema: BundleSchema }]), DampLabServicesModule],
providers: [BundlesService, BundlesResolver]
providers: [BundlesService, BundlesResolver, BundleUpdatePipe]
})
export class BundlesModule {}
17 changes: 17 additions & 0 deletions src/bundles/bundles.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Injectable, NotFoundException, PipeTransform } from '@nestjs/common';
import { Bundle } from './bundles.model';
import { BundlesService } from './bundles.service';

@Injectable()
export class BundlesPipe implements PipeTransform<string, Promise<Bundle>> {
constructor(private readonly bundleService: BundlesService) {}

async transform(value: string): Promise<Bundle> {
const bundle = await this.bundleService.find(value);

if (!bundle) {
throw new NotFoundException(`Bundle with id ${value} not found`);
}
return bundle;
}
}
10 changes: 9 additions & 1 deletion src/bundles/bundles.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
import { Parent, Query, ResolveField, Resolver, Args, Mutation, ID } from '@nestjs/graphql';
import { DampLabServices } from '../services/damplab-services.services';
import { DampLabService } from '../services/models/damplab-service.model';
import { Bundle } from './bundles.model';
import { BundlesService } from './bundles.service';
import { BundlesPipe } from './bundles.pipe';
import { BundleChange } from './dtos/update.dto';
import { BundleUpdatePipe } from './update.pipe';

@Resolver(() => Bundle)
export class BundlesResolver {
Expand All @@ -13,6 +16,11 @@ export class BundlesResolver {
return this.bundlesService.findAll();
}

@Mutation(() => Bundle)
async updateBundle(@Args('bundle', { type: () => ID }, BundlesPipe) bundle: Bundle, @Args('changes', { type: () => BundleChange }, BundleUpdatePipe) changes: BundleChange): Promise<Bundle> {
return this.bundlesService.update(bundle, changes);
}

@ResolveField()
async services(@Parent() bundle: Bundle): Promise<DampLabService[]> {
return this.dampLabServices.findByIds(bundle.services);
Expand Down
10 changes: 10 additions & 0 deletions src/bundles/bundles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Bundle } from './bundles.model';
import { Model } from 'mongoose';
import { BundleChange } from './dtos/update.dto';

@Injectable()
export class BundlesService {
constructor(@InjectModel(Bundle.name) private readonly bundleModel: Model<Bundle>) {}

async find(id: string): Promise<Bundle | null> {
return this.bundleModel.findById(id);
}

async findAll(): Promise<Bundle[]> {
return this.bundleModel.find().exec();
}

async update(bundle: Bundle, changes: BundleChange): Promise<Bundle> {
await this.bundleModel.updateOne({ _id: bundle.id }, changes);
return (await this.find(bundle.id))!;
}
}
8 changes: 8 additions & 0 deletions src/bundles/dtos/update.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Bundle } from '../bundles.model';
import { ID, InputType, OmitType, PartialType, Field } from '@nestjs/graphql';

@InputType()
export class BundleChange extends PartialType(OmitType(Bundle, ['id', 'services'] as const), InputType) {
@Field(() => [ID], { nullable: true })
services: string[];
}
17 changes: 17 additions & 0 deletions src/bundles/update.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Injectable, PipeTransform } from '@nestjs/common';
import { DampLabServicePipe } from '../services/damplab-services.pipe';
import { BundleChange } from './dtos/update.dto';

@Injectable()
export class BundleUpdatePipe implements PipeTransform<BundleChange, Promise<BundleChange>> {
constructor(private readonly damplabServicePipe: DampLabServicePipe) {}

async transform(value: BundleChange): Promise<BundleChange> {
// If services is includes, make sure they are all valid
if (value.services) {
await Promise.all(value.services.map((service) => this.damplabServicePipe.transform(service)));
}

return value;
}
}
4 changes: 3 additions & 1 deletion src/categories/categories.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { Category, CategorySchema } from './category.model';
import { CategoryService } from './categories.service';
import { CategoryResolver } from './categories.resolver';
import { DampLabServicesModule } from '../services/damplab-services.module';
import { CategoryPipe } from './categories.pipe';
import { CategoryUpdatePipe } from './update.pipe';

@Module({
imports: [MongooseModule.forFeature([{ name: Category.name, schema: CategorySchema }]), DampLabServicesModule],
providers: [CategoryService, CategoryResolver]
providers: [CategoryService, CategoryResolver, CategoryPipe, CategoryUpdatePipe]
})
export class CategoriesModule {}
19 changes: 19 additions & 0 deletions src/categories/categories.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { NotFoundException, Injectable, PipeTransform } from '@nestjs/common';
import { Category } from './category.model';
import { CategoryService } from './categories.service';

@Injectable()
export class CategoryPipe implements PipeTransform<string, Promise<Category>> {
constructor(private readonly categoryService: CategoryService) {}

async transform(value: string): Promise<Category> {
try {
const category = await this.categoryService.find(value);
if (category) {
return category;
}
} catch (e) {}

throw new NotFoundException(`Category with id ${value} not found`);
}
}
26 changes: 25 additions & 1 deletion src/categories/categories.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Query, ResolveField, Resolver } from '@nestjs/graphql';
import { Args, Mutation, Query, ResolveField, Resolver, ID } from '@nestjs/graphql';
import { Category } from './category.model';
import { CategoryService } from './categories.service';
import { DampLabServices } from '../services/damplab-services.services';
import { DampLabService } from '../services/models/damplab-service.model';
import { CategoryPipe } from './categories.pipe';
import { CategoryChange } from './dtos/update.dto';
import { CategoryUpdatePipe } from './update.pipe';
import { CreateCategory } from './dtos/create.dto';
import { CreateCategoryPipe } from './create.pipe';

@Resolver(() => Category)
export class CategoryResolver {
Expand All @@ -13,6 +18,25 @@ export class CategoryResolver {
return this.categoryService.findAll();
}

@Mutation(() => Category)
async updateCategory(
@Args('category', { type: () => ID }, CategoryPipe) category: Category,
@Args('changes', { type: () => CategoryChange }, CategoryUpdatePipe) changes: CategoryChange
): Promise<Category> {
return this.categoryService.update(category, changes);
}

@Mutation(() => Boolean)
async deleteCategory(@Args('category', { type: () => ID }, CategoryPipe) category: Category): Promise<boolean> {
await this.categoryService.delete(category);
return true;
}

@Mutation(() => Category)
async createCategory(@Args('category', CreateCategoryPipe) category: CreateCategory): Promise<Category> {
return this.categoryService.create(category);
}

/**
* Resolver for the services field of the Category type
*/
Expand Down
19 changes: 19 additions & 0 deletions src/categories/categories.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Category, CategoryDocument } from './category.model';
import { Model } from 'mongoose';
import { CategoryChange } from './dtos/update.dto';
import { CreateCategory } from './dtos/create.dto';

@Injectable()
export class CategoryService {
Expand All @@ -10,4 +12,21 @@ export class CategoryService {
async findAll(): Promise<Category[]> {
return this.categoryModel.find().exec();
}

async find(id: string): Promise<Category | null> {
return this.categoryModel.findById(id);
}

async update(category: Category, change: CategoryChange): Promise<Category> {
await this.categoryModel.updateOne({ _id: category._id }, change);
return (await this.find(category._id))!;
}

async delete(category: Category): Promise<void> {
await this.categoryModel.deleteOne({ _id: category._id });
}

async create(category: CreateCategory): Promise<Category> {
return this.categoryModel.create(category);
}
}
17 changes: 17 additions & 0 deletions src/categories/create.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Injectable, PipeTransform } from '@nestjs/common';
import { DampLabServicePipe } from '../services/damplab-services.pipe';
import { CreateCategory } from './dtos/create.dto';

@Injectable()
export class CreateCategoryPipe implements PipeTransform<CreateCategory, Promise<CreateCategory>> {
constructor(private readonly damplabServicePipe: DampLabServicePipe) {}

async transform(value: CreateCategory): Promise<CreateCategory> {
// Ensure the services are valid
for (const service of value.services) {
await this.damplabServicePipe.transform(service);
}

return value;
}
}
8 changes: 8 additions & 0 deletions src/categories/dtos/create.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ID, InputType, OmitType, Field } from '@nestjs/graphql';
import { Category } from '../category.model';

@InputType()
export class CreateCategory extends OmitType(Category, ['_id', 'services'] as const, InputType) {
@Field(() => [ID])
services: string[];
}
8 changes: 8 additions & 0 deletions src/categories/dtos/update.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Category } from '../category.model';
import { ID, OmitType, PartialType, Field, InputType } from '@nestjs/graphql';

@InputType()
export class CategoryChange extends PartialType(OmitType(Category, ['_id', 'services'] as const), InputType) {
@Field(() => [ID], { nullable: true })
services: string[];
}
17 changes: 17 additions & 0 deletions src/categories/update.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Injectable, PipeTransform } from '@nestjs/common';
import { DampLabServicePipe } from '../services/damplab-services.pipe';
import { CategoryChange } from './dtos/update.dto';

@Injectable()
export class CategoryUpdatePipe implements PipeTransform<CategoryChange, Promise<CategoryChange>> {
constructor(private readonly damplabServicePipe: DampLabServicePipe) {}

async transform(value: CategoryChange): Promise<CategoryChange> {
// If services is includes, make sure they are all valid
if (value.services) {
await Promise.all(value.services.map((service) => this.damplabServicePipe.transform(service)));
}

return value;
}
}
3 changes: 3 additions & 0 deletions src/reset/dtos/service.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ export class ServiceInput {

@Field(() => [String], { nullable: true })
resultParams?: string[];

@Field(() => JSON, { nullable: true })
paramGroups?: any[];
}
3 changes: 2 additions & 1 deletion src/reset/reset.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export class ResetService {
allowedConnections: [],
result: service.result,
description: service.description,
resultParams: service.resultParams
resultParams: service.resultParams,
paramGroups: service.paramGroups
});

// Update the map
Expand Down
17 changes: 17 additions & 0 deletions src/services/create.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Injectable, PipeTransform } from '@nestjs/common';
import { DampLabServicePipe } from '../services/damplab-services.pipe';
import { CreateService } from './dtos/create.dto';

@Injectable()
export class CreateServicePipe implements PipeTransform<CreateService, Promise<CreateService>> {
constructor(private readonly damplabServicePipe: DampLabServicePipe) {}

async transform(value: CreateService): Promise<CreateService> {
// Ensure the services are valid
for (const service of value.allowedConnections) {
await this.damplabServicePipe.transform(service);
}

return value;
}
}
4 changes: 3 additions & 1 deletion src/services/damplab-services.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { CreateServicePipe } from './create.pipe';
import { DampLabServicePipe } from './damplab-services.pipe';
import { DampLabServicesResolver } from './damplab-services.resolver';
import { DampLabServices } from './damplab-services.services';
import { DampLabService, DampLabServiceSchema } from './models/damplab-service.model';
import { ServiceUpdatePipe } from './update.pipe';

@Module({
imports: [MongooseModule.forFeature([{ name: DampLabService.name, schema: DampLabServiceSchema }])],
providers: [DampLabServicesResolver, DampLabServices, DampLabServicePipe],
providers: [DampLabServicesResolver, DampLabServices, DampLabServicePipe, ServiceUpdatePipe, CreateServicePipe],
exports: [DampLabServices, DampLabServicePipe]
})
export class DampLabServicesModule {}
Loading

0 comments on commit 2c72c24

Please sign in to comment.