Skip to content

Commit

Permalink
Add configuration for setting isDefault of LocalAttributes (#241)
Browse files Browse the repository at this point in the history
* feat: add consumptionConfig

* feat: use consumptionConfig

* feat: adjust attributes controller tests

* feat: adjust runtime tests

* fix: runtime tests

* refactor: (re)move comments

* chore: remove todo comments

* simplify (#244)

* refactor: simplify

* chore: make clear that Partial<ConsumptionConfig> is a ConsumptionConfigOverride

* chore: set correct default

* fix: tests

* chore: audit fix

* chore: undo index changes

* refactor: make config required

* refactor: update TestRuntime constructor to use ConsumptionConfig instead of ConsumptionConfigOverride

* fix: test

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Julian König <33655937+jkoenig134@users.noreply.github.com>
Co-authored-by: Julian König <julian.koenig@js-soft.com>
  • Loading branch information
4 people committed Aug 22, 2024
1 parent 2aa0d06 commit dc824d9
Show file tree
Hide file tree
Showing 29 changed files with 353 additions and 159 deletions.
103 changes: 56 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/app-runtime/src/AppRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ export class AppRuntime extends Runtime<AppConfig> {
if (!localAccount.address) {
throw AppRuntimeErrors.general.addressUnavailable().logWith(this.logger);
}
const consumptionController = await new ConsumptionController(this.transport, accountController).init();

const consumptionController = await new ConsumptionController(this.transport, accountController, { setDefaultRepositoryAttributes: true }).init();

const services = await this.login(accountController, consumptionController);

Expand Down
3 changes: 3 additions & 0 deletions packages/consumption/src/consumption/ConsumptionConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ConsumptionConfig {
setDefaultRepositoryAttributes: boolean;
}
11 changes: 9 additions & 2 deletions packages/consumption/src/consumption/ConsumptionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ import {
ShareAttributeRequestItemProcessor,
ThirdPartyOwnedRelationshipAttributeDeletedByPeerNotificationItemProcessor
} from "../modules";
import { ConsumptionConfig } from "./ConsumptionConfig";

export class ConsumptionController {
public constructor(
public readonly transport: Transport,
public readonly accountController: AccountController
public readonly accountController: AccountController,
public readonly consumptionConfig: ConsumptionConfig
) {}

private _attributes: AttributesController;
Expand Down Expand Up @@ -87,7 +89,12 @@ export class ConsumptionController {
requestItemProcessorOverrides = new Map<RequestItemConstructor, RequestItemProcessorConstructor>(),
notificationItemProcessorOverrides = new Map<NotificationItemConstructor, NotificationItemProcessorConstructor>()
): Promise<ConsumptionController> {
this._attributes = await new AttributesController(this, this.transport.eventBus, this.accountController.identity).init();
this._attributes = await new AttributesController(
this,
this.transport.eventBus,
this.accountController.identity,
this.consumptionConfig.setDefaultRepositoryAttributes
).init();
this._drafts = await new DraftsController(this).init();

const requestItemProcessorRegistry = new RequestItemProcessorRegistry(this, this.getDefaultRequestItemProcessors());
Expand Down
4 changes: 4 additions & 0 deletions packages/consumption/src/consumption/CoreErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ class Attributes {
`The sender (id: '${senderId}') of the Notification is not the peer you shared the Attribute (id: '${attributeId}') with.`
);
}

public setDefaultRepositoryAttributesIsDisabled() {
return new CoreError("error.consumption.attributes.setDefaultRepositoryAttributesIsDisabled", "Setting default RepositoryAttributes is disabled for this Account.");
}
}

class Requests {
Expand Down
1 change: 1 addition & 0 deletions packages/consumption/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./buildInformation";
export * from "./consumption/ConsumptionBaseController";
export * from "./consumption/ConsumptionConfig";
export * from "./consumption/ConsumptionController";
export * from "./consumption/ConsumptionControllerName";
export * from "./consumption/ConsumptionError";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export class AttributesController extends ConsumptionBaseController {
public constructor(
parent: ConsumptionController,
private readonly eventBus: EventBus,
private readonly identity: { address: CoreAddress }
private readonly identity: { address: CoreAddress },
private readonly setDefaultRepositoryAttributes: boolean
) {
super(ConsumptionControllerName.AttributesController, parent);
}
Expand Down Expand Up @@ -248,7 +249,9 @@ export class AttributesController extends ConsumptionBaseController {

await this.attributes.create(localAttribute);

localAttribute = await this.setAsDefaultRepositoryAttribute(localAttribute, true);
if (this.setDefaultRepositoryAttributes) {
localAttribute = await this.setAsDefaultRepositoryAttribute(localAttribute, true);
}

if (localAttribute.content.value instanceof AbstractComplexValue) {
await this.createLocalAttributesForChildrenOfComplexAttribute(localAttribute);
Expand Down Expand Up @@ -280,6 +283,8 @@ export class AttributesController extends ConsumptionBaseController {
}

public async setAsDefaultRepositoryAttribute(newDefaultAttribute: LocalAttribute, skipOverwrite?: boolean): Promise<LocalAttribute> {
if (!this.setDefaultRepositoryAttributes) throw CoreErrors.attributes.setDefaultRepositoryAttributesIsDisabled();

if (!newDefaultAttribute.isRepositoryAttribute(this.identity.address)) {
throw CoreErrors.attributes.isNotRepositoryAttribute(newDefaultAttribute.id);
}
Expand Down Expand Up @@ -1063,7 +1068,9 @@ export class AttributesController extends ConsumptionBaseController {

await this.deletePredecessorsOfAttribute(attribute.id);

await this.transferDefault(attribute);
if (this.setDefaultRepositoryAttributes) {
await this.transferDefault(attribute);
}

await this.deleteAttribute(attribute);
}
Expand Down Expand Up @@ -1134,6 +1141,7 @@ export class AttributesController extends ConsumptionBaseController {
}

private async transferDefault(attribute: LocalAttribute): Promise<void> {
if (!this.setDefaultRepositoryAttributes) throw CoreErrors.attributes.setDefaultRepositoryAttributesIsDisabled();
if (!attribute.isDefault) return;

const valueType = attribute.content.value.constructor.name;
Expand Down
22 changes: 17 additions & 5 deletions packages/consumption/test/core/TestUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ import {
TransportLoggerFactory
} from "@nmshd/transport";
import { LogLevel } from "typescript-logging";
import { ConsumptionController, NotificationItemConstructor, NotificationItemProcessorConstructor, RequestItemConstructor, RequestItemProcessorConstructor } from "../../src";
import {
ConsumptionConfig,
ConsumptionController,
NotificationItemConstructor,
NotificationItemProcessorConstructor,
RequestItemConstructor,
RequestItemProcessorConstructor
} from "../../src";

export const loggerFactory = new NodeLoggerFactory({
appenders: {
Expand Down Expand Up @@ -177,12 +184,13 @@ export class TestUtil {
transport: Transport,
count: number,
requestItemProcessors = new Map<RequestItemConstructor, RequestItemProcessorConstructor>(),
notificationItemProcessors = new Map<NotificationItemConstructor, NotificationItemProcessorConstructor>()
notificationItemProcessors = new Map<NotificationItemConstructor, NotificationItemProcessorConstructor>(),
customConsumptionConfig?: ConsumptionConfig
): Promise<{ accountController: AccountController; consumptionController: ConsumptionController }[]> {
const accounts = [];

for (let i = 0; i < count; i++) {
const account = await this.createAccount(transport, requestItemProcessors, notificationItemProcessors);
const account = await this.createAccount(transport, requestItemProcessors, notificationItemProcessors, customConsumptionConfig);
accounts.push(account);
}

Expand All @@ -192,13 +200,17 @@ export class TestUtil {
private static async createAccount(
transport: Transport,
requestItemProcessors = new Map<RequestItemConstructor, RequestItemProcessorConstructor>(),
notificationItemProcessors = new Map<NotificationItemConstructor, NotificationItemProcessorConstructor>()
notificationItemProcessors = new Map<NotificationItemConstructor, NotificationItemProcessorConstructor>(),
customConsumptionConfig?: ConsumptionConfig
): Promise<{ accountController: AccountController; consumptionController: ConsumptionController }> {
const db = await transport.createDatabase(`x${Math.random().toString(36).substring(7)}`);
const accountController = new AccountController(transport, db, transport.config);
await accountController.init();

const consumptionController = await new ConsumptionController(transport, accountController).init(requestItemProcessors, notificationItemProcessors);
const consumptionController = await new ConsumptionController(transport, accountController, customConsumptionConfig ?? { setDefaultRepositoryAttributes: false }).init(
requestItemProcessors,
notificationItemProcessors
);

return { accountController, consumptionController };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe("AttributeListenersController", function () {
beforeAll(async function () {
connection = await TestUtil.createConnection();
transport = TestUtil.createTransport(connection, mockEventBus);

await transport.init();

const account = (await TestUtil.provideAccounts(transport, 1))[0];
Expand Down
Loading

0 comments on commit dc824d9

Please sign in to comment.