Skip to content

Commit 7071817

Browse files
Query RelationshipTemplates for password protection (#353)
* feat: query templates * fix: use custom query * test: cleanup tests * fix: !true accepts undefined * refactor: naming * feat: !true to ! * chore: build schemas * test: use queryconditions * test: naming * test: move password query into existing tests * feat/refactor: passwordProtection, passwordIsPin query * feat: restrict passwordIsPin query * refactor: name values * test: add passwordProtection query tests --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 4a2eea3 commit 7071817

File tree

3 files changed

+106
-8
lines changed

3 files changed

+106
-8
lines changed

packages/runtime/src/useCases/common/Schemas.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22772,6 +22772,33 @@ export const GetRelationshipTemplatesRequest: any = {
2277222772
}
2277322773
}
2277422774
]
22775+
},
22776+
"passwordProtection": {
22777+
"type": "string",
22778+
"enum": [
22779+
"",
22780+
"!"
22781+
]
22782+
},
22783+
"passwordProtection.password": {
22784+
"anyOf": [
22785+
{
22786+
"type": "string"
22787+
},
22788+
{
22789+
"type": "array",
22790+
"items": {
22791+
"type": "string"
22792+
}
22793+
}
22794+
]
22795+
},
22796+
"passwordProtection.passwordIsPin": {
22797+
"type": "string",
22798+
"enum": [
22799+
"true",
22800+
"!"
22801+
]
2277522802
}
2277622803
},
2277722804
"additionalProperties": false

packages/runtime/src/useCases/transport/relationshipTemplates/GetRelationshipTemplates.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { QueryTranslator } from "@js-soft/docdb-querytranslator";
22
import { Result } from "@js-soft/ts-utils";
3-
import { CachedRelationshipTemplate, RelationshipTemplate, RelationshipTemplateController } from "@nmshd/transport";
3+
import { CachedRelationshipTemplate, PasswordProtection, RelationshipTemplate, RelationshipTemplateController } from "@nmshd/transport";
44
import { Inject } from "@nmshd/typescript-ioc";
55
import { nameof } from "ts-simple-nameof";
66
import { RelationshipTemplateDTO } from "../../../types";
@@ -15,6 +15,9 @@ export interface GetRelationshipTemplatesQuery {
1515
createdByDevice?: string | string[];
1616
maxNumberOfAllocations?: string | string[];
1717
forIdentity?: string | string[];
18+
passwordProtection?: "" | "!";
19+
"passwordProtection.password"?: string | string[];
20+
"passwordProtection.passwordIsPin"?: "true" | "!";
1821
}
1922

2023
export interface GetRelationshipTemplatesRequest {
@@ -37,7 +40,10 @@ export class GetRelationshipTemplatesUseCase extends UseCase<GetRelationshipTemp
3740
[nameof<RelationshipTemplateDTO>((r) => r.createdBy)]: true,
3841
[nameof<RelationshipTemplateDTO>((r) => r.createdByDevice)]: true,
3942
[nameof<RelationshipTemplateDTO>((r) => r.maxNumberOfAllocations)]: true,
40-
[nameof<RelationshipTemplateDTO>((r) => r.forIdentity)]: true
43+
[nameof<RelationshipTemplateDTO>((r) => r.forIdentity)]: true,
44+
[nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)]: true,
45+
[`${nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)}.password`]: true,
46+
[`${nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)}.passwordIsPin`]: true
4147
},
4248
alias: {
4349
[nameof<RelationshipTemplateDTO>((r) => r.isOwn)]: nameof<RelationshipTemplate>((r) => r.isOwn),
@@ -50,7 +56,23 @@ export class GetRelationshipTemplatesUseCase extends UseCase<GetRelationshipTemp
5056
[nameof<RelationshipTemplateDTO>((r) => r.maxNumberOfAllocations)]: `${nameof<RelationshipTemplate>((r) => r.cache)}.${nameof<CachedRelationshipTemplate>(
5157
(t) => t.maxNumberOfAllocations
5258
)}`,
53-
[nameof<RelationshipTemplateDTO>((r) => r.forIdentity)]: `${nameof<RelationshipTemplate>((r) => r.cache)}.${nameof<CachedRelationshipTemplate>((t) => t.forIdentity)}`
59+
[nameof<RelationshipTemplateDTO>((r) => r.forIdentity)]: `${nameof<RelationshipTemplate>((r) => r.cache)}.${nameof<CachedRelationshipTemplate>((t) => t.forIdentity)}`,
60+
[nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)]: nameof<RelationshipTemplate>((r) => r.passwordProtection)
61+
},
62+
custom: {
63+
[`${nameof<RelationshipTemplateDTO>((r) => r.passwordProtection)}.password`]: (query: any, input: string) => {
64+
query[`${nameof<RelationshipTemplate>((t) => t.passwordProtection)}.${nameof<PasswordProtection>((t) => t.password)}`] = input;
65+
},
66+
[`${nameof<RelationshipTemplateDTO>((t) => t.passwordProtection)}.passwordIsPin`]: (query: any, input: string) => {
67+
if (input === "true") {
68+
query[`${nameof<RelationshipTemplate>((t) => t.passwordProtection)}.${nameof<PasswordProtection>((t) => t.passwordType)}`] = {
69+
$regex: "^pin"
70+
};
71+
}
72+
if (input === "!") {
73+
query[`${nameof<RelationshipTemplate>((t) => t.passwordProtection)}.${nameof<PasswordProtection>((t) => t.passwordType)}`] = "pw";
74+
}
75+
}
5476
}
5577
});
5678

packages/runtime/test/transport/relationshipTemplates.test.ts

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@ describe("RelationshipTemplates query", () => {
277277
maxNumberOfAllocations: 1,
278278
expiresAt: DateTime.utc().plus({ minutes: 10 }).toString(),
279279
content: emptyRelationshipTemplateContent,
280-
forIdentity: runtimeServices1.address
280+
forIdentity: runtimeServices1.address,
281+
passwordProtection: {
282+
password: "1234",
283+
passwordIsPin: true
284+
}
281285
})
282286
).value;
283287
const conditions = new QueryParamConditions<GetRelationshipTemplatesQuery>(template, runtimeServices1.transport)
@@ -287,7 +291,28 @@ describe("RelationshipTemplates query", () => {
287291
.addStringSet("createdBy")
288292
.addStringSet("createdByDevice")
289293
.addNumberSet("maxNumberOfAllocations")
290-
.addStringSet("forIdentity");
294+
.addStringSet("forIdentity")
295+
.addSingleCondition({
296+
expectedResult: true,
297+
key: "passwordProtection",
298+
value: ""
299+
})
300+
.addSingleCondition({
301+
expectedResult: false,
302+
key: "passwordProtection",
303+
value: "!"
304+
})
305+
.addStringSet("passwordProtection.password")
306+
.addSingleCondition({
307+
expectedResult: true,
308+
key: "passwordProtection.passwordIsPin",
309+
value: "true"
310+
})
311+
.addSingleCondition({
312+
expectedResult: false,
313+
key: "passwordProtection.passwordIsPin",
314+
value: "!"
315+
});
291316

292317
await conditions.executeTests((c, q) => c.relationshipTemplates.getRelationshipTemplates({ query: q }));
293318
});
@@ -297,15 +322,29 @@ describe("RelationshipTemplates query", () => {
297322
await runtimeServices1.transport.relationshipTemplates.createOwnRelationshipTemplate({
298323
maxNumberOfAllocations: 1,
299324
expiresAt: DateTime.utc().plus({ minutes: 10 }).toString(),
300-
content: emptyRelationshipTemplateContent
325+
content: emptyRelationshipTemplateContent,
326+
passwordProtection: {
327+
password: "password"
328+
}
301329
})
302330
).value;
303331
const conditions = new QueryParamConditions<GetRelationshipTemplatesQuery>(template, runtimeServices1.transport)
304332
.addDateSet("createdAt")
305333
.addDateSet("expiresAt")
306334
.addStringSet("createdBy")
307335
.addStringSet("createdByDevice")
308-
.addNumberSet("maxNumberOfAllocations");
336+
.addNumberSet("maxNumberOfAllocations")
337+
.addStringSet("passwordProtection.password")
338+
.addSingleCondition({
339+
expectedResult: false,
340+
key: "passwordProtection.passwordIsPin",
341+
value: "true"
342+
})
343+
.addSingleCondition({
344+
expectedResult: true,
345+
key: "passwordProtection.passwordIsPin",
346+
value: "!"
347+
});
309348
await conditions.executeTests((c, q) => c.relationshipTemplates.getRelationshipTemplates({ query: q, ownerRestriction: OwnerRestriction.Own }));
310349
});
311350

@@ -323,7 +362,17 @@ describe("RelationshipTemplates query", () => {
323362
.addDateSet("expiresAt")
324363
.addStringSet("createdBy")
325364
.addStringSet("createdByDevice")
326-
.addNumberSet("maxNumberOfAllocations");
365+
.addNumberSet("maxNumberOfAllocations")
366+
.addSingleCondition({
367+
expectedResult: false,
368+
key: "passwordProtection",
369+
value: ""
370+
})
371+
.addSingleCondition({
372+
expectedResult: true,
373+
key: "passwordProtection",
374+
value: "!"
375+
});
327376

328377
await conditions.executeTests((c, q) => c.relationshipTemplates.getRelationshipTemplates({ query: q, ownerRestriction: OwnerRestriction.Peer }));
329378
});

0 commit comments

Comments
 (0)