adula 0.0.0-stage → 2.0.0

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 (210) hide show
  1. package/LICENSE.md +10 -0
  2. package/README.md +40 -2
  3. package/build/agent/AGENTS.template.md +28 -0
  4. package/build/agent/capabilities.md +60 -0
  5. package/build/agent/skills/adula-frontend-design/SKILL.md +59 -0
  6. package/build/agent/skills/idea-review/SKILL.md +31 -0
  7. package/build/agent/skills/module-review/SKILL.md +22 -0
  8. package/build/agent/skills/perf-review/SKILL.md +24 -0
  9. package/build/agent/skills/schema-review/SKILL.md +22 -0
  10. package/build/agent/skills/security-review/SKILL.md +24 -0
  11. package/build/agent/skills/ui-review/SKILL.md +22 -0
  12. package/build/commands/capabilities.d.ts +10 -0
  13. package/build/commands/capabilities.js +40 -0
  14. package/build/commands/doctor.d.ts +9 -0
  15. package/build/commands/doctor.js +72 -0
  16. package/build/commands/gaps.d.ts +12 -0
  17. package/build/commands/gaps.js +62 -0
  18. package/build/commands/install.d.ts +9 -0
  19. package/build/commands/install.js +105 -0
  20. package/build/commands/main.d.ts +8 -0
  21. package/build/commands/main.js +33 -0
  22. package/build/commands/module_add.d.ts +8 -0
  23. package/build/commands/module_add.js +57 -0
  24. package/build/commands/module_remove.d.ts +10 -0
  25. package/build/commands/module_remove.js +94 -0
  26. package/build/commands/resource.d.ts +8 -0
  27. package/build/commands/resource.js +29 -0
  28. package/build/commands/resource_snapshot.d.ts +15 -0
  29. package/build/commands/resource_snapshot.js +61 -0
  30. package/build/commands/storage_migrate.d.ts +12 -0
  31. package/build/commands/storage_migrate.js +39 -0
  32. package/build/commands/ui.d.ts +11 -0
  33. package/build/commands/ui.js +210 -0
  34. package/build/commands/upgrade.d.ts +15 -0
  35. package/build/commands/upgrade.js +34 -0
  36. package/build/configure.d.ts +2 -0
  37. package/build/configure.js +79 -0
  38. package/build/database/migrations/1770000000000_core.d.ts +5 -0
  39. package/build/database/migrations/1770000000000_core.js +10 -0
  40. package/build/database/migrations/1770000000001_attachments.d.ts +5 -0
  41. package/build/database/migrations/1770000000001_attachments.js +10 -0
  42. package/build/database/migrations/1770000000002_saved_views.d.ts +5 -0
  43. package/build/database/migrations/1770000000002_saved_views.js +10 -0
  44. package/build/database/migrations/1770000000003_user_invitations.d.ts +5 -0
  45. package/build/database/migrations/1770000000003_user_invitations.js +18 -0
  46. package/build/database/migrations/1770000000004_collaboration.d.ts +5 -0
  47. package/build/database/migrations/1770000000004_collaboration.js +10 -0
  48. package/build/database/migrations/1770000000005_assignments.d.ts +5 -0
  49. package/build/database/migrations/1770000000005_assignments.js +10 -0
  50. package/build/database/migrations/1770000000006_messaging.d.ts +5 -0
  51. package/build/database/migrations/1770000000006_messaging.js +10 -0
  52. package/build/database/migrations/1770000000007_webhooks.d.ts +5 -0
  53. package/build/database/migrations/1770000000007_webhooks.js +10 -0
  54. package/build/database/migrations/1770000000008_imports.d.ts +5 -0
  55. package/build/database/migrations/1770000000008_imports.js +10 -0
  56. package/build/database/migrations/1770000000010_workflows.d.ts +5 -0
  57. package/build/database/migrations/1770000000010_workflows.js +10 -0
  58. package/build/database/migrations/1770000000011_managed_assignments.d.ts +5 -0
  59. package/build/database/migrations/1770000000011_managed_assignments.js +10 -0
  60. package/build/database/migrations/1770000000012_role_keys.d.ts +5 -0
  61. package/build/database/migrations/1770000000012_role_keys.js +10 -0
  62. package/build/database/migrations/1770000000013_notification_targets.d.ts +5 -0
  63. package/build/database/migrations/1770000000013_notification_targets.js +10 -0
  64. package/build/database/migrations/1770000000014_upload_grants.d.ts +5 -0
  65. package/build/database/migrations/1770000000014_upload_grants.js +10 -0
  66. package/build/database/migrations/1770000000015_inbound_webhooks.d.ts +5 -0
  67. package/build/database/migrations/1770000000015_inbound_webhooks.js +10 -0
  68. package/build/database/migrations/1770000000016_adulajs_names.d.ts +6 -0
  69. package/build/database/migrations/1770000000016_adulajs_names.js +11 -0
  70. package/build/index.d.ts +80 -0
  71. package/build/index.js +49 -0
  72. package/build/providers/adula_provider.d.ts +6 -0
  73. package/build/providers/adula_provider.js +10 -0
  74. package/build/src/admin/contracts.d.ts +19 -0
  75. package/build/src/admin/contracts.js +115 -0
  76. package/build/src/admin/controller.d.ts +32 -0
  77. package/build/src/admin/controller.js +167 -0
  78. package/build/src/admin/errors.d.ts +5 -0
  79. package/build/src/admin/errors.js +9 -0
  80. package/build/src/admin/presentation.d.ts +28 -0
  81. package/build/src/admin/presentation.js +1 -0
  82. package/build/src/admin/record_title.d.ts +14 -0
  83. package/build/src/admin/record_title.js +50 -0
  84. package/build/src/admin/resource_service.d.ts +376 -0
  85. package/build/src/admin/resource_service.js +1448 -0
  86. package/build/src/attachments/attachment_service.d.ts +100 -0
  87. package/build/src/attachments/attachment_service.js +270 -0
  88. package/build/src/attachments/doctor.d.ts +4 -0
  89. package/build/src/attachments/doctor.js +28 -0
  90. package/build/src/attachments/storage_migrate.d.ts +39 -0
  91. package/build/src/attachments/storage_migrate.js +64 -0
  92. package/build/src/attachments/upload_grants.d.ts +44 -0
  93. package/build/src/attachments/upload_grants.js +105 -0
  94. package/build/src/auth/ability.d.ts +24 -0
  95. package/build/src/auth/ability.js +34 -0
  96. package/build/src/auth/actor_store.d.ts +14 -0
  97. package/build/src/auth/actor_store.js +70 -0
  98. package/build/src/auth/conditions.d.ts +26 -0
  99. package/build/src/auth/conditions.js +117 -0
  100. package/build/src/auth/packed_rules.d.ts +4 -0
  101. package/build/src/auth/packed_rules.js +13 -0
  102. package/build/src/auth/sql.d.ts +11 -0
  103. package/build/src/auth/sql.js +118 -0
  104. package/build/src/collaboration/assignments.d.ts +129 -0
  105. package/build/src/collaboration/assignments.js +333 -0
  106. package/build/src/collaboration/record_collaboration.d.ts +86 -0
  107. package/build/src/collaboration/record_collaboration.js +348 -0
  108. package/build/src/commands/agent_assets.d.ts +7 -0
  109. package/build/src/commands/agent_assets.js +66 -0
  110. package/build/src/commands/capabilities.d.ts +19 -0
  111. package/build/src/commands/capabilities.js +179 -0
  112. package/build/src/commands/doctor.d.ts +42 -0
  113. package/build/src/commands/doctor.js +375 -0
  114. package/build/src/commands/gap_report.d.ts +56 -0
  115. package/build/src/commands/gap_report.js +110 -0
  116. package/build/src/commands/generator.d.ts +1 -0
  117. package/build/src/commands/generator.js +70 -0
  118. package/build/src/commands/snapshot.d.ts +28 -0
  119. package/build/src/commands/snapshot.js +48 -0
  120. package/build/src/commands/source_markers.d.ts +11 -0
  121. package/build/src/commands/source_markers.js +41 -0
  122. package/build/src/core/activity.d.ts +43 -0
  123. package/build/src/core/activity.js +77 -0
  124. package/build/src/core/administration_guard.d.ts +3 -0
  125. package/build/src/core/administration_guard.js +39 -0
  126. package/build/src/core/health.d.ts +42 -0
  127. package/build/src/core/health.js +44 -0
  128. package/build/src/core/mail_test.d.ts +18 -0
  129. package/build/src/core/mail_test.js +88 -0
  130. package/build/src/core/message_templates.d.ts +83 -0
  131. package/build/src/core/message_templates.js +293 -0
  132. package/build/src/core/module_seed.d.ts +15 -0
  133. package/build/src/core/module_seed.js +31 -0
  134. package/build/src/core/notifications.d.ts +42 -0
  135. package/build/src/core/notifications.js +91 -0
  136. package/build/src/core/org_units.d.ts +30 -0
  137. package/build/src/core/org_units.js +123 -0
  138. package/build/src/core/roles.d.ts +98 -0
  139. package/build/src/core/roles.js +447 -0
  140. package/build/src/core/saved_views.d.ts +37 -0
  141. package/build/src/core/saved_views.js +164 -0
  142. package/build/src/core/settings.d.ts +29 -0
  143. package/build/src/core/settings.js +113 -0
  144. package/build/src/core/setup.d.ts +14 -0
  145. package/build/src/core/setup.js +89 -0
  146. package/build/src/core/ui_preferences.d.ts +12 -0
  147. package/build/src/core/ui_preferences.js +30 -0
  148. package/build/src/core/user_invitations.d.ts +23 -0
  149. package/build/src/core/user_invitations.js +144 -0
  150. package/build/src/core/users.d.ts +44 -0
  151. package/build/src/core/users.js +182 -0
  152. package/build/src/database/configure.d.ts +3 -0
  153. package/build/src/database/configure.js +41 -0
  154. package/build/src/database/schema.d.ts +52 -0
  155. package/build/src/database/schema.js +538 -0
  156. package/build/src/eslint/index.d.ts +3 -0
  157. package/build/src/eslint/index.js +90 -0
  158. package/build/src/events/adonis_jobs.d.ts +9 -0
  159. package/build/src/events/adonis_jobs.js +13 -0
  160. package/build/src/events/outbox.d.ts +19 -0
  161. package/build/src/events/outbox.js +33 -0
  162. package/build/src/events/record_mutation.d.ts +24 -0
  163. package/build/src/events/record_mutation.js +62 -0
  164. package/build/src/integrations/imports.d.ts +74 -0
  165. package/build/src/integrations/imports.js +333 -0
  166. package/build/src/integrations/inbound_webhooks.d.ts +94 -0
  167. package/build/src/integrations/inbound_webhooks.js +276 -0
  168. package/build/src/integrations/openapi.d.ts +39 -0
  169. package/build/src/integrations/openapi.js +323 -0
  170. package/build/src/integrations/print.d.ts +37 -0
  171. package/build/src/integrations/print.js +124 -0
  172. package/build/src/integrations/webhooks.d.ts +98 -0
  173. package/build/src/integrations/webhooks.js +298 -0
  174. package/build/src/mcp/tools.d.ts +8 -0
  175. package/build/src/mcp/tools.js +156 -0
  176. package/build/src/org/org_service.d.ts +2 -0
  177. package/build/src/org/org_service.js +16 -0
  178. package/build/src/resource/define_resource.d.ts +5 -0
  179. package/build/src/resource/define_resource.js +68 -0
  180. package/build/src/resource/registry.d.ts +12 -0
  181. package/build/src/resource/registry.js +101 -0
  182. package/build/src/resource/types.d.ts +155 -0
  183. package/build/src/resource/types.js +1 -0
  184. package/build/src/resource/values.d.ts +5 -0
  185. package/build/src/resource/values.js +88 -0
  186. package/build/src/services/backup.d.ts +14 -0
  187. package/build/src/services/backup.js +59 -0
  188. package/build/src/services/settings.d.ts +21 -0
  189. package/build/src/services/settings.js +30 -0
  190. package/build/src/upgrade/legacy.d.ts +27 -0
  191. package/build/src/upgrade/legacy.js +58 -0
  192. package/build/src/upgrade/rename.d.ts +17 -0
  193. package/build/src/upgrade/rename.js +94 -0
  194. package/build/src/version.d.ts +1 -0
  195. package/build/src/version.js +2 -0
  196. package/build/src/workflows/define_workflow.d.ts +124 -0
  197. package/build/src/workflows/define_workflow.js +123 -0
  198. package/build/src/workflows/engine.d.ts +141 -0
  199. package/build/src/workflows/engine.js +752 -0
  200. package/build/stubs/README.md +6 -0
  201. package/build/stubs/controller.stub +8 -0
  202. package/build/stubs/main.d.ts +5 -0
  203. package/build/stubs/main.js +5 -0
  204. package/build/stubs/main.ts +5 -0
  205. package/build/stubs/modules.stub +6 -0
  206. package/build/stubs/resource_contract.stub +1 -0
  207. package/build/stubs/resource_contract.txt +452 -0
  208. package/build/stubs/routes.stub +16 -0
  209. package/build/stubs/service.stub +9 -0
  210. package/package.json +95 -3
@@ -0,0 +1,538 @@
1
+ import { identifier, columnName } from '../resource/define_resource.js';
2
+ export async function createCoreSchema(db) {
3
+ await db.raw('CREATE EXTENSION IF NOT EXISTS ltree WITH SCHEMA public');
4
+ await db.schema.createTable('org_units', (t) => {
5
+ t.increments('id');
6
+ t.integer('parent_id').references('id').inTable('org_units').onDelete('RESTRICT');
7
+ t.string('name').notNullable();
8
+ t.string('type').notNullable();
9
+ t.specificType('path', 'ltree').notNullable().unique();
10
+ });
11
+ await db.raw('CREATE INDEX org_units_path_gist ON org_units USING gist(path)');
12
+ await db.schema.createTable('roles', (t) => {
13
+ t.increments('id');
14
+ t.string('name').notNullable().unique();
15
+ t.integer('permission_level').notNullable().defaultTo(0);
16
+ });
17
+ await db.schema.createTable('role_rules', (t) => {
18
+ t.increments('id');
19
+ t.integer('role_id').notNullable().references('id').inTable('roles').onDelete('CASCADE').index();
20
+ t.string('subject').notNullable();
21
+ t.string('action').notNullable();
22
+ t.jsonb('conditions');
23
+ t.jsonb('fields');
24
+ t.boolean('inverted').notNullable().defaultTo(false);
25
+ });
26
+ for (const name of ['user_roles', 'user_org_units']) {
27
+ await db.schema.createTable(name, (t) => {
28
+ t.increments('id');
29
+ t.integer('user_id')
30
+ .notNullable()
31
+ .references('id')
32
+ .inTable('users')
33
+ .onDelete('RESTRICT')
34
+ .index();
35
+ t.integer('org_unit_id').references('id').inTable('org_units').onDelete('RESTRICT').index();
36
+ if (name === 'user_roles')
37
+ t.integer('role_id')
38
+ .notNullable()
39
+ .references('id')
40
+ .inTable('roles')
41
+ .onDelete('RESTRICT')
42
+ .index();
43
+ else
44
+ t.unique(['user_id', 'org_unit_id']);
45
+ });
46
+ }
47
+ await db.schema.createTable('authorization_revision', (t) => {
48
+ t.integer('id').primary();
49
+ t.bigInteger('version').notNullable();
50
+ });
51
+ await db('authorization_revision').insert({ id: 1, version: 1 });
52
+ await db.raw(`CREATE FUNCTION adula_bump_auth_revision() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN UPDATE authorization_revision SET version=version+1 WHERE id=1; RETURN NULL; END $$`);
53
+ for (const table of ['roles', 'role_rules', 'user_roles', 'user_org_units', 'org_units'])
54
+ await db.raw(`CREATE TRIGGER adula_invalidate_auth AFTER INSERT OR UPDATE OR DELETE ON ${table} FOR EACH STATEMENT EXECUTE FUNCTION adula_bump_auth_revision()`);
55
+ await db.schema.createTable('settings', (t) => {
56
+ t.increments('id');
57
+ t.string('key').notNullable();
58
+ t.string('scope').notNullable().defaultTo('system');
59
+ t.string('scope_id').notNullable().defaultTo('0');
60
+ t.jsonb('value').notNullable();
61
+ t.unique(['key', 'scope', 'scope_id']);
62
+ });
63
+ await db.schema.createTable('lookups', (t) => {
64
+ t.increments('id');
65
+ t.string('group').notNullable();
66
+ t.string('key').notNullable();
67
+ t.string('label_ar').notNullable();
68
+ t.string('label_en').notNullable();
69
+ t.integer('sort').defaultTo(0);
70
+ t.boolean('active').defaultTo(true);
71
+ t.unique(['group', 'key']);
72
+ });
73
+ await db.schema.createTable('sequences', (t) => {
74
+ t.string('key').primary();
75
+ t.bigInteger('value').notNullable().defaultTo(0);
76
+ });
77
+ await db.schema.createTable('activities', (t) => {
78
+ t.bigIncrements('id');
79
+ t.string('resource').notNullable();
80
+ t.integer('record_id').notNullable();
81
+ t.integer('actor_id').notNullable().references('id').inTable('users');
82
+ t.string('action').notNullable();
83
+ t.jsonb('changes').notNullable();
84
+ t.timestamp('created_at', { useTz: true }).defaultTo(db.fn.now());
85
+ t.index(['resource', 'record_id']);
86
+ });
87
+ await db.schema.createTable('outbox', (t) => {
88
+ t.uuid('id').primary();
89
+ t.string('event').notNullable();
90
+ t.jsonb('payload').notNullable();
91
+ t.timestamp('created_at', { useTz: true }).defaultTo(db.fn.now());
92
+ t.timestamp('published_at', { useTz: true });
93
+ t.integer('attempts').notNullable().defaultTo(0);
94
+ t.text('last_error');
95
+ t.index(['published_at', 'created_at']);
96
+ });
97
+ await db.schema.createTable('processed_events', (t) => {
98
+ t.uuid('event_id').notNullable();
99
+ t.string('listener').notNullable();
100
+ t.timestamp('processed_at', { useTz: true }).defaultTo(db.fn.now());
101
+ t.primary(['event_id', 'listener']);
102
+ });
103
+ await db.schema.createTable('notifications', (t) => {
104
+ t.increments('id');
105
+ t.integer('user_id').notNullable().references('id').inTable('users').index();
106
+ t.string('title').notNullable();
107
+ t.text('body').notNullable();
108
+ t.timestamp('read_at', { useTz: true });
109
+ t.timestamp('created_at', { useTz: true }).defaultTo(db.fn.now());
110
+ });
111
+ await db.schema.createTable('workflow_runs', (t) => {
112
+ t.uuid('id').primary();
113
+ t.string('resource').notNullable();
114
+ t.integer('record_id').notNullable();
115
+ t.string('definition').notNullable();
116
+ t.integer('definition_version').notNullable();
117
+ t.jsonb('snapshot').notNullable();
118
+ t.string('status').notNullable();
119
+ t.timestamp('created_at', { useTz: true }).defaultTo(db.fn.now());
120
+ });
121
+ }
122
+ /** Attachment metadata lives in one table so ownership, restore drills and disk moves see every file. */
123
+ export async function createAttachmentsSchema(db) {
124
+ await db.schema.createTable('attachments', (t) => {
125
+ t.increments('id');
126
+ t.string('disk').notNullable();
127
+ t.string('path', 1024).notNullable();
128
+ t.string('name').notNullable();
129
+ t.string('original_name').notNullable();
130
+ t.bigInteger('size').notNullable();
131
+ t.string('mime_type').notNullable();
132
+ t.string('extname').notNullable();
133
+ t.jsonb('data').notNullable();
134
+ t.integer('uploaded_by')
135
+ .notNullable()
136
+ .references('id')
137
+ .inTable('users')
138
+ .onDelete('RESTRICT')
139
+ .index();
140
+ t.integer('org_unit_id').references('id').inTable('org_units').onDelete('RESTRICT').index();
141
+ t.string('resource');
142
+ t.integer('record_id');
143
+ t.string('field');
144
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
145
+ t.timestamp('deleted_at', { useTz: true });
146
+ t.index(['resource', 'record_id', 'field']);
147
+ t.index(['disk', 'deleted_at']);
148
+ });
149
+ }
150
+ export async function createSavedViewsSchema(db) {
151
+ await db.schema.createTable('saved_views', (t) => {
152
+ t.increments('id');
153
+ t.string('resource').notNullable();
154
+ t.string('name').notNullable();
155
+ t.jsonb('query').notNullable();
156
+ t.integer('user_id').notNullable().references('id').inTable('users').onDelete('CASCADE');
157
+ t.boolean('shared').notNullable().defaultTo(false);
158
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
159
+ t.timestamp('updated_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
160
+ t.unique(['user_id', 'resource', 'name']);
161
+ t.index(['resource', 'shared']);
162
+ });
163
+ }
164
+ export async function createResourceTable(db, resource) {
165
+ const table = identifier(resource.name);
166
+ await db.schema.createTable(table, (t) => {
167
+ t.increments('id');
168
+ if (resource.scoped)
169
+ t.integer('org_unit_id')
170
+ .notNullable()
171
+ .references('id')
172
+ .inTable('org_units')
173
+ .onDelete('RESTRICT')
174
+ .index();
175
+ for (const name of ['created_by', 'updated_by'])
176
+ t.integer(name).notNullable().references('id').inTable('users').onDelete('RESTRICT').index();
177
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
178
+ t.timestamp('updated_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
179
+ t.timestamp('deleted_at', { useTz: true });
180
+ if (resource.version)
181
+ t.integer('version').notNullable().defaultTo(1);
182
+ if (resource.submittable) {
183
+ t.smallint('doc_status').notNullable().defaultTo(0);
184
+ t.integer('amended_from_id').references('id').inTable(table).onDelete('RESTRICT').index();
185
+ }
186
+ if (resource.customFields)
187
+ t.jsonb('custom_fields').notNullable().defaultTo('{}');
188
+ for (const [key, field] of Object.entries(resource.fields)) {
189
+ if (field.type === 'hasMany')
190
+ continue;
191
+ const name = field.column ?? columnName(key);
192
+ let column;
193
+ switch (field.type) {
194
+ case 'belongsTo':
195
+ column = t.integer(name);
196
+ column.references('id').inTable(identifier(field.resource)).onDelete('RESTRICT').index();
197
+ break;
198
+ case 'attachment':
199
+ column = t.integer(name);
200
+ column.references('id').inTable('attachments').onDelete('RESTRICT').index();
201
+ break;
202
+ case 'user':
203
+ column = t.integer(name);
204
+ column.references('id').inTable('users').onDelete('RESTRICT').index();
205
+ break;
206
+ case 'integer':
207
+ column = t.integer(name);
208
+ break;
209
+ case 'money':
210
+ column = t.bigInteger(name);
211
+ break;
212
+ case 'boolean':
213
+ column = t.boolean(name);
214
+ break;
215
+ case 'date':
216
+ column = t.date(name);
217
+ break;
218
+ case 'datetime':
219
+ column = t.timestamp(name, { useTz: true });
220
+ break;
221
+ case 'json':
222
+ column = t.jsonb(name);
223
+ break;
224
+ case 'text':
225
+ column = t.text(name);
226
+ break;
227
+ default:
228
+ column = t.string(name);
229
+ }
230
+ if (field.required || field.sequence)
231
+ column.notNullable();
232
+ }
233
+ if (resource.scoped)
234
+ t.index(['org_unit_id', 'deleted_at']);
235
+ });
236
+ for (const [key, field] of Object.entries(resource.fields)) {
237
+ if (field.unique)
238
+ await db.raw('CREATE UNIQUE INDEX ?? ON ?? (??) WHERE deleted_at IS NULL', [
239
+ `${table}_${columnName(key)}_active_unique`,
240
+ table,
241
+ field.column ?? columnName(key),
242
+ ]);
243
+ }
244
+ const searchable = Object.entries(resource.fields).filter(([, f]) => f.searchable);
245
+ if (searchable.length) {
246
+ const expression = searchable
247
+ .map(([key, field]) => `coalesce("${identifier(field.column ?? columnName(key))}", '')`)
248
+ .join(` || ' ' || `);
249
+ await db.raw(`ALTER TABLE "${table}" ADD COLUMN search_vector tsvector GENERATED ALWAYS AS (to_tsvector('simple', ${expression})) STORED`);
250
+ await db.raw('CREATE INDEX ?? ON ?? USING gin(search_vector)', [`${table}_search_gin`, table]);
251
+ }
252
+ }
253
+ /** Record collaboration (phase 3): comments with mentions, followers, tags and field history. */
254
+ export async function createCollaborationSchema(db) {
255
+ await db.schema.createTable('comments', (t) => {
256
+ t.bigIncrements('id');
257
+ t.string('resource').notNullable();
258
+ t.integer('record_id').notNullable();
259
+ t.integer('author_id').notNullable().references('id').inTable('users').onDelete('RESTRICT');
260
+ t.text('body').notNullable();
261
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
262
+ t.timestamp('edited_at', { useTz: true });
263
+ t.timestamp('deleted_at', { useTz: true });
264
+ t.index(['resource', 'record_id', 'id']);
265
+ });
266
+ await db.schema.createTable('comment_mentions', (t) => {
267
+ t.bigInteger('comment_id')
268
+ .notNullable()
269
+ .references('id')
270
+ .inTable('comments')
271
+ .onDelete('CASCADE');
272
+ t.integer('user_id').notNullable().references('id').inTable('users').onDelete('CASCADE');
273
+ t.primary(['comment_id', 'user_id']);
274
+ t.index(['user_id']);
275
+ });
276
+ await db.schema.createTable('followers', (t) => {
277
+ t.string('resource').notNullable();
278
+ t.integer('record_id').notNullable();
279
+ t.integer('user_id').notNullable().references('id').inTable('users').onDelete('CASCADE');
280
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
281
+ t.primary(['resource', 'record_id', 'user_id']);
282
+ t.index(['user_id']);
283
+ });
284
+ await db.schema.createTable('tags', (t) => {
285
+ t.increments('id');
286
+ t.string('name', 60).notNullable().unique();
287
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
288
+ });
289
+ await db.schema.createTable('taggables', (t) => {
290
+ t.integer('tag_id').notNullable().references('id').inTable('tags').onDelete('CASCADE');
291
+ t.string('resource').notNullable();
292
+ t.integer('record_id').notNullable();
293
+ t.primary(['tag_id', 'resource', 'record_id']);
294
+ t.index(['resource', 'record_id']);
295
+ });
296
+ await db.schema.createTable('field_changes', (t) => {
297
+ t.bigIncrements('id');
298
+ t.bigInteger('activity_id')
299
+ .notNullable()
300
+ .references('id')
301
+ .inTable('activities')
302
+ .onDelete('CASCADE')
303
+ .index();
304
+ t.string('resource').notNullable();
305
+ t.integer('record_id').notNullable();
306
+ t.string('field').notNullable();
307
+ t.jsonb('before');
308
+ t.jsonb('after');
309
+ t.index(['resource', 'record_id', 'id']);
310
+ });
311
+ }
312
+ /** Work assigned to a user on a record; approval steps of workflows reuse it (phase 4). */
313
+ export async function createAssignmentsSchema(db) {
314
+ await db.schema.createTable('assignments', (t) => {
315
+ t.bigIncrements('id');
316
+ t.string('resource').notNullable();
317
+ t.integer('record_id').notNullable();
318
+ t.integer('assignee_id').notNullable().references('id').inTable('users').onDelete('RESTRICT');
319
+ t.integer('assigned_by').references('id').inTable('users').onDelete('RESTRICT');
320
+ t.string('kind', 20).notNullable().defaultTo('task');
321
+ t.string('title', 200).notNullable();
322
+ t.text('note');
323
+ t.date('due_on');
324
+ t.string('status', 20).notNullable().defaultTo('open');
325
+ t.uuid('workflow_run_id');
326
+ t.string('workflow_step', 100);
327
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
328
+ t.timestamp('completed_at', { useTz: true });
329
+ t.integer('completed_by').references('id').inTable('users').onDelete('RESTRICT');
330
+ t.string('outcome', 20);
331
+ t.index(['assignee_id', 'status', 'id']);
332
+ t.index(['resource', 'record_id']);
333
+ t.index(['workflow_run_id']);
334
+ });
335
+ }
336
+ /**
337
+ * Managed assignments (1.1): opened and closed by module code with the record's state,
338
+ * never completed by hand. The close reason is shown with the closed task.
339
+ */
340
+ export async function createManagedAssignmentsSchema(db) {
341
+ await db.schema.alterTable('assignments', (t) => {
342
+ t.boolean('managed').notNullable().defaultTo(false);
343
+ t.string('close_reason', 500);
344
+ });
345
+ }
346
+ /**
347
+ * Message templates, notification e-mail delivery state and the realtime signal.
348
+ * The trigger's NOTIFY is delivered only when the inserting transaction commits.
349
+ */
350
+ export async function createMessagingSchema(db) {
351
+ await db.schema.createTable('message_templates', (t) => {
352
+ t.string('key', 100).primary();
353
+ t.string('subject', 255).notNullable();
354
+ t.text('body').notNullable();
355
+ t.boolean('mail').notNullable().defaultTo(false);
356
+ t.integer('updated_by').references('id').inTable('users').onDelete('SET NULL');
357
+ t.timestamp('updated_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
358
+ });
359
+ await db.schema.alterTable('notifications', (t) => {
360
+ t.string('template_key', 100);
361
+ t.string('mail_state', 20);
362
+ t.integer('mail_attempts').notNullable().defaultTo(0);
363
+ t.string('mail_error', 500);
364
+ t.timestamp('mailed_at', { useTz: true });
365
+ });
366
+ await db.raw("CREATE INDEX notifications_mail_pending ON notifications (id) WHERE mail_state = 'pending'");
367
+ await db.raw(`CREATE FUNCTION adula_notification_signal() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN PERFORM pg_notify('adula_notifications', json_build_object('userId', NEW.user_id, 'id', NEW.id)::text); RETURN NULL; END $$`);
368
+ await db.raw('CREATE TRIGGER adula_notification_signal AFTER INSERT ON notifications FOR EACH ROW EXECUTE FUNCTION adula_notification_signal()');
369
+ }
370
+ /** Outgoing webhooks and their delivery log (exactly one row per webhook and event). */
371
+ export async function createWebhooksSchema(db) {
372
+ await db.schema.createTable('webhooks', (t) => {
373
+ t.increments('id');
374
+ t.string('name', 100).notNullable();
375
+ t.string('url', 2000).notNullable();
376
+ t.text('secret').notNullable();
377
+ t.jsonb('events').notNullable();
378
+ t.boolean('active').notNullable().defaultTo(true);
379
+ t.integer('failing').notNullable().defaultTo(0);
380
+ t.integer('created_by').references('id').inTable('users').onDelete('SET NULL');
381
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
382
+ t.timestamp('updated_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
383
+ t.timestamp('last_delivery_at', { useTz: true });
384
+ });
385
+ await db.raw('CREATE INDEX webhooks_events_gin ON webhooks USING gin(events)');
386
+ await db.schema.createTable('webhook_deliveries', (t) => {
387
+ t.uuid('id').primary();
388
+ t.integer('webhook_id').notNullable().references('id').inTable('webhooks').onDelete('CASCADE');
389
+ t.uuid('event_id').notNullable();
390
+ t.string('event').notNullable();
391
+ t.jsonb('payload').notNullable();
392
+ t.string('status', 20).notNullable().defaultTo('pending');
393
+ t.integer('attempts').notNullable().defaultTo(0);
394
+ t.integer('last_status');
395
+ t.string('last_error', 500);
396
+ t.timestamp('next_attempt_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
397
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
398
+ t.timestamp('delivered_at', { useTz: true });
399
+ t.unique(['webhook_id', 'event_id']);
400
+ t.index(['webhook_id', 'created_at']);
401
+ });
402
+ await db.raw("CREATE INDEX webhook_deliveries_due ON webhook_deliveries (next_attempt_at) WHERE status = 'pending'");
403
+ }
404
+ /** CSV import batches: parsed rows, column mapping, progress and per-row errors. */
405
+ export async function createImportsSchema(db) {
406
+ await db.schema.createTable('import_batches', (t) => {
407
+ t.increments('id');
408
+ t.string('resource').notNullable();
409
+ t.integer('user_id').notNullable().references('id').inTable('users').onDelete('CASCADE');
410
+ t.string('file_name', 200).notNullable();
411
+ t.string('status', 20).notNullable();
412
+ t.jsonb('headers').notNullable();
413
+ t.jsonb('mapping').notNullable();
414
+ t.jsonb('rows').notNullable();
415
+ t.integer('total').notNullable();
416
+ t.integer('processed').notNullable().defaultTo(0);
417
+ t.integer('created').notNullable().defaultTo(0);
418
+ t.integer('failed').notNullable().defaultTo(0);
419
+ t.jsonb('errors').notNullable().defaultTo('[]');
420
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
421
+ t.timestamp('started_at', { useTz: true });
422
+ t.timestamp('finished_at', { useTz: true });
423
+ t.index(['user_id', 'id']);
424
+ t.index(['status']);
425
+ });
426
+ }
427
+ /**
428
+ * Workflow engine state (phase 4): the submission envelope rows gain execution
429
+ * columns, and every transition is appended to workflow_events.
430
+ */
431
+ export async function createWorkflowSchema(db) {
432
+ await db.schema.alterTable('workflow_runs', (t) => {
433
+ t.string('current_step', 100);
434
+ t.timestamp('wake_at', { useTz: true });
435
+ t.integer('attempts').notNullable().defaultTo(0);
436
+ t.string('last_error', 1000);
437
+ t.string('outcome', 30);
438
+ t.integer('started_by').references('id').inTable('users').onDelete('SET NULL');
439
+ t.timestamp('updated_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
440
+ t.timestamp('completed_at', { useTz: true });
441
+ t.index(['resource', 'record_id']);
442
+ });
443
+ await db.raw("CREATE INDEX workflow_runs_due ON workflow_runs (wake_at) WHERE status = 'running'");
444
+ await db.schema.createTable('workflow_events', (t) => {
445
+ t.bigIncrements('id');
446
+ t.uuid('run_id').notNullable().references('id').inTable('workflow_runs').onDelete('CASCADE');
447
+ t.string('step', 100);
448
+ t.string('event', 50).notNullable();
449
+ t.integer('actor_id').references('id').inTable('users').onDelete('SET NULL');
450
+ t.jsonb('detail').notNullable().defaultTo('{}');
451
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
452
+ t.index(['run_id', 'id']);
453
+ });
454
+ }
455
+ /**
456
+ * Roles gain a stable key (1.1) that workflows and module defaults address, so an
457
+ * administrator can rename the display name freely. Existing identifier-like names
458
+ * (such as the bootstrap "administrator") become their key.
459
+ */
460
+ export async function createRoleKeysSchema(db) {
461
+ await db.schema.alterTable('roles', (t) => {
462
+ t.string('key', 100).unique();
463
+ });
464
+ await db.raw("UPDATE roles SET key = name WHERE name ~ '^[a-z][a-z0-9_]*$'");
465
+ }
466
+ /**
467
+ * Notifications may name the record they are about (1.1). Links are authorized when
468
+ * opened, by the record page itself, never when the notification is written.
469
+ */
470
+ export async function createNotificationTargetsSchema(db) {
471
+ await db.schema.alterTable('notifications', (t) => {
472
+ t.string('resource', 100);
473
+ t.integer('record_id');
474
+ });
475
+ }
476
+ /**
477
+ * Short-lived upload grants (1.1): module code lets one user upload to one attachment field
478
+ * of one record without a role rule. Only the SHA-256 hash of the token is stored.
479
+ */
480
+ export async function createUploadGrantsSchema(db) {
481
+ await db.schema.createTable('upload_grants', (t) => {
482
+ t.increments('id');
483
+ t.string('token_hash', 64).notNullable().unique();
484
+ t.integer('user_id').notNullable().references('users.id').onDelete('CASCADE');
485
+ t.string('resource', 100).notNullable();
486
+ t.integer('record_id').notNullable();
487
+ t.string('field', 100).notNullable();
488
+ t.integer('org_unit_id');
489
+ t.timestamp('expires_at', { useTz: true }).notNullable();
490
+ t.integer('created_by').notNullable();
491
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
492
+ t.index(['expires_at']);
493
+ });
494
+ }
495
+ /**
496
+ * Signed inbound webhooks (1.2): administrator-managed sources with sealed secrets,
497
+ * and a delivery log deduplicated by the sender's delivery id. Accepted deliveries are
498
+ * dispatched as domain events through the outbox.
499
+ */
500
+ export async function createInboundWebhooksSchema(db) {
501
+ await db.schema.createTable('inbound_sources', (t) => {
502
+ t.increments('id');
503
+ t.string('key', 60).notNullable().unique();
504
+ t.string('name', 100).notNullable();
505
+ t.text('secret').notNullable();
506
+ t.string('algorithm', 10).notNullable().defaultTo('sha256');
507
+ t.string('signature_header', 100).notNullable().defaultTo('x-hub-signature-256');
508
+ t.string('signature_prefix', 20).notNullable().defaultTo('sha256=');
509
+ t.string('event_header', 100).notNullable().defaultTo('x-github-event');
510
+ t.string('delivery_header', 100).notNullable().defaultTo('x-github-delivery');
511
+ t.boolean('dedupe_body').notNullable().defaultTo(true);
512
+ t.boolean('active').notNullable().defaultTo(true);
513
+ t.integer('created_by').references('id').inTable('users').onDelete('SET NULL');
514
+ t.timestamp('created_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
515
+ t.timestamp('updated_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
516
+ t.timestamp('last_received_at', { useTz: true });
517
+ });
518
+ await db.schema.createTable('inbound_deliveries', (t) => {
519
+ t.uuid('id').primary();
520
+ t.integer('source_id')
521
+ .notNullable()
522
+ .references('id')
523
+ .inTable('inbound_sources')
524
+ .onDelete('CASCADE');
525
+ t.string('delivery_id', 200).notNullable();
526
+ /** SHA-256 of the raw body while the source deduplicates bodies; null otherwise. */
527
+ t.string('body_key', 64);
528
+ t.string('event', 100).notNullable();
529
+ t.jsonb('payload').notNullable();
530
+ t.uuid('event_id').notNullable();
531
+ t.integer('dispatches').notNullable().defaultTo(1);
532
+ t.timestamp('received_at', { useTz: true }).notNullable().defaultTo(db.fn.now());
533
+ t.unique(['source_id', 'delivery_id']);
534
+ t.unique(['source_id', 'body_key']);
535
+ t.index(['source_id', 'received_at']);
536
+ t.index(['received_at']);
537
+ });
538
+ }
@@ -0,0 +1,3 @@
1
+ import type { ESLint } from 'eslint'
2
+ declare const plugin: ESLint.Plugin
3
+ export default plugin
@@ -0,0 +1,90 @@
1
+ export default {
2
+ rules: {
3
+ 'no-cross-module-controller': {
4
+ meta: {
5
+ type: 'problem',
6
+ schema: [],
7
+ messages: {
8
+ forbidden: 'Modules communicate through events, not cross-module controller imports.',
9
+ },
10
+ },
11
+ create(context) {
12
+ return {
13
+ ImportDeclaration(node) {
14
+ const owner = context.filename
15
+ .replaceAll('\\', '/')
16
+ .match(/app\/modules\/([^/]+)\//)?.[1]
17
+ const target = String(node.source.value).match(
18
+ /(?:#modules\/|modules\/)([^/]+)\/controllers\//
19
+ )?.[1]
20
+ if (owner && target && owner !== target)
21
+ context.report({ node, messageId: 'forbidden' })
22
+ },
23
+ }
24
+ },
25
+ },
26
+ 'no-direct-to-json': {
27
+ meta: {
28
+ type: 'problem',
29
+ schema: [],
30
+ messages: { forbidden: 'Use the AdulaJS serialize contract rather than toJSON().' },
31
+ },
32
+ create(context) {
33
+ return {
34
+ CallExpression(node) {
35
+ if (
36
+ node.callee.type === 'MemberExpression' &&
37
+ node.callee.property.type === 'Identifier' &&
38
+ node.callee.property.name === 'toJSON'
39
+ )
40
+ context.report({ node, messageId: 'forbidden' })
41
+ },
42
+ }
43
+ },
44
+ },
45
+ 'no-system-save-in-controllers': {
46
+ meta: {
47
+ type: 'problem',
48
+ schema: [],
49
+ messages: {
50
+ forbidden:
51
+ 'systemSave and rehome skip role rules. Call them from listeners or module services after an explicit authorization, never from a controller or route.',
52
+ },
53
+ },
54
+ create(context) {
55
+ const file = context.filename.replaceAll('\\', '/')
56
+ // Request handlers act for a user, so their writes go through save() and its rules.
57
+ if (!/(?:^|\/)(?:app\/controllers|app\/modules\/[^/]+\/controllers)\/|(?:^|\/)start\/routes\./.test(file))
58
+ return {}
59
+ return {
60
+ CallExpression(node) {
61
+ if (
62
+ node.callee.type === 'MemberExpression' &&
63
+ node.callee.property.type === 'Identifier' &&
64
+ ['systemSave', 'rehome'].includes(node.callee.property.name)
65
+ )
66
+ context.report({ node, messageId: 'forbidden' })
67
+ },
68
+ }
69
+ },
70
+ },
71
+ 'no-core-patching': {
72
+ meta: {
73
+ type: 'problem',
74
+ schema: [],
75
+ messages: { forbidden: 'Do not patch installed AdulaJS packages or use patch-package.' },
76
+ },
77
+ create(context) {
78
+ return {
79
+ Literal(node) {
80
+ if (
81
+ typeof node.value === 'string' &&
82
+ /patch-package|node_modules[/\\](?:@adula|adula)(?:[/\\]|$)/.test(node.value)
83
+ )
84
+ context.report({ node, messageId: 'forbidden' })
85
+ },
86
+ }
87
+ },
88
+ },
89
+ },
90
+ }
@@ -0,0 +1,9 @@
1
+ import type { Jobs } from './outbox.js';
2
+ /** Adapt an application's adonis-jobs dispatcher without importing its job classes. */
3
+ export declare class AdonisJobs implements Jobs {
4
+ private dispatchers;
5
+ constructor(dispatchers: Record<string, (payload: Record<string, unknown>, id: string) => Promise<unknown>>);
6
+ dispatch(name: string, payload: Record<string, unknown>, options: {
7
+ id: string;
8
+ }): Promise<void>;
9
+ }
@@ -0,0 +1,13 @@
1
+ /** Adapt an application's adonis-jobs dispatcher without importing its job classes. */
2
+ export class AdonisJobs {
3
+ dispatchers;
4
+ constructor(dispatchers) {
5
+ this.dispatchers = dispatchers;
6
+ }
7
+ async dispatch(name, payload, options) {
8
+ const dispatch = this.dispatchers[name];
9
+ if (!dispatch)
10
+ throw new Error(`No job dispatcher registered for ${name}`);
11
+ await dispatch(payload, options.id);
12
+ }
13
+ }