@tearleads/client-sdk 0.1.39 → 0.1.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/organizations/organizationGroupPresentation.d.ts +1 -12
- package/data/persistence/organizations/organizationReadModelGrantPersistence.js +2 -1
- package/data/persistence/organizations/organizationReadModelGrantPersistence.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelMemberLoad.d.ts +9 -0
- package/data/persistence/organizations/organizationReadModelMemberLoad.js +29 -21
- package/data/persistence/organizations/organizationReadModelMemberLoad.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelMembershipPersistence.js +91 -46
- package/data/persistence/organizations/organizationReadModelMembershipPersistence.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelPersistence.js +95 -73
- package/data/persistence/organizations/organizationReadModelPersistence.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelProjection.js +78 -89
- package/data/persistence/organizations/organizationReadModelProjection.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelPurge.js +14 -15
- package/data/persistence/organizations/organizationReadModelPurge.js.map +1 -1
- package/data/sqlite/organizationReadModelSchema.js +23 -28
- package/data/sqlite/organizationReadModelSchema.js.map +1 -1
- package/data/sqlite/organizationReadModelTableRegistry.d.ts +116 -263
- package/data/sqlite/organizationReadModelTableRegistry.js +10 -7
- package/data/sqlite/organizationReadModelTableRegistry.js.map +1 -1
- package/data/sqlite/schema.d.ts +116 -263
- package/data/sqlite/sqliteBindBudget.js +30 -0
- package/data/sqlite/sqliteBindBudget.js.map +1 -0
- package/internal/validators/operation/organizations.d.ts +0 -16
- package/internal/validators/response/index.d.ts +1 -1
- package/internal/validators/response/index.js.map +1 -1
- package/internal/validators/response/organizationReadModel.d.ts +10 -48
- package/internal/validators/response/organizationReadModel.js +9 -1
- package/internal/validators/response/organizationReadModel.js.map +1 -1
- package/package.json +1 -1
- package/workflows/organizations/readModel.d.ts +8 -3
- package/workflows/organizations/readModel.js.map +1 -1
- package/workflows/organizations/readModelProjection.d.ts +2 -12
- package/workflows/organizations/readModelProjection.js +1 -1
- package/workflows/organizations/readModelProjection.js.map +1 -1
- package/workflows/sync/remoteReset.js +1 -3
- package/workflows/sync/remoteReset.js.map +1 -1
- package/data/persistence/organizations/organizationReadModelPolicyHeadPersistence.js +0 -64
- package/data/persistence/organizations/organizationReadModelPolicyHeadPersistence.js.map +0 -1
|
@@ -72,21 +72,23 @@ declare const OrganizationReadModelGrantsResponseSchema: z.ZodObject<{
|
|
|
72
72
|
organizationId: z.ZodString;
|
|
73
73
|
}, z.core.$strict>;
|
|
74
74
|
export type OrganizationReadModelGrantsResponse = z.infer<typeof OrganizationReadModelGrantsResponseSchema>;
|
|
75
|
+
/**
|
|
76
|
+
* A projected member names only the user and role. Public keys live once per
|
|
77
|
+
* user in the directory lane; keying never reads either copy from the read
|
|
78
|
+
* model.
|
|
79
|
+
*/
|
|
80
|
+
declare const OrganizationReadModelGroupMemberResponseSchema: z.ZodObject<{
|
|
81
|
+
role: z.ZodLiteral<"member" | "admin">;
|
|
82
|
+
userId: z.ZodString;
|
|
83
|
+
}, z.core.$strict>;
|
|
84
|
+
export type OrganizationReadModelGroupMemberResponse = z.infer<typeof OrganizationReadModelGroupMemberResponseSchema>;
|
|
75
85
|
declare const OrganizationReadModelGroupMembershipResponseSchema: z.ZodObject<{
|
|
76
86
|
groupId: z.ZodString;
|
|
77
87
|
members: z.ZodCustom<{
|
|
78
|
-
encapsulationKeyFingerprint: string | null;
|
|
79
|
-
encapsulationPublicKey: string | null;
|
|
80
88
|
role: "member" | "admin";
|
|
81
|
-
signingKeyFingerprint: string | null;
|
|
82
|
-
signingPublicKey: string | null;
|
|
83
89
|
userId: string;
|
|
84
90
|
}[], {
|
|
85
|
-
encapsulationKeyFingerprint: string | null;
|
|
86
|
-
encapsulationPublicKey: string | null;
|
|
87
91
|
role: "member" | "admin";
|
|
88
|
-
signingKeyFingerprint: string | null;
|
|
89
|
-
signingPublicKey: string | null;
|
|
90
92
|
userId: string;
|
|
91
93
|
}[]>;
|
|
92
94
|
stateHash: z.ZodString;
|
|
@@ -97,22 +99,14 @@ declare const OrganizationReadModelGroupMembershipsResponseSchema: z.ZodObject<{
|
|
|
97
99
|
groups: z.ZodCustom<{
|
|
98
100
|
groupId: string;
|
|
99
101
|
members: {
|
|
100
|
-
encapsulationKeyFingerprint: string | null;
|
|
101
|
-
encapsulationPublicKey: string | null;
|
|
102
102
|
role: "member" | "admin";
|
|
103
|
-
signingKeyFingerprint: string | null;
|
|
104
|
-
signingPublicKey: string | null;
|
|
105
103
|
userId: string;
|
|
106
104
|
}[];
|
|
107
105
|
stateHash: string;
|
|
108
106
|
}[], {
|
|
109
107
|
groupId: string;
|
|
110
108
|
members: {
|
|
111
|
-
encapsulationKeyFingerprint: string | null;
|
|
112
|
-
encapsulationPublicKey: string | null;
|
|
113
109
|
role: "member" | "admin";
|
|
114
|
-
signingKeyFingerprint: string | null;
|
|
115
|
-
signingPublicKey: string | null;
|
|
116
110
|
userId: string;
|
|
117
111
|
}[];
|
|
118
112
|
stateHash: string;
|
|
@@ -210,22 +204,14 @@ declare const OrganizationReadModelSnapshotResponseSchema: z.ZodObject<{
|
|
|
210
204
|
groups: z.ZodCustom<{
|
|
211
205
|
groupId: string;
|
|
212
206
|
members: {
|
|
213
|
-
encapsulationKeyFingerprint: string | null;
|
|
214
|
-
encapsulationPublicKey: string | null;
|
|
215
207
|
role: "member" | "admin";
|
|
216
|
-
signingKeyFingerprint: string | null;
|
|
217
|
-
signingPublicKey: string | null;
|
|
218
208
|
userId: string;
|
|
219
209
|
}[];
|
|
220
210
|
stateHash: string;
|
|
221
211
|
}[], {
|
|
222
212
|
groupId: string;
|
|
223
213
|
members: {
|
|
224
|
-
encapsulationKeyFingerprint: string | null;
|
|
225
|
-
encapsulationPublicKey: string | null;
|
|
226
214
|
role: "member" | "admin";
|
|
227
|
-
signingKeyFingerprint: string | null;
|
|
228
|
-
signingPublicKey: string | null;
|
|
229
215
|
userId: string;
|
|
230
216
|
}[];
|
|
231
217
|
stateHash: string;
|
|
@@ -359,22 +345,14 @@ declare const OrganizationReadModelDeltaResponseSchema: z.ZodObject<{
|
|
|
359
345
|
groups: z.ZodCustom<{
|
|
360
346
|
groupId: string;
|
|
361
347
|
members: {
|
|
362
|
-
encapsulationKeyFingerprint: string | null;
|
|
363
|
-
encapsulationPublicKey: string | null;
|
|
364
348
|
role: "member" | "admin";
|
|
365
|
-
signingKeyFingerprint: string | null;
|
|
366
|
-
signingPublicKey: string | null;
|
|
367
349
|
userId: string;
|
|
368
350
|
}[];
|
|
369
351
|
stateHash: string;
|
|
370
352
|
}[], {
|
|
371
353
|
groupId: string;
|
|
372
354
|
members: {
|
|
373
|
-
encapsulationKeyFingerprint: string | null;
|
|
374
|
-
encapsulationPublicKey: string | null;
|
|
375
355
|
role: "member" | "admin";
|
|
376
|
-
signingKeyFingerprint: string | null;
|
|
377
|
-
signingPublicKey: string | null;
|
|
378
356
|
userId: string;
|
|
379
357
|
}[];
|
|
380
358
|
stateHash: string;
|
|
@@ -509,22 +487,14 @@ export declare const OrganizationReadModelResponseSchema: z.ZodDiscriminatedUnio
|
|
|
509
487
|
groups: z.ZodCustom<{
|
|
510
488
|
groupId: string;
|
|
511
489
|
members: {
|
|
512
|
-
encapsulationKeyFingerprint: string | null;
|
|
513
|
-
encapsulationPublicKey: string | null;
|
|
514
490
|
role: "member" | "admin";
|
|
515
|
-
signingKeyFingerprint: string | null;
|
|
516
|
-
signingPublicKey: string | null;
|
|
517
491
|
userId: string;
|
|
518
492
|
}[];
|
|
519
493
|
stateHash: string;
|
|
520
494
|
}[], {
|
|
521
495
|
groupId: string;
|
|
522
496
|
members: {
|
|
523
|
-
encapsulationKeyFingerprint: string | null;
|
|
524
|
-
encapsulationPublicKey: string | null;
|
|
525
497
|
role: "member" | "admin";
|
|
526
|
-
signingKeyFingerprint: string | null;
|
|
527
|
-
signingPublicKey: string | null;
|
|
528
498
|
userId: string;
|
|
529
499
|
}[];
|
|
530
500
|
stateHash: string;
|
|
@@ -658,22 +628,14 @@ export declare const OrganizationReadModelResponseSchema: z.ZodDiscriminatedUnio
|
|
|
658
628
|
groups: z.ZodCustom<{
|
|
659
629
|
groupId: string;
|
|
660
630
|
members: {
|
|
661
|
-
encapsulationKeyFingerprint: string | null;
|
|
662
|
-
encapsulationPublicKey: string | null;
|
|
663
631
|
role: "member" | "admin";
|
|
664
|
-
signingKeyFingerprint: string | null;
|
|
665
|
-
signingPublicKey: string | null;
|
|
666
632
|
userId: string;
|
|
667
633
|
}[];
|
|
668
634
|
stateHash: string;
|
|
669
635
|
}[], {
|
|
670
636
|
groupId: string;
|
|
671
637
|
members: {
|
|
672
|
-
encapsulationKeyFingerprint: string | null;
|
|
673
|
-
encapsulationPublicKey: string | null;
|
|
674
638
|
role: "member" | "admin";
|
|
675
|
-
signingKeyFingerprint: string | null;
|
|
676
|
-
signingPublicKey: string | null;
|
|
677
639
|
userId: string;
|
|
678
640
|
}[];
|
|
679
641
|
stateHash: string;
|
|
@@ -14,7 +14,15 @@ const OrganizationReadModelGrantsResponseSchema = z.strictObject({
|
|
|
14
14
|
grants: arraySchema(OrganizationReadModelGrantResponseSchema),
|
|
15
15
|
organizationId: z.string(),
|
|
16
16
|
});
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* A projected member names only the user and role. Public keys live once per
|
|
19
|
+
* user in the directory lane; keying never reads either copy from the read
|
|
20
|
+
* model.
|
|
21
|
+
*/
|
|
22
|
+
const OrganizationReadModelGroupMemberResponseSchema = z.strictObject({
|
|
23
|
+
role: OrganizationGroupMemberResponseShape.role,
|
|
24
|
+
userId: OrganizationGroupMemberResponseShape.userId,
|
|
25
|
+
});
|
|
18
26
|
const OrganizationReadModelGroupMembershipResponseSchema = z.strictObject({
|
|
19
27
|
groupId: z.string(),
|
|
20
28
|
members: arraySchema(OrganizationReadModelGroupMemberResponseSchema),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"organizationReadModel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AAK,CAAC,kBAAkB,CAAC,cAAc,KAAK,cAAc,CAAC,CAC9D,CAAC;AACJ,CAAC;AAED,SAAS,0BAA0B,CACjC,KAAwC;IAExC,IACE,CAAC,sBAAsB,CAAC,KAAK,CAAC;QAC9B,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,KAAK,SAAS;YAC/B,CAAC,cAAc,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAC7C,CAAC,KAAK,CAAC,KAAK,CAAC,gBAAgB,KAAK,SAAS;YACzC,CAAC,wBAAwB,CAAC,KAAK,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC,EAC1D,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,CACL,KAAK,CAAC,IAAI,KAAK,OAAO;QACtB,KAAK,CAAC,KAAK,CAAC,gBAAgB,CAAC,eAAe,CAAC,MAAM,KAAK,CAAC,CAC1D,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,mCAAmC,GAC9C,oCAAoC,CAClC,sBAAsB,CACpB,uCAAuC,CAAC,MAAM,CAC5C,0BAA0B,CAC3B,EACD,uCAAuC,CACxC,EACD,+CAA+C,CAChD,CAAC;AAMJ,MAAM,UAAU,+BAA+B,CAC7C,KAAc;IAEd,OAAO,mCAAmC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC;AACtE,CAAC","sourcesContent":["import { z } from \"zod\";\nimport {\n registerJsonSchemaRuntimeRefinements,\n registerJsonSchemaView,\n} from \"../jsonSchema\";\nimport { organizationReadModelResponseRuntimeRefinements } from \"../organizationReadModelRefinements\";\nimport { arraySchema } from \"../schema\";\nimport {\n OrganizationContainerGrantResponseShape,\n OrganizationDirectoryCurrentUserResponseShape,\n OrganizationDirectoryUserResponseShape,\n OrganizationGroupCurrentStateResponseShape,\n OrganizationGroupMemberResponseShape,\n OrganizationGroupSummaryResponseShape,\n} from \"./organization\";\n\nconst OrganizationReadModelDirectoryUserResponseSchema = z.strictObject(\n OrganizationDirectoryUserResponseShape,\n);\n\nconst OrganizationReadModelDirectoryResponseSchema = z.strictObject({\n organizationId: z.string(),\n profileDocumentId: z.string().nullable(),\n users: arraySchema(OrganizationReadModelDirectoryUserResponseSchema),\n});\n\nexport type OrganizationReadModelDirectoryResponse = z.infer<\n typeof OrganizationReadModelDirectoryResponseSchema\n>;\n\nconst OrganizationReadModelGrantResponseSchema = z.strictObject(\n OrganizationContainerGrantResponseShape,\n);\n\nconst OrganizationReadModelGrantsResponseSchema = z.strictObject({\n grants: arraySchema(OrganizationReadModelGrantResponseSchema),\n organizationId: z.string(),\n});\n\nexport type OrganizationReadModelGrantsResponse = z.infer<\n typeof OrganizationReadModelGrantsResponseSchema\n>;\n\nconst OrganizationReadModelGroupMemberResponseSchema = z.strictObject(\n OrganizationGroupMemberResponseShape,\n);\n\nconst OrganizationReadModelGroupMembershipResponseSchema = z.strictObject({\n groupId: z.string(),\n members: arraySchema(OrganizationReadModelGroupMemberResponseSchema),\n stateHash: z.string().min(1),\n});\n\nexport type OrganizationReadModelGroupMembershipResponse = z.infer<\n typeof OrganizationReadModelGroupMembershipResponseSchema\n>;\n\nconst OrganizationReadModelGroupMembershipsResponseSchema = z.strictObject({\n deletedGroupIds: arraySchema(z.string().min(1)),\n groups: arraySchema(OrganizationReadModelGroupMembershipResponseSchema),\n organizationId: z.string(),\n});\n\nexport type OrganizationReadModelGroupMembershipsResponse = z.infer<\n typeof OrganizationReadModelGroupMembershipsResponseSchema\n>;\n\nconst OrganizationReadModelGroupCurrentStateResponseSchema = z.strictObject(\n OrganizationGroupCurrentStateResponseShape,\n);\n\nconst OrganizationReadModelGroupSummaryResponseSchema = z.strictObject({\n ...OrganizationGroupSummaryResponseShape,\n currentState: OrganizationReadModelGroupCurrentStateResponseSchema.nullable(),\n});\n\nconst OrganizationReadModelGroupsResponseSchema = z.strictObject({\n groups: arraySchema(OrganizationReadModelGroupSummaryResponseSchema),\n memberGroupId: z.string(),\n organizationId: z.string(),\n});\n\nconst OrganizationReadModelOrganizationPolicyResponseSchema = z.strictObject({\n currentState: z.strictObject({\n ...OrganizationGroupCurrentStateResponseShape,\n stateHash: z.string().min(1),\n }),\n organizationId: z.string(),\n});\n\nexport type OrganizationReadModelOrganizationPolicyResponse = z.infer<\n typeof OrganizationReadModelOrganizationPolicyResponseSchema\n>;\n\nconst OrganizationReadModelCurrentUserResponseSchema = z.strictObject(\n OrganizationDirectoryCurrentUserResponseShape,\n);\n\nconst OrganizationReadModelResponseBaseShape = {\n currentUser: OrganizationReadModelCurrentUserResponseSchema,\n hasMore: z.boolean(),\n nextCursor: z.string().min(1),\n organizationId: z.string(),\n version: z.literal(6),\n};\n\nconst OrganizationReadModelSnapshotResponseSchema = z.strictObject({\n ...OrganizationReadModelResponseBaseShape,\n hasMore: z.literal(false),\n lanes: z.strictObject({\n directory: OrganizationReadModelDirectoryResponseSchema,\n grants: OrganizationReadModelGrantsResponseSchema,\n groupMemberships: OrganizationReadModelGroupMembershipsResponseSchema,\n groups: OrganizationReadModelGroupsResponseSchema,\n organizationPolicy: OrganizationReadModelOrganizationPolicyResponseSchema,\n }),\n mode: z.literal(\"snapshot\"),\n});\n\nexport type OrganizationReadModelSnapshotResponse = z.infer<\n typeof OrganizationReadModelSnapshotResponseSchema\n>;\n\nconst OrganizationReadModelDeltaResponseSchema = z.strictObject({\n ...OrganizationReadModelResponseBaseShape,\n lanes: z.strictObject({\n directory: OrganizationReadModelDirectoryResponseSchema.optional(),\n grants: OrganizationReadModelGrantsResponseSchema.optional(),\n groupMemberships:\n OrganizationReadModelGroupMembershipsResponseSchema.optional(),\n groups: OrganizationReadModelGroupsResponseSchema.optional(),\n organizationPolicy:\n OrganizationReadModelOrganizationPolicyResponseSchema.optional(),\n }),\n mode: z.literal(\"delta\"),\n});\n\nexport type OrganizationReadModelDeltaResponse = z.infer<\n typeof OrganizationReadModelDeltaResponseSchema\n>;\n\nconst OrganizationReadModelResponseSchemaView = z.discriminatedUnion(\"mode\", [\n OrganizationReadModelDeltaResponseSchema,\n OrganizationReadModelSnapshotResponseSchema,\n]);\n\ntype OrganizationReadModelResponseView = z.infer<\n typeof OrganizationReadModelResponseSchemaView\n>;\n\nfunction hasValidGrantSubjectFields(\n grant: z.infer<typeof OrganizationReadModelGrantResponseSchema>,\n): boolean {\n if (grant.subjectType === \"user\") {\n return grant.userId === grant.subjectId && grant.groupId === null;\n }\n if (grant.subjectType === \"group\") {\n return (\n grant.userId === null &&\n grant.signingKeyFingerprint === null &&\n grant.groupId === grant.subjectId\n );\n }\n return false;\n}\n\nfunction hasValidGrants(\n grants: OrganizationReadModelGrantsResponse[\"grants\"],\n): boolean {\n const keys = new Set<string>();\n return grants.every((grant) => {\n const key = `${grant.subjectType}:${grant.subjectId}:${grant.containerId}`;\n if (!hasValidGrantSubjectFields(grant) || keys.has(key)) {\n return false;\n }\n keys.add(key);\n return true;\n });\n}\n\nfunction hasValidGroupMemberships(\n lane: OrganizationReadModelGroupMembershipsResponse,\n): boolean {\n const groupIds = new Set<string>();\n const validGroups = lane.groups.every((group) => {\n if (groupIds.has(group.groupId)) {\n return false;\n }\n groupIds.add(group.groupId);\n const memberIds = new Set<string>();\n return group.members.every((member) => {\n if (memberIds.has(member.userId)) {\n return false;\n }\n memberIds.add(member.userId);\n return true;\n });\n });\n if (!validGroups) {\n return false;\n }\n\n const deletedGroupIds = new Set<string>();\n return lane.deletedGroupIds.every((groupId) => {\n if (groupIds.has(groupId) || deletedGroupIds.has(groupId)) {\n return false;\n }\n deletedGroupIds.add(groupId);\n return true;\n });\n}\n\nfunction laneOrganizationsMatch(\n value: OrganizationReadModelResponseView,\n): boolean {\n const { lanes, organizationId } = value;\n return (\n (lanes.directory === undefined ||\n lanes.directory.organizationId === organizationId) &&\n (lanes.grants === undefined ||\n lanes.grants.organizationId === organizationId) &&\n (lanes.groupMemberships === undefined ||\n lanes.groupMemberships.organizationId === organizationId) &&\n (lanes.groups === undefined ||\n (lanes.groups.organizationId === organizationId &&\n lanes.groups.groups.every(\n (group) => group.organizationId === organizationId,\n ))) &&\n (lanes.organizationPolicy === undefined ||\n lanes.organizationPolicy.organizationId === organizationId)\n );\n}\n\nfunction hasValidReadModelSemantics(\n value: OrganizationReadModelResponseView,\n): boolean {\n if (\n !laneOrganizationsMatch(value) ||\n (value.lanes.grants !== undefined &&\n !hasValidGrants(value.lanes.grants.grants)) ||\n (value.lanes.groupMemberships !== undefined &&\n !hasValidGroupMemberships(value.lanes.groupMemberships))\n ) {\n return false;\n }\n\n return (\n value.mode === \"delta\" ||\n value.lanes.groupMemberships.deletedGroupIds.length === 0\n );\n}\n\nexport const OrganizationReadModelResponseSchema =\n registerJsonSchemaRuntimeRefinements(\n registerJsonSchemaView(\n OrganizationReadModelResponseSchemaView.refine(\n hasValidReadModelSemantics,\n ),\n OrganizationReadModelResponseSchemaView,\n ),\n organizationReadModelResponseRuntimeRefinements,\n );\n\nexport type OrganizationReadModelResponse = z.infer<\n typeof OrganizationReadModelResponseSchema\n>;\n\nexport function isOrganizationReadModelResponse(\n value: unknown,\n): value is OrganizationReadModelResponse {\n return OrganizationReadModelResponseSchema.safeParse(value).success;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"organizationReadModel.js","sourceRoot":"","sources":["organizationReadModel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EACL,oCAAoC,EACpC,sBAAsB,GACvB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,+CAA+C,EAAE,MAAM,qCAAqC,CAAC;AACtG,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EACL,uCAAuC,EACvC,6CAA6C,EAC7C,sCAAsC,EACtC,0CAA0C,EAC1C,oCAAoC,EACpC,qCAAqC,GACtC,MAAM,gBAAgB,CAAC;AAExB,MAAM,gDAAgD,GAAG,CAAC,CAAC,YAAY,CACrE,sCAAsC,CACvC,CAAC;AAEF,MAAM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AC,KAAK,CAAC,gBAAgB,CAAC,CAAC,EAC1D,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,CACL,KAAK,CAAC,IAAI,KAAK,OAAO;QACtB,KAAK,CAAC,KAAK,CAAC,gBAAgB,CAAC,eAAe,CAAC,MAAM,KAAK,CAAC,CAC1D,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,mCAAmC,GAC9C,oCAAoC,CAClC,sBAAsB,CACpB,uCAAuC,CAAC,MAAM,CAC5C,0BAA0B,CAC3B,EACD,uCAAuC,CACxC,EACD,+CAA+C,CAChD,CAAC;AAMJ,MAAM,UAAU,+BAA+B,CAC7C,KAAc;IAEd,OAAO,mCAAmC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC;AACtE,CAAC","sourcesContent":["import { z } from \"zod\";\nimport {\n registerJsonSchemaRuntimeRefinements,\n registerJsonSchemaView,\n} from \"../jsonSchema\";\nimport { organizationReadModelResponseRuntimeRefinements } from \"../organizationReadModelRefinements\";\nimport { arraySchema } from \"../schema\";\nimport {\n OrganizationContainerGrantResponseShape,\n OrganizationDirectoryCurrentUserResponseShape,\n OrganizationDirectoryUserResponseShape,\n OrganizationGroupCurrentStateResponseShape,\n OrganizationGroupMemberResponseShape,\n OrganizationGroupSummaryResponseShape,\n} from \"./organization\";\n\nconst OrganizationReadModelDirectoryUserResponseSchema = z.strictObject(\n OrganizationDirectoryUserResponseShape,\n);\n\nconst OrganizationReadModelDirectoryResponseSchema = z.strictObject({\n organizationId: z.string(),\n profileDocumentId: z.string().nullable(),\n users: arraySchema(OrganizationReadModelDirectoryUserResponseSchema),\n});\n\nexport type OrganizationReadModelDirectoryResponse = z.infer<\n typeof OrganizationReadModelDirectoryResponseSchema\n>;\n\nconst OrganizationReadModelGrantResponseSchema = z.strictObject(\n OrganizationContainerGrantResponseShape,\n);\n\nconst OrganizationReadModelGrantsResponseSchema = z.strictObject({\n grants: arraySchema(OrganizationReadModelGrantResponseSchema),\n organizationId: z.string(),\n});\n\nexport type OrganizationReadModelGrantsResponse = z.infer<\n typeof OrganizationReadModelGrantsResponseSchema\n>;\n\n/**\n * A projected member names only the user and role. Public keys live once per\n * user in the directory lane; keying never reads either copy from the read\n * model.\n */\nconst OrganizationReadModelGroupMemberResponseSchema = z.strictObject({\n role: OrganizationGroupMemberResponseShape.role,\n userId: OrganizationGroupMemberResponseShape.userId,\n});\n\nexport type OrganizationReadModelGroupMemberResponse = z.infer<\n typeof OrganizationReadModelGroupMemberResponseSchema\n>;\n\nconst OrganizationReadModelGroupMembershipResponseSchema = z.strictObject({\n groupId: z.string(),\n members: arraySchema(OrganizationReadModelGroupMemberResponseSchema),\n stateHash: z.string().min(1),\n});\n\nexport type OrganizationReadModelGroupMembershipResponse = z.infer<\n typeof OrganizationReadModelGroupMembershipResponseSchema\n>;\n\nconst OrganizationReadModelGroupMembershipsResponseSchema = z.strictObject({\n deletedGroupIds: arraySchema(z.string().min(1)),\n groups: arraySchema(OrganizationReadModelGroupMembershipResponseSchema),\n organizationId: z.string(),\n});\n\nexport type OrganizationReadModelGroupMembershipsResponse = z.infer<\n typeof OrganizationReadModelGroupMembershipsResponseSchema\n>;\n\nconst OrganizationReadModelGroupCurrentStateResponseSchema = z.strictObject(\n OrganizationGroupCurrentStateResponseShape,\n);\n\nconst OrganizationReadModelGroupSummaryResponseSchema = z.strictObject({\n ...OrganizationGroupSummaryResponseShape,\n currentState: OrganizationReadModelGroupCurrentStateResponseSchema.nullable(),\n});\n\nconst OrganizationReadModelGroupsResponseSchema = z.strictObject({\n groups: arraySchema(OrganizationReadModelGroupSummaryResponseSchema),\n memberGroupId: z.string(),\n organizationId: z.string(),\n});\n\nconst OrganizationReadModelOrganizationPolicyResponseSchema = z.strictObject({\n currentState: z.strictObject({\n ...OrganizationGroupCurrentStateResponseShape,\n stateHash: z.string().min(1),\n }),\n organizationId: z.string(),\n});\n\nexport type OrganizationReadModelOrganizationPolicyResponse = z.infer<\n typeof OrganizationReadModelOrganizationPolicyResponseSchema\n>;\n\nconst OrganizationReadModelCurrentUserResponseSchema = z.strictObject(\n OrganizationDirectoryCurrentUserResponseShape,\n);\n\nconst OrganizationReadModelResponseBaseShape = {\n currentUser: OrganizationReadModelCurrentUserResponseSchema,\n hasMore: z.boolean(),\n nextCursor: z.string().min(1),\n organizationId: z.string(),\n version: z.literal(6),\n};\n\nconst OrganizationReadModelSnapshotResponseSchema = z.strictObject({\n ...OrganizationReadModelResponseBaseShape,\n hasMore: z.literal(false),\n lanes: z.strictObject({\n directory: OrganizationReadModelDirectoryResponseSchema,\n grants: OrganizationReadModelGrantsResponseSchema,\n groupMemberships: OrganizationReadModelGroupMembershipsResponseSchema,\n groups: OrganizationReadModelGroupsResponseSchema,\n organizationPolicy: OrganizationReadModelOrganizationPolicyResponseSchema,\n }),\n mode: z.literal(\"snapshot\"),\n});\n\nexport type OrganizationReadModelSnapshotResponse = z.infer<\n typeof OrganizationReadModelSnapshotResponseSchema\n>;\n\nconst OrganizationReadModelDeltaResponseSchema = z.strictObject({\n ...OrganizationReadModelResponseBaseShape,\n lanes: z.strictObject({\n directory: OrganizationReadModelDirectoryResponseSchema.optional(),\n grants: OrganizationReadModelGrantsResponseSchema.optional(),\n groupMemberships:\n OrganizationReadModelGroupMembershipsResponseSchema.optional(),\n groups: OrganizationReadModelGroupsResponseSchema.optional(),\n organizationPolicy:\n OrganizationReadModelOrganizationPolicyResponseSchema.optional(),\n }),\n mode: z.literal(\"delta\"),\n});\n\nexport type OrganizationReadModelDeltaResponse = z.infer<\n typeof OrganizationReadModelDeltaResponseSchema\n>;\n\nconst OrganizationReadModelResponseSchemaView = z.discriminatedUnion(\"mode\", [\n OrganizationReadModelDeltaResponseSchema,\n OrganizationReadModelSnapshotResponseSchema,\n]);\n\ntype OrganizationReadModelResponseView = z.infer<\n typeof OrganizationReadModelResponseSchemaView\n>;\n\nfunction hasValidGrantSubjectFields(\n grant: z.infer<typeof OrganizationReadModelGrantResponseSchema>,\n): boolean {\n if (grant.subjectType === \"user\") {\n return grant.userId === grant.subjectId && grant.groupId === null;\n }\n if (grant.subjectType === \"group\") {\n return (\n grant.userId === null &&\n grant.signingKeyFingerprint === null &&\n grant.groupId === grant.subjectId\n );\n }\n return false;\n}\n\nfunction hasValidGrants(\n grants: OrganizationReadModelGrantsResponse[\"grants\"],\n): boolean {\n const keys = new Set<string>();\n return grants.every((grant) => {\n const key = `${grant.subjectType}:${grant.subjectId}:${grant.containerId}`;\n if (!hasValidGrantSubjectFields(grant) || keys.has(key)) {\n return false;\n }\n keys.add(key);\n return true;\n });\n}\n\nfunction hasValidGroupMemberships(\n lane: OrganizationReadModelGroupMembershipsResponse,\n): boolean {\n const groupIds = new Set<string>();\n const validGroups = lane.groups.every((group) => {\n if (groupIds.has(group.groupId)) {\n return false;\n }\n groupIds.add(group.groupId);\n const memberIds = new Set<string>();\n return group.members.every((member) => {\n if (memberIds.has(member.userId)) {\n return false;\n }\n memberIds.add(member.userId);\n return true;\n });\n });\n if (!validGroups) {\n return false;\n }\n\n const deletedGroupIds = new Set<string>();\n return lane.deletedGroupIds.every((groupId) => {\n if (groupIds.has(groupId) || deletedGroupIds.has(groupId)) {\n return false;\n }\n deletedGroupIds.add(groupId);\n return true;\n });\n}\n\nfunction laneOrganizationsMatch(\n value: OrganizationReadModelResponseView,\n): boolean {\n const { lanes, organizationId } = value;\n return (\n (lanes.directory === undefined ||\n lanes.directory.organizationId === organizationId) &&\n (lanes.grants === undefined ||\n lanes.grants.organizationId === organizationId) &&\n (lanes.groupMemberships === undefined ||\n lanes.groupMemberships.organizationId === organizationId) &&\n (lanes.groups === undefined ||\n (lanes.groups.organizationId === organizationId &&\n lanes.groups.groups.every(\n (group) => group.organizationId === organizationId,\n ))) &&\n (lanes.organizationPolicy === undefined ||\n lanes.organizationPolicy.organizationId === organizationId)\n );\n}\n\nfunction hasValidReadModelSemantics(\n value: OrganizationReadModelResponseView,\n): boolean {\n if (\n !laneOrganizationsMatch(value) ||\n (value.lanes.grants !== undefined &&\n !hasValidGrants(value.lanes.grants.grants)) ||\n (value.lanes.groupMemberships !== undefined &&\n !hasValidGroupMemberships(value.lanes.groupMemberships))\n ) {\n return false;\n }\n\n return (\n value.mode === \"delta\" ||\n value.lanes.groupMemberships.deletedGroupIds.length === 0\n );\n}\n\nexport const OrganizationReadModelResponseSchema =\n registerJsonSchemaRuntimeRefinements(\n registerJsonSchemaView(\n OrganizationReadModelResponseSchemaView.refine(\n hasValidReadModelSemantics,\n ),\n OrganizationReadModelResponseSchemaView,\n ),\n organizationReadModelResponseRuntimeRefinements,\n );\n\nexport type OrganizationReadModelResponse = z.infer<\n typeof OrganizationReadModelResponseSchema\n>;\n\nexport function isOrganizationReadModelResponse(\n value: unknown,\n): value is OrganizationReadModelResponse {\n return OrganizationReadModelResponseSchema.safeParse(value).success;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { UpdateOrganizationProfileRequest, UpdateOrganizationRosterEntryRequest } from "../../internal/validators/request/index.js";
|
|
2
|
-
import type { OrganizationContainerGrantResponse, OrganizationContainerGrantsResponse, OrganizationDirectoryResponse, OrganizationDirectoryUserResponse, OrganizationGroupContainerResponse, OrganizationGroupContainersResponse,
|
|
2
|
+
import type { OrganizationContainerGrantResponse, OrganizationContainerGrantsResponse, OrganizationDirectoryResponse, OrganizationDirectoryUserResponse, OrganizationGroupContainerResponse, OrganizationGroupContainersResponse, OrganizationGroupSummaryResponse, OrganizationProfileResponse, OrganizationReadModelGroupMemberResponse, OrganizationUserDetailResponse } from "../../internal/validators/response/index.js";
|
|
3
3
|
import type { OrganizationGroupPolicyHistory } from "./policyHistoryReadModel.js";
|
|
4
4
|
export type { OrganizationGroupPolicyHistory, OrganizationGroupPolicyHistoryEntry, OrganizationPolicyGrantChange, OrganizationPolicyGroupChange, OrganizationPolicyHistory, OrganizationPolicyHistoryEntry, OrganizationPrincipalMemberChange, OrganizationPrincipalMemberChangeType, OrganizationPrincipalPolicyHistory, OrganizationPrincipalPolicyHistoryEntry, } from "./policyHistoryReadModel.js";
|
|
5
5
|
export { buildOrganizationGroupPolicyHistory, buildOrganizationPolicyHistory, } from "./policyHistoryReadModel.js";
|
|
@@ -26,8 +26,13 @@ export interface OrganizationUserDetail extends Omit<OrganizationUserDetailRespo
|
|
|
26
26
|
readonly groupGrants: OrganizationContainerGrant[];
|
|
27
27
|
};
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
export type
|
|
29
|
+
/** A projected group member: public keys live in the directory lane only. */
|
|
30
|
+
export type OrganizationGroupMember = OrganizationReadModelGroupMemberResponse;
|
|
31
|
+
export interface OrganizationGroupMembers {
|
|
32
|
+
readonly groupId: string;
|
|
33
|
+
readonly members: OrganizationGroupMember[];
|
|
34
|
+
readonly organizationId: string;
|
|
35
|
+
}
|
|
31
36
|
export type OrganizationGroupSummary = OrganizationGroupSummaryResponse & {
|
|
32
37
|
readonly name: string;
|
|
33
38
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readModel.js","sourceRoot":"","sources":["readModel.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"readModel.js","sourceRoot":"","sources":["readModel.ts"],"names":[],"mappings":"AA8BA,OAAO,EACL,mCAAmC,EACnC,8BAA8B,GAC/B,MAAM,0BAA0B,CAAC;AAqElC,MAAM,CAAC,KAAK,UAAU,6BAA6B,CAAC,KAQnD;IACC,OAAO,KAAK,CAAC,SAAS,CAAC,6BAA6B,CAClD,KAAK,CAAC,cAAc,EACpB,KAAK,CAAC,MAAM,EACZ,EAAE,iBAAiB,EAAE,KAAK,CAAC,iBAAiB,EAAE,CAC/C,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAAC,KAI/C;IACC,OAAO,KAAK,CAAC,SAAS,CAAC,yBAAyB,CAAC,KAAK,CAAC,cAAc,EAAE;QACrE,iBAAiB,EAAE,KAAK,CAAC,iBAAiB;KAC3C,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type {\n UpdateOrganizationProfileRequest,\n UpdateOrganizationRosterEntryRequest,\n} from \"@tearleads/validators/request\";\nimport type {\n OrganizationContainerGrantResponse,\n OrganizationContainerGrantsResponse,\n OrganizationDirectoryResponse,\n OrganizationDirectoryUserResponse,\n OrganizationGroupContainerResponse,\n OrganizationGroupContainersResponse,\n OrganizationGroupSummaryResponse,\n OrganizationProfileResponse,\n OrganizationReadModelGroupMemberResponse,\n OrganizationUserDetailResponse,\n} from \"@tearleads/validators/response\";\nimport type { OrganizationGroupPolicyHistory } from \"./policyHistoryReadModel\";\n\nexport type {\n OrganizationGroupPolicyHistory,\n OrganizationGroupPolicyHistoryEntry,\n OrganizationPolicyGrantChange,\n OrganizationPolicyGroupChange,\n OrganizationPolicyHistory,\n OrganizationPolicyHistoryEntry,\n OrganizationPrincipalMemberChange,\n OrganizationPrincipalMemberChangeType,\n OrganizationPrincipalPolicyHistory,\n OrganizationPrincipalPolicyHistoryEntry,\n} from \"./policyHistoryReadModel\";\nexport {\n buildOrganizationGroupPolicyHistory,\n buildOrganizationPolicyHistory,\n} from \"./policyHistoryReadModel\";\n\nexport type OrganizationDirectory = OrganizationDirectoryResponse;\nexport type OrganizationDirectoryUser = OrganizationDirectoryUserResponse;\nexport type OrganizationProfile = OrganizationProfileResponse;\nexport type OrganizationGroupContainer = OrganizationGroupContainerResponse & {\n readonly containerDisplayName: string | null;\n};\nexport type OrganizationContainerGrant = OrganizationContainerGrantResponse & {\n readonly groupName: string | null;\n readonly containerDisplayName: string | null;\n};\nexport interface OrganizationGroupContainers\n extends Omit<OrganizationGroupContainersResponse, \"containers\"> {\n readonly containers: OrganizationGroupContainer[];\n}\nexport interface OrganizationContainerGrants\n extends Omit<OrganizationContainerGrantsResponse, \"grants\"> {\n readonly grants: OrganizationContainerGrant[];\n}\nexport interface OrganizationUserDetail\n extends Omit<OrganizationUserDetailResponse, \"grants\" | \"groups\"> {\n readonly groups: OrganizationGroupSummary[];\n readonly grants: {\n readonly directGrants: OrganizationContainerGrant[];\n readonly groupGrants: OrganizationContainerGrant[];\n };\n}\n/** A projected group member: public keys live in the directory lane only. */\nexport type OrganizationGroupMember = OrganizationReadModelGroupMemberResponse;\nexport interface OrganizationGroupMembers {\n readonly groupId: string;\n readonly members: OrganizationGroupMember[];\n readonly organizationId: string;\n}\nexport type OrganizationGroupSummary = OrganizationGroupSummaryResponse & {\n readonly name: string;\n /**\n * The group's signed name does not open under the metadata key this member\n * holds; only a dishonest admin signs such a name. `name` is then empty, and\n * the group must not be offered for selection by name.\n */\n readonly nameUnreadable: boolean;\n};\nexport interface OrganizationDirectoryAndGroups {\n readonly directory: OrganizationDirectory;\n readonly groups: ReadonlyArray<OrganizationGroupSummary>;\n readonly memberGroupId: string;\n readonly readModelCursor: string;\n}\n\nexport interface OrganizationGroupDetails {\n readonly members: OrganizationGroupMembers | null;\n readonly containers: OrganizationGroupContainers | null;\n readonly policyHistory: OrganizationGroupPolicyHistory | null;\n}\n\ninterface OrganizationReadApi {\n readonly updateOrganizationRosterEntry: (\n organizationId: string,\n userId: string,\n input: UpdateOrganizationRosterEntryRequest,\n ) => Promise<OrganizationDirectoryUserResponse | null>;\n readonly updateOrganizationProfile: (\n organizationId: string,\n input: UpdateOrganizationProfileRequest,\n ) => Promise<OrganizationProfileResponse | null>;\n}\n\nexport async function updateOrganizationRosterEntry(input: {\n readonly apiClient: Pick<\n OrganizationReadApi,\n \"updateOrganizationRosterEntry\"\n >;\n readonly organizationId: string;\n readonly profileDocumentId: string | null;\n readonly userId: string;\n}): Promise<OrganizationDirectoryUser | null> {\n return input.apiClient.updateOrganizationRosterEntry(\n input.organizationId,\n input.userId,\n { profileDocumentId: input.profileDocumentId },\n );\n}\n\nexport async function updateOrganizationProfile(input: {\n readonly apiClient: Pick<OrganizationReadApi, \"updateOrganizationProfile\">;\n readonly organizationId: string;\n readonly profileDocumentId: string | null;\n}): Promise<OrganizationProfileResponse | null> {\n return input.apiClient.updateOrganizationProfile(input.organizationId, {\n profileDocumentId: input.profileDocumentId,\n });\n}\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { RequestResult, RequestResultOptions } from "../../internal/api-client/index.js";
|
|
2
2
|
import { type OrganizationReadModelResponse } from "../../internal/validators/response/index.js";
|
|
3
|
+
import { type OrganizationReadModelGroupMembers } from "../../data/persistence/organizations/organizationReadModelMemberLoad.js";
|
|
3
4
|
import type { ExecSql } from "../../data/sqlite/sqlSchema.js";
|
|
4
5
|
import type { OrganizationDirectoryAndGroups } from "./readModel.js";
|
|
5
6
|
interface OrganizationReadModelApi {
|
|
@@ -17,18 +18,7 @@ export interface ReconcileOrganizationDirectoryAndGroupsInput extends Organizati
|
|
|
17
18
|
export declare function loadLocalOrganizationDirectoryAndGroups(input: OrganizationReadModelProjectionInput): Promise<OrganizationDirectoryAndGroups | null>;
|
|
18
19
|
export declare function loadLocalOrganizationGroupMembers(input: OrganizationReadModelProjectionInput & {
|
|
19
20
|
readonly groupId: string;
|
|
20
|
-
}): Promise<
|
|
21
|
-
groupId: string;
|
|
22
|
-
members: {
|
|
23
|
-
encapsulationKeyFingerprint: string | null;
|
|
24
|
-
encapsulationPublicKey: string | null;
|
|
25
|
-
role: "member" | "admin";
|
|
26
|
-
signingKeyFingerprint: string | null;
|
|
27
|
-
signingPublicKey: string | null;
|
|
28
|
-
userId: string;
|
|
29
|
-
}[];
|
|
30
|
-
organizationId: string;
|
|
31
|
-
} | null>;
|
|
21
|
+
}): Promise<OrganizationReadModelGroupMembers | null>;
|
|
32
22
|
/**
|
|
33
23
|
* Resolves the reconciled projection, `null` when the pass completed
|
|
34
24
|
* authoritatively with nothing presentable (including a denial that purged
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ORGANIZATION_READ_MODEL_ERROR_CODES, } from "../../internal/validators/response/index.js";
|
|
2
2
|
import { purgeOrganizationAccessProjection } from "../../data/persistence/organizations/organizationAccessRevocationPersistence.js";
|
|
3
3
|
import { recordOrganizationPresentationDenials } from "../../data/persistence/organizations/organizationPresentationDenialPersistence.js";
|
|
4
|
-
import { loadOrganizationReadModelGroupMembers } from "../../data/persistence/organizations/organizationReadModelMemberLoad.js";
|
|
4
|
+
import { loadOrganizationReadModelGroupMembers, } from "../../data/persistence/organizations/organizationReadModelMemberLoad.js";
|
|
5
5
|
import { OrganizationReadModelBindingError } from "../../data/persistence/organizations/organizationReadModelMembershipPersistence.js";
|
|
6
6
|
import { applyOrganizationReadModelResponse, loadOrganizationReadModelProjection, purgeOrganizationReadModelProjection, } from "../../data/persistence/organizations/organizationReadModelPersistence.js";
|
|
7
7
|
import { captureOrganizationPresentationAccessAttempt, denyOrganizationPresentationAccess, isOrganizationPresentationAccessAttemptCurrent, isOrganizationPresentationAccessReadable, restoreOrganizationPresentationAccess, runOrganizationPresentationMutation, runOrganizationPresentationRead, } from "./organizationPresentationAccessState.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"readModelProjection.js","sourceRoot":"","sources":["readModelProjection.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,mCAAmC,GAEpC,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,iCAAiC,EAAE,MAAM,8EAA8E,CAAC;AACjI,OAAO,EAAE,qCAAqC,EAAE,MAAM,gFAAgF,CAAC;AACvI,OAAO,EAAE,qCAAqC,EAAE,MAAM,sEAAsE,CAAC;AAC7H,OAAO,EAAE,iCAAiC,EAAE,MAAM,iFAAiF,CAAC;AACpI,OAAO,EACL,kCAAkC,EAClC,mCAAmC,EAEnC,oCAAoC,GACrC,MAAM,uEAAuE,CAAC;AAE/E,OAAO,EACL,4CAA4C,EAC5C,kCAAkC,EAClC,8CAA8C,EAC9C,wCAAwC,EAGxC,qCAAqC,EACrC,mCAAmC,EACnC,+BAA+B,GAChC,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAE,6CAA6C,EAAE,MAAM,oCAAoC,CAAC;AAGnG,MAAM,oBAAoB,GAAG,GAAG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C7D,CAAC;IACF,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7D,OAAO,QAAQ,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AAC1D,CAAC","sourcesContent":["import type {\n RequestResult,\n RequestResultOptions,\n} from \"@tearleads/api-client\";\nimport {\n ORGANIZATION_READ_MODEL_ERROR_CODES,\n type OrganizationReadModelResponse,\n} from \"@tearleads/validators/response\";\nimport { purgeOrganizationAccessProjection } from \"../../data/persistence/organizations/organizationAccessRevocationPersistence\";\nimport { recordOrganizationPresentationDenials } from \"../../data/persistence/organizations/organizationPresentationDenialPersistence\";\nimport { loadOrganizationReadModelGroupMembers } from \"../../data/persistence/organizations/organizationReadModelMemberLoad\";\nimport { OrganizationReadModelBindingError } from \"../../data/persistence/organizations/organizationReadModelMembershipPersistence\";\nimport {\n applyOrganizationReadModelResponse,\n loadOrganizationReadModelProjection,\n type OrganizationReadModelProjection,\n purgeOrganizationReadModelProjection,\n} from \"../../data/persistence/organizations/organizationReadModelPersistence\";\nimport type { ExecSql } from \"../../data/sqlite/sqlSchema\";\nimport {\n captureOrganizationPresentationAccessAttempt,\n denyOrganizationPresentationAccess,\n isOrganizationPresentationAccessAttemptCurrent,\n isOrganizationPresentationAccessReadable,\n type OrganizationPresentationAccessAttempt,\n type OrganizationPresentationAccessInput,\n restoreOrganizationPresentationAccess,\n runOrganizationPresentationMutation,\n runOrganizationPresentationRead,\n} from \"./organizationPresentationAccessState\";\nimport { isOrganizationPresentationAccessDeniedFailure } from \"./organizationPresentationFailures\";\nimport type { OrganizationDirectoryAndGroups } from \"./readModel\";\n\nconst MAX_READ_MODEL_PAGES = 100;\nconst MAX_STALE_RETRIES = 1;\n\ninterface OrganizationReadModelApi {\n getOrganizationReadModelResult(\n organizationId: string,\n cursor?: string,\n options?: RequestResultOptions,\n ): Promise<RequestResult<OrganizationReadModelResponse>>;\n}\n\ninterface OrganizationReadModelProjectionInput {\n readonly currentUserId: string;\n readonly execSql: ExecSql;\n readonly organizationId: string;\n}\n\nexport interface ReconcileOrganizationDirectoryAndGroupsInput\n extends OrganizationReadModelProjectionInput {\n readonly apiClient: OrganizationReadModelApi;\n readonly logError?:\n | ((message: string | Error, cause?: unknown) => void)\n | undefined;\n}\n\nfunction toDirectoryAndGroups(\n projection: OrganizationReadModelProjection | null,\n): OrganizationDirectoryAndGroups | null {\n if (!projection?.requester) {\n return null;\n }\n\n return {\n directory: {\n ...projection.directory,\n currentUser: projection.requester,\n },\n // Hydration decides whether a name is unreadable; a stored blank name may\n // only mean the group has not been hydrated yet.\n groups: projection.groups.groups.map((group) => ({\n ...group,\n nameUnreadable: false,\n })),\n memberGroupId: projection.groups.memberGroupId,\n readModelCursor: projection.cursor,\n };\n}\n\nasync function loadProjection(\n input: OrganizationReadModelProjectionInput,\n): Promise<OrganizationReadModelProjection | null> {\n return runOrganizationPresentationRead(\n organizationPresentationAccessInput(input),\n \"readModel\",\n () => loadRawProjection(input),\n );\n}\n\nfunction loadRawProjection(\n input: OrganizationReadModelProjectionInput,\n): Promise<OrganizationReadModelProjection | null> {\n return loadOrganizationReadModelProjection(\n input.execSql,\n input.organizationId,\n input.currentUserId,\n );\n}\n\nfunction organizationPresentationAccessInput(\n input: OrganizationReadModelProjectionInput,\n): OrganizationPresentationAccessInput {\n return {\n execSql: input.execSql,\n organizationId: input.organizationId,\n requesterUserId: input.currentUserId,\n };\n}\n\ninterface ReconciliationState {\n readonly accessAttempt: OrganizationPresentationAccessAttempt;\n expectedLocalCursor: string | null;\n preparedDeniedProjection: boolean;\n projection: OrganizationReadModelProjection | null;\n requestCursor: string | undefined;\n retriedBindingFailure: boolean;\n retriedInvalidCursor: boolean;\n staleRetries: number;\n}\n\ntype ReconciliationStep =\n | { readonly kind: \"continue\" }\n | {\n readonly kind: \"done\";\n readonly value: OrganizationDirectoryAndGroups | null | undefined;\n };\n\ntype PageRequestStep =\n | ReconciliationStep\n | {\n readonly kind: \"response\";\n readonly response: OrganizationReadModelResponse;\n };\n\nfunction localResult(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n outcome: { readonly failed: boolean },\n): ReconciliationStep {\n const accessInput = organizationPresentationAccessInput(input);\n const readable =\n isOrganizationPresentationAccessAttemptCurrent(\n accessInput,\n state.accessAttempt,\n ) && isOrganizationPresentationAccessReadable(accessInput, \"readModel\");\n if (!readable) {\n return { kind: \"done\", value: null };\n }\n const value = toDirectoryAndGroups(state.projection);\n // A failed pass that retained nothing must not read as a completed (null)\n // pass: consumers mark a scope caught up only on completed passes, and this\n // scope still needs a fresh catch-up.\n if (value === null && outcome.failed) {\n return { kind: \"done\", value: undefined };\n }\n return { kind: \"done\", value };\n}\n\nfunction resetRequestCursor(state: ReconciliationState): void {\n state.expectedLocalCursor = state.projection?.cursor ?? null;\n state.requestCursor = state.projection?.cursor ?? undefined;\n}\n\nfunction isInvalidReadModelCursorFailure(\n result: RequestResult<OrganizationReadModelResponse>,\n): boolean {\n return (\n !result.ok &&\n result.kind === \"http\" &&\n result.status === 400 &&\n result.code === ORGANIZATION_READ_MODEL_ERROR_CODES.cursorInvalid\n );\n}\n\nasync function requestReadModelPage(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n): Promise<PageRequestStep> {\n const result = await input.apiClient.getOrganizationReadModelResult(\n input.organizationId,\n state.requestCursor,\n { reportErrors: false },\n );\n if (result.ok) {\n return { kind: \"response\", response: result.data };\n }\n if (isOrganizationPresentationAccessDeniedFailure(result)) {\n const accessInput = organizationPresentationAccessInput(input);\n denyOrganizationPresentationAccess(accessInput, [\"readModel\", \"usage\"]);\n // The durable marker lands before the purge attempt: if the purge fails,\n // an offline restart must still refuse to serve the revoked projection.\n try {\n await runOrganizationPresentationMutation(input.execSql, () =>\n recordOrganizationPresentationDenials(accessInput, [\n \"readModel\",\n \"usage\",\n ]),\n );\n } catch (error) {\n input.logError?.(\"Failed to record denied organization access\", error);\n }\n try {\n await runOrganizationPresentationMutation(input.execSql, () =>\n purgeOrganizationAccessProjection({\n execSql: input.execSql,\n organizationId: input.organizationId,\n requesterUserId: input.currentUserId,\n }),\n );\n } catch (error) {\n input.logError?.(\"Failed to purge denied organization access\", error);\n }\n return { kind: \"done\", value: null };\n }\n if (\n isInvalidReadModelCursorFailure(result) &&\n state.requestCursor !== undefined &&\n !state.retriedInvalidCursor\n ) {\n state.requestCursor = undefined;\n state.retriedInvalidCursor = true;\n return { kind: \"continue\" };\n }\n\n result.report();\n return localResult(input, state, { failed: true });\n}\n\nfunction responseMatchesRequest(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n response: OrganizationReadModelResponse,\n): boolean {\n if (response.organizationId !== input.organizationId) {\n input.logError?.(\n `Organization read-model response scope does not match ${input.organizationId}`,\n );\n return false;\n }\n if (response.hasMore && response.nextCursor === state.expectedLocalCursor) {\n input.logError?.(\"Organization read-model page did not advance its cursor\");\n return false;\n }\n return true;\n}\n\nfunction reconciliationIsCurrent(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n): boolean {\n return isOrganizationPresentationAccessAttemptCurrent(\n organizationPresentationAccessInput(input),\n state.accessAttempt,\n );\n}\n\ntype ApplyResponseStep =\n | ReconciliationStep\n | {\n readonly kind: \"applied\";\n readonly result: Awaited<\n ReturnType<typeof applyOrganizationReadModelResponse>\n >;\n };\n\nasync function applyResponse(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n response: OrganizationReadModelResponse,\n): Promise<ApplyResponseStep> {\n try {\n return {\n kind: \"applied\",\n result: await applyOrganizationReadModelResponse({\n currentUserId: input.currentUserId,\n execSql: input.execSql,\n requestedCursor: state.expectedLocalCursor,\n response,\n }),\n };\n } catch (error) {\n input.logError?.(\"Failed to apply organization read-model response\", error);\n if (\n !(error instanceof OrganizationReadModelBindingError) ||\n state.retriedBindingFailure\n ) {\n return localResult(input, state, { failed: true });\n }\n state.retriedBindingFailure = true;\n await purgeOrganizationReadModelProjection(\n input.execSql,\n input.organizationId,\n );\n if (!reconciliationIsCurrent(input, state)) {\n return { kind: \"done\", value: null };\n }\n state.projection = null;\n resetRequestCursor(state);\n return { kind: \"continue\" };\n }\n}\n\nasync function prepareDeniedProjectionForApply(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n): Promise<ReconciliationStep | null> {\n if (state.preparedDeniedProjection) {\n return null;\n }\n try {\n // The apply that follows replaces the shared rows authoritatively, so\n // only this requester's row is dropped: another local identity sharing\n // the database keeps its access and reads the replacement.\n await purgeOrganizationReadModelProjection(\n input.execSql,\n input.organizationId,\n { onlyRequesterUserId: input.currentUserId },\n );\n } catch (error) {\n input.logError?.(\"Failed to replace denied organization read model\", error);\n return { kind: \"done\", value: null };\n }\n if (!reconciliationIsCurrent(input, state)) {\n return { kind: \"done\", value: null };\n }\n state.expectedLocalCursor = null;\n state.preparedDeniedProjection = true;\n state.projection = null;\n return null;\n}\n\nasync function applyReadModelPage(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n response: OrganizationReadModelResponse,\n): Promise<ReconciliationStep> {\n if (!responseMatchesRequest(input, state, response)) {\n return localResult(input, state, { failed: true });\n }\n\n const accessInput = organizationPresentationAccessInput(input);\n return runOrganizationPresentationMutation(input.execSql, async () => {\n if (!reconciliationIsCurrent(input, state)) {\n return { kind: \"done\", value: null };\n }\n const preparation = await prepareDeniedProjectionForApply(input, state);\n if (preparation) {\n return preparation;\n }\n\n const applied = await applyResponse(input, state, response);\n if (applied.kind !== \"applied\") {\n return applied;\n }\n\n state.projection = await loadRawProjection(input);\n if (!reconciliationIsCurrent(input, state)) {\n return { kind: \"done\", value: null };\n }\n if (applied.result === \"stale\") {\n if (state.staleRetries >= MAX_STALE_RETRIES) {\n return localResult(input, state, { failed: true });\n }\n state.staleRetries += 1;\n resetRequestCursor(state);\n return { kind: \"continue\" };\n }\n\n state.expectedLocalCursor = state.projection?.cursor ?? response.nextCursor;\n if (!response.hasMore) {\n return restoreOrganizationPresentationAccess(\n accessInput,\n state.accessAttempt,\n )\n ? localResult(input, state, { failed: false })\n : { kind: \"done\", value: null };\n }\n state.requestCursor = response.nextCursor;\n return { kind: \"continue\" };\n });\n}\n\nexport async function loadLocalOrganizationDirectoryAndGroups(\n input: OrganizationReadModelProjectionInput,\n): Promise<OrganizationDirectoryAndGroups | null> {\n return toDirectoryAndGroups(await loadProjection(input));\n}\n\nexport async function loadLocalOrganizationGroupMembers(\n input: OrganizationReadModelProjectionInput & { readonly groupId: string },\n) {\n return runOrganizationPresentationRead(\n organizationPresentationAccessInput(input),\n \"readModel\",\n () =>\n loadOrganizationReadModelGroupMembers(\n input.execSql,\n input.organizationId,\n input.groupId,\n input.currentUserId,\n ),\n );\n}\n\n/**\n * Resolves the reconciled projection, `null` when the pass completed\n * authoritatively with nothing presentable (including a denial that purged\n * the projection), or `undefined` when the feed failed and no local\n * projection was retained — a pass that must not count as caught up.\n */\nexport async function reconcileOrganizationDirectoryAndGroups(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n): Promise<OrganizationDirectoryAndGroups | null | undefined> {\n const accessInput = organizationPresentationAccessInput(input);\n const accessAttempt = captureOrganizationPresentationAccessAttempt(\n accessInput,\n \"readModel\",\n );\n const state: ReconciliationState = {\n accessAttempt,\n expectedLocalCursor: null,\n preparedDeniedProjection: !accessAttempt.startedDenied,\n projection: await loadProjection(input),\n requestCursor: undefined,\n retriedBindingFailure: false,\n retriedInvalidCursor: false,\n staleRetries: 0,\n };\n resetRequestCursor(state);\n\n for (let page = 0; page < MAX_READ_MODEL_PAGES; page += 1) {\n const request = await requestReadModelPage(input, state);\n if (request.kind === \"done\") {\n return request.value;\n }\n if (request.kind === \"continue\") {\n continue;\n }\n const applied = await applyReadModelPage(input, state, request.response);\n if (applied.kind === \"done\") {\n return applied.value;\n }\n }\n\n input.logError?.(\n \"Organization read-model reconciliation exceeded page limit\",\n );\n const retained = localResult(input, state, { failed: true });\n return retained.kind === \"done\" ? retained.value : null;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"readModelProjection.js","sourceRoot":"","sources":["readModelProjection.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,mCAAmC,GAEpC,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,iCAAiC,EAAE,MAAM,8EAA8E,CAAC;AACjI,OAAO,EAAE,qCAAqC,EAAE,MAAM,gFAAgF,CAAC;AACvI,OAAO,EACL,qCAAqC,GAEtC,MAAM,sEAAsE,CAAC;AAC9E,OAAO,EAAE,iCAAiC,EAAE,MAAM,iFAAiF,CAAC;AACpI,OAAO,EACL,kCAAkC,EAClC,mCAAmC,EAEnC,oCAAoC,GACrC,MAAM,uEAAuE,CAAC;AAE/E,OAAO,EACL,4CAA4C,EAC5C,kCAAkC,EAClC,8CAA8C,EAC9C,wCAAwC,EAGxC,qCAAqC,EACrC,mCAAmC,EACnC,+BAA+B,GAChC,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAE,6CAA6C,EAAE,MAAM,oCAAoC,CAAC;AAGnG,MAAM,oBAAoB,GAAG,GAAG,CAAC;AACjC,MAAM,iBAAiB,GAAG,CAAC,CAAC;AAwB5B,SAAS,oBAAoB,CAC3B,UAAkD;IAElD,IAAI,CAAC,UAAU,EAAE,SAAS,EAAE,CAAC;QAC3B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO;QACL,SAAS,EAAE;YACT,GAAG,UAAU,CAAC,SAAS;YACvB,WAAW,EAAE,UAAU,CAAC,SAAS;SAClC;QACD,0EAA0E;QAC1E,iDAAiD;QACjD,MAAM,EAAE,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;YAC/C,GAAG,KAAK;YACR,cAAc,EAAE,KAAK;SACtB,CAAC,CA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KAAK,CAAC;QACvC,aAAa,EAAE,SAAS;QACxB,qBAAqB,EAAE,KAAK;QAC5B,oBAAoB,EAAE,KAAK;QAC3B,YAAY,EAAE,CAAC;KAChB,CAAC;IACF,kBAAkB,CAAC,KAAK,CAAC,CAAC;IAE1B,KAAK,IAAI,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,oBAAoB,EAAE,IAAI,IAAI,CAAC,EAAE,CAAC;QAC1D,MAAM,OAAO,GAAG,MAAM,oBAAoB,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QACzD,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAC5B,OAAO,OAAO,CAAC,KAAK,CAAC;QACvB,CAAC;QACD,IAAI,OAAO,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;YAChC,SAAS;QACX,CAAC;QACD,MAAM,OAAO,GAAG,MAAM,kBAAkB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;QACzE,IAAI,OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAC5B,OAAO,OAAO,CAAC,KAAK,CAAC;QACvB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,QAAQ,EAAE,CACd,4DAA4D,CAC7D,CAAC;IACF,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;IAC7D,OAAO,QAAQ,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AAC1D,CAAC","sourcesContent":["import type {\n RequestResult,\n RequestResultOptions,\n} from \"@tearleads/api-client\";\nimport {\n ORGANIZATION_READ_MODEL_ERROR_CODES,\n type OrganizationReadModelResponse,\n} from \"@tearleads/validators/response\";\nimport { purgeOrganizationAccessProjection } from \"../../data/persistence/organizations/organizationAccessRevocationPersistence\";\nimport { recordOrganizationPresentationDenials } from \"../../data/persistence/organizations/organizationPresentationDenialPersistence\";\nimport {\n loadOrganizationReadModelGroupMembers,\n type OrganizationReadModelGroupMembers,\n} from \"../../data/persistence/organizations/organizationReadModelMemberLoad\";\nimport { OrganizationReadModelBindingError } from \"../../data/persistence/organizations/organizationReadModelMembershipPersistence\";\nimport {\n applyOrganizationReadModelResponse,\n loadOrganizationReadModelProjection,\n type OrganizationReadModelProjection,\n purgeOrganizationReadModelProjection,\n} from \"../../data/persistence/organizations/organizationReadModelPersistence\";\nimport type { ExecSql } from \"../../data/sqlite/sqlSchema\";\nimport {\n captureOrganizationPresentationAccessAttempt,\n denyOrganizationPresentationAccess,\n isOrganizationPresentationAccessAttemptCurrent,\n isOrganizationPresentationAccessReadable,\n type OrganizationPresentationAccessAttempt,\n type OrganizationPresentationAccessInput,\n restoreOrganizationPresentationAccess,\n runOrganizationPresentationMutation,\n runOrganizationPresentationRead,\n} from \"./organizationPresentationAccessState\";\nimport { isOrganizationPresentationAccessDeniedFailure } from \"./organizationPresentationFailures\";\nimport type { OrganizationDirectoryAndGroups } from \"./readModel\";\n\nconst MAX_READ_MODEL_PAGES = 100;\nconst MAX_STALE_RETRIES = 1;\n\ninterface OrganizationReadModelApi {\n getOrganizationReadModelResult(\n organizationId: string,\n cursor?: string,\n options?: RequestResultOptions,\n ): Promise<RequestResult<OrganizationReadModelResponse>>;\n}\n\ninterface OrganizationReadModelProjectionInput {\n readonly currentUserId: string;\n readonly execSql: ExecSql;\n readonly organizationId: string;\n}\n\nexport interface ReconcileOrganizationDirectoryAndGroupsInput\n extends OrganizationReadModelProjectionInput {\n readonly apiClient: OrganizationReadModelApi;\n readonly logError?:\n | ((message: string | Error, cause?: unknown) => void)\n | undefined;\n}\n\nfunction toDirectoryAndGroups(\n projection: OrganizationReadModelProjection | null,\n): OrganizationDirectoryAndGroups | null {\n if (!projection?.requester) {\n return null;\n }\n\n return {\n directory: {\n ...projection.directory,\n currentUser: projection.requester,\n },\n // Hydration decides whether a name is unreadable; a stored blank name may\n // only mean the group has not been hydrated yet.\n groups: projection.groups.groups.map((group) => ({\n ...group,\n nameUnreadable: false,\n })),\n memberGroupId: projection.groups.memberGroupId,\n readModelCursor: projection.cursor,\n };\n}\n\nasync function loadProjection(\n input: OrganizationReadModelProjectionInput,\n): Promise<OrganizationReadModelProjection | null> {\n return runOrganizationPresentationRead(\n organizationPresentationAccessInput(input),\n \"readModel\",\n () => loadRawProjection(input),\n );\n}\n\nfunction loadRawProjection(\n input: OrganizationReadModelProjectionInput,\n): Promise<OrganizationReadModelProjection | null> {\n return loadOrganizationReadModelProjection(\n input.execSql,\n input.organizationId,\n input.currentUserId,\n );\n}\n\nfunction organizationPresentationAccessInput(\n input: OrganizationReadModelProjectionInput,\n): OrganizationPresentationAccessInput {\n return {\n execSql: input.execSql,\n organizationId: input.organizationId,\n requesterUserId: input.currentUserId,\n };\n}\n\ninterface ReconciliationState {\n readonly accessAttempt: OrganizationPresentationAccessAttempt;\n expectedLocalCursor: string | null;\n preparedDeniedProjection: boolean;\n projection: OrganizationReadModelProjection | null;\n requestCursor: string | undefined;\n retriedBindingFailure: boolean;\n retriedInvalidCursor: boolean;\n staleRetries: number;\n}\n\ntype ReconciliationStep =\n | { readonly kind: \"continue\" }\n | {\n readonly kind: \"done\";\n readonly value: OrganizationDirectoryAndGroups | null | undefined;\n };\n\ntype PageRequestStep =\n | ReconciliationStep\n | {\n readonly kind: \"response\";\n readonly response: OrganizationReadModelResponse;\n };\n\nfunction localResult(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n outcome: { readonly failed: boolean },\n): ReconciliationStep {\n const accessInput = organizationPresentationAccessInput(input);\n const readable =\n isOrganizationPresentationAccessAttemptCurrent(\n accessInput,\n state.accessAttempt,\n ) && isOrganizationPresentationAccessReadable(accessInput, \"readModel\");\n if (!readable) {\n return { kind: \"done\", value: null };\n }\n const value = toDirectoryAndGroups(state.projection);\n // A failed pass that retained nothing must not read as a completed (null)\n // pass: consumers mark a scope caught up only on completed passes, and this\n // scope still needs a fresh catch-up.\n if (value === null && outcome.failed) {\n return { kind: \"done\", value: undefined };\n }\n return { kind: \"done\", value };\n}\n\nfunction resetRequestCursor(state: ReconciliationState): void {\n state.expectedLocalCursor = state.projection?.cursor ?? null;\n state.requestCursor = state.projection?.cursor ?? undefined;\n}\n\nfunction isInvalidReadModelCursorFailure(\n result: RequestResult<OrganizationReadModelResponse>,\n): boolean {\n return (\n !result.ok &&\n result.kind === \"http\" &&\n result.status === 400 &&\n result.code === ORGANIZATION_READ_MODEL_ERROR_CODES.cursorInvalid\n );\n}\n\nasync function requestReadModelPage(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n): Promise<PageRequestStep> {\n const result = await input.apiClient.getOrganizationReadModelResult(\n input.organizationId,\n state.requestCursor,\n { reportErrors: false },\n );\n if (result.ok) {\n return { kind: \"response\", response: result.data };\n }\n if (isOrganizationPresentationAccessDeniedFailure(result)) {\n const accessInput = organizationPresentationAccessInput(input);\n denyOrganizationPresentationAccess(accessInput, [\"readModel\", \"usage\"]);\n // The durable marker lands before the purge attempt: if the purge fails,\n // an offline restart must still refuse to serve the revoked projection.\n try {\n await runOrganizationPresentationMutation(input.execSql, () =>\n recordOrganizationPresentationDenials(accessInput, [\n \"readModel\",\n \"usage\",\n ]),\n );\n } catch (error) {\n input.logError?.(\"Failed to record denied organization access\", error);\n }\n try {\n await runOrganizationPresentationMutation(input.execSql, () =>\n purgeOrganizationAccessProjection({\n execSql: input.execSql,\n organizationId: input.organizationId,\n requesterUserId: input.currentUserId,\n }),\n );\n } catch (error) {\n input.logError?.(\"Failed to purge denied organization access\", error);\n }\n return { kind: \"done\", value: null };\n }\n if (\n isInvalidReadModelCursorFailure(result) &&\n state.requestCursor !== undefined &&\n !state.retriedInvalidCursor\n ) {\n state.requestCursor = undefined;\n state.retriedInvalidCursor = true;\n return { kind: \"continue\" };\n }\n\n result.report();\n return localResult(input, state, { failed: true });\n}\n\nfunction responseMatchesRequest(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n response: OrganizationReadModelResponse,\n): boolean {\n if (response.organizationId !== input.organizationId) {\n input.logError?.(\n `Organization read-model response scope does not match ${input.organizationId}`,\n );\n return false;\n }\n if (response.hasMore && response.nextCursor === state.expectedLocalCursor) {\n input.logError?.(\"Organization read-model page did not advance its cursor\");\n return false;\n }\n return true;\n}\n\nfunction reconciliationIsCurrent(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n): boolean {\n return isOrganizationPresentationAccessAttemptCurrent(\n organizationPresentationAccessInput(input),\n state.accessAttempt,\n );\n}\n\ntype ApplyResponseStep =\n | ReconciliationStep\n | {\n readonly kind: \"applied\";\n readonly result: Awaited<\n ReturnType<typeof applyOrganizationReadModelResponse>\n >;\n };\n\nasync function applyResponse(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n response: OrganizationReadModelResponse,\n): Promise<ApplyResponseStep> {\n try {\n return {\n kind: \"applied\",\n result: await applyOrganizationReadModelResponse({\n currentUserId: input.currentUserId,\n execSql: input.execSql,\n requestedCursor: state.expectedLocalCursor,\n response,\n }),\n };\n } catch (error) {\n input.logError?.(\"Failed to apply organization read-model response\", error);\n if (\n !(error instanceof OrganizationReadModelBindingError) ||\n state.retriedBindingFailure\n ) {\n return localResult(input, state, { failed: true });\n }\n state.retriedBindingFailure = true;\n await purgeOrganizationReadModelProjection(\n input.execSql,\n input.organizationId,\n );\n if (!reconciliationIsCurrent(input, state)) {\n return { kind: \"done\", value: null };\n }\n state.projection = null;\n resetRequestCursor(state);\n return { kind: \"continue\" };\n }\n}\n\nasync function prepareDeniedProjectionForApply(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n): Promise<ReconciliationStep | null> {\n if (state.preparedDeniedProjection) {\n return null;\n }\n try {\n // The apply that follows replaces the shared rows authoritatively, so\n // only this requester's row is dropped: another local identity sharing\n // the database keeps its access and reads the replacement.\n await purgeOrganizationReadModelProjection(\n input.execSql,\n input.organizationId,\n { onlyRequesterUserId: input.currentUserId },\n );\n } catch (error) {\n input.logError?.(\"Failed to replace denied organization read model\", error);\n return { kind: \"done\", value: null };\n }\n if (!reconciliationIsCurrent(input, state)) {\n return { kind: \"done\", value: null };\n }\n state.expectedLocalCursor = null;\n state.preparedDeniedProjection = true;\n state.projection = null;\n return null;\n}\n\nasync function applyReadModelPage(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n state: ReconciliationState,\n response: OrganizationReadModelResponse,\n): Promise<ReconciliationStep> {\n if (!responseMatchesRequest(input, state, response)) {\n return localResult(input, state, { failed: true });\n }\n\n const accessInput = organizationPresentationAccessInput(input);\n return runOrganizationPresentationMutation(input.execSql, async () => {\n if (!reconciliationIsCurrent(input, state)) {\n return { kind: \"done\", value: null };\n }\n const preparation = await prepareDeniedProjectionForApply(input, state);\n if (preparation) {\n return preparation;\n }\n\n const applied = await applyResponse(input, state, response);\n if (applied.kind !== \"applied\") {\n return applied;\n }\n\n state.projection = await loadRawProjection(input);\n if (!reconciliationIsCurrent(input, state)) {\n return { kind: \"done\", value: null };\n }\n if (applied.result === \"stale\") {\n if (state.staleRetries >= MAX_STALE_RETRIES) {\n return localResult(input, state, { failed: true });\n }\n state.staleRetries += 1;\n resetRequestCursor(state);\n return { kind: \"continue\" };\n }\n\n state.expectedLocalCursor = state.projection?.cursor ?? response.nextCursor;\n if (!response.hasMore) {\n return restoreOrganizationPresentationAccess(\n accessInput,\n state.accessAttempt,\n )\n ? localResult(input, state, { failed: false })\n : { kind: \"done\", value: null };\n }\n state.requestCursor = response.nextCursor;\n return { kind: \"continue\" };\n });\n}\n\nexport async function loadLocalOrganizationDirectoryAndGroups(\n input: OrganizationReadModelProjectionInput,\n): Promise<OrganizationDirectoryAndGroups | null> {\n return toDirectoryAndGroups(await loadProjection(input));\n}\n\nexport async function loadLocalOrganizationGroupMembers(\n input: OrganizationReadModelProjectionInput & { readonly groupId: string },\n): Promise<OrganizationReadModelGroupMembers | null> {\n return runOrganizationPresentationRead(\n organizationPresentationAccessInput(input),\n \"readModel\",\n () =>\n loadOrganizationReadModelGroupMembers(\n input.execSql,\n input.organizationId,\n input.groupId,\n input.currentUserId,\n ),\n );\n}\n\n/**\n * Resolves the reconciled projection, `null` when the pass completed\n * authoritatively with nothing presentable (including a denial that purged\n * the projection), or `undefined` when the feed failed and no local\n * projection was retained — a pass that must not count as caught up.\n */\nexport async function reconcileOrganizationDirectoryAndGroups(\n input: ReconcileOrganizationDirectoryAndGroupsInput,\n): Promise<OrganizationDirectoryAndGroups | null | undefined> {\n const accessInput = organizationPresentationAccessInput(input);\n const accessAttempt = captureOrganizationPresentationAccessAttempt(\n accessInput,\n \"readModel\",\n );\n const state: ReconciliationState = {\n accessAttempt,\n expectedLocalCursor: null,\n preparedDeniedProjection: !accessAttempt.startedDenied,\n projection: await loadProjection(input),\n requestCursor: undefined,\n retriedBindingFailure: false,\n retriedInvalidCursor: false,\n staleRetries: 0,\n };\n resetRequestCursor(state);\n\n for (let page = 0; page < MAX_READ_MODEL_PAGES; page += 1) {\n const request = await requestReadModelPage(input, state);\n if (request.kind === \"done\") {\n return request.value;\n }\n if (request.kind === \"continue\") {\n continue;\n }\n const applied = await applyReadModelPage(input, state, request.response);\n if (applied.kind === \"done\") {\n return applied.value;\n }\n }\n\n input.logError?.(\n \"Organization read-model reconciliation exceeded page limit\",\n );\n const retained = localResult(input, state, { failed: true });\n return retained.kind === \"done\" ? retained.value : null;\n}\n"]}
|
|
@@ -4,7 +4,7 @@ import { deleteDocumentPlacementRowsByContainerIds, deleteDocumentPlacementRowsB
|
|
|
4
4
|
import { clearDocumentDiscoveryEvidenceOnRemoteReset } from "../../data/persistence/documents/documentDiscoveryReset.js";
|
|
5
5
|
import { documentIntentLinkTargets } from "../../data/sqlite/documentPlacementIntentSchema.js";
|
|
6
6
|
import { organizationDataUsageCategories, organizationDataUsageSnapshots, } from "../../data/sqlite/organizationDataUsageSchema.js";
|
|
7
|
-
import { organizationPresentationDenials, organizationReadModelContainerGrants, organizationReadModelDirectoryUsers, organizationReadModelGroupMembers,
|
|
7
|
+
import { organizationPresentationDenials, organizationReadModelContainerGrants, organizationReadModelDirectoryUsers, organizationReadModelGroupMembers, organizationReadModelGroups, organizationReadModelRequesters, organizationReadModelState, } from "../../data/sqlite/organizationReadModelSchema.js";
|
|
8
8
|
import { clientSqlTables, containerCreateIntents, containerHydrationTombstones, containerMoveIntents, containers, documentAttachmentBlobProjection, documentMoveIntents, documentPendingAttachments, documentPendingUpdates, documentProjection, documentSyncFailures, documents, dormantContainerMetadata, dormantMetadataSweepRequests, } from "../../data/sqlite/schema.js";
|
|
9
9
|
import { getClientSQLitePersistenceRuntime, } from "../../data/sqlite/sqlitePersistenceRuntime.js";
|
|
10
10
|
import { ensureSqlTables } from "../../data/sqlite/sqlTableSchema.js";
|
|
@@ -22,10 +22,8 @@ async function clearOrganizationPresentationRows(tx, organizationId) {
|
|
|
22
22
|
organizationDataUsageSnapshots,
|
|
23
23
|
organizationReadModelContainerGrants,
|
|
24
24
|
organizationReadModelGroupMembers,
|
|
25
|
-
organizationReadModelGroupMemberships,
|
|
26
25
|
organizationReadModelDirectoryUsers,
|
|
27
26
|
organizationReadModelGroups,
|
|
28
|
-
organizationReadModelPolicyHeads,
|
|
29
27
|
organizationReadModelRequesters,
|
|
30
28
|
organizationReadModelState,
|
|
31
29
|
organizationPresentationDenials,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remoteReset.js","sourceRoot":"","sources":["remoteReset.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,8CAA8C,EAAE,MAAM,uEAAuE,CAAC;AACvI,OAAO,EACL,yCAAyC,EACzC,wCAAwC,GACzC,MAAM,0EAA0E,CAAC;AAClF,OAAO,EAAE,2CAA2C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ACF,MAAM,KAAK,GAAG,MAAM,eAAe,CAAC,EAAE,EAAE,QAAQ,CAAC,YAAY,CAAC,CAAC;YAC/D,OAAO,iCAAiC,CAAC;gBACvC,KAAK;gBACL,KAAK;gBACL,QAAQ;gBACR,EAAE;aACH,CAAC,CAAC;QACL,CAAC,CAAC;QACF,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,OAAO,OAAO,CAAC,WAAW,CAAC,kBAAkB,CAAC,CAAC;QACjD,CAAC;QACD,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,kBAAkB,CAC9C,kBAAkB,EAClB,SAAS,CACV,CAAC;QACF,IAAI,CAAC,OAAO,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YAC1C,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;QACzE,CAAC;QACD,OAAO,OAAO,CAAC,MAAM,CAAC;IACxB,CAAC,CACF,CAAC;AACJ,CAAC","sourcesContent":["import { and, eq, inArray, or } from \"drizzle-orm\";\nimport { recordSupersededContainerBindingsInTransaction } from \"../../data/persistence/container-contents/supersededContainerBindings\";\nimport {\n deleteDocumentPlacementRowsByContainerIds,\n deleteDocumentPlacementRowsByDocumentIds,\n} from \"../../data/persistence/containers/documentContainerProjectionPersistence\";\nimport { clearDocumentDiscoveryEvidenceOnRemoteReset } from \"../../data/persistence/documents/documentDiscoveryReset\";\nimport { documentIntentLinkTargets } from \"../../data/sqlite/documentPlacementIntentSchema\";\nimport {\n organizationDataUsageCategories,\n organizationDataUsageSnapshots,\n} from \"../../data/sqlite/organizationDataUsageSchema\";\nimport {\n organizationPresentationDenials,\n organizationReadModelContainerGrants,\n organizationReadModelDirectoryUsers,\n organizationReadModelGroupMembers,\n organizationReadModelGroupMemberships,\n organizationReadModelGroups,\n organizationReadModelPolicyHeads,\n organizationReadModelRequesters,\n organizationReadModelState,\n} from \"../../data/sqlite/organizationReadModelSchema\";\nimport {\n clientSqlTables,\n containerCreateIntents,\n containerHydrationTombstones,\n containerMoveIntents,\n containers,\n documentAttachmentBlobProjection,\n documentMoveIntents,\n documentPendingAttachments,\n documentPendingUpdates,\n documentProjection,\n documentSyncFailures,\n documents,\n dormantContainerMetadata,\n dormantMetadataSweepRequests,\n} from \"../../data/sqlite/schema\";\nimport {\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../../data/sqlite/sqlitePersistenceRuntime\";\nimport type { ExecSql } from \"../../data/sqlite/sqlSchema\";\nimport { ensureSqlTables } from \"../../data/sqlite/sqlTableSchema\";\nimport { runOrganizationPresentationReset } from \"../organizations/organizationPresentationAccessState\";\nimport { clearResetPendingAttachments } from \"./remoteResetAttachments\";\nimport { remoteResetBatches } from \"./remoteResetBatches\";\nimport { clearRemoteResetSyncCursors } from \"./remoteResetCursorScope\";\nimport {\n buildResetPlans,\n type ResetAttachmentUpload,\n type ResetDocumentUpdate,\n} from \"./remoteResetPlans\";\nimport { clearRemoteResetPrincipalRows } from \"./remoteResetPrincipalScope\";\nimport {\n type RemoteResetInput,\n type RemoteSyncStateSnapshot,\n readRemoteSyncStateSnapshot,\n} from \"./remoteResetScope\";\n\nexport type {\n RemoteResetInput,\n RemoteResetReplacement,\n} from \"./remoteResetScope\";\n\nconst CONTAINER_CREATE_INTENT_TYPE = \"container.create\";\n\nexport interface ClearRemoteSyncStateResult {\n readonly clearedContainerCreateIntentCount: number;\n readonly clearedContainerMoveIntentCount: number;\n readonly clearedDocumentMoveIntentCount: number;\n readonly clearedPrincipalPolicyCount: number;\n readonly clearedSyncCursorCount: number;\n readonly queuedAttachmentUploadCount: number;\n readonly queuedContainerCreateCount: number;\n readonly queuedDocumentUpdateCount: number;\n readonly resetContainerCount: number;\n readonly resetDocumentCount: number;\n}\n\nasync function clearOrganizationPresentationRows(\n tx: ClientSQLiteTransactionScope,\n organizationId: string,\n): Promise<void> {\n for (const table of [\n organizationDataUsageCategories,\n organizationDataUsageSnapshots,\n organizationReadModelContainerGrants,\n organizationReadModelGroupMembers,\n organizationReadModelGroupMemberships,\n organizationReadModelDirectoryUsers,\n organizationReadModelGroups,\n organizationReadModelPolicyHeads,\n organizationReadModelRequesters,\n organizationReadModelState,\n organizationPresentationDenials,\n ] as const) {\n await tx\n .delete(table)\n .where(eq(table.organizationId, organizationId))\n .run();\n }\n}\n\ninterface ScopedRemoteRowsInput {\n attachmentUploads: readonly ResetAttachmentUpload[];\n organizationId: string;\n snapshot: RemoteSyncStateSnapshot;\n tx: ClientSQLiteTransactionScope;\n}\n\nasync function clearScopedContainerRows(\n input: ScopedRemoteRowsInput,\n): Promise<void> {\n for (const containerBatch of remoteResetBatches(\n input.snapshot.containerIds,\n )) {\n await deleteDocumentPlacementRowsByContainerIds(input.tx, containerBatch);\n await input.tx\n .delete(containerMoveIntents)\n .where(inArray(containerMoveIntents.containerId, containerBatch))\n .run();\n await input.tx\n .delete(containerCreateIntents)\n .where(inArray(containerCreateIntents.containerId, containerBatch))\n .run();\n await input.tx\n .delete(containerHydrationTombstones)\n .where(inArray(containerHydrationTombstones.containerId, containerBatch))\n .run();\n }\n}\n\nasync function clearScopedDocumentRows(\n input: ScopedRemoteRowsInput,\n): Promise<void> {\n const { snapshot, tx } = input;\n for (const documentIdBatch of remoteResetBatches(snapshot.oldDocumentIds)) {\n await deleteDocumentPlacementRowsByDocumentIds(tx, documentIdBatch);\n }\n await clearResetPendingAttachments({\n attachmentUploads: input.attachmentUploads,\n documentLocalIds: snapshot.documentLocalIds,\n tx,\n });\n for (const localIdBatch of remoteResetBatches(snapshot.documentLocalIds)) {\n await tx\n .delete(documentIntentLinkTargets)\n .where(\n inArray(\n documentIntentLinkTargets.intentId,\n tx\n .select({ id: documentMoveIntents.id })\n .from(documentMoveIntents)\n .where(inArray(documentMoveIntents.localId, localIdBatch)),\n ),\n )\n .run();\n await tx\n .delete(documentMoveIntents)\n .where(inArray(documentMoveIntents.localId, localIdBatch))\n .run();\n }\n for (const documentBatch of remoteResetBatches(snapshot.documentRows)) {\n await tx\n .delete(documentPendingUpdates)\n .where(\n or(\n ...documentBatch.map((row) =>\n and(\n eq(documentPendingUpdates.appKind, row.appKind),\n eq(documentPendingUpdates.localId, row.localId),\n ),\n ),\n ),\n )\n .run();\n await tx\n .delete(documentSyncFailures)\n .where(\n or(\n ...documentBatch.map((row) =>\n and(\n eq(documentSyncFailures.appKind, row.appKind),\n eq(documentSyncFailures.localId, row.localId),\n ),\n ),\n ),\n )\n .run();\n }\n}\n\nasync function clearScopedRemoteRows(\n input: ScopedRemoteRowsInput,\n): Promise<void> {\n const { organizationId, tx } = input;\n await clearOrganizationPresentationRows(tx, organizationId);\n for (const table of [\n dormantContainerMetadata,\n dormantMetadataSweepRequests,\n ] as const) {\n await tx\n .delete(table)\n .where(eq(table.organizationId, organizationId))\n .run();\n }\n await clearDocumentDiscoveryEvidenceOnRemoteReset(tx, {\n containerIds: input.snapshot.containerIds,\n documentIds: input.snapshot.oldDocumentIds,\n });\n await clearRemoteResetPrincipalRows(input);\n await clearScopedContainerRows(input);\n await clearScopedDocumentRows(input);\n await clearRemoteResetSyncCursors({\n containerIds: input.snapshot.containerIds,\n tx,\n });\n}\n\nasync function resetRemoteColumns(input: {\n now: string;\n reset: RemoteResetInput;\n snapshot: RemoteSyncStateSnapshot;\n tx: ClientSQLiteTransactionScope;\n}): Promise<void> {\n const { now, reset, snapshot, tx } = input;\n for (const row of snapshot.documentRows) {\n await tx\n .update(documents)\n .set({\n accessEpoch: 1,\n accessStateHash: null,\n contentKeyBundle: null,\n documentId: null,\n documentKekTargets: null,\n documentManifestBundle: null,\n lastCommitLsn: null,\n pullContinuation: null,\n recoveryDocumentId: row.documentId ?? row.recoveryDocumentId,\n updatedAt: now,\n })\n .where(\n and(\n eq(documents.appKind, row.appKind),\n eq(documents.localId, row.localId),\n ),\n )\n .run();\n }\n for (const localIdBatch of remoteResetBatches(snapshot.documentLocalIds)) {\n await tx\n .update(documentProjection)\n .set({\n documentId: null,\n organizationId:\n reset.replacement?.organizationId ?? reset.organizationId,\n })\n .where(inArray(documentProjection.localId, localIdBatch))\n .run();\n }\n // Replacement re-homes folders under their ids; the purged organization\n // can never bind them again on this device.\n for (const containerIds of reset.replacement\n ? remoteResetBatches(snapshot.containerIds)\n : []) {\n await recordSupersededContainerBindingsInTransaction(tx, {\n containerIds,\n organizationId: reset.organizationId,\n supersededAt: now,\n });\n }\n for (const row of snapshot.containerRows) {\n await tx\n .update(containers)\n .set({\n metadataDocumentId: null,\n organizationId:\n reset.replacement?.organizationId ?? reset.organizationId,\n parentId:\n reset.replacement && row.parentId === null\n ? reset.replacement.rootContainerId\n : row.parentId,\n serverCreatedAt: null,\n serverUpdatedAt: null,\n ...(reset.replacement ? { systemSlot: null } : {}),\n localUpdatedAt: now,\n })\n .where(eq(containers.id, row.id))\n .run();\n }\n}\n\nasync function queueResetDocumentUpdates(input: {\n documentUpdates: readonly ResetDocumentUpdate[];\n now: string;\n tx: ClientSQLiteTransactionScope;\n}): Promise<void> {\n for (const updateBatch of remoteResetBatches(input.documentUpdates)) {\n await input.tx\n .insert(documentPendingUpdates)\n .values(\n updateBatch.map((update) => ({\n ...update,\n id: crypto.randomUUID(),\n createdAt: input.now,\n })),\n )\n .run();\n }\n}\n\nasync function queueResetContainerCreates(input: {\n now: string;\n reset: RemoteResetInput;\n snapshot: RemoteSyncStateSnapshot;\n tx: ClientSQLiteTransactionScope;\n}): Promise<number> {\n const rows = input.snapshot.containerRows.flatMap((row) => {\n const parentId =\n row.parentId ?? input.reset.replacement?.rootContainerId ?? null;\n return parentId ? [{ id: row.id, parentId }] : [];\n });\n if (rows.length === 0) return 0;\n for (const rowBatch of remoteResetBatches(rows)) {\n await input.tx\n .insert(containerCreateIntents)\n .values(\n rowBatch.map((row) => ({\n id: crypto.randomUUID(),\n containerId: row.id,\n parentContainerId: row.parentId,\n intentType: CONTAINER_CREATE_INTENT_TYPE,\n syncStatus: \"pending\" as const,\n remoteContainerId: null,\n remoteMetadataAccessStateHash: null,\n remoteMetadataDocumentId: null,\n lastError: null,\n createdAt: input.now,\n updatedAt: input.now,\n })),\n )\n .run();\n }\n return rows.length;\n}\n\nasync function queueResetAttachmentUploads(input: {\n attachmentUploads: readonly ResetAttachmentUpload[];\n now: string;\n tx: ClientSQLiteTransactionScope;\n}): Promise<void> {\n for (const attachmentBatch of remoteResetBatches(input.attachmentUploads)) {\n await input.tx\n .insert(documentPendingAttachments)\n .values(\n attachmentBatch.map((attachment) => ({\n ...attachment,\n createdAt: input.now,\n })),\n )\n .run();\n await input.tx\n .delete(documentAttachmentBlobProjection)\n .where(\n or(\n ...attachmentBatch.map((attachment) =>\n and(\n eq(documentAttachmentBlobProjection.localId, attachment.localId),\n eq(documentAttachmentBlobProjection.slotId, attachment.slotId),\n ),\n ),\n ),\n )\n .run();\n }\n}\n\nasync function clearRemoteSyncStateInTransaction(input: {\n plans: Awaited<ReturnType<typeof buildResetPlans>>;\n reset: RemoteResetInput;\n snapshot: RemoteSyncStateSnapshot;\n tx: ClientSQLiteTransactionScope;\n}): Promise<ClearRemoteSyncStateResult> {\n const now = new Date().toISOString();\n const { snapshot } = input;\n await clearScopedRemoteRows({\n attachmentUploads: input.plans.attachmentUploads,\n organizationId: input.reset.organizationId,\n snapshot,\n tx: input.tx,\n });\n await resetRemoteColumns({ now, reset: input.reset, snapshot, tx: input.tx });\n await queueResetDocumentUpdates({\n documentUpdates: input.plans.documentUpdates,\n now,\n tx: input.tx,\n });\n const queuedContainerCreateCount = await queueResetContainerCreates({\n now,\n reset: input.reset,\n snapshot,\n tx: input.tx,\n });\n await queueResetAttachmentUploads({\n attachmentUploads: input.plans.attachmentUploads,\n now,\n tx: input.tx,\n });\n return {\n clearedContainerCreateIntentCount:\n snapshot.clearedContainerCreateIntentCount,\n clearedContainerMoveIntentCount: snapshot.clearedContainerMoveIntentCount,\n clearedDocumentMoveIntentCount: snapshot.clearedDocumentMoveIntentCount,\n clearedPrincipalPolicyCount: snapshot.clearedPrincipalPolicyCount,\n clearedSyncCursorCount: snapshot.clearedSyncCursorCount,\n queuedAttachmentUploadCount: input.plans.attachmentUploads.length,\n queuedContainerCreateCount,\n queuedDocumentUpdateCount: input.plans.documentUpdates.length,\n resetContainerCount: snapshot.containerRows.length,\n resetDocumentCount: snapshot.documentRows.length,\n };\n}\n\nexport async function clearRemoteSyncState(\n execSql: ExecSql,\n reset: RemoteResetInput,\n canCommit?: (() => boolean) | undefined,\n): Promise<ClearRemoteSyncStateResult> {\n return runOrganizationPresentationReset(\n execSql,\n reset.organizationId,\n async () => {\n await ensureSqlTables(execSql, clientSqlTables);\n const runtime = getClientSQLitePersistenceRuntime(execSql);\n const resetInTransaction = async (tx: ClientSQLiteTransactionScope) => {\n if (canCommit && !canCommit()) {\n throw new Error(\n \"Remote sync reset aborted: session identity changed\",\n );\n }\n const snapshot = await readRemoteSyncStateSnapshot(\n tx,\n reset.organizationId,\n );\n const plans = await buildResetPlans(tx, snapshot.documentRows);\n return clearRemoteSyncStateInTransaction({\n plans,\n reset,\n snapshot,\n tx,\n });\n };\n if (!canCommit) {\n return runtime.transaction(resetInTransaction);\n }\n const outcome = await runtime.guardedTransaction(\n resetInTransaction,\n canCommit,\n );\n if (!outcome.committed || !outcome.result) {\n throw new Error(\"Remote sync reset aborted: session identity changed\");\n }\n return outcome.result;\n },\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"remoteReset.js","sourceRoot":"","sources":["remoteReset.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,8CAA8C,EAAE,MAAM,uEAAuE,CAAC;AACvI,OAAO,EACL,yCAAyC,EACzC,wCAAwC,GACzC,MAAM,0EAA0E,CAAC;AAClF,OAAO,EAAE,2CAA2C,EAAE,MAAM,yDAAyD,CAAC;AACtH,OAAO,EAAE,yBAAyB,EAAE,MAAM,iDAAiD,CAAC;AAC5F,OAAO,EACL,+BAA+B,EAC/B,8BAA8B,GAC/B,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,CAAC;QACF,IAAI,CAAC,SAAS,EAAE,CAAC;YACf,OAAO,OAAO,CAAC,WAAW,CAAC,kBAAkB,CAAC,CAAC;QACjD,CAAC;QACD,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,kBAAkB,CAC9C,kBAAkB,EAClB,SAAS,CACV,CAAC;QACF,IAAI,CAAC,OAAO,CAAC,SAAS,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YAC1C,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;QACzE,CAAC;QACD,OAAO,OAAO,CAAC,MAAM,CAAC;IACxB,CAAC,CACF,CAAC;AACJ,CAAC","sourcesContent":["import { and, eq, inArray, or } from \"drizzle-orm\";\nimport { recordSupersededContainerBindingsInTransaction } from \"../../data/persistence/container-contents/supersededContainerBindings\";\nimport {\n deleteDocumentPlacementRowsByContainerIds,\n deleteDocumentPlacementRowsByDocumentIds,\n} from \"../../data/persistence/containers/documentContainerProjectionPersistence\";\nimport { clearDocumentDiscoveryEvidenceOnRemoteReset } from \"../../data/persistence/documents/documentDiscoveryReset\";\nimport { documentIntentLinkTargets } from \"../../data/sqlite/documentPlacementIntentSchema\";\nimport {\n organizationDataUsageCategories,\n organizationDataUsageSnapshots,\n} from \"../../data/sqlite/organizationDataUsageSchema\";\nimport {\n organizationPresentationDenials,\n organizationReadModelContainerGrants,\n organizationReadModelDirectoryUsers,\n organizationReadModelGroupMembers,\n organizationReadModelGroups,\n organizationReadModelRequesters,\n organizationReadModelState,\n} from \"../../data/sqlite/organizationReadModelSchema\";\nimport {\n clientSqlTables,\n containerCreateIntents,\n containerHydrationTombstones,\n containerMoveIntents,\n containers,\n documentAttachmentBlobProjection,\n documentMoveIntents,\n documentPendingAttachments,\n documentPendingUpdates,\n documentProjection,\n documentSyncFailures,\n documents,\n dormantContainerMetadata,\n dormantMetadataSweepRequests,\n} from \"../../data/sqlite/schema\";\nimport {\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../../data/sqlite/sqlitePersistenceRuntime\";\nimport type { ExecSql } from \"../../data/sqlite/sqlSchema\";\nimport { ensureSqlTables } from \"../../data/sqlite/sqlTableSchema\";\nimport { runOrganizationPresentationReset } from \"../organizations/organizationPresentationAccessState\";\nimport { clearResetPendingAttachments } from \"./remoteResetAttachments\";\nimport { remoteResetBatches } from \"./remoteResetBatches\";\nimport { clearRemoteResetSyncCursors } from \"./remoteResetCursorScope\";\nimport {\n buildResetPlans,\n type ResetAttachmentUpload,\n type ResetDocumentUpdate,\n} from \"./remoteResetPlans\";\nimport { clearRemoteResetPrincipalRows } from \"./remoteResetPrincipalScope\";\nimport {\n type RemoteResetInput,\n type RemoteSyncStateSnapshot,\n readRemoteSyncStateSnapshot,\n} from \"./remoteResetScope\";\n\nexport type {\n RemoteResetInput,\n RemoteResetReplacement,\n} from \"./remoteResetScope\";\n\nconst CONTAINER_CREATE_INTENT_TYPE = \"container.create\";\n\nexport interface ClearRemoteSyncStateResult {\n readonly clearedContainerCreateIntentCount: number;\n readonly clearedContainerMoveIntentCount: number;\n readonly clearedDocumentMoveIntentCount: number;\n readonly clearedPrincipalPolicyCount: number;\n readonly clearedSyncCursorCount: number;\n readonly queuedAttachmentUploadCount: number;\n readonly queuedContainerCreateCount: number;\n readonly queuedDocumentUpdateCount: number;\n readonly resetContainerCount: number;\n readonly resetDocumentCount: number;\n}\n\nasync function clearOrganizationPresentationRows(\n tx: ClientSQLiteTransactionScope,\n organizationId: string,\n): Promise<void> {\n for (const table of [\n organizationDataUsageCategories,\n organizationDataUsageSnapshots,\n organizationReadModelContainerGrants,\n organizationReadModelGroupMembers,\n organizationReadModelDirectoryUsers,\n organizationReadModelGroups,\n organizationReadModelRequesters,\n organizationReadModelState,\n organizationPresentationDenials,\n ] as const) {\n await tx\n .delete(table)\n .where(eq(table.organizationId, organizationId))\n .run();\n }\n}\n\ninterface ScopedRemoteRowsInput {\n attachmentUploads: readonly ResetAttachmentUpload[];\n organizationId: string;\n snapshot: RemoteSyncStateSnapshot;\n tx: ClientSQLiteTransactionScope;\n}\n\nasync function clearScopedContainerRows(\n input: ScopedRemoteRowsInput,\n): Promise<void> {\n for (const containerBatch of remoteResetBatches(\n input.snapshot.containerIds,\n )) {\n await deleteDocumentPlacementRowsByContainerIds(input.tx, containerBatch);\n await input.tx\n .delete(containerMoveIntents)\n .where(inArray(containerMoveIntents.containerId, containerBatch))\n .run();\n await input.tx\n .delete(containerCreateIntents)\n .where(inArray(containerCreateIntents.containerId, containerBatch))\n .run();\n await input.tx\n .delete(containerHydrationTombstones)\n .where(inArray(containerHydrationTombstones.containerId, containerBatch))\n .run();\n }\n}\n\nasync function clearScopedDocumentRows(\n input: ScopedRemoteRowsInput,\n): Promise<void> {\n const { snapshot, tx } = input;\n for (const documentIdBatch of remoteResetBatches(snapshot.oldDocumentIds)) {\n await deleteDocumentPlacementRowsByDocumentIds(tx, documentIdBatch);\n }\n await clearResetPendingAttachments({\n attachmentUploads: input.attachmentUploads,\n documentLocalIds: snapshot.documentLocalIds,\n tx,\n });\n for (const localIdBatch of remoteResetBatches(snapshot.documentLocalIds)) {\n await tx\n .delete(documentIntentLinkTargets)\n .where(\n inArray(\n documentIntentLinkTargets.intentId,\n tx\n .select({ id: documentMoveIntents.id })\n .from(documentMoveIntents)\n .where(inArray(documentMoveIntents.localId, localIdBatch)),\n ),\n )\n .run();\n await tx\n .delete(documentMoveIntents)\n .where(inArray(documentMoveIntents.localId, localIdBatch))\n .run();\n }\n for (const documentBatch of remoteResetBatches(snapshot.documentRows)) {\n await tx\n .delete(documentPendingUpdates)\n .where(\n or(\n ...documentBatch.map((row) =>\n and(\n eq(documentPendingUpdates.appKind, row.appKind),\n eq(documentPendingUpdates.localId, row.localId),\n ),\n ),\n ),\n )\n .run();\n await tx\n .delete(documentSyncFailures)\n .where(\n or(\n ...documentBatch.map((row) =>\n and(\n eq(documentSyncFailures.appKind, row.appKind),\n eq(documentSyncFailures.localId, row.localId),\n ),\n ),\n ),\n )\n .run();\n }\n}\n\nasync function clearScopedRemoteRows(\n input: ScopedRemoteRowsInput,\n): Promise<void> {\n const { organizationId, tx } = input;\n await clearOrganizationPresentationRows(tx, organizationId);\n for (const table of [\n dormantContainerMetadata,\n dormantMetadataSweepRequests,\n ] as const) {\n await tx\n .delete(table)\n .where(eq(table.organizationId, organizationId))\n .run();\n }\n await clearDocumentDiscoveryEvidenceOnRemoteReset(tx, {\n containerIds: input.snapshot.containerIds,\n documentIds: input.snapshot.oldDocumentIds,\n });\n await clearRemoteResetPrincipalRows(input);\n await clearScopedContainerRows(input);\n await clearScopedDocumentRows(input);\n await clearRemoteResetSyncCursors({\n containerIds: input.snapshot.containerIds,\n tx,\n });\n}\n\nasync function resetRemoteColumns(input: {\n now: string;\n reset: RemoteResetInput;\n snapshot: RemoteSyncStateSnapshot;\n tx: ClientSQLiteTransactionScope;\n}): Promise<void> {\n const { now, reset, snapshot, tx } = input;\n for (const row of snapshot.documentRows) {\n await tx\n .update(documents)\n .set({\n accessEpoch: 1,\n accessStateHash: null,\n contentKeyBundle: null,\n documentId: null,\n documentKekTargets: null,\n documentManifestBundle: null,\n lastCommitLsn: null,\n pullContinuation: null,\n recoveryDocumentId: row.documentId ?? row.recoveryDocumentId,\n updatedAt: now,\n })\n .where(\n and(\n eq(documents.appKind, row.appKind),\n eq(documents.localId, row.localId),\n ),\n )\n .run();\n }\n for (const localIdBatch of remoteResetBatches(snapshot.documentLocalIds)) {\n await tx\n .update(documentProjection)\n .set({\n documentId: null,\n organizationId:\n reset.replacement?.organizationId ?? reset.organizationId,\n })\n .where(inArray(documentProjection.localId, localIdBatch))\n .run();\n }\n // Replacement re-homes folders under their ids; the purged organization\n // can never bind them again on this device.\n for (const containerIds of reset.replacement\n ? remoteResetBatches(snapshot.containerIds)\n : []) {\n await recordSupersededContainerBindingsInTransaction(tx, {\n containerIds,\n organizationId: reset.organizationId,\n supersededAt: now,\n });\n }\n for (const row of snapshot.containerRows) {\n await tx\n .update(containers)\n .set({\n metadataDocumentId: null,\n organizationId:\n reset.replacement?.organizationId ?? reset.organizationId,\n parentId:\n reset.replacement && row.parentId === null\n ? reset.replacement.rootContainerId\n : row.parentId,\n serverCreatedAt: null,\n serverUpdatedAt: null,\n ...(reset.replacement ? { systemSlot: null } : {}),\n localUpdatedAt: now,\n })\n .where(eq(containers.id, row.id))\n .run();\n }\n}\n\nasync function queueResetDocumentUpdates(input: {\n documentUpdates: readonly ResetDocumentUpdate[];\n now: string;\n tx: ClientSQLiteTransactionScope;\n}): Promise<void> {\n for (const updateBatch of remoteResetBatches(input.documentUpdates)) {\n await input.tx\n .insert(documentPendingUpdates)\n .values(\n updateBatch.map((update) => ({\n ...update,\n id: crypto.randomUUID(),\n createdAt: input.now,\n })),\n )\n .run();\n }\n}\n\nasync function queueResetContainerCreates(input: {\n now: string;\n reset: RemoteResetInput;\n snapshot: RemoteSyncStateSnapshot;\n tx: ClientSQLiteTransactionScope;\n}): Promise<number> {\n const rows = input.snapshot.containerRows.flatMap((row) => {\n const parentId =\n row.parentId ?? input.reset.replacement?.rootContainerId ?? null;\n return parentId ? [{ id: row.id, parentId }] : [];\n });\n if (rows.length === 0) return 0;\n for (const rowBatch of remoteResetBatches(rows)) {\n await input.tx\n .insert(containerCreateIntents)\n .values(\n rowBatch.map((row) => ({\n id: crypto.randomUUID(),\n containerId: row.id,\n parentContainerId: row.parentId,\n intentType: CONTAINER_CREATE_INTENT_TYPE,\n syncStatus: \"pending\" as const,\n remoteContainerId: null,\n remoteMetadataAccessStateHash: null,\n remoteMetadataDocumentId: null,\n lastError: null,\n createdAt: input.now,\n updatedAt: input.now,\n })),\n )\n .run();\n }\n return rows.length;\n}\n\nasync function queueResetAttachmentUploads(input: {\n attachmentUploads: readonly ResetAttachmentUpload[];\n now: string;\n tx: ClientSQLiteTransactionScope;\n}): Promise<void> {\n for (const attachmentBatch of remoteResetBatches(input.attachmentUploads)) {\n await input.tx\n .insert(documentPendingAttachments)\n .values(\n attachmentBatch.map((attachment) => ({\n ...attachment,\n createdAt: input.now,\n })),\n )\n .run();\n await input.tx\n .delete(documentAttachmentBlobProjection)\n .where(\n or(\n ...attachmentBatch.map((attachment) =>\n and(\n eq(documentAttachmentBlobProjection.localId, attachment.localId),\n eq(documentAttachmentBlobProjection.slotId, attachment.slotId),\n ),\n ),\n ),\n )\n .run();\n }\n}\n\nasync function clearRemoteSyncStateInTransaction(input: {\n plans: Awaited<ReturnType<typeof buildResetPlans>>;\n reset: RemoteResetInput;\n snapshot: RemoteSyncStateSnapshot;\n tx: ClientSQLiteTransactionScope;\n}): Promise<ClearRemoteSyncStateResult> {\n const now = new Date().toISOString();\n const { snapshot } = input;\n await clearScopedRemoteRows({\n attachmentUploads: input.plans.attachmentUploads,\n organizationId: input.reset.organizationId,\n snapshot,\n tx: input.tx,\n });\n await resetRemoteColumns({ now, reset: input.reset, snapshot, tx: input.tx });\n await queueResetDocumentUpdates({\n documentUpdates: input.plans.documentUpdates,\n now,\n tx: input.tx,\n });\n const queuedContainerCreateCount = await queueResetContainerCreates({\n now,\n reset: input.reset,\n snapshot,\n tx: input.tx,\n });\n await queueResetAttachmentUploads({\n attachmentUploads: input.plans.attachmentUploads,\n now,\n tx: input.tx,\n });\n return {\n clearedContainerCreateIntentCount:\n snapshot.clearedContainerCreateIntentCount,\n clearedContainerMoveIntentCount: snapshot.clearedContainerMoveIntentCount,\n clearedDocumentMoveIntentCount: snapshot.clearedDocumentMoveIntentCount,\n clearedPrincipalPolicyCount: snapshot.clearedPrincipalPolicyCount,\n clearedSyncCursorCount: snapshot.clearedSyncCursorCount,\n queuedAttachmentUploadCount: input.plans.attachmentUploads.length,\n queuedContainerCreateCount,\n queuedDocumentUpdateCount: input.plans.documentUpdates.length,\n resetContainerCount: snapshot.containerRows.length,\n resetDocumentCount: snapshot.documentRows.length,\n };\n}\n\nexport async function clearRemoteSyncState(\n execSql: ExecSql,\n reset: RemoteResetInput,\n canCommit?: (() => boolean) | undefined,\n): Promise<ClearRemoteSyncStateResult> {\n return runOrganizationPresentationReset(\n execSql,\n reset.organizationId,\n async () => {\n await ensureSqlTables(execSql, clientSqlTables);\n const runtime = getClientSQLitePersistenceRuntime(execSql);\n const resetInTransaction = async (tx: ClientSQLiteTransactionScope) => {\n if (canCommit && !canCommit()) {\n throw new Error(\n \"Remote sync reset aborted: session identity changed\",\n );\n }\n const snapshot = await readRemoteSyncStateSnapshot(\n tx,\n reset.organizationId,\n );\n const plans = await buildResetPlans(tx, snapshot.documentRows);\n return clearRemoteSyncStateInTransaction({\n plans,\n reset,\n snapshot,\n tx,\n });\n };\n if (!canCommit) {\n return runtime.transaction(resetInTransaction);\n }\n const outcome = await runtime.guardedTransaction(\n resetInTransaction,\n canCommit,\n );\n if (!outcome.committed || !outcome.result) {\n throw new Error(\"Remote sync reset aborted: session identity changed\");\n }\n return outcome.result;\n },\n );\n}\n"]}
|