Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CanCreateRelationshipUseCase and handling of expired RelationshipTemplates #212

Merged
merged 199 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 189 commits
Commits
Show all changes
199 commits
Select commit Hold shift + click to select a range
e308947
Fix: throw error when responding to Request of expired RelationshipTe…
britsta Jul 30, 2024
627bb4f
fix: throw error when try to create Relationship with expired Relatio…
britsta Jul 30, 2024
76de78e
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Jul 30, 2024
ec847df
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Jul 31, 2024
6fec688
test: add first version of test
britsta Jul 31, 2024
9a6ccea
feat: adjust auxiliary function for RelationshipTemplate
britsta Jul 31, 2024
5fe1a8e
feat: adjust createTemplate testUtil function
britsta Jul 31, 2024
8d932cf
fix: return error instead of throwing error
britsta Jul 31, 2024
08c4837
test: temporary changes for tests
britsta Jul 31, 2024
eb38a69
fix: test by using auxiliary function
britsta Jul 31, 2024
67b8515
test: shortened test duration
britsta Jul 31, 2024
23b4a8d
chore: comment out code more consistently
britsta Jul 31, 2024
3e695d2
chore: make default expirationDateTime recognizable
britsta Jul 31, 2024
d178b83
chore: add error for delay function
britsta Jul 31, 2024
f33adf4
feat: LocalRequest is expired when source RelationshipTemplate is exp…
britsta Jul 31, 2024
b21399d
test: add test for CreateRelationshipUseCase
britsta Jul 31, 2024
f709e88
chore: remove comments
britsta Aug 1, 2024
59be7f7
test: add tests for updateStatusBasedOnTemplateExpiration function
britsta Aug 1, 2024
6c2b6b4
chore: adjust isExpired function for reasons of consistency
britsta Aug 1, 2024
9ddee1c
fix: ensure to throw error only if no other is more relevant
britsta Aug 1, 2024
a1df55f
chore: adjust test naming to be more appropriate
britsta Aug 1, 2024
58d86ab
fix: ensure more precisely to throw error only if no other is more re…
britsta Aug 1, 2024
3018c14
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 1, 2024
f806649
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 1, 2024
fd4207c
feat: throw error only for onNewRelationship
britsta Aug 1, 2024
1f889ac
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 5, 2024
ebd2e21
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 7, 2024
16515cf
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Aug 15, 2024
8d0d697
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Aug 15, 2024
f984898
fix: prettier
britsta Aug 15, 2024
87da568
chore: renaming variables and use $in operator
britsta Aug 15, 2024
cb3c291
feat: implement canCreate route for Relationships
britsta Aug 16, 2024
a672d8f
chore: use CanCreateRelationshipUseCase in other use cases
britsta Aug 16, 2024
39894e2
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 16, 2024
e8f5d07
chore: use CanCreateRelationshipUseCase in more other use cases
britsta Aug 16, 2024
2df3108
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 19, 2024
d6a2854
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 19, 2024
6930e78
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 20, 2024
cf3d28d
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 20, 2024
d5b2cb3
chore: rename error message relationshipAlreadyExists
britsta Aug 20, 2024
96ca947
refactor: naming of response variable of CanCreateRelationshipUseCase
britsta Aug 20, 2024
715d40b
fix: do not use CanCreateRelationshipUseCase in other UseCases
britsta Aug 20, 2024
e644124
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Aug 20, 2024
1067f11
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 21, 2024
b727fe1
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 21, 2024
b1ca797
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Aug 22, 2024
21a02a9
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Aug 23, 2024
802c355
feat: add noRequestToAccept error for CanCreateRelationshipUseCase
britsta Aug 23, 2024
20a9029
refactor: do not expose the term LocalRequest in error messages
britsta Aug 23, 2024
9e5ecb7
feat: expose CanCreateRelationshipUseCase to facade
britsta Aug 23, 2024
5065953
test: should not create Relationship with CreateRelationshipUseCase i…
britsta Aug 23, 2024
141525e
feat: use CreateRelationshipUseCase only for ArbitraryRelationshipTem…
britsta Aug 23, 2024
411d38a
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Sep 9, 2024
a0f48ca
fix: missing imports
britsta Sep 9, 2024
82ceed6
fix: unused import
britsta Sep 9, 2024
c2edf69
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Sep 10, 2024
9908034
chore: build schema of CanCreateRelationshipUseCase
britsta Sep 10, 2024
57dfb0d
refactor: choose more appropriate variable name
britsta Sep 10, 2024
6756aef
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Sep 10, 2024
42550a4
chore: reduce CanCreateRelationshipUseCase to split pull requests
britsta Sep 10, 2024
dc02e89
chore: reduce CanCreateRelationshipUseCase to split pull requests
britsta Sep 10, 2024
f326c20
fix: error message of CreateRelationship use case
britsta Sep 10, 2024
0c9d42c
feat: update expiry of Requests also within CreateRelationshipUseCase
britsta Sep 10, 2024
6af3236
fix: error message used for contradicting cases
britsta Sep 10, 2024
5cf7211
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Sep 12, 2024
5cf232e
feat: only allow Request which expires after RelationshipTemplate for…
britsta Sep 12, 2024
67eba34
test: request for new Relationship cannot expire after RelationshipTe…
britsta Sep 12, 2024
ece2ab1
fix: tests for expiration date of Requests
britsta Sep 12, 2024
adf4188
refactor: rename to error for Relationships instead of RelationshipTe…
britsta Sep 12, 2024
f045d80
chore: remove unused Runtime error
britsta Sep 12, 2024
32cd97a
refactor: throw only Runtime errors in use cases
britsta Sep 12, 2024
8458f7d
refactor: add function for getting existing Relationships
britsta Sep 12, 2024
95a81df
refactor: reuse CreateRelationshipRequest for CanCreateUseCase
britsta Sep 12, 2024
4aaa654
fix: incorrect use of recordNotFound error
britsta Sep 13, 2024
77121e8
feat: define canSendRelationship method in Controller
britsta Sep 13, 2024
dd67f5b
fix: failed Result for CanAcceptIncomingRequest and CanRejectIncoming…
britsta Sep 13, 2024
1f576f2
feat: add canCreateRelationship Backbone service
britsta Sep 13, 2024
97d98d4
fix: value not used for sendRelationship method
britsta Sep 13, 2024
4fcbba8
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Sep 13, 2024
ceaf939
fix: value not used for sendRelationship method
britsta Sep 13, 2024
4356109
feat: use canCreate route of Backbone
britsta Sep 13, 2024
4e242b9
fix: request type of canCreateRelationship Backbone route
britsta Sep 16, 2024
7c640db
fix: canCreateRelationship Backbone route
britsta Sep 16, 2024
2d92755
fix: return value of canCreateRelationship Backbone route
britsta Sep 16, 2024
cf4af4c
refactor: use variable for peerAddress
britsta Sep 16, 2024
b402b6a
chore: do not use imprecise Backbone result
britsta Sep 16, 2024
587866a
fix: return value of canSendRelationship method
britsta Sep 16, 2024
3cc6c33
feat: use canSendRelationship method in corresponding use case
britsta Sep 16, 2024
5b93169
chore: remove unneccessary type conversion
britsta Sep 16, 2024
3e4410e
feat: incorporate RelationshipTemplate expiry in canSendRelationship …
britsta Sep 16, 2024
6db2b7b
refactor: CanCreateRelationshipUseCase
britsta Sep 16, 2024
480fa50
feat: taking rejected Requests into account
britsta Sep 16, 2024
99202de
refactor: return code and message instead of error to avoid misunders…
britsta Sep 17, 2024
954f32d
refactor: take rejected Requests at a lower level into account
britsta Sep 17, 2024
55d2bd1
chore: simply code because cases handled at a lower level
britsta Sep 17, 2024
f7ef873
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Sep 17, 2024
8dbeabc
feat: handle expired RelationshipTemplate error appropriately
britsta Sep 17, 2024
b479e48
test: does not change status of rejected Request when RelationshipTem…
britsta Sep 17, 2024
40de3fd
fix: tests
britsta Sep 17, 2024
43318ed
chore: update Backbone version
britsta Sep 17, 2024
fef7344
test: automatic update of Request of expired RelationshipTemplate
britsta Sep 17, 2024
f903acd
test: CanCreateRelationshipUseCase
britsta Sep 17, 2024
f0f7973
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Sep 17, 2024
fc2c9da
fix: LocalRequest test
britsta Sep 18, 2024
9b29c20
refactor: rename temporary error message
britsta Sep 18, 2024
b99c9c2
chore: remove todo comments
britsta Sep 18, 2024
1973ddb
feat: apply Request expiry update in GetIncomingRequest(s)UseCase
britsta Sep 18, 2024
7dd7bf4
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Sep 18, 2024
493767c
test: update Request status to expired when querying Requests if Rela…
britsta Sep 18, 2024
2797562
chore: remove unused Runtime error relationshipCurrentlyExists
britsta Sep 18, 2024
c8ca8ba
chore: use imported sleep function instead of own delay function
britsta Sep 18, 2024
49dfdd8
feat: add minimum of type validation for creationContent of CanCreate…
britsta Sep 18, 2024
8742559
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Sep 18, 2024
a388c41
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Sep 19, 2024
ad619cb
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Sep 19, 2024
d271924
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Sep 20, 2024
cd0bf02
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Sep 23, 2024
4b58b9a
feat: integrate comments
Milena-Czierlinski Sep 23, 2024
2e1fdcf
fix: adjust test
Milena-Czierlinski Sep 23, 2024
a6a1282
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Sep 23, 2024
3222dcc
feat: generate expiration date for Request in onNewRelationship prope…
Milena-Czierlinski Sep 23, 2024
843d24e
refactor: use elvis operator
Milena-Czierlinski Sep 23, 2024
35aded4
test: remove assertIsCanCreateRelationshipFailureResponse
Milena-Czierlinski Sep 23, 2024
2ac0360
Merge branch 'fix/handling-of-expired-relationshiptemplates' of githu…
Milena-Czierlinski Sep 23, 2024
4475fd2
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Sep 23, 2024
c13e6d3
feat: update expirationDate of Request if RelationshipTemplate expires
Milena-Czierlinski Sep 23, 2024
c31bc8d
test: set expiresAt of Request if none provided
Milena-Czierlinski Sep 23, 2024
0df4def
Merge branch 'fix/handling-of-expired-relationshiptemplates' of githu…
Milena-Czierlinski Sep 23, 2024
6b8d2ed
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Sep 23, 2024
d935765
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Sep 25, 2024
deeda88
fix: clone template content in tests
Magnus-Kuhn Sep 30, 2024
43346ed
feat: remove 10 second tolerance
Magnus-Kuhn Sep 30, 2024
106695e
refactor: move validation into controller
Magnus-Kuhn Sep 30, 2024
cdbf51c
refactor: getExistingRelationship
Magnus-Kuhn Sep 30, 2024
1f62e60
feat: add min to CoreDate
Magnus-Kuhn Sep 30, 2024
5c1efc7
refactor: remove updates from create rel and get request use cases
Magnus-Kuhn Sep 30, 2024
ac580a5
feat/refactor: update after received, update during get
Magnus-Kuhn Sep 30, 2024
cedbcfe
refactor: adapt local request
Magnus-Kuhn Sep 30, 2024
0095292
refactor: rename incoming requests getter
Magnus-Kuhn Sep 30, 2024
5c28d0c
refactor: remove now unnecessary validation
Magnus-Kuhn Sep 30, 2024
77d6e07
fix: remove debugging stuff from tests
Magnus-Kuhn Sep 30, 2024
e265a40
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Sep 30, 2024
08bf659
fix: consumption
Magnus-Kuhn Oct 1, 2024
b00a790
fix: runtime tests
Magnus-Kuhn Oct 1, 2024
d95411e
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Oct 1, 2024
63be0e4
Merge branch 'fix/handling-of-expired-relationshiptemplates' of https…
Magnus-Kuhn Oct 1, 2024
d8236fc
test: reduce sleep duration after removing tolerance
Magnus-Kuhn Oct 1, 2024
062418b
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Oct 1, 2024
e8a35ce
Merge branch 'fix/handling-of-expired-relationshiptemplates' of https…
Magnus-Kuhn Oct 1, 2024
cb7e709
test/fix: add tests and fix revealed errors
Magnus-Kuhn Oct 1, 2024
1fa1a48
test: typos and status settings
Magnus-Kuhn Oct 2, 2024
619150c
chore: remove unused package
Magnus-Kuhn Oct 2, 2024
dc99bfb
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
Magnus-Kuhn Oct 2, 2024
6163dc0
Merge branch 'fix/handling-of-expired-relationshiptemplates' of https…
Magnus-Kuhn Oct 2, 2024
5eadf50
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Oct 2, 2024
2578cde
test: sleep longer
Magnus-Kuhn Oct 4, 2024
43b081a
refactor/test: getOutgoingRequestWithUpdateExpiry and use it
Magnus-Kuhn Oct 4, 2024
09afdc7
refactor: try-catch in sendRelationship
Magnus-Kuhn Oct 7, 2024
70408da
fix: adapt the tests to the refactoring
Magnus-Kuhn Oct 7, 2024
bbd5cb5
fix: more adaptations
Magnus-Kuhn Oct 7, 2024
f35048b
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Oct 7, 2024
7a52610
feat: canCreate never throws
Magnus-Kuhn Oct 9, 2024
811a0d3
feat: canCreate with error codes
Magnus-Kuhn Oct 9, 2024
2a0e271
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
Magnus-Kuhn Oct 9, 2024
91679ab
refactor: use regex, debugging cleanup
Magnus-Kuhn Oct 9, 2024
6683d49
fix: variable access
Magnus-Kuhn Oct 9, 2024
a3e25c9
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Oct 10, 2024
6c10e15
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Oct 11, 2024
5e00a42
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Oct 11, 2024
ca254d4
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Oct 17, 2024
b5966b7
fix: unfound typescript-ioc module
britsta Oct 17, 2024
04b1616
fix: unused caught error
britsta Oct 17, 2024
e4ea09d
fix: unknown transportServices1 variable
britsta Oct 17, 2024
4e4d84a
fix: failing test of DeciderModule
britsta Oct 17, 2024
82f0afb
fix: shady instanceOf check
britsta Oct 17, 2024
cf81137
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Oct 18, 2024
d6908c6
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Oct 21, 2024
9bfa6ae
refactor: add empty line
britsta Oct 21, 2024
9277d19
refactor: switch some lines in tests
britsta Oct 21, 2024
254e43e
test: proper variable naming
britsta Oct 21, 2024
2576bfd
refactor: remove redundant test
britsta Oct 21, 2024
b50465d
refactor: rephrase of test name to give more context
britsta Oct 21, 2024
6c903c9
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
mergify[bot] Oct 21, 2024
5bfd66e
Merge branch 'fix/handling-of-expired-relationshiptemplates' of githu…
britsta Oct 21, 2024
0c8e6ac
fix: copy-paste error in tests
britsta Oct 21, 2024
2af452f
chore: bump backbone
Magnus-Kuhn Oct 21, 2024
5990866
refactor: remove unused code from request controller
Magnus-Kuhn Oct 21, 2024
d95a87b
Merge branch 'main' into fix/handling-of-expired-relationshiptemplates
britsta Oct 22, 2024
24bb806
test: emphazise tests expecting errors
britsta Oct 22, 2024
bbb243e
refactor: re-simplify naming
jkoenig134 Oct 22, 2024
5a75128
fix: do not get the template for every incoming request query
jkoenig134 Oct 22, 2024
521af05
refactor: de-dupe code
jkoenig134 Oct 22, 2024
13b48e0
refactor: remove empty line again
britsta Oct 22, 2024
6411962
refactor: distinguish success and failure response of CanCreateRelati…
britsta Oct 22, 2024
1e08529
fix: use correct CanCreateRelationshipResonse type in tests
britsta Oct 22, 2024
28b6da9
refactor: remove unused expiration code
Magnus-Kuhn Oct 22, 2024
daeec41
refactor: simplify
jkoenig134 Oct 22, 2024
3688a91
Merge branch 'fix/handling-of-expired-relationshiptemplates' of githu…
jkoenig134 Oct 22, 2024
ab9bad9
fix: satisfy compiler
jkoenig134 Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .dev/compose.backbone.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
BACKBONE_VERSION=6.10.0
BACKBONE_VERSION=6.13.2
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ export class ConsumptionController {
this,
this.transport.eventBus,
this.accountController.identity,
this.accountController.relationships
this.accountController.relationships,
this.accountController.relationshipTemplates
).init();

const notificationItemProcessorRegistry = new NotificationItemProcessorRegistry(this, this.getDefaultNotificationItemProcessors());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export class IncomingRequestsController extends ConsumptionBaseController {
private readonly identity: { address: CoreAddress },
private readonly relationshipResolver: {
getRelationshipToIdentity(id: CoreAddress): Promise<Relationship | undefined>;
getExistingRelationshipToIdentity(id: CoreAddress): Promise<Relationship | undefined>;
},
private readonly relationshipTemplateResolver: {
getRelationshipTemplate(id: CoreId): Promise<RelationshipTemplate | undefined>;
}
) {
super(ConsumptionControllerName.RequestsController, parent);
Expand All @@ -60,6 +64,11 @@ export class IncomingRequestsController extends ConsumptionBaseController {
statusLog: []
});

if (!(await this.relationshipResolver.getExistingRelationshipToIdentity(CoreAddress.from(infoFromSource.peer))) && infoFromSource.expiresAt) {
request.content.expiresAt = CoreDate.min(infoFromSource.expiresAt, request.content.expiresAt);
request.updateStatusBasedOnExpiration();
}

await this.localRequests.create(request);

this.eventBus.publish(new IncomingRequestReceivedEvent(this.identity.address.toString(), request));
Expand Down Expand Up @@ -95,7 +104,8 @@ export class IncomingRequestsController extends ConsumptionBaseController {
source: {
reference: template.id,
type: "RelationshipTemplate"
}
},
expiresAt: template.cache!.expiresAt
};
}

Expand Down Expand Up @@ -377,7 +387,7 @@ export class IncomingRequestsController extends ConsumptionBaseController {
return request;
}

public async getIncomingRequests(query?: any): Promise<LocalRequest[]> {
public async getIncomingRequestsWithUpdatedExpiry(query?: any): Promise<LocalRequest[]> {
const requestDocs = await this.localRequests.find({
...query,
isOwn: false
Expand All @@ -387,16 +397,17 @@ export class IncomingRequestsController extends ConsumptionBaseController {
return await Promise.all(requestPromises);
}

public async getIncomingRequest(idIncomingRequest: ICoreId): Promise<LocalRequest | undefined> {
public async getIncomingRequestWithUpdatedExpiry(idIncomingRequest: ICoreId): Promise<LocalRequest | undefined> {
const requestDoc = await this.localRequests.findOne({ id: idIncomingRequest.toString(), isOwn: false });
if (!requestDoc) return;

const localRequest = LocalRequest.from(requestDoc);

return await this.updateRequestExpiry(localRequest);
}

private async getOrThrow(id: CoreId | string) {
const request = await this.getIncomingRequest(CoreId.from(id));
const request = await this.getIncomingRequestWithUpdatedExpiry(CoreId.from(id));
if (!request) {
throw TransportCoreErrors.general.recordNotFound(LocalRequest, id.toString());
}
Expand All @@ -412,7 +423,7 @@ export class IncomingRequestsController extends ConsumptionBaseController {
}

public async deleteRequestsFromPeer(peer: CoreAddress): Promise<void> {
const requests = await this.getIncomingRequests({ peer: peer.toString() });
const requests = await this.getIncomingRequestsWithUpdatedExpiry({ peer: peer.toString() });
for (const request of requests) {
await this.localRequests.delete(request);
}
Expand All @@ -425,13 +436,26 @@ export class IncomingRequestsController extends ConsumptionBaseController {
}

private async updateRequestExpiry(request: LocalRequest) {
let expirationDateUpdated;
if (request.source?.type === "RelationshipTemplate" && !(await this.relationshipResolver.getExistingRelationshipToIdentity(request.peer))) {
const template = await this.relationshipTemplateResolver.getRelationshipTemplate(request.source.reference);
if (!template) {
throw TransportCoreErrors.general.recordNotFound(RelationshipTemplate, request.source.reference.toString());
}
if (template.cache?.expiresAt) {
expirationDateUpdated = request.updateExpirationDateBasedOnTemplateExpiration(template.cache.expiresAt);
}
}

const statusUpdated = request.updateStatusBasedOnExpiration();
if (statusUpdated) await this.update(request);

if (expirationDateUpdated ?? statusUpdated) await this.update(request);
return request;
}
}

interface InfoFromSource {
peer: ICoreAddress;
source: ILocalRequestSource;
expiresAt?: CoreDate;
}
15 changes: 14 additions & 1 deletion packages/consumption/src/modules/requests/local/LocalRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class LocalRequest extends CoreSynchronizable implements ILocalRequest {
public isExpired(comparisonDate: CoreDate = CoreDate.utc()): boolean {
if (!this.content.expiresAt) return false;

return comparisonDate.isAfter(this.content.expiresAt.add({ seconds: 10 }));
return comparisonDate.isAfter(this.content.expiresAt);
}

public updateStatusBasedOnExpiration(comparisonDate: CoreDate = CoreDate.utc()): boolean {
Expand All @@ -130,4 +130,17 @@ export class LocalRequest extends CoreSynchronizable implements ILocalRequest {

return false;
}

public updateExpirationDateBasedOnTemplateExpiration(templateExpiresAt: CoreDate): boolean {
if (this.source?.type !== "RelationshipTemplate") return false;

if (this.status === LocalRequestStatus.Completed || this.status === LocalRequestStatus.Expired) return false;

if (!this.isExpired()) {
this.content.expiresAt = CoreDate.min(templateExpiresAt, this.content.expiresAt);
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class OutgoingRequestsController extends ConsumptionBaseController {
}

public async deleteRequestsToPeer(peer: CoreAddress): Promise<void> {
const requests = await this.getOutgoingRequests({ peer: peer.toString() });
const requests = await this.getOutgoingRequestsWithUpdatedExpiry({ peer: peer.toString() });
for (const request of requests) {
await this.localRequests.delete(request);
}
Expand Down Expand Up @@ -369,7 +369,7 @@ export class OutgoingRequestsController extends ConsumptionBaseController {
await processor.applyIncomingResponseItem(responseItem, requestItem, request);
}

public async getOutgoingRequests(query?: any): Promise<LocalRequest[]> {
public async getOutgoingRequestsWithUpdatedExpiry(query?: any): Promise<LocalRequest[]> {
const requestDocs = await this.localRequests.find({
...query,
isOwn: true
Expand All @@ -386,7 +386,7 @@ export class OutgoingRequestsController extends ConsumptionBaseController {
await this.localRequests.delete(request);
}

public async getOutgoingRequest(id: ICoreId): Promise<LocalRequest | undefined> {
public async getOutgoingRequestWithUpdatedExpiry(id: ICoreId): Promise<LocalRequest | undefined> {
const requestDoc = await this.localRequests.findOne({ id: id.toString(), isOwn: true });
if (!requestDoc) return;

Expand All @@ -395,7 +395,7 @@ export class OutgoingRequestsController extends ConsumptionBaseController {
}

private async getOrThrow(id: CoreId) {
const request = await this.getOutgoingRequest(id);
const request = await this.getOutgoingRequestWithUpdatedExpiry(id);
if (!request) {
throw TransportCoreErrors.general.recordNotFound(LocalRequest, id.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ describe("Delete requests", function () {
await TestUtil.terminateRelationship(sAccountController, rAccountController);
await TestUtil.decomposeRelationship(sAccountController, sConsumptionController, rAccountController);
await TestUtil.decomposeRelationship(rAccountController, rConsumptionController, sAccountController);
const sRequest = await sConsumptionController.outgoingRequests.getOutgoingRequest(sLocalRequest.id);
const rRequest = await sConsumptionController.incomingRequests.getIncomingRequest(rLocalRequest.id);
const sRequest = await sConsumptionController.outgoingRequests.getOutgoingRequestWithUpdatedExpiry(sLocalRequest.id);
const rRequest = await sConsumptionController.incomingRequests.getIncomingRequestWithUpdatedExpiry(rLocalRequest.id);
expect(sRequest).toBeUndefined();
expect(rRequest).toBeUndefined();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IDatabaseConnection } from "@js-soft/docdb-access-abstractions";
import { IRequest, IRequestItemGroup, RejectResponseItem, Request, RequestItemGroup, ResponseItem, ResponseItemGroup, ResponseItemResult } from "@nmshd/content";
import { CoreDate, CoreId } from "@nmshd/core-types";
import { CoreIdHelper, TransportLoggerFactory } from "@nmshd/transport";
import { CoreIdHelper, RelationshipTemplate, TransportLoggerFactory } from "@nmshd/transport";
import {
ConsumptionIds,
DecideRequestItemGroupParametersJSON,
Expand Down Expand Up @@ -65,6 +65,34 @@ describe("IncomingRequestsController", function () {
await Then.eventHasBeenPublished(IncomingRequestReceivedEvent);
});

test("takes the expiration date from the Template if the Request has no expiration date", async function () {
const timestamp = CoreDate.utc();
const incomingTemplate = TestObjectFactory.createIncomingRelationshipTemplate(timestamp);
await When.iCreateAnIncomingRequestWith({ requestSourceObject: incomingTemplate });
await Then.theRequestHasExpirationDate(timestamp);
await Then.theRequestIsInStatus(LocalRequestStatus.Expired);
});

test("takes the expiration date from the Template if the Request has a later expiration date", async function () {
const timestamp = CoreDate.utc().add({ days: 1 });
const incomingTemplate = TestObjectFactory.createIncomingRelationshipTemplate(timestamp);
await When.iCreateAnIncomingRequestWith({
requestSourceObject: incomingTemplate,
receivedRequest: TestObjectFactory.createRequestWithOneItem({ expiresAt: timestamp.add({ days: 1 }) })
});
await Then.theRequestHasExpirationDate(timestamp);
});

test("takes the expiration date from the Request if the Template has a later expiration date", async function () {
const timestamp = CoreDate.utc().add({ days: 1 });
const incomingTemplate = TestObjectFactory.createIncomingRelationshipTemplate(timestamp.add({ days: 1 }));
await When.iCreateAnIncomingRequestWith({
requestSourceObject: incomingTemplate,
receivedRequest: TestObjectFactory.createRequestWithOneItem({ expiresAt: timestamp })
});
await Then.theRequestHasExpirationDate(timestamp);
});

test("uses the ID of the given Request if it exists", async function () {
const request = TestObjectFactory.createRequestWithOneItem({ id: await CoreIdHelper.notPrefixed.generate() });

Expand Down Expand Up @@ -954,6 +982,37 @@ describe("IncomingRequestsController", function () {
await Then.theNumberOfReturnedRequestsIs(2);
});

test("updates the expiration date if the Template expires before the Request", async function () {
const timestamp = CoreDate.utc();
const request = await Given.anIncomingRequestWith({
Magnus-Kuhn marked this conversation as resolved.
Show resolved Hide resolved
content: TestObjectFactory.createRequestWithOneItem({ expiresAt: timestamp.add({ days: 1 }) }),
requestSource: TestObjectFactory.createIncomingRelationshipTemplate(timestamp)
});
await When.iGetTheIncomingRequestWith(request.id);
await Then.theRequestHasExpirationDate(timestamp);
await Then.theRequestIsInStatus(LocalRequestStatus.Expired);
});

test("updates the expiration date if the Request has no expiration date", async function () {
const timestamp = CoreDate.utc().add({ days: 1 });
const request = await Given.anIncomingRequestWith({
content: TestObjectFactory.createRequestWithOneItem(),
requestSource: TestObjectFactory.createIncomingRelationshipTemplate(timestamp)
});
await When.iGetTheIncomingRequestWith(request.id);
await Then.theRequestHasExpirationDate(timestamp);
});

test("doesn't update the expiration date if the Request expires before the Template", async function () {
const timestamp = CoreDate.utc().add({ days: 1 });
const request = await Given.anIncomingRequestWith({
content: TestObjectFactory.createRequestWithOneItem({ expiresAt: timestamp }),
requestSource: TestObjectFactory.createIncomingRelationshipTemplate(timestamp.add({ days: 1 }))
});
await When.iGetTheIncomingRequestWith(request.id);
await Then.theRequestHasExpirationDate(timestamp);
});

test("moves the Request to status 'Expired' when expiredAt is reached", async function () {
const outgoingRequest = await Given.anIncomingRequestWith({
status: LocalRequestStatus.Draft,
Expand Down Expand Up @@ -999,6 +1058,7 @@ describe("IncomingRequestsController", function () {
items: [TestRequestItem.from({ mustBeAccepted: false })]
});
const template = TestObjectFactory.createIncomingIRelationshipTemplate();
context.templateToReturnFromGetTemplate = RelationshipTemplate.from(template);

let cnsRequest = await context.incomingRequestsController.received({
receivedRequest: request,
Expand Down
Loading