Skip to content

Commit

Permalink
Prevent the creation of duplicated RepositoryAttributes (#367)
Browse files Browse the repository at this point in the history
* chore: prevent the creation of duplicated RepositoryAttributes

* chore: remove comment

* chore: update naming

* Update packages/runtime/src/useCases/common/RuntimeErrors.ts

Co-authored-by: Magnus Kuhn <[email protected]>

* Update packages/runtime/test/consumption/attributes.test.ts

Co-authored-by: Magnus Kuhn <[email protected]>

* Update packages/runtime/test/consumption/attributes.test.ts

Co-authored-by: Magnus Kuhn <[email protected]>

* chore: pr comments

* chore: pr comments

* chore: fix prettier

* chore: fix test and styling

* chore: test

* chore: improve attribute naming

* chore: pr comments

* chore: fix test

* chore: generate random values

* chore: generate random strings

* chore: use transport random

* chore: add filter to only show latest succession attribute

* chore: add filter to only show latest succession attribute

* test: add test for a near duplicate

* chore: add filter to only use latest succession attribute in duplicate check

* chore: add filter to check the conten.value for equality

* chore: fix tests

* chore: add more tests

* chore: test

* chore: test tests

* chore: fix tests

* chore: fix tests

* chore: pr comments

* chore: pr comments

* chore: add newlines

* chore: pr comments

* chore: pr comments

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Magnus Kuhn <[email protected]>
Co-authored-by: mkuhn <[email protected]>
  • Loading branch information
4 people authored Jan 7, 2025
1 parent 6a66c18 commit df54a6a
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 47 deletions.
7 changes: 7 additions & 0 deletions packages/runtime/src/useCases/common/RuntimeErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ class Attributes {
);
}

public cannotCreateDuplicateRepositoryAttribute(attributeId: CoreId | string): ApplicationError {
return new ApplicationError(
"error.runtime.attributes.cannotCreateDuplicateRepositoryAttribute",
`The RepositoryAttribute cannot be created because it has the same content.value as the already existing RepositoryAttribute with id '${attributeId.toString()}'.`
);
}

public setDefaultRepositoryAttributesIsDisabled(): ApplicationError {
return new ApplicationError("error.runtime.attributes.setDefaultRepositoryAttributesIsDisabled", "Setting default RepositoryAttributes is disabled for this Account.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { AttributesController, CreateRepositoryAttributeParams } from "@nmshd/co
import { AttributeValues } from "@nmshd/content";
import { AccountController } from "@nmshd/transport";
import { Inject } from "@nmshd/typescript-ioc";
import _ from "lodash";
import { LocalAttributeDTO } from "../../../types";
import { ISO8601DateTimeString, SchemaRepository, SchemaValidator, UseCase } from "../../common";
import { flattenObject, ISO8601DateTimeString, RuntimeErrors, SchemaRepository, SchemaValidator, UseCase } from "../../common";
import { AttributeMapper } from "./AttributeMapper";

export interface CreateRepositoryAttributeRequest {
Expand Down Expand Up @@ -39,6 +40,25 @@ export class CreateRepositoryAttributeUseCase extends UseCase<CreateRepositoryAt
...request.content
}
});

const queryForRepositoryAttributeDuplicates = flattenObject({
content: {
"@type": "IdentityAttribute",
owner: this.accountController.identity.address.toString(),
value: request.content.value
}
});

queryForRepositoryAttributeDuplicates["succeededBy"] = { $exists: false };

const existingRepositoryAttributes = await this.attributeController.getLocalAttributes(queryForRepositoryAttributeDuplicates);

const exactMatchExists = existingRepositoryAttributes.some((duplicate) => _.isEqual(duplicate.content.value.toJSON(), request.content.value));

if (exactMatchExists) {
return Result.fail(RuntimeErrors.attributes.cannotCreateDuplicateRepositoryAttribute(existingRepositoryAttributes[0].id));
}

const createdLocalAttribute = await this.attributeController.createRepositoryAttribute(params);

await this.accountController.syncDatawallet();
Expand Down
Loading

0 comments on commit df54a6a

Please sign in to comment.