Skip to content

Commit

Permalink
feat: add usecase for fetching tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbi08 committed Oct 23, 2024
1 parent 7b0bb79 commit fe8ed1f
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/runtime/src/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
TokenController,
Transport
} from "@nmshd/transport";
import { TagController } from "@nmshd/transport/src/modules/tags/TagController";
import { Container, Scope } from "@nmshd/typescript-ioc";
import { buildInformation } from "./buildInformation";
import { DatabaseSchemaUpgrader } from "./DatabaseSchemaUpgrader";
Expand Down Expand Up @@ -250,6 +251,10 @@ export abstract class Runtime<TConfig extends RuntimeConfig = RuntimeConfig> {
.factory(() => this.getAccountController().relationshipTemplates)
.scope(Scope.Request);

Container.bind(TagController)
.factory(() => this.getAccountController().tags)
.scope(Scope.Request);

Container.bind(RelationshipsController)
.factory(() => this.getAccountController().relationships)
.scope(Scope.Request);
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime/src/extensibility/TransportServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
MessagesFacade,
RelationshipsFacade,
RelationshipTemplatesFacade,
TagsFacade,
TokensFacade
} from "./facades/transport";

Expand All @@ -21,6 +22,7 @@ export class TransportServices {
@Inject public readonly account: AccountFacade,
@Inject public readonly devices: DevicesFacade,
@Inject public readonly challenges: ChallengesFacade,
@Inject public readonly identityDeletionProcesses: IdentityDeletionProcessesFacade
@Inject public readonly identityDeletionProcesses: IdentityDeletionProcessesFacade,
@Inject public readonly tags: TagsFacade
) {}
}
12 changes: 12 additions & 0 deletions packages/runtime/src/extensibility/facades/transport/TagsFacade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Result } from "@js-soft/ts-utils";
import { BackboneGetTag } from "@nmshd/transport/src/modules/tags/backbone/BackboneGetTag";
import { Inject } from "@nmshd/typescript-ioc";
import { GetTagsUseCase } from "../../..";

export class TagsFacade {
public constructor(@Inject private readonly getTagsUseCase: GetTagsUseCase) {}

public async getTags(): Promise<Result<BackboneGetTag>> {
return await this.getTagsUseCase.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./DevicesFacade";
export * from "./FilesFacade";
export * from "./IdentityDeletionProcessesFacade";
export * from "./MessagesFacade";
export * from "./RelationshipTemplatesFacade";
export * from "./RelationshipsFacade";
export * from "./RelationshipTemplatesFacade";
export * from "./TagsFacade";
export * from "./TokensFacade";
3 changes: 2 additions & 1 deletion packages/runtime/src/useCases/transport/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./devices";
export * from "./files";
export * from "./identityDeletionProcesses";
export * from "./messages";
export * from "./relationshipTemplates";
export * from "./relationships";
export * from "./relationshipTemplates";
export * from "./tags";
export * from "./tokens";
15 changes: 15 additions & 0 deletions packages/runtime/src/useCases/transport/tags/GetTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Result } from "@js-soft/ts-utils";
import { TagController } from "@nmshd/transport/src/modules/tags/TagController";
import { BackboneGetTag } from "@nmshd/transport/src/modules/tags/backbone/BackboneGetTag";
import { Inject } from "@nmshd/typescript-ioc";
import { UseCase } from "../../common";

export class GetTagsUseCase extends UseCase<void, BackboneGetTag> {
public constructor(@Inject private readonly tagController: TagController) {
super();
}

protected async executeInternal(): Promise<Result<BackboneGetTag>> {
return Result.ok(await this.tagController.getTags());
}
}
1 change: 1 addition & 0 deletions packages/runtime/src/useCases/transport/tags/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./GetTags";
3 changes: 2 additions & 1 deletion packages/transport/src/core/TransportController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export enum ControllerName {
RelationshipTemplator = "RelationshipTemplator",
Secret = "Secret",
Sync = "Sync",
Token = "Token"
Token = "Token",
Tag = "Tag"
}

export class TransportController {
Expand Down
3 changes: 3 additions & 0 deletions packages/transport/src/modules/accounts/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { SecretController } from "../secrets/SecretController";
import { ChangedItems } from "../sync/ChangedItems";
import { SyncController } from "../sync/SyncController";
import { SynchronizedCollection } from "../sync/SynchronizedCollection";
import { TagController } from "../tags/TagController";
import { TokenController } from "../tokens/TokenController";
import { IdentityController } from "./IdentityController";
import { IdentityDeletionProcessController } from "./IdentityDeletionProcessController";
Expand Down Expand Up @@ -63,6 +64,7 @@ export class AccountController {
public relationshipTemplates: RelationshipTemplateController;
private synchronization: SyncController;
public tokens: TokenController;
public tags: TagController;

private relationshipSecrets: RelationshipSecretController;
private readonly _log: ILogger;
Expand Down Expand Up @@ -213,6 +215,7 @@ export class AccountController {
this.relationshipTemplates = await new RelationshipTemplateController(this, this.relationshipSecrets).init();
this.messages = await new MessageController(this).init();
this.tokens = await new TokenController(this).init();
this.tags = await new TagController(this).init();

this.synchronization = await new SyncController(this, this.dependencyOverrides, this.unpushedDatawalletModifications, this.config.datawalletEnabled).init();

Expand Down
24 changes: 24 additions & 0 deletions packages/transport/src/modules/tags/TagController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { AccountController, ControllerName, TransportController } from "../..";
import { BackboneGetTag } from "./backbone/BackboneGetTag";
import { TagClient } from "./backbone/TagClient";

export class TagController extends TransportController {
private client: TagClient;

public constructor(parent: AccountController) {
super(ControllerName.Tag, parent);
}

public override async init(): Promise<this> {
await super.init();

this.client = new TagClient(this.config, this.parent.authenticator, this.transport.correlator);

return this;
}

public async getTags(): Promise<BackboneGetTag> {
const tags = (await this.client.getTags()).value;
return tags;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface BackboneGetTag {
supportedLanguages: string[];
tagsForAttributeValueTypes: Record<string, Record<string, Tag>>;
}

interface Tag {
displayNames: Record<string, string>;
children?: Record<string, Tag>;
}
9 changes: 9 additions & 0 deletions packages/transport/src/modules/tags/backbone/TagClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ClientResult } from "../../../core/backbone/ClientResult";
import { RESTClientAuthenticate } from "../../../core/backbone/RESTClientAuthenticate";
import { BackboneGetTag } from "./BackboneGetTag";

export class TagClient extends RESTClientAuthenticate {
public async getTags(): Promise<ClientResult<BackboneGetTag>> {
return await this.get<BackboneGetTag>("/api/v1/Tags");
}
}
111 changes: 111 additions & 0 deletions packages/transport/test/modules/tags/TagsController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { IDatabaseConnection } from "@js-soft/docdb-access-abstractions";
import { AccountController, ClientResult, Transport } from "../../../src";
import { BackboneGetTag } from "../../../src/modules/tags/backbone/BackboneGetTag";
import { TagClient } from "../../../src/modules/tags/backbone/TagClient";
import { TestUtil } from "../../testHelpers/TestUtil";

describe("AccountController", function () {
let connection: IDatabaseConnection;

let transport: Transport;

let account: AccountController;

/* eslint-disable @typescript-eslint/naming-convention */
const mockTags: BackboneGetTag = {
supportedLanguages: ["de", "en"],
tagsForAttributeValueTypes: {
IdentityFileReference: {
schulabschluss: {
displayNames: {
de: "Abschluss",
en: "Degree"
},
children: {
realschule: {
displayNames: {
de: "Realschule",
en: "Secondary School"
},
children: {
zeugnis: {
displayNames: {
de: "Zeugnis",
en: "Diploma"
}
}
}
},

gymnasium: {
displayNames: {
de: "Gymnasium",
en: "High School"
},
children: {
zeugnis: {
displayNames: {
de: "Zeugnis",
en: "Diploma"
}
}
}
}
}
}
},
PhoneNumber: {
notfall: {
displayNames: {
de: "Notfallkontakt",
en: "Emergency Contact"
}
}
},
StreetAddress: {
lieferung: {
displayNames: {
de: "Lieferadresse",
en: "Deliver Address"
}
},
heimat: {
displayNames: {
de: "Heimatadresse",
en: "Home Address"
}
}
}
}
};
/* eslint-enable @typescript-eslint/naming-convention */

beforeAll(async function () {
connection = await TestUtil.createDatabaseConnection();
transport = TestUtil.createTransport(connection);

await transport.init();

const accounts = await TestUtil.provideAccounts(transport, 1);
account = accounts[0];
const tagClient = (account.tags as any).client as TagClient;

const mockGetTags = jest.fn().mockImplementation(() => {
return Promise.resolve(ClientResult.ok(mockTags));
});

tagClient.getTags = mockGetTags.bind(tagClient);
});

afterAll(async function () {
await account.close();

await connection.close();
});

test("should receive the legal tags", async function () {
const tags = await account.tags.getTags();

expect(tags).toStrictEqual(mockTags);
});
});

0 comments on commit fe8ed1f

Please sign in to comment.