Skip to content

Commit

Permalink
Make creationContent parameter of CanCreateRelationshipUseCase opti…
Browse files Browse the repository at this point in the history
…onal (#323)

* feat: add CanCreateRelationshipRequest type

* chore: build schema of CanCreateRelationshipRequest

* fix: request type of canCreateRelationship facade method

* feat: use schema validator of CanCreateRelationshipRequest

* test: use case can be called without creationContent

* chore: add barrel to index file

* chore: remove useless empty import
  • Loading branch information
britsta authored Nov 6, 2024
1 parent 0420ead commit c0673cb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
AcceptRelationshipReactivationUseCase,
AcceptRelationshipRequest,
AcceptRelationshipUseCase,
CanCreateRelationshipRequest,
CanCreateRelationshipResponse,
CanCreateRelationshipUseCase,
CreateRelationshipRequest,
CreateRelationshipUseCase,
DecomposeRelationshipRequest,
Expand All @@ -32,7 +35,6 @@ import {
TerminateRelationshipRequest,
TerminateRelationshipUseCase
} from "../../../useCases";
import { CanCreateRelationshipResponse, CanCreateRelationshipUseCase } from "../../../useCases/transport/relationships/CanCreateRelationship";

export class RelationshipsFacade {
public constructor(
Expand Down Expand Up @@ -65,7 +67,7 @@ export class RelationshipsFacade {
return await this.getRelationshipByAddressUseCase.execute(request);
}

public async canCreateRelationship(request: CreateRelationshipRequest): Promise<Result<CanCreateRelationshipResponse>> {
public async canCreateRelationship(request: CanCreateRelationshipRequest): Promise<Result<CanCreateRelationshipResponse>> {
return await this.canCreateRelationshipUseCase.execute(request);
}

Expand Down
24 changes: 24 additions & 0 deletions packages/runtime/src/useCases/common/Schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22895,4 +22895,28 @@ export const LoadPeerTokenRequest: any = {
"pattern": "VE9L.{84}"
}
}
}

export const CanCreateRelationshipRequest: any = {
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/CanCreateRelationshipRequest",
"definitions": {
"CanCreateRelationshipRequest": {
"type": "object",
"properties": {
"templateId": {
"$ref": "#/definitions/RelationshipTemplateIdString"
},
"creationContent": {}
},
"required": [
"templateId"
],
"additionalProperties": false
},
"RelationshipTemplateIdString": {
"type": "string",
"pattern": "RLT[A-Za-z0-9]{17}"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ import { ArbitraryRelationshipCreationContent, RelationshipCreationContent } fro
import { CoreId } from "@nmshd/core-types";
import { RelationshipsController, RelationshipTemplate, RelationshipTemplateController } from "@nmshd/transport";
import { Inject } from "@nmshd/typescript-ioc";
import { RuntimeErrors, UseCase } from "../../common";
import { CreateRelationshipRequest } from "./CreateRelationship";
import { RelationshipTemplateIdString, RuntimeErrors, SchemaRepository, SchemaValidator, UseCase } from "../../common";

export interface CanCreateRelationshipRequest {
templateId: RelationshipTemplateIdString;
creationContent?: any;
}

class Validator extends SchemaValidator<CanCreateRelationshipRequest> {
public constructor(@Inject schemaRepository: SchemaRepository) {
super(schemaRepository.getSchema("CanCreateRelationshipRequest"));
}
}

export type CanCreateRelationshipResponse =
| { isSuccess: true }
Expand All @@ -15,15 +25,16 @@ export type CanCreateRelationshipResponse =
message: string;
};

export class CanCreateRelationshipUseCase extends UseCase<CreateRelationshipRequest, CanCreateRelationshipResponse> {
export class CanCreateRelationshipUseCase extends UseCase<CanCreateRelationshipRequest, CanCreateRelationshipResponse> {
public constructor(
@Inject private readonly relationshipController: RelationshipsController,
@Inject private readonly relationshipTemplateController: RelationshipTemplateController
@Inject private readonly relationshipTemplateController: RelationshipTemplateController,
@Inject validator: Validator
) {
super();
super(validator);
}

protected async executeInternal(request: CreateRelationshipRequest): Promise<Result<CanCreateRelationshipResponse>> {
protected async executeInternal(request: CanCreateRelationshipRequest): Promise<Result<CanCreateRelationshipResponse>> {
const template = await this.relationshipTemplateController.getRelationshipTemplate(CoreId.from(request.templateId));
if (!template) {
const error = RuntimeErrors.general.recordNotFound(RelationshipTemplate);
Expand All @@ -35,12 +46,14 @@ export class CanCreateRelationshipUseCase extends UseCase<CreateRelationshipRequ
return Result.ok({ isSuccess: false, code: error.code, message: error.message });
}

const transformedCreationContent = Serializable.fromUnknown(request.creationContent);
if (!(transformedCreationContent instanceof ArbitraryRelationshipCreationContent || transformedCreationContent instanceof RelationshipCreationContent)) {
const error = RuntimeErrors.general.invalidPropertyValue(
"The creationContent of a Relationship must either be an ArbitraryRelationshipCreationContent or a RelationshipCreationContent."
);
return Result.ok({ isSuccess: false, code: error.code, message: error.message });
if (request.creationContent) {
const transformedCreationContent = Serializable.fromUnknown(request.creationContent);
if (!(transformedCreationContent instanceof ArbitraryRelationshipCreationContent || transformedCreationContent instanceof RelationshipCreationContent)) {
const error = RuntimeErrors.general.invalidPropertyValue(
"The creationContent of a Relationship must either be an ArbitraryRelationshipCreationContent or a RelationshipCreationContent."
);
return Result.ok({ isSuccess: false, code: error.code, message: error.message });
}
}

const canSendRelationship = await this.relationshipController.canSendRelationship({ creationContent: request.creationContent, template });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./AcceptRelationship";
export * from "./AcceptRelationshipReactivation";
export * from "./CanCreateRelationship";
export * from "./CreateRelationship";
export * from "./DecomposeRelationship";
export * from "./GetAttributesForRelationship";
Expand Down
12 changes: 4 additions & 8 deletions packages/runtime/test/transport/relationships.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ describe("Can Create / Create Relationship", () => {

const canCreateRelationshipResponse = (
await services2.transport.relationships.canCreateRelationship({
templateId: templateId,
creationContent: emptyRelationshipCreationContent
templateId: templateId
})
).value;

Expand Down Expand Up @@ -167,8 +166,7 @@ describe("Can Create / Create Relationship", () => {

const canCreateRelationshipResponse = (
await services2.transport.relationships.canCreateRelationship({
templateId: templateId,
creationContent: emptyRelationshipCreationContent
templateId: templateId
})
).value;

Expand Down Expand Up @@ -219,8 +217,7 @@ describe("Can Create / Create Relationship", () => {

const canCreateRelationshipResponse = (
await services2.transport.relationships.canCreateRelationship({
templateId: templateId,
creationContent: emptyRelationshipCreationContent
templateId: templateId
})
).value;

Expand Down Expand Up @@ -977,8 +974,7 @@ describe("RelationshipDecomposition", () => {

const canCreateRelationshipResponse = (
await services1.transport.relationships.canCreateRelationship({
templateId: templateId,
creationContent: emptyRelationshipCreationContent
templateId: templateId
})
).value;

Expand Down

0 comments on commit c0673cb

Please sign in to comment.