adula 2.1.0 → 2.3.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 (269) hide show
  1. package/README.md +149 -18
  2. package/build/agent/AGENTS.template.md +4 -4
  3. package/build/agent/capabilities.md +22 -12
  4. package/build/agent/skills/adula-frontend-design/SKILL.md +6 -4
  5. package/build/agent/skills/idea-review/SKILL.md +2 -2
  6. package/build/commands/adula_assistant_prune.d.ts +10 -0
  7. package/build/commands/adula_assistant_prune.js +12 -0
  8. package/build/commands/adula_outbox.d.ts +10 -0
  9. package/build/commands/adula_outbox.js +11 -0
  10. package/build/commands/adula_restore_reconcile.d.ts +10 -0
  11. package/build/commands/adula_restore_reconcile.js +34 -0
  12. package/build/commands/adula_runtime_health.d.ts +10 -0
  13. package/build/commands/adula_runtime_health.js +27 -0
  14. package/build/commands/adula_uploads_prune.d.ts +10 -0
  15. package/build/commands/adula_uploads_prune.js +16 -0
  16. package/build/commands/app.d.ts +10 -0
  17. package/build/commands/app.js +64 -0
  18. package/build/commands/backup_create.d.ts +11 -0
  19. package/build/commands/backup_create.js +99 -0
  20. package/build/commands/backup_open.d.ts +16 -0
  21. package/build/commands/backup_open.js +48 -0
  22. package/build/commands/backup_restore_files.d.ts +11 -0
  23. package/build/commands/backup_restore_files.js +44 -0
  24. package/build/commands/backup_restore_test.d.ts +12 -0
  25. package/build/commands/backup_restore_test.js +188 -0
  26. package/build/commands/backup_seal.d.ts +12 -0
  27. package/build/commands/backup_seal.js +36 -0
  28. package/build/commands/backup_services.d.ts +33 -0
  29. package/build/commands/backup_services.js +6 -0
  30. package/build/commands/backup_verify.d.ts +9 -0
  31. package/build/commands/backup_verify.js +59 -0
  32. package/build/commands/backup_verify_snapshot.d.ts +10 -0
  33. package/build/commands/backup_verify_snapshot.js +37 -0
  34. package/build/commands/client.d.ts +16 -0
  35. package/build/commands/client.js +71 -0
  36. package/build/commands/doctor.js +21 -1
  37. package/build/commands/install.d.ts +8 -0
  38. package/build/commands/install.js +69 -45
  39. package/build/commands/main.d.ts +3 -2
  40. package/build/commands/main.js +32 -0
  41. package/build/commands/module_add.js +4 -20
  42. package/build/commands/resource.d.ts +2 -0
  43. package/build/commands/resource.js +12 -3
  44. package/build/commands/resource_migration.d.ts +15 -0
  45. package/build/commands/resource_migration.js +81 -0
  46. package/build/commands/resource_snapshot.d.ts +2 -0
  47. package/build/commands/resource_snapshot.js +21 -4
  48. package/build/commands/sla_evaluate.d.ts +14 -0
  49. package/build/commands/sla_evaluate.js +16 -0
  50. package/build/commands/ui.js +4 -2
  51. package/build/commands/ui_css.d.ts +9 -0
  52. package/build/commands/ui_css.js +19 -0
  53. package/build/commands/upgrade.d.ts +1 -1
  54. package/build/commands/upgrade.js +1 -1
  55. package/build/configure.js +1 -1
  56. package/build/database/migrations/1770000000020_two_factor.d.ts +5 -0
  57. package/build/database/migrations/1770000000020_two_factor.js +10 -0
  58. package/build/database/migrations/1770000000041_import_modes.d.ts +5 -0
  59. package/build/database/migrations/1770000000041_import_modes.js +10 -0
  60. package/build/database/migrations/1770000000042_sync_jobs.d.ts +5 -0
  61. package/build/database/migrations/1770000000042_sync_jobs.js +10 -0
  62. package/build/database/migrations/1770000000043_notification_channels.d.ts +5 -0
  63. package/build/database/migrations/1770000000043_notification_channels.js +10 -0
  64. package/build/database/migrations/1770000000044_channel_confirmations.d.ts +5 -0
  65. package/build/database/migrations/1770000000044_channel_confirmations.js +10 -0
  66. package/build/database/migrations/1770000000045_import_leases.d.ts +5 -0
  67. package/build/database/migrations/1770000000045_import_leases.js +10 -0
  68. package/build/database/migrations/1770000000046_workflow_delegation_sla.d.ts +5 -0
  69. package/build/database/migrations/1770000000046_workflow_delegation_sla.js +10 -0
  70. package/build/database/migrations/1770000000049_print_templates.d.ts +5 -0
  71. package/build/database/migrations/1770000000049_print_templates.js +10 -0
  72. package/build/database/migrations/1770000000051_comment_notifications.d.ts +5 -0
  73. package/build/database/migrations/1770000000051_comment_notifications.js +10 -0
  74. package/build/database/migrations/1770000000054_cloud_backups.d.ts +5 -0
  75. package/build/database/migrations/1770000000054_cloud_backups.js +10 -0
  76. package/build/database/migrations/1770000000055_ai_assistant.d.ts +5 -0
  77. package/build/database/migrations/1770000000055_ai_assistant.js +10 -0
  78. package/build/database/migrations/1770000000056_appearance.d.ts +5 -0
  79. package/build/database/migrations/1770000000056_appearance.js +10 -0
  80. package/build/index.d.ts +87 -11
  81. package/build/index.js +50 -7
  82. package/build/providers/adula_provider.d.ts +2 -0
  83. package/build/providers/adula_provider.js +7 -0
  84. package/build/src/admin/contracts.js +1 -1
  85. package/build/src/admin/controller.d.ts +14 -1
  86. package/build/src/admin/controller.js +91 -13
  87. package/build/src/admin/errors.d.ts +32 -0
  88. package/build/src/admin/errors.js +63 -1
  89. package/build/src/admin/presentation.d.ts +21 -2
  90. package/build/src/admin/resource/editor.d.ts +60 -0
  91. package/build/src/admin/resource/editor.js +345 -0
  92. package/build/src/admin/resource/export.d.ts +54 -0
  93. package/build/src/admin/resource/export.js +262 -0
  94. package/build/src/admin/resource/lifecycle.d.ts +23 -0
  95. package/build/src/admin/resource/lifecycle.js +91 -0
  96. package/build/src/admin/resource/query.d.ts +111 -0
  97. package/build/src/admin/resource/query.js +595 -0
  98. package/build/src/admin/resource/read.d.ts +109 -0
  99. package/build/src/admin/resource/read.js +388 -0
  100. package/build/src/admin/resource/shared.d.ts +93 -0
  101. package/build/src/admin/resource/shared.js +265 -0
  102. package/build/src/admin/resource/trash.d.ts +10 -0
  103. package/build/src/admin/resource/trash.js +56 -0
  104. package/build/src/admin/resource/write.d.ts +57 -0
  105. package/build/src/admin/resource/write.js +603 -0
  106. package/build/src/admin/resource_service.d.ts +80 -193
  107. package/build/src/admin/resource_service.js +81 -1343
  108. package/build/src/admin/resource_tools.d.ts +111 -0
  109. package/build/src/admin/resource_tools.js +139 -0
  110. package/build/src/ai/assistant.d.ts +100 -0
  111. package/build/src/ai/assistant.js +487 -0
  112. package/build/src/ai/client.d.ts +49 -0
  113. package/build/src/ai/client.js +85 -0
  114. package/build/src/ai/prompt.d.ts +23 -0
  115. package/build/src/ai/prompt.js +83 -0
  116. package/build/src/ai/settings.d.ts +56 -0
  117. package/build/src/ai/settings.js +214 -0
  118. package/build/src/ai/usage.d.ts +16 -0
  119. package/build/src/ai/usage.js +33 -0
  120. package/build/src/appearance/appearance.d.ts +134 -0
  121. package/build/src/appearance/appearance.js +461 -0
  122. package/build/src/appearance/colors.d.ts +43 -0
  123. package/build/src/appearance/colors.js +99 -0
  124. package/build/src/appearance/logo.d.ts +32 -0
  125. package/build/src/appearance/logo.js +112 -0
  126. package/build/src/attachments/attachment_service.d.ts +46 -2
  127. package/build/src/attachments/attachment_service.js +96 -4
  128. package/build/src/auth/actor_store.d.ts +5 -1
  129. package/build/src/auth/actor_store.js +8 -4
  130. package/build/src/auth/conditions.d.ts +5 -0
  131. package/build/src/auth/conditions.js +26 -3
  132. package/build/src/auth/sql.js +17 -5
  133. package/build/src/auth/two_factor.d.ts +96 -0
  134. package/build/src/auth/two_factor.js +259 -0
  135. package/build/src/backups/archive.d.ts +52 -0
  136. package/build/src/backups/archive.js +362 -0
  137. package/build/src/backups/cloud_backups.d.ts +133 -0
  138. package/build/src/backups/cloud_backups.js +650 -0
  139. package/build/src/backups/doctor.d.ts +7 -0
  140. package/build/src/backups/doctor.js +55 -0
  141. package/build/src/backups/providers.d.ts +81 -0
  142. package/build/src/backups/providers.js +470 -0
  143. package/build/src/collaboration/assignments.d.ts +59 -2
  144. package/build/src/collaboration/assignments.js +196 -74
  145. package/build/src/collaboration/record_collaboration.d.ts +21 -4
  146. package/build/src/collaboration/record_collaboration.js +126 -51
  147. package/build/src/collaboration/shared.d.ts +24 -0
  148. package/build/src/collaboration/shared.js +34 -0
  149. package/build/src/commands/app_scaffold.d.ts +72 -0
  150. package/build/src/commands/app_scaffold.js +285 -0
  151. package/build/src/commands/capabilities.js +35 -9
  152. package/build/src/commands/doctor.d.ts +24 -2
  153. package/build/src/commands/doctor.js +81 -6
  154. package/build/src/commands/gap_report.d.ts +1 -1
  155. package/build/src/commands/gap_report.js +2 -2
  156. package/build/src/commands/generator.d.ts +37 -1
  157. package/build/src/commands/generator.js +61 -18
  158. package/build/src/commands/snapshot.d.ts +24 -13
  159. package/build/src/commands/snapshot.js +54 -14
  160. package/build/src/commands/source_markers.d.ts +10 -0
  161. package/build/src/commands/source_markers.js +29 -0
  162. package/build/src/core/activity.d.ts +2 -0
  163. package/build/src/core/activity.js +7 -1
  164. package/build/src/core/administrators.d.ts +38 -0
  165. package/build/src/core/administrators.js +145 -0
  166. package/build/src/core/avatar_image.d.ts +33 -0
  167. package/build/src/core/avatar_image.js +278 -0
  168. package/build/src/core/html_sanitizer.d.ts +20 -0
  169. package/build/src/core/html_sanitizer.js +261 -0
  170. package/build/src/core/like.d.ts +5 -0
  171. package/build/src/core/like.js +5 -0
  172. package/build/src/core/lookups.d.ts +80 -0
  173. package/build/src/core/lookups.js +319 -0
  174. package/build/src/core/mail_settings.d.ts +83 -0
  175. package/build/src/core/mail_settings.js +274 -0
  176. package/build/src/core/mail_test.d.ts +16 -0
  177. package/build/src/core/mail_test.js +81 -6
  178. package/build/src/core/message_templates.d.ts +15 -2
  179. package/build/src/core/message_templates.js +81 -11
  180. package/build/src/core/module_seed.js +2 -1
  181. package/build/src/core/notification_channels.d.ts +235 -0
  182. package/build/src/core/notification_channels.js +660 -0
  183. package/build/src/core/roles.js +26 -9
  184. package/build/src/core/saved_views.d.ts +8 -1
  185. package/build/src/core/saved_views.js +91 -26
  186. package/build/src/core/sequences.d.ts +32 -0
  187. package/build/src/core/sequences.js +97 -0
  188. package/build/src/core/settings.d.ts +5 -0
  189. package/build/src/core/settings.js +31 -3
  190. package/build/src/core/template_language.d.ts +61 -0
  191. package/build/src/core/template_language.js +141 -0
  192. package/build/src/core/ui_preferences.d.ts +10 -0
  193. package/build/src/core/ui_preferences.js +15 -1
  194. package/build/src/core/user_avatars.d.ts +79 -0
  195. package/build/src/core/user_avatars.js +186 -0
  196. package/build/src/core/user_invitations.d.ts +5 -1
  197. package/build/src/core/user_invitations.js +7 -1
  198. package/build/src/core/user_preferences.d.ts +36 -0
  199. package/build/src/core/user_preferences.js +125 -0
  200. package/build/src/core/users.d.ts +2 -0
  201. package/build/src/core/users.js +5 -3
  202. package/build/src/database/calendar_days.d.ts +19 -0
  203. package/build/src/database/calendar_days.js +36 -0
  204. package/build/src/database/schema.d.ts +201 -2
  205. package/build/src/database/schema.js +713 -59
  206. package/build/src/database/schema_diff.d.ts +70 -0
  207. package/build/src/database/schema_diff.js +190 -0
  208. package/build/src/events/record_mutation.d.ts +2 -1
  209. package/build/src/events/record_mutation.js +1 -0
  210. package/build/src/integrations/api_client.d.ts +37 -0
  211. package/build/src/integrations/api_client.js +567 -0
  212. package/build/src/integrations/import_adapters.d.ts +37 -0
  213. package/build/src/integrations/import_adapters.js +35 -0
  214. package/build/src/integrations/imports.d.ts +130 -16
  215. package/build/src/integrations/imports.js +434 -73
  216. package/build/src/integrations/openapi.d.ts +3 -1
  217. package/build/src/integrations/openapi.js +465 -31
  218. package/build/src/integrations/outbound.d.ts +94 -0
  219. package/build/src/integrations/outbound.js +276 -0
  220. package/build/src/integrations/print.d.ts +22 -1
  221. package/build/src/integrations/print.js +68 -22
  222. package/build/src/integrations/print_templates.d.ts +216 -0
  223. package/build/src/integrations/print_templates.js +1017 -0
  224. package/build/src/integrations/sync_jobs.d.ts +139 -0
  225. package/build/src/integrations/sync_jobs.js +416 -0
  226. package/build/src/integrations/webhooks.d.ts +12 -12
  227. package/build/src/integrations/webhooks.js +34 -43
  228. package/build/src/integrations/xlsx.d.ts +41 -0
  229. package/build/src/integrations/xlsx.js +519 -0
  230. package/build/src/languages/ar.d.ts +11 -0
  231. package/build/src/languages/ar.js +358 -0
  232. package/build/src/languages/ar_ui.d.ts +461 -0
  233. package/build/src/languages/ar_ui.js +464 -0
  234. package/build/src/languages/catalog.d.ts +66 -0
  235. package/build/src/languages/catalog.js +152 -0
  236. package/build/src/languages/en.d.ts +7 -0
  237. package/build/src/languages/en.js +352 -0
  238. package/build/src/languages/en_ui.d.ts +3 -0
  239. package/build/src/languages/en_ui.js +458 -0
  240. package/build/src/languages/validation.d.ts +26 -0
  241. package/build/src/languages/validation.js +60 -0
  242. package/build/src/lists/bulk_actions.d.ts +89 -0
  243. package/build/src/lists/bulk_actions.js +268 -0
  244. package/build/src/lists/dashboards.d.ts +138 -0
  245. package/build/src/lists/dashboards.js +290 -0
  246. package/build/src/mcp/tools.js +9 -104
  247. package/build/src/resource/define_resource.d.ts +18 -1
  248. package/build/src/resource/define_resource.js +212 -18
  249. package/build/src/resource/definition.d.ts +44 -0
  250. package/build/src/resource/definition.js +171 -0
  251. package/build/src/resource/registry.js +15 -4
  252. package/build/src/resource/types.d.ts +313 -16
  253. package/build/src/resource/values.d.ts +56 -1
  254. package/build/src/resource/values.js +237 -5
  255. package/build/src/services/settings.d.ts +2 -0
  256. package/build/src/services/settings.js +5 -1
  257. package/build/src/upgrade/legacy.js +2 -2
  258. package/build/src/workflows/calendar.d.ts +9 -0
  259. package/build/src/workflows/calendar.js +44 -0
  260. package/build/src/workflows/define_workflow.d.ts +48 -3
  261. package/build/src/workflows/define_workflow.js +57 -13
  262. package/build/src/workflows/delegations.d.ts +107 -0
  263. package/build/src/workflows/delegations.js +326 -0
  264. package/build/src/workflows/engine.d.ts +80 -4
  265. package/build/src/workflows/engine.js +466 -86
  266. package/build/src/workflows/sla.d.ts +15 -0
  267. package/build/src/workflows/sla.js +8 -0
  268. package/build/stubs/resource_contract.txt +12 -1
  269. package/package.json +9 -2
@@ -0,0 +1,268 @@
1
+ import { subject } from '@casl/ability';
2
+ import { fieldValue } from '../resource/values.js';
3
+ import { buildAbility } from '../auth/ability.js';
4
+ import { AdulaError, forbiddenAction } from '../admin/errors.js';
5
+ import { labelText, translate } from '../languages/catalog.js';
6
+ import { fieldLevel } from '../admin/resource/shared.js';
7
+ import { fromRow } from '../admin/contracts.js';
8
+ /** Most records one bulk request may change. */
9
+ export const BULK_ID_LIMIT = 100;
10
+ const BUILT_IN = ['delete', 'restore', 'submit', 'cancel'];
11
+ const KEY = /^[a-z][a-zA-Z0-9_]*$/;
12
+ /**
13
+ * Declares bulk actions for one resource. Register the result on the BulkActions service.
14
+ * Definitions are checked here for shape and against the resource when registered.
15
+ */
16
+ export function defineBulkActions(resource, actions) {
17
+ if (typeof resource !== 'string' || !resource)
18
+ throw new Error('Bulk actions need a resource');
19
+ const keys = new Set();
20
+ for (const action of actions) {
21
+ if (typeof action?.key !== 'string' || !KEY.test(action.key))
22
+ throw new Error(`${resource}: invalid bulk action key ${String(action?.key)}`);
23
+ if (BUILT_IN.includes(action.key))
24
+ throw new Error(`${resource}: ${action.key} is a built-in bulk action`);
25
+ if (keys.has(action.key))
26
+ throw new Error(`${resource}: duplicate bulk action ${action.key}`);
27
+ keys.add(action.key);
28
+ if (!action.label?.ar?.trim())
29
+ throw new Error(`${resource}.${action.key}: label.ar is required`);
30
+ if (action.confirm !== undefined && !action.confirm?.ar?.trim())
31
+ throw new Error(`${resource}.${action.key}: confirm.ar is required`);
32
+ if (!action.values ||
33
+ typeof action.values !== 'object' ||
34
+ Array.isArray(action.values) ||
35
+ !Object.keys(action.values).length)
36
+ throw new Error(`${resource}.${action.key}: values must set at least one field`);
37
+ }
38
+ return Object.freeze({ resource, actions: Object.freeze([...actions]) });
39
+ }
40
+ /**
41
+ * Runs one action over selected records. Every record runs in its own transaction
42
+ * through the ordinary paths (transition, restore, save), with the actor's per-row
43
+ * policy check and audit, so one refused row does not undo the others.
44
+ */
45
+ export class BulkActions {
46
+ registry;
47
+ resources;
48
+ options;
49
+ defined;
50
+ constructor(registry, resources, definitions = [], options = {}) {
51
+ this.registry = registry;
52
+ this.resources = resources;
53
+ this.options = options;
54
+ this.defined = BulkActions.validate(registry, definitions);
55
+ }
56
+ /**
57
+ * Checks bulk action sets against the registered resources: each set names a resource,
58
+ * keys are unique per resource and every value is a writable form field value. Call it
59
+ * when the application loads its definitions, so a bad definition fails at boot.
60
+ */
61
+ static validate(registry, definitions) {
62
+ const defined = new Map();
63
+ for (const set of definitions) {
64
+ const resource = registry.get(set.resource);
65
+ const actions = defined.get(resource.name) ?? new Map();
66
+ for (const action of set.actions) {
67
+ if (actions.has(action.key))
68
+ throw new Error(`${resource.name}: duplicate bulk action ${action.key}`);
69
+ for (const [key, value] of Object.entries(action.values)) {
70
+ const field = resource.fields[key];
71
+ if (!field || !resource.form.includes(key) || field.type === 'hasMany' || field.sequence)
72
+ throw new Error(`${resource.name}.${action.key}: ${key} is not a writable form field`);
73
+ try {
74
+ fieldValue(field, value);
75
+ }
76
+ catch (error) {
77
+ throw new Error(`${resource.name}.${action.key}.${key}: ${error.message}`);
78
+ }
79
+ }
80
+ actions.set(action.key, action);
81
+ }
82
+ defined.set(resource.name, actions);
83
+ }
84
+ return defined;
85
+ }
86
+ /**
87
+ * The actions the actor may offer on this resource's list (resource-level permission).
88
+ * 403 E_FORBIDDEN when the actor may not view the resource at all.
89
+ */
90
+ available(name, actor) {
91
+ const resource = this.registry.get(name);
92
+ const ability = buildAbility(actor.rules, this.registry.all());
93
+ const holds = (action) => resource.actions.includes(action) && ability.can(action, resource.name);
94
+ const options = [];
95
+ if (!resource.actions.includes('view') || !ability.can('view', resource.name))
96
+ throw forbiddenAction();
97
+ if (holds('delete'))
98
+ options.push(builtIn('delete'));
99
+ if (resource.submittable && holds('submit'))
100
+ options.push(builtIn('submit'));
101
+ if (resource.submittable && holds('cancel'))
102
+ options.push(builtIn('cancel'));
103
+ if (holds('delete'))
104
+ options.push(builtIn('restore'));
105
+ if (holds('update'))
106
+ for (const action of this.defined.get(resource.name)?.values() ?? [])
107
+ if (Object.keys(action.values).every((key) => ability.can('update', resource.name, key) &&
108
+ actor.permissionLevel >= fieldLevel(resource, key)))
109
+ options.push({
110
+ key: action.key,
111
+ label: labelText(action.label),
112
+ builtIn: false,
113
+ scope: 'records',
114
+ confirm: action.confirm ? labelText(action.confirm) : null,
115
+ destructive: Boolean(action.destructive),
116
+ });
117
+ return options;
118
+ }
119
+ async run(name, actor, request) {
120
+ const resource = this.registry.get(name);
121
+ const offered = this.available(name, actor);
122
+ const { action, ids, versions } = parse(request);
123
+ const known = BUILT_IN.includes(action) ||
124
+ Boolean(this.defined.get(resource.name)?.has(action));
125
+ if (!known)
126
+ throw new AdulaError(422, 'E_BULK', 'الإجراء الجماعي غير معروف');
127
+ if (!offered.some((option) => option.key === action))
128
+ throw forbiddenAction();
129
+ const result = { succeeded: [], failed: [] };
130
+ for (const id of ids) {
131
+ try {
132
+ await this.one(resource, actor, action, id, versions[id]);
133
+ result.succeeded.push(id);
134
+ }
135
+ catch (error) {
136
+ const failure = failureOf(error);
137
+ if (failure.code === 'E_BULK_ROW')
138
+ this.report(error, { resource: name, action, id });
139
+ result.failed.push({ id, ...failure });
140
+ }
141
+ }
142
+ return result;
143
+ }
144
+ async one(resource, actor, action, id, version) {
145
+ const name = resource.name;
146
+ if (action === 'restore')
147
+ return this.resources.restore(name, id, actor);
148
+ if (action === 'delete' || action === 'submit' || action === 'cancel')
149
+ return this.resources.transition(name, id, actor, action, version);
150
+ const definition = this.defined.get(name).get(action);
151
+ const ability = buildAbility(actor.rules, this.registry.all());
152
+ // Read and write under one row lock, so a concurrent edit between reading the stored
153
+ // values and saving them cannot be overwritten. Authorization stays in save().
154
+ return this.resources.transaction(async (trx) => {
155
+ // Authorize the row (organization scope and update conditions) before locking it, so
156
+ // an actor cannot hold locks on records outside their reach.
157
+ await this.resources.access(name, id, actor, 'update', trx);
158
+ const row = await trx(name).where({ id }).whereNull('deleted_at').forUpdate().first();
159
+ const stored = row ? fromRow(row, resource) : {};
160
+ const after = { ...stored, ...definition.values };
161
+ // The edit form resubmits the stored values the actor may write on this record (the
162
+ // same per-record checks save() applies); the action's values replace their fields,
163
+ // so validation sees a complete record. Attachments resend their stored id, which
164
+ // save() treats as unchanged.
165
+ const input = {};
166
+ for (const key of resource.form) {
167
+ const field = resource.fields[key];
168
+ if (field.type === 'hasMany' ||
169
+ field.sequence ||
170
+ !(key in stored) ||
171
+ !ability.can('update', subject(name, stored), key) ||
172
+ !ability.can('update', subject(name, after), key) ||
173
+ actor.permissionLevel < fieldLevel(resource, key))
174
+ continue;
175
+ input[key] = stored[key];
176
+ }
177
+ Object.assign(input, definition.values);
178
+ if (resource.version)
179
+ input.version = version;
180
+ return this.resources.save(name, actor, input, id, trx);
181
+ });
182
+ }
183
+ report(error, context) {
184
+ if (this.options.report)
185
+ return this.options.report(error, context);
186
+ // No record values: only where it happened and what kind of error it was.
187
+ console.error('Bulk action row failed', {
188
+ ...context,
189
+ error: error instanceof Error ? error.name : typeof error,
190
+ code: error?.code,
191
+ });
192
+ }
193
+ }
194
+ function builtIn(key) {
195
+ return {
196
+ key,
197
+ label: translate(`list.bulk.${key}`),
198
+ builtIn: true,
199
+ scope: key === 'restore' ? 'trash' : 'records',
200
+ confirm: key === 'restore' ? null : translate(`list.bulk.${key}Confirm`),
201
+ destructive: key === 'delete' || key === 'cancel',
202
+ };
203
+ }
204
+ function parse(request) {
205
+ const invalid = () => new AdulaError(422, 'E_BULK', 'اختر من سجل واحد إلى 100 سجل لتنفيذ الإجراء الجماعي');
206
+ if (!request || typeof request !== 'object')
207
+ throw invalid();
208
+ const action = request.action;
209
+ if (typeof action !== 'string' || !KEY.test(action))
210
+ throw new AdulaError(422, 'E_BULK', 'الإجراء الجماعي غير معروف');
211
+ const positive = (value) => {
212
+ const number = typeof value === 'string' && /^\d{1,10}$/.test(value) ? Number(value) : value;
213
+ return Number.isSafeInteger(number) && Number(number) > 0 && Number(number) <= 2147483647
214
+ ? Number(number)
215
+ : null;
216
+ };
217
+ if (!Array.isArray(request.ids) || !request.ids.length || request.ids.length > BULK_ID_LIMIT)
218
+ throw invalid();
219
+ const ids = request.ids.map(positive);
220
+ if (ids.some((id) => id === null) || new Set(ids).size !== ids.length)
221
+ throw invalid();
222
+ const versions = {};
223
+ if (request.versions !== undefined && request.versions !== null) {
224
+ if (typeof request.versions !== 'object' || Array.isArray(request.versions))
225
+ throw invalid();
226
+ for (const [key, value] of Object.entries(request.versions)) {
227
+ const id = positive(key);
228
+ const version = typeof value === 'string' && /^\d{1,10}$/.test(value) ? Number(value) : value;
229
+ if (id === null || !Number.isSafeInteger(version))
230
+ throw invalid();
231
+ versions[id] = version;
232
+ }
233
+ }
234
+ return { action, ids: ids, versions };
235
+ }
236
+ /** Catalog text for row refusals whose service message is a developer (English) string. */
237
+ const ROW_MESSAGES = {
238
+ E_DOCUMENT_STATE: 'errors.E_DOCUMENT_STATE',
239
+ E_DOCUMENT_LOCKED: 'errors.E_DOCUMENT_LOCKED.2',
240
+ };
241
+ /** The code and Arabic message of one refused row; unexpected errors stay generic. */
242
+ function failureOf(error) {
243
+ if (error instanceof AdulaError)
244
+ return {
245
+ code: error.code,
246
+ // Catalog or Arabic text is user text; a developer (English) message is replaced
247
+ // by the code's catalog message.
248
+ message: error.messageKey || /[؀-ۿ]/.test(error.message)
249
+ ? error.message
250
+ : translate(ROW_MESSAGES[error.code] ?? 'errors.E_BULK_ROW'),
251
+ };
252
+ const code = error?.code;
253
+ if (code === 'E_VALIDATION_ERROR') {
254
+ // VineJS messages are already in the request language (installCatalogMessages).
255
+ const [first] = (error.messages ?? []);
256
+ return {
257
+ code: 'E_VALIDATION',
258
+ message: typeof first?.message === 'string' && first.message
259
+ ? first.message
260
+ : translate('validation.invalid'),
261
+ };
262
+ }
263
+ if (code === '23505')
264
+ return { code: 'E_DUPLICATE', message: translate('errors.E_DUPLICATE') };
265
+ if (code === '23503')
266
+ return { code: 'E_RELATION', message: translate('errors.E_RELATION') };
267
+ return { code: 'E_BULK_ROW', message: translate('errors.E_BULK_ROW') };
268
+ }
@@ -0,0 +1,138 @@
1
+ import type { Knex } from 'knex';
2
+ import type { ResourceRegistry } from '../resource/registry.js';
3
+ import type { JsonValue, Label } from '../resource/types.js';
4
+ import type { Conditions } from '../auth/conditions.js';
5
+ import { type Actor } from '../auth/ability.js';
6
+ import type { ResourceService } from '../admin/resource_service.js';
7
+ export type DashboardMeasure = 'count' | {
8
+ sum: string;
9
+ };
10
+ export type DashboardWidgetDefinition = {
11
+ /** Unique within the dashboard: /^[a-z][a-zA-Z0-9_]*$/. */
12
+ key: string;
13
+ label: Label;
14
+ resource: string;
15
+ measure: DashboardMeasure;
16
+ /** One queryable field (or docStatus / orgUnitId) to group the measure by. */
17
+ groupBy?: string;
18
+ /** List filters, including the operators of ADR 046. */
19
+ filters?: Record<string, unknown>;
20
+ /** Role-rule style conditions, as in ResourceService.aggregate. */
21
+ where?: Conditions;
22
+ /**
23
+ * Offers the widget on the personal overview page (ADR 050) as `dashboard.<name>.<key>`;
24
+ * each user shows, hides and orders it there, and it appears only when they may read it.
25
+ */
26
+ overview?: boolean;
27
+ };
28
+ export type DashboardDefinition = {
29
+ /** /^[a-z][a-z0-9_]*$/, used in the URL /dashboards/:name. */
30
+ name: string;
31
+ label: Label;
32
+ description?: Label;
33
+ widgets: DashboardWidgetDefinition[];
34
+ };
35
+ export type DashboardSummary = {
36
+ name: string;
37
+ label: string;
38
+ };
39
+ export type DashboardGroupRow = {
40
+ value: JsonValue;
41
+ label: string;
42
+ measure: number | string | null;
43
+ };
44
+ export type DashboardWidgetResult = {
45
+ key: string;
46
+ label: string;
47
+ resource: string;
48
+ /** The list page of the widget's resource. */
49
+ href: string;
50
+ measure: DashboardMeasure;
51
+ /**
52
+ * count: an integer; integer, decimal and percent: a decimal string total (percent points
53
+ * for percent); money: minor units as a string.
54
+ */
55
+ format: DashboardFormat;
56
+ kind: 'total' | 'grouped';
57
+ groupBy: string | null;
58
+ value?: number | string | null;
59
+ rows?: DashboardGroupRow[];
60
+ truncated: boolean;
61
+ };
62
+ /** A widget the host draws itself on the overview (its data is already the viewer's own). */
63
+ export type OverviewBuiltin = {
64
+ id: string;
65
+ label: string;
66
+ };
67
+ /** A user's saved overview arrangement; ids they may not see are ignored on every read. */
68
+ export type OverviewLayout = {
69
+ order?: readonly string[];
70
+ hidden?: readonly string[];
71
+ };
72
+ export type OverviewWidget = {
73
+ id: string;
74
+ kind: 'builtin';
75
+ label: string;
76
+ hidden: boolean;
77
+ } | {
78
+ id: string;
79
+ kind: 'dashboard';
80
+ label: string;
81
+ hidden: boolean;
82
+ dashboard: string;
83
+ /** Computed for shown widgets only; null when hidden. */
84
+ data: DashboardWidgetResult | null;
85
+ };
86
+ export type OverviewState = {
87
+ /** Every widget the viewer may see, in their order; hidden ones carry no data. */
88
+ widgets: OverviewWidget[];
89
+ /** True when the saved arrangement differs from the default. */
90
+ customized: boolean;
91
+ };
92
+ export type DashboardResult = {
93
+ name: string;
94
+ label: string;
95
+ description: string | null;
96
+ widgets: DashboardWidgetResult[];
97
+ };
98
+ /** How a widget's measure reads: a count, or the sum's field type. */
99
+ export type DashboardFormat = 'count' | 'integer' | 'money' | 'decimal' | 'percent';
100
+ /** Declares an indicators page outside the resource type (ADR 046). */
101
+ export declare function defineDashboard(definition: DashboardDefinition): DashboardDefinition;
102
+ /**
103
+ * Renders dashboards as the viewer: every widget runs through ResourceService.aggregate
104
+ * with the viewer's rules, scope and field access, and a widget the viewer cannot read
105
+ * is omitted rather than failed.
106
+ */
107
+ export declare class Dashboards {
108
+ private db;
109
+ private registry;
110
+ private resources;
111
+ private dashboards;
112
+ constructor(db: Knex, registry: ResourceRegistry, resources: ResourceService, definitions?: readonly DashboardDefinition[]);
113
+ /**
114
+ * Checks dashboards against the registered resources: unique names, existing widget
115
+ * resources, integer, money, decimal or percent totals, groupable fields and filter or condition keys
116
+ * that exist on the resource. Call it when the application loads its definitions, so a
117
+ * bad definition fails at boot.
118
+ */
119
+ static validate(registry: ResourceRegistry, definitions: readonly DashboardDefinition[]): Map<string, DashboardDefinition>;
120
+ /**
121
+ * Whether the actor may read this widget: the resource is viewable and every field it
122
+ * totals, groups, filters or conditions on is one the actor may query.
123
+ */
124
+ private readable;
125
+ /** Dashboards with at least one widget the actor may read. */
126
+ list(actor: Actor): DashboardSummary[];
127
+ /**
128
+ * The personal overview (ADR 050): the host's built-in widgets and every `overview: true`
129
+ * dashboard widget the actor may read, arranged by the saved layout. Saved ids that are
130
+ * unknown or no longer readable are dropped, widgets added later join in default order, and
131
+ * only shown dashboard widgets are computed, so a stored id never reveals data.
132
+ */
133
+ overview(actor: Actor, saved: OverviewLayout | null | undefined, builtins?: readonly OverviewBuiltin[]): Promise<OverviewState>;
134
+ render(name: string, actor: Actor): Promise<DashboardResult>;
135
+ private widget;
136
+ /** Display labels of one grouped field: lookups, titles, user names, «نعم/لا». */
137
+ private labeller;
138
+ }