@powerhousedao/reactor-api 6.0.0-dev.3 → 6.0.0-dev.31

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.
Files changed (104) hide show
  1. package/dist/src/graphql/auth/subgraph.js +1 -1
  2. package/dist/src/graphql/auth/subgraph.js.map +1 -1
  3. package/dist/src/graphql/document-model-subgraph.d.ts +89 -43
  4. package/dist/src/graphql/document-model-subgraph.d.ts.map +1 -1
  5. package/dist/src/graphql/document-model-subgraph.js +510 -81
  6. package/dist/src/graphql/document-model-subgraph.js.map +1 -1
  7. package/dist/src/graphql/drive-subgraph.d.ts.map +1 -1
  8. package/dist/src/graphql/drive-subgraph.js +1 -0
  9. package/dist/src/graphql/drive-subgraph.js.map +1 -1
  10. package/dist/src/graphql/graphql-manager.d.ts +4 -1
  11. package/dist/src/graphql/graphql-manager.d.ts.map +1 -1
  12. package/dist/src/graphql/graphql-manager.js +77 -32
  13. package/dist/src/graphql/graphql-manager.js.map +1 -1
  14. package/dist/src/graphql/reactor/adapters.d.ts +10 -2
  15. package/dist/src/graphql/reactor/adapters.d.ts.map +1 -1
  16. package/dist/src/graphql/reactor/adapters.js +35 -1
  17. package/dist/src/graphql/reactor/adapters.js.map +1 -1
  18. package/dist/src/graphql/reactor/factory.d.ts.map +1 -1
  19. package/dist/src/graphql/reactor/gen/graphql.d.ts +36 -4
  20. package/dist/src/graphql/reactor/gen/graphql.d.ts.map +1 -1
  21. package/dist/src/graphql/reactor/gen/graphql.js.map +1 -1
  22. package/dist/src/graphql/reactor/resolvers.d.ts +17 -1
  23. package/dist/src/graphql/reactor/resolvers.d.ts.map +1 -1
  24. package/dist/src/graphql/reactor/resolvers.js +64 -31
  25. package/dist/src/graphql/reactor/resolvers.js.map +1 -1
  26. package/dist/src/graphql/reactor/schema.graphql +33 -30
  27. package/dist/src/graphql/reactor/subgraph.d.ts.map +1 -1
  28. package/dist/src/graphql/reactor/subgraph.js +21 -0
  29. package/dist/src/graphql/reactor/subgraph.js.map +1 -1
  30. package/dist/src/graphql/system/system-subgraph.d.ts.map +1 -1
  31. package/dist/src/graphql/system/system-subgraph.js +2 -2
  32. package/dist/src/graphql/system/system-subgraph.js.map +1 -1
  33. package/dist/src/graphql/utils.d.ts.map +1 -1
  34. package/dist/src/graphql/utils.js +7 -3
  35. package/dist/src/graphql/utils.js.map +1 -1
  36. package/dist/src/packages/import-loader.d.ts +3 -1
  37. package/dist/src/packages/import-loader.d.ts.map +1 -1
  38. package/dist/src/packages/import-loader.js +19 -10
  39. package/dist/src/packages/import-loader.js.map +1 -1
  40. package/dist/src/packages/types.d.ts +4 -0
  41. package/dist/src/packages/types.d.ts.map +1 -1
  42. package/dist/src/packages/util.d.ts +2 -1
  43. package/dist/src/packages/util.d.ts.map +1 -1
  44. package/dist/src/packages/util.js +1 -1
  45. package/dist/src/packages/util.js.map +1 -1
  46. package/dist/src/packages/vite-loader.d.ts +4 -3
  47. package/dist/src/packages/vite-loader.d.ts.map +1 -1
  48. package/dist/src/packages/vite-loader.js +21 -8
  49. package/dist/src/packages/vite-loader.js.map +1 -1
  50. package/dist/src/server.d.ts +14 -6
  51. package/dist/src/server.d.ts.map +1 -1
  52. package/dist/src/server.js +114 -49
  53. package/dist/src/server.js.map +1 -1
  54. package/dist/src/services/auth.service.d.ts.map +1 -1
  55. package/dist/src/services/auth.service.js +11 -20
  56. package/dist/src/services/auth.service.js.map +1 -1
  57. package/dist/src/tracing.js +1 -1
  58. package/dist/src/tracing.js.map +1 -1
  59. package/dist/src/types.d.ts +1 -1
  60. package/dist/src/types.d.ts.map +1 -1
  61. package/dist/src/utils/create-schema.d.ts +21 -1
  62. package/dist/src/utils/create-schema.d.ts.map +1 -1
  63. package/dist/src/utils/create-schema.js +283 -13
  64. package/dist/src/utils/create-schema.js.map +1 -1
  65. package/dist/src/utils/drive-url.d.ts +2 -0
  66. package/dist/src/utils/drive-url.d.ts.map +1 -0
  67. package/dist/src/utils/drive-url.js +3 -0
  68. package/dist/src/utils/drive-url.js.map +1 -0
  69. package/dist/src/utils/index.d.ts +1 -0
  70. package/dist/src/utils/index.d.ts.map +1 -1
  71. package/dist/src/utils/index.js +1 -0
  72. package/dist/src/utils/index.js.map +1 -1
  73. package/dist/test/document-model-subgraph-legacy-permissions.test.d.ts +2 -0
  74. package/dist/test/document-model-subgraph-legacy-permissions.test.d.ts.map +1 -0
  75. package/dist/test/document-model-subgraph-legacy-permissions.test.js +518 -0
  76. package/dist/test/document-model-subgraph-legacy-permissions.test.js.map +1 -0
  77. package/dist/test/document-model-subgraph-permissions.test.d.ts +2 -0
  78. package/dist/test/document-model-subgraph-permissions.test.d.ts.map +1 -0
  79. package/dist/test/document-model-subgraph-permissions.test.js +635 -0
  80. package/dist/test/document-model-subgraph-permissions.test.js.map +1 -0
  81. package/dist/test/document-model-subgraph.test.d.ts +2 -0
  82. package/dist/test/document-model-subgraph.test.d.ts.map +1 -0
  83. package/dist/test/document-model-subgraph.test.js +441 -0
  84. package/dist/test/document-model-subgraph.test.js.map +1 -0
  85. package/dist/test/permissions-integration.test.js +4 -4
  86. package/dist/test/permissions-integration.test.js.map +1 -1
  87. package/dist/test/reactor-resolvers.test.js +4 -8
  88. package/dist/test/reactor-resolvers.test.js.map +1 -1
  89. package/dist/test/reactor-subgraph-permissions.test.js +4 -7
  90. package/dist/test/reactor-subgraph-permissions.test.js.map +1 -1
  91. package/dist/test/two-reactor-gql-catchup-duplicate.test.d.ts +2 -0
  92. package/dist/test/two-reactor-gql-catchup-duplicate.test.d.ts.map +1 -0
  93. package/dist/test/two-reactor-gql-catchup-duplicate.test.js +264 -0
  94. package/dist/test/two-reactor-gql-catchup-duplicate.test.js.map +1 -0
  95. package/dist/test/two-reactor-gql-sync.test.js +33 -105
  96. package/dist/test/two-reactor-gql-sync.test.js.map +1 -1
  97. package/dist/test/utils/gql-resolver-bridge.d.ts.map +1 -1
  98. package/dist/test/utils/gql-resolver-bridge.js.map +1 -1
  99. package/dist/tsconfig.tsbuildinfo +1 -1
  100. package/package.json +9 -10
  101. package/dist/test/three-reactor-gql-sync.test.d.ts +0 -2
  102. package/dist/test/three-reactor-gql-sync.test.d.ts.map +0 -1
  103. package/dist/test/three-reactor-gql-sync.test.js +0 -368
  104. package/dist/test/three-reactor-gql-sync.test.js.map +0 -1
@@ -1,104 +1,533 @@
1
1
  import { camelCase, kebabCase } from "change-case";
2
2
  import { addFile } from "document-drive";
3
- import { setName } from "document-model";
4
- import { generateDocumentModelSchemaLegacy, getDocumentModelSchemaName, } from "../utils/create-schema.js";
3
+ import { setName, } from "document-model";
4
+ import { GraphQLError } from "graphql";
5
+ import { generateDocumentModelSchema, getDocumentModelSchemaName, } from "../utils/create-schema.js";
5
6
  import { BaseSubgraph } from "./base-subgraph.js";
7
+ import { toGqlPhDocument } from "./reactor/adapters.js";
8
+ import { createGetParentIdsFn, document as documentResolver, documentChildren as documentChildrenResolver, documentParents as documentParentsResolver, findDocuments as findDocumentsResolver, createEmptyDocument as createEmptyDocumentResolver, } from "./reactor/resolvers.js";
6
9
  import { buildGraphQlDocument } from "./utils.js";
7
- export function generateDocumentModelResolversLegacy(documentModel, reactor) {
8
- const documentType = documentModel.documentModel.global.id;
9
- const documentName = getDocumentModelSchemaName(documentModel.documentModel.global);
10
- const operations = documentModel.documentModel.global.specifications
11
- .at(-1)
12
- ?.modules.flatMap((module) => module.operations.filter((op) => op.name)) ?? [];
13
- return {
14
- Query: {
15
- [documentName]: () => {
16
- return {
17
- getDocument: async (args) => {
18
- const { docId, driveId } = args;
19
- if (!docId) {
20
- throw new Error("Document id is required");
21
- }
22
- if (driveId) {
23
- const docIds = await reactor.getDocuments(driveId);
24
- if (!docIds.includes(docId)) {
25
- throw new Error(`Document with id ${docId} is not part of ${driveId}`);
10
+ /**
11
+ * New document model subgraph that uses reactorClient instead of legacy reactor.
12
+ * This class auto-generates GraphQL queries and mutations for a document model.
13
+ */
14
+ export class DocumentModelSubgraph extends BaseSubgraph {
15
+ documentModel;
16
+ constructor(documentModel, args) {
17
+ super(args);
18
+ this.documentModel = documentModel;
19
+ this.name = kebabCase(documentModel.documentModel.global.name);
20
+ this.typeDefs = generateDocumentModelSchema(this.documentModel.documentModel.global, { useNewApi: true });
21
+ this.resolvers = this.generateResolvers();
22
+ }
23
+ /**
24
+ * Check if user has global read access (admin, user, or guest)
25
+ */
26
+ hasGlobalReadAccess(ctx) {
27
+ const isGlobalAdmin = ctx.isAdmin?.(ctx.user?.address ?? "");
28
+ const isGlobalUser = ctx.isUser?.(ctx.user?.address ?? "");
29
+ const isGlobalGuest = ctx.isGuest?.(ctx.user?.address ?? "") ||
30
+ process.env.FREE_ENTRY === "true";
31
+ return !!(isGlobalAdmin || isGlobalUser || isGlobalGuest);
32
+ }
33
+ /**
34
+ * Check if user has global write access (admin or user, not guest)
35
+ */
36
+ hasGlobalWriteAccess(ctx) {
37
+ const isGlobalAdmin = ctx.isAdmin?.(ctx.user?.address ?? "");
38
+ const isGlobalUser = ctx.isUser?.(ctx.user?.address ?? "");
39
+ return !!(isGlobalAdmin || isGlobalUser);
40
+ }
41
+ /**
42
+ * Get the parent IDs function for hierarchical permission checks.
43
+ * Uses the shared createGetParentIdsFn from reactor/resolvers.
44
+ */
45
+ getParentIdsFn() {
46
+ return createGetParentIdsFn(this.reactorClient);
47
+ }
48
+ /**
49
+ * Check if user can read a document (with hierarchy)
50
+ */
51
+ async canReadDocument(documentId, ctx) {
52
+ if (this.hasGlobalReadAccess(ctx)) {
53
+ return true;
54
+ }
55
+ if (this.documentPermissionService) {
56
+ return this.documentPermissionService.canRead(documentId, ctx.user?.address, this.getParentIdsFn());
57
+ }
58
+ return false;
59
+ }
60
+ /**
61
+ * Check if user can write to a document (with hierarchy)
62
+ */
63
+ async canWriteDocument(documentId, ctx) {
64
+ if (this.hasGlobalWriteAccess(ctx)) {
65
+ return true;
66
+ }
67
+ if (this.documentPermissionService) {
68
+ return this.documentPermissionService.canWrite(documentId, ctx.user?.address, this.getParentIdsFn());
69
+ }
70
+ return false;
71
+ }
72
+ /**
73
+ * Throw an error if user cannot read the document
74
+ */
75
+ async assertCanRead(documentId, ctx) {
76
+ const canRead = await this.canReadDocument(documentId, ctx);
77
+ if (!canRead) {
78
+ throw new GraphQLError("Forbidden: insufficient permissions to read this document");
79
+ }
80
+ }
81
+ /**
82
+ * Throw an error if user cannot write to the document
83
+ */
84
+ async assertCanWrite(documentId, ctx) {
85
+ const canWrite = await this.canWriteDocument(documentId, ctx);
86
+ if (!canWrite) {
87
+ throw new GraphQLError("Forbidden: insufficient permissions to write to this document");
88
+ }
89
+ }
90
+ /**
91
+ * Check if user can execute a specific operation on a document.
92
+ * Throws an error if the operation is restricted and user lacks permission.
93
+ */
94
+ async assertCanExecuteOperation(documentId, operationType, ctx) {
95
+ if (!this.documentPermissionService) {
96
+ return;
97
+ }
98
+ if (ctx.isAdmin?.(ctx.user?.address ?? "")) {
99
+ return;
100
+ }
101
+ const isRestricted = await this.documentPermissionService.isOperationRestricted(documentId, operationType);
102
+ if (isRestricted) {
103
+ const canExecute = await this.documentPermissionService.canExecuteOperation(documentId, operationType, ctx.user?.address);
104
+ if (!canExecute) {
105
+ throw new GraphQLError(`Forbidden: insufficient permissions to execute operation "${operationType}" on this document`);
106
+ }
107
+ }
108
+ }
109
+ /**
110
+ * Generate resolvers for this document model using reactorClient
111
+ * Uses flat queries (not nested) consistent with ReactorSubgraph patterns
112
+ */
113
+ generateResolvers() {
114
+ const documentType = this.documentModel.documentModel.global.id;
115
+ const documentName = getDocumentModelSchemaName(this.documentModel.documentModel.global);
116
+ const operations = this.documentModel.documentModel.global.specifications
117
+ .at(-1)
118
+ ?.modules.flatMap((module) => module.operations.filter((op) => op.name)) ?? [];
119
+ return {
120
+ Query: {
121
+ // Flat query: Get a specific document by identifier
122
+ // Uses shared documentResolver from reactor/resolvers.ts
123
+ [`${documentName}_document`]: async (_, args, ctx) => {
124
+ const { identifier, view } = args;
125
+ if (!identifier) {
126
+ throw new GraphQLError("Document identifier is required");
127
+ }
128
+ // Use shared resolver function
129
+ const result = await documentResolver(this.reactorClient, {
130
+ identifier,
131
+ view,
132
+ });
133
+ // Validate document type
134
+ if (result.document.documentType !== documentType) {
135
+ throw new GraphQLError(`Document with id ${identifier} is not of type ${documentType}`);
136
+ }
137
+ // Check permissions
138
+ await this.assertCanRead(result.document.id, ctx);
139
+ // Return shared resolver result directly (matches PHDocument format)
140
+ return result;
141
+ },
142
+ // Flat query: Find documents by search criteria (type is built-in)
143
+ // Uses shared findDocumentsResolver from reactor/resolvers.ts
144
+ [`${documentName}_findDocuments`]: async (_, args, ctx) => {
145
+ const { search, view, paging } = args;
146
+ // Use shared resolver function with built-in type filter
147
+ const result = await findDocumentsResolver(this.reactorClient, {
148
+ search: {
149
+ type: documentType, // Type is built-in for this document model subgraph
150
+ parentId: search.parentId,
151
+ },
152
+ view,
153
+ paging,
154
+ });
155
+ // Filter by permission if needed
156
+ if (!this.hasGlobalReadAccess(ctx) &&
157
+ this.documentPermissionService) {
158
+ const filteredItems = [];
159
+ for (const item of result.items) {
160
+ const canRead = await this.canReadDocument(item.id, ctx);
161
+ if (canRead) {
162
+ filteredItems.push(item);
26
163
  }
27
164
  }
28
- const doc = await reactor.getDocument(docId);
29
- if (doc.header.documentType !== documentType) {
30
- throw new Error(`Document with id ${docId} is not of type ${documentType}`);
31
- }
32
165
  return {
33
- driveId: driveId,
34
- ...buildGraphQlDocument(doc),
166
+ ...result,
167
+ items: filteredItems,
168
+ totalCount: filteredItems.length,
35
169
  };
36
- },
37
- getDocuments: async (args) => {
38
- const { driveId } = args;
39
- const docsIds = await reactor.getDocuments(driveId);
40
- const docs = await Promise.all(docsIds.map(async (docId) => {
41
- const doc = await reactor.getDocument(docId);
42
- return {
43
- driveId: driveId,
44
- ...buildGraphQlDocument(doc),
45
- };
46
- }));
47
- return docs.filter((doc) => doc.documentType === documentType);
48
- },
49
- };
50
- },
51
- },
52
- Mutation: {
53
- [`${documentName}_createDocument`]: async (_, args) => {
54
- const { driveId, name } = args;
55
- const document = await reactor.addDocument(documentType);
56
- if (driveId) {
57
- await reactor.addAction(driveId, addFile({
58
- name,
59
- id: document.header.id,
60
- documentType: documentType,
61
- }));
62
- }
63
- if (name) {
64
- await reactor.addAction(document.header.id, setName(name));
65
- }
66
- return document.header.id;
170
+ }
171
+ // Return shared resolver result directly (matches PHDocument format)
172
+ return result;
173
+ },
174
+ // Flat query: Get children of a document (filtered by this document type)
175
+ // Uses shared documentChildrenResolver from reactor/resolvers.ts
176
+ [`${documentName}_documentChildren`]: async (_, args, ctx) => {
177
+ const { parentIdentifier, view, paging } = args;
178
+ await this.assertCanRead(parentIdentifier, ctx);
179
+ // Use shared resolver function
180
+ const result = await documentChildrenResolver(this.reactorClient, {
181
+ parentIdentifier,
182
+ view,
183
+ paging,
184
+ });
185
+ // Filter children by this document type
186
+ const filteredItems = result.items.filter((item) => item.documentType === documentType);
187
+ return {
188
+ ...result,
189
+ items: filteredItems,
190
+ totalCount: filteredItems.length,
191
+ };
192
+ },
193
+ // Flat query: Get parents of a document
194
+ // Uses shared documentParentsResolver from reactor/resolvers.ts
195
+ [`${documentName}_documentParents`]: async (_, args, ctx) => {
196
+ const { childIdentifier, view, paging } = args;
197
+ await this.assertCanRead(childIdentifier, ctx);
198
+ // Use shared resolver function - return directly
199
+ return documentParentsResolver(this.reactorClient, {
200
+ childIdentifier,
201
+ view,
202
+ paging,
203
+ });
204
+ },
67
205
  },
68
- ...operations.reduce((mutations, op) => {
69
- mutations[`${documentName}_${camelCase(op.name)}`] = async (_, args) => {
70
- const { docId, input } = args;
71
- const doc = await reactor.getDocument(docId);
72
- if (!doc) {
73
- throw new Error("Document not found");
206
+ Mutation: {
207
+ // Uses shared createEmptyDocumentResolver from reactor/resolvers.ts
208
+ [`${documentName}_createDocument`]: async (_, args, ctx) => {
209
+ const { parentIdentifier, name } = args;
210
+ if (parentIdentifier) {
211
+ await this.assertCanWrite(parentIdentifier, ctx);
74
212
  }
75
- const action = documentModel.actions[camelCase(op.name)];
76
- if (!action) {
77
- throw new Error(`Action ${op.name} not found`);
213
+ else if (!this.hasGlobalWriteAccess(ctx)) {
214
+ throw new GraphQLError("Forbidden: insufficient permissions to create documents");
78
215
  }
79
- const result = await reactor.addAction(docId, action(input));
80
- if (result.status !== "SUCCESS") {
81
- throw new Error(result.error?.message ?? `Failed to ${op.name}`);
216
+ // Use shared resolver function - returns PHDocument format
217
+ const createdDoc = await createEmptyDocumentResolver(this.reactorClient, {
218
+ documentType,
219
+ parentIdentifier,
220
+ });
221
+ if (name) {
222
+ const updatedDoc = await this.reactorClient.execute(createdDoc.id, "main", [setName(name)]);
223
+ // Use toGqlPhDocument for PHDocument format with revisionsList
224
+ return toGqlPhDocument(updatedDoc);
82
225
  }
83
- const errorOp = result.operations.find((op) => op.error);
84
- if (errorOp) {
85
- throw new Error(errorOp.error);
226
+ // Return directly - already in PHDocument format
227
+ return createdDoc;
228
+ },
229
+ // Uses shared createEmptyDocumentResolver from reactor/resolvers.ts
230
+ [`${documentName}_createEmptyDocument`]: async (_, args, ctx) => {
231
+ const { parentIdentifier } = args;
232
+ if (parentIdentifier) {
233
+ await this.assertCanWrite(parentIdentifier, ctx);
86
234
  }
87
- return result.operations.at(-1)?.index ?? -1;
88
- };
89
- return mutations;
90
- }, {}),
91
- },
92
- };
235
+ else if (!this.hasGlobalWriteAccess(ctx)) {
236
+ throw new GraphQLError("Forbidden: insufficient permissions to create documents");
237
+ }
238
+ // Use shared resolver function - returns PHDocument format directly
239
+ return createEmptyDocumentResolver(this.reactorClient, {
240
+ documentType,
241
+ parentIdentifier,
242
+ });
243
+ },
244
+ // Generate sync and async mutations for each operation
245
+ ...operations.reduce((mutations, op) => {
246
+ // Sync mutation
247
+ mutations[`${documentName}_${camelCase(op.name)}`] = async (_, args, ctx) => {
248
+ const { docId, input } = args;
249
+ await this.assertCanWrite(docId, ctx);
250
+ await this.assertCanExecuteOperation(docId, op.name, ctx);
251
+ const doc = await this.reactorClient.get(docId);
252
+ if (doc.header.documentType !== documentType) {
253
+ throw new GraphQLError(`Document with id ${docId} is not of type ${documentType}`);
254
+ }
255
+ const action = this.documentModel.actions[camelCase(op.name)];
256
+ if (!action) {
257
+ throw new GraphQLError(`Action ${op.name} not found`);
258
+ }
259
+ try {
260
+ const updatedDoc = await this.reactorClient.execute(docId, "main", [action(input)]);
261
+ // Use toGqlPhDocument for PHDocument format with revisionsList
262
+ return toGqlPhDocument(updatedDoc);
263
+ }
264
+ catch (error) {
265
+ throw new GraphQLError(error instanceof Error
266
+ ? error.message
267
+ : `Failed to ${op.name}`);
268
+ }
269
+ };
270
+ // Async mutation - returns job ID
271
+ mutations[`${documentName}_${camelCase(op.name)}Async`] = async (_, args, ctx) => {
272
+ const { docId, input } = args;
273
+ await this.assertCanWrite(docId, ctx);
274
+ await this.assertCanExecuteOperation(docId, op.name, ctx);
275
+ const doc = await this.reactorClient.get(docId);
276
+ if (doc.header.documentType !== documentType) {
277
+ throw new GraphQLError(`Document with id ${docId} is not of type ${documentType}`);
278
+ }
279
+ const action = this.documentModel.actions[camelCase(op.name)];
280
+ if (!action) {
281
+ throw new GraphQLError(`Action ${op.name} not found`);
282
+ }
283
+ try {
284
+ const jobInfo = await this.reactorClient.executeAsync(docId, "main", [action(input)]);
285
+ return jobInfo.id;
286
+ }
287
+ catch (error) {
288
+ throw new GraphQLError(error instanceof Error
289
+ ? error.message
290
+ : `Failed to ${op.name}`);
291
+ }
292
+ };
293
+ return mutations;
294
+ }, {}),
295
+ },
296
+ };
297
+ }
93
298
  }
299
+ /**
300
+ * @deprecated Use `DocumentModelSubgraph` instead. This class uses the legacy `reactor` (IDocumentDriveServer)
301
+ * interface. The new `DocumentModelSubgraph` class uses `reactorClient` (IReactorClient) which provides
302
+ * better patterns and more capabilities. Enable via `useNewDocumentModelSubgraph: true` in GraphQLManager.
303
+ */
94
304
  export class DocumentModelSubgraphLegacy extends BaseSubgraph {
95
305
  documentModel;
96
306
  constructor(documentModel, args) {
97
307
  super(args);
98
308
  this.documentModel = documentModel;
99
309
  this.name = kebabCase(documentModel.documentModel.global.name);
100
- this.typeDefs = generateDocumentModelSchemaLegacy(this.documentModel.documentModel.global);
101
- this.resolvers = generateDocumentModelResolversLegacy(this.documentModel, args.reactor);
310
+ this.typeDefs = generateDocumentModelSchema(this.documentModel.documentModel.global);
311
+ this.resolvers = this.generateResolvers();
312
+ }
313
+ /**
314
+ * Check if user has global read access (admin, user, or guest)
315
+ */
316
+ hasGlobalReadAccess(ctx) {
317
+ const isGlobalAdmin = ctx.isAdmin?.(ctx.user?.address ?? "");
318
+ const isGlobalUser = ctx.isUser?.(ctx.user?.address ?? "");
319
+ const isGlobalGuest = ctx.isGuest?.(ctx.user?.address ?? "") ||
320
+ process.env.FREE_ENTRY === "true";
321
+ return !!(isGlobalAdmin || isGlobalUser || isGlobalGuest);
322
+ }
323
+ /**
324
+ * Check if user has global write access (admin or user, not guest)
325
+ */
326
+ hasGlobalWriteAccess(ctx) {
327
+ const isGlobalAdmin = ctx.isAdmin?.(ctx.user?.address ?? "");
328
+ const isGlobalUser = ctx.isUser?.(ctx.user?.address ?? "");
329
+ return !!(isGlobalAdmin || isGlobalUser);
330
+ }
331
+ /**
332
+ * Get the parent IDs function for hierarchical permission checks
333
+ */
334
+ getParentIdsFn() {
335
+ return async (documentId) => {
336
+ try {
337
+ const result = await this.reactorClient.getParents(documentId);
338
+ return result.results.map((doc) => doc.header.id);
339
+ }
340
+ catch {
341
+ return [];
342
+ }
343
+ };
344
+ }
345
+ /**
346
+ * Check if user can read a document (with hierarchy)
347
+ */
348
+ async canReadDocument(documentId, ctx) {
349
+ // Global access allows reading
350
+ if (this.hasGlobalReadAccess(ctx)) {
351
+ return true;
352
+ }
353
+ // Check document-level permissions with hierarchy
354
+ if (this.documentPermissionService) {
355
+ return this.documentPermissionService.canRead(documentId, ctx.user?.address, this.getParentIdsFn());
356
+ }
357
+ return false;
358
+ }
359
+ /**
360
+ * Check if user can write to a document (with hierarchy)
361
+ */
362
+ async canWriteDocument(documentId, ctx) {
363
+ // Global write access allows writing
364
+ if (this.hasGlobalWriteAccess(ctx)) {
365
+ return true;
366
+ }
367
+ // Check document-level permissions with hierarchy
368
+ if (this.documentPermissionService) {
369
+ return this.documentPermissionService.canWrite(documentId, ctx.user?.address, this.getParentIdsFn());
370
+ }
371
+ return false;
372
+ }
373
+ /**
374
+ * Throw an error if user cannot read the document
375
+ */
376
+ async assertCanRead(documentId, ctx) {
377
+ const canRead = await this.canReadDocument(documentId, ctx);
378
+ if (!canRead) {
379
+ throw new GraphQLError("Forbidden: insufficient permissions to read this document");
380
+ }
381
+ }
382
+ /**
383
+ * Throw an error if user cannot write to the document
384
+ */
385
+ async assertCanWrite(documentId, ctx) {
386
+ const canWrite = await this.canWriteDocument(documentId, ctx);
387
+ if (!canWrite) {
388
+ throw new GraphQLError("Forbidden: insufficient permissions to write to this document");
389
+ }
390
+ }
391
+ /**
392
+ * Check if user can execute a specific operation on a document.
393
+ * Throws an error if the operation is restricted and user lacks permission.
394
+ */
395
+ async assertCanExecuteOperation(documentId, operationType, ctx) {
396
+ // Skip if no permission service
397
+ if (!this.documentPermissionService) {
398
+ return;
399
+ }
400
+ // Global admins bypass operation-level restrictions
401
+ if (ctx.isAdmin?.(ctx.user?.address ?? "")) {
402
+ return;
403
+ }
404
+ // Check if this operation has any restrictions set
405
+ const isRestricted = await this.documentPermissionService.isOperationRestricted(documentId, operationType);
406
+ if (isRestricted) {
407
+ // Operation is restricted, check if user has permission
408
+ const canExecute = await this.documentPermissionService.canExecuteOperation(documentId, operationType, ctx.user?.address);
409
+ if (!canExecute) {
410
+ throw new GraphQLError(`Forbidden: insufficient permissions to execute operation "${operationType}" on this document`);
411
+ }
412
+ }
413
+ }
414
+ /**
415
+ * Generate resolvers for this document model with permission checks
416
+ */
417
+ generateResolvers() {
418
+ const documentType = this.documentModel.documentModel.global.id;
419
+ const documentName = getDocumentModelSchemaName(this.documentModel.documentModel.global);
420
+ const operations = this.documentModel.documentModel.global.specifications
421
+ .at(-1)
422
+ ?.modules.flatMap((module) => module.operations.filter((op) => op.name)) ?? [];
423
+ return {
424
+ Query: {
425
+ [documentName]: (_, __, ctx) => {
426
+ return {
427
+ getDocument: async (args) => {
428
+ const { docId, driveId } = args;
429
+ if (!docId) {
430
+ throw new Error("Document id is required");
431
+ }
432
+ // Check read permission before accessing document
433
+ await this.assertCanRead(docId, ctx);
434
+ if (driveId) {
435
+ const docIds = await this.reactor.getDocuments(driveId);
436
+ if (!docIds.includes(docId)) {
437
+ throw new Error(`Document with id ${docId} is not part of ${driveId}`);
438
+ }
439
+ }
440
+ const doc = await this.reactor.getDocument(docId);
441
+ if (doc.header.documentType !== documentType) {
442
+ throw new Error(`Document with id ${docId} is not of type ${documentType}`);
443
+ }
444
+ return {
445
+ driveId: driveId,
446
+ ...buildGraphQlDocument(doc),
447
+ };
448
+ },
449
+ getDocuments: async (args) => {
450
+ const { driveId } = args;
451
+ // Check read permission on drive before listing documents
452
+ await this.assertCanRead(driveId, ctx);
453
+ const docsIds = await this.reactor.getDocuments(driveId);
454
+ const docs = await Promise.all(docsIds.map(async (docId) => {
455
+ const doc = await this.reactor.getDocument(docId);
456
+ return {
457
+ driveId: driveId,
458
+ ...buildGraphQlDocument(doc),
459
+ };
460
+ }));
461
+ const filteredByType = docs.filter((doc) => doc.documentType === documentType);
462
+ // If user doesn't have global read access, filter by document-level permissions
463
+ if (!this.hasGlobalReadAccess(ctx) &&
464
+ this.documentPermissionService) {
465
+ const filteredDocs = [];
466
+ for (const doc of filteredByType) {
467
+ const canRead = await this.canReadDocument(doc.id, ctx);
468
+ if (canRead) {
469
+ filteredDocs.push(doc);
470
+ }
471
+ }
472
+ return filteredDocs;
473
+ }
474
+ return filteredByType;
475
+ },
476
+ };
477
+ },
478
+ },
479
+ Mutation: {
480
+ [`${documentName}_createDocument`]: async (_, args, ctx) => {
481
+ const { driveId, name } = args;
482
+ // If creating under a drive, check write permission on drive
483
+ if (driveId) {
484
+ await this.assertCanWrite(driveId, ctx);
485
+ }
486
+ else if (!this.hasGlobalWriteAccess(ctx)) {
487
+ throw new GraphQLError("Forbidden: insufficient permissions to create documents");
488
+ }
489
+ const document = await this.reactor.addDocument(documentType);
490
+ if (driveId) {
491
+ await this.reactor.addAction(driveId, addFile({
492
+ name,
493
+ id: document.header.id,
494
+ documentType: documentType,
495
+ }));
496
+ }
497
+ if (name) {
498
+ await this.reactor.addAction(document.header.id, setName(name));
499
+ }
500
+ return document.header.id;
501
+ },
502
+ ...operations.reduce((mutations, op) => {
503
+ mutations[`${documentName}_${camelCase(op.name)}`] = async (_, args, ctx) => {
504
+ const { docId, input } = args;
505
+ // Check write permission before mutating document
506
+ await this.assertCanWrite(docId, ctx);
507
+ // Check operation-level permissions
508
+ await this.assertCanExecuteOperation(docId, op.name, ctx);
509
+ const doc = await this.reactor.getDocument(docId);
510
+ if (!doc) {
511
+ throw new Error("Document not found");
512
+ }
513
+ const action = this.documentModel.actions[camelCase(op.name)];
514
+ if (!action) {
515
+ throw new Error(`Action ${op.name} not found`);
516
+ }
517
+ const result = await this.reactor.addAction(docId, action(input));
518
+ if (result.status !== "SUCCESS") {
519
+ throw new Error(result.error?.message ?? `Failed to ${op.name}`);
520
+ }
521
+ const errorOp = result.operations.find((op) => op.error);
522
+ if (errorOp) {
523
+ throw new Error(errorOp.error);
524
+ }
525
+ return result.operations.at(-1)?.index ?? -1;
526
+ };
527
+ return mutations;
528
+ }, {}),
529
+ },
530
+ };
102
531
  }
103
532
  }
104
533
  //# sourceMappingURL=document-model-subgraph.js.map