Skip to content

Commit

Permalink
working?
Browse files Browse the repository at this point in the history
  • Loading branch information
mbystedt committed Dec 10, 2024
1 parent 5441e24 commit b822870
Show file tree
Hide file tree
Showing 20 changed files with 461 additions and 84 deletions.
10 changes: 8 additions & 2 deletions src/graph/graph.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,13 @@ export class GraphController {
})
@UseGuards(BrokerOidcAuthGuard)
@ApiBearerAuth()
@UsePipes(new ValidationPipe({ transform: true }))
editEdge(
@Req() request: Request,
@Param('id') id: string,
@Body() edge: EdgeInsertDto,
) {
console.log(edge);
return this.graph.editEdge(request, id, edge);
}

Expand Down Expand Up @@ -237,7 +239,9 @@ export class GraphController {
])
@UseGuards(BrokerOidcAuthGuard)
@ApiBearerAuth()
@UsePipes(new ValidationPipe({ transform: true }))
@UsePipes(
new ValidationPipe({ transform: true, skipUndefinedProperties: true }),
)
addVertex(@Req() request: Request, @Body() vertex: VertexInsertDto) {
return this.graph.addVertex(request, vertex);
}
Expand Down Expand Up @@ -282,7 +286,9 @@ export class GraphController {
})
@UseGuards(BrokerOidcAuthGuard)
@ApiBearerAuth()
@UsePipes(new ValidationPipe({ transform: true }))
@UsePipes(
new ValidationPipe({ transform: true, skipUndefinedProperties: true }),
)
editVertex(
@Req() request: Request,
@Param('id') id: string,
Expand Down
43 changes: 22 additions & 21 deletions src/graph/graph.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Request } from 'express';
import ejs from 'ejs';
import { ValidationError, wrap } from '@mikro-orm/core';
import { get, set } from 'lodash';
// import { validate } from 'class-validator';
import { validate } from 'class-validator';

import { GraphRepository } from '../persistence/interfaces/graph.repository';
import { VertexEntity } from '../persistence/entity/vertex.entity';
Expand Down Expand Up @@ -36,7 +36,7 @@ import { AuthService } from '../auth/auth.service';
import { ServiceInstanceDto } from '../persistence/dto/service-instance.dto';
import { EnvironmentDto } from '../persistence/dto/environment.dto';
import { CollectionEntityUnion } from '../persistence/entity/collection-entity-union.type';
// import { ValidatorUtil } from '../util/validator.util';
import { ValidatorUtil } from '../util/validator.util';

@Injectable()
export class GraphService {
Expand All @@ -46,7 +46,7 @@ export class GraphService {
private readonly collectionRepository: CollectionRepository,
private readonly graphRepository: GraphRepository,
private readonly redisService: RedisService,
// private readonly validatorUtil: ValidatorUtil,
private readonly validatorUtil: ValidatorUtil,
) {}

public async getData(
Expand Down Expand Up @@ -385,12 +385,25 @@ export class GraphService {
});
}

for (const [key, field] of Object.entries(config.fields)) {
if (field.type === 'date' && vertexInsert.data[key]) {
vertexInsert.data[key] = new Date(
vertexInsert.data[key].split('T')[0] + 'T00:00:00.000Z',
);
}
// for (const [key, field] of Object.entries(config.fields)) {
// if (field.type === 'date' && vertexInsert.data[key]) {
// vertexInsert.data[key] = new Date(
// vertexInsert.data[key].split('T')[0] + 'T00:00:00.000Z',
// );
// }
// }
console.log(vertexInsert);
const errors = await validate(vertexInsert, {
whitelist: true,
forbidNonWhitelisted: true,
forbidUnknownValues: true,
});
if (errors.length > 0) {
throw new BadRequestException({
statusCode: 400,
message: 'Collection validation error',
error: this.validatorUtil.buildFirstFailedPropertyErrorMsg(errors[0]),
});
}

const collection = this.collectionRepository.assignCollection(
Expand All @@ -403,18 +416,6 @@ export class GraphService {
message: 'No data',
});
}
// const errors = await validate(collection, {
// whitelist: true,
// forbidNonWhitelisted: true,
// forbidUnknownValues: true,
// });
// if (errors.length > 0) {
// throw new BadRequestException({
// statusCode: 400,
// message: 'Collection validation error',
// error: this.validatorUtil.buildFirstFailedPropertyErrorMsg(errors[0]),
// });
// }

if (
checkPermission &&
Expand Down
7 changes: 5 additions & 2 deletions src/intention/entity/cloud-object.embeddable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ export class CloudObjectEmbeddable {
})
project?: CloudObjectProjectEmbeddable;

@Embedded({ entity: () => EdgePropEmbeddable, nullable: true, object: true })
prop?: EdgePropEmbeddable;
@Property({
type: 'json',
nullable: true,
})
prop?: EdgePropEmbeddable = new EdgePropEmbeddable();

@Enum({ items: () => PROP_STRATEGY_VALUES, nullable: true })
propStrategy?: PROP_STRATEGY_VALUES;
Expand Down
6 changes: 5 additions & 1 deletion src/intention/entity/intention-service.embeddable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export class IntentionServiceEmbeddable {
@Property()
project: string;

@Embedded({ entity: () => ServiceTargetEmbeddable, nullable: true, object: true })
@Embedded({
entity: () => ServiceTargetEmbeddable,
nullable: true,
object: true,
})
target?: ServiceTargetEmbeddable;
}
42 changes: 39 additions & 3 deletions src/persistence/dto/broker-account.dto.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
// Shared DTO: Copy in back-end and front-end should be identical

import { VertexPointerDto } from './vertex-pointer.dto';
import { IsString, IsDefined, IsBoolean } from 'class-validator';
import { CollectionBaseDto, VertexPointerDto } from './vertex-pointer.dto';

export class BrokerAccountDto extends VertexPointerDto {
id!: string;
export class BrokerAccountBaseDto extends CollectionBaseDto {
@IsString()
@IsDefined()
email!: string;

@IsString()
@IsDefined()
clientId!: string;

@IsString()
@IsDefined()
name!: string;

@IsBoolean()
@IsDefined()
requireRoleId!: boolean;

@IsBoolean()
@IsDefined()
requireProjectExists!: boolean;

@IsBoolean()
@IsDefined()
requireServiceExists!: boolean;

@IsBoolean()
@IsDefined()
skipUserValidation!: boolean;

@IsBoolean()
@IsDefined()
maskSemverFailures!: boolean;
}

export class BrokerAccountDto
extends BrokerAccountBaseDto
implements VertexPointerDto
{
@IsString()
@IsDefined()
id!: string;

@IsString()
@IsDefined()
vertex!: string;
}
55 changes: 39 additions & 16 deletions src/persistence/dto/collection-dto-union.type.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
import { BrokerAccountDto } from './broker-account.dto';
import { EnvironmentDto } from './environment.dto';
import { ProjectDto } from './project.dto';
import { ServiceInstanceDto } from './service-instance.dto';
import { ServiceDto } from './service.dto';
import { TeamDto } from './team.dto';
import { UserDto } from './user.dto';
import { BrokerAccountBaseDto, BrokerAccountDto } from './broker-account.dto';
import { EnvironmentBaseDto, EnvironmentDto } from './environment.dto';
import { ProjectBaseDto, ProjectDto } from './project.dto';
import { ServerBaseDto, ServerDto } from './server.dto';
import {
ServiceInstanceBaseDto,
ServiceInstanceDto,
} from './service-instance.dto';
import { ServiceBaseDto, ServiceDto } from './service.dto';
import { TeamBaseDto, TeamDto } from './team.dto';
import { UserBaseDto, UserDto } from './user.dto';

export type CollectionDtoUnion = {
brokerAccount: BrokerAccountDto;
environment: EnvironmentDto;
project: ProjectDto;
server: ServiceDto;
serviceInstance: ServiceInstanceDto;
service: ServiceDto;
team: TeamDto;
user: UserDto;
export const CollectionBaseDtoUnionObject = {
brokerAccount: BrokerAccountBaseDto,
environment: EnvironmentBaseDto,
project: ProjectBaseDto,
server: ServerBaseDto,
serviceInstance: ServiceInstanceBaseDto,
service: ServiceBaseDto,
team: TeamBaseDto,
user: UserBaseDto,
};
export const CollectionDtoUnionObject = {
brokerAccount: BrokerAccountDto,
environment: EnvironmentDto,
project: ProjectDto,
server: ServerDto,
serviceInstance: ServiceInstanceDto,
service: ServiceDto,
team: TeamDto,
user: UserDto,
};
export type CollectionDtoUnion = typeof CollectionDtoUnionObject;

export const CollectionBaseSubTypes = Object.entries(
CollectionBaseDtoUnionObject,
).map(([key, value]) => ({
name: key,
value: value,
}));

export type CollectionNames = keyof CollectionDtoUnion;
export type CollectionValues = CollectionDtoUnion[CollectionNames];

export const CollectionNameEnum: {
[Property in CollectionNames]: number;
Expand Down
5 changes: 0 additions & 5 deletions src/persistence/dto/edge-prop.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
// Shared DTO: Copy in back-end and front-end should be identical
import { Embeddable, Property } from '@mikro-orm/core';

@Embeddable()
export class EdgePropDto {
/** Dummy prop as Mikro-orm doesn't like empty classes */
@Property({ nullable: true })
___ignore?: string;
[key: string]: string;
}
17 changes: 16 additions & 1 deletion src/persistence/dto/edge.dto.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import { IsObject, IsOptional, IsString } from 'class-validator';
import {
IsDefined,
IsObject,
IsOptional,
IsString,
ValidateNested,
} from 'class-validator';
import { EdgePropDto } from './edge-prop.dto';
import { TimestampDto } from './timestamp.dto';
import { Type } from 'class-transformer';
// Shared DTO: Copy in back-end and front-end should be identical

export class EdgeInsertDto {
@IsString()
@IsDefined()
name!: string;

@ValidateNested()
@IsOptional()
@IsObject()
@Type(() => EdgePropDto)
prop?: EdgePropDto;

@IsString()
@IsDefined()
source!: string;

@IsString()
@IsDefined()
target!: string;
}

Expand Down
35 changes: 32 additions & 3 deletions src/persistence/dto/environment.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
import { VertexPointerDto } from './vertex-pointer.dto';
import { IsArray, IsDefined, IsNumber, IsString } from 'class-validator';
import { CollectionBaseDto, VertexPointerDto } from './vertex-pointer.dto';
import { Type } from 'class-transformer';

// Shared DTO: Copy in back-end and front-end should be identical

export class EnvironmentDto extends VertexPointerDto {
id!: string;
export class EnvironmentBaseDto extends CollectionBaseDto {
@IsString()
@IsDefined()
name!: string;

@IsString()
@IsDefined()
short!: string;

@IsArray()
@IsDefined()
@Type(() => String)
aliases!: string[];

@IsString()
@IsDefined()
title!: string;

@IsNumber()
@IsDefined()
position!: number;
}

export class EnvironmentDto
extends EnvironmentBaseDto
implements VertexPointerDto
{
@IsString()
@IsDefined()
id!: string;

@IsString()
@IsDefined()
vertex!: string;
}
7 changes: 7 additions & 0 deletions src/persistence/dto/intention-action-pointer.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { IsString, IsOptional, IsDefined } from 'class-validator';

// Shared DTO: Copy in back-end and front-end should be identical

export class IntentionActionPointerDto {
@IsString()
@IsOptional()
action?: string;

@IsString()
@IsDefined()
intention: string;
source?: any;
}
Loading

0 comments on commit b822870

Please sign in to comment.