adula 2.2.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 (132) hide show
  1. package/README.md +1 -1
  2. package/build/agent/capabilities.md +2 -3
  3. package/build/agent/skills/adula-frontend-design/SKILL.md +5 -3
  4. package/build/commands/adula_assistant_prune.d.ts +10 -0
  5. package/build/commands/adula_assistant_prune.js +12 -0
  6. package/build/commands/backup_open.d.ts +16 -0
  7. package/build/commands/backup_open.js +48 -0
  8. package/build/commands/backup_restore_test.js +3 -0
  9. package/build/commands/backup_seal.d.ts +12 -0
  10. package/build/commands/backup_seal.js +36 -0
  11. package/build/commands/backup_services.d.ts +3 -1
  12. package/build/commands/doctor.js +18 -1
  13. package/build/commands/install.js +6 -0
  14. package/build/commands/main.d.ts +2 -2
  15. package/build/commands/main.js +6 -0
  16. package/build/commands/resource_migration.js +12 -3
  17. package/build/commands/resource_snapshot.js +18 -4
  18. package/build/database/migrations/1770000000051_comment_notifications.d.ts +5 -0
  19. package/build/database/migrations/1770000000051_comment_notifications.js +10 -0
  20. package/build/database/migrations/1770000000054_cloud_backups.d.ts +5 -0
  21. package/build/database/migrations/1770000000054_cloud_backups.js +10 -0
  22. package/build/database/migrations/1770000000055_ai_assistant.d.ts +5 -0
  23. package/build/database/migrations/1770000000055_ai_assistant.js +10 -0
  24. package/build/database/migrations/1770000000056_appearance.d.ts +5 -0
  25. package/build/database/migrations/1770000000056_appearance.js +10 -0
  26. package/build/index.d.ts +36 -6
  27. package/build/index.js +18 -3
  28. package/build/src/admin/controller.js +18 -4
  29. package/build/src/admin/resource/editor.d.ts +1 -0
  30. package/build/src/admin/resource/editor.js +26 -4
  31. package/build/src/admin/resource/export.d.ts +21 -4
  32. package/build/src/admin/resource/export.js +117 -36
  33. package/build/src/admin/resource/lifecycle.d.ts +2 -0
  34. package/build/src/admin/resource/query.d.ts +14 -3
  35. package/build/src/admin/resource/query.js +98 -11
  36. package/build/src/admin/resource/read.d.ts +3 -0
  37. package/build/src/admin/resource/read.js +75 -14
  38. package/build/src/admin/resource/shared.d.ts +16 -0
  39. package/build/src/admin/resource/shared.js +47 -4
  40. package/build/src/admin/resource/write.js +37 -8
  41. package/build/src/admin/resource_service.d.ts +17 -3
  42. package/build/src/admin/resource_service.js +16 -2
  43. package/build/src/admin/resource_tools.d.ts +111 -0
  44. package/build/src/admin/resource_tools.js +139 -0
  45. package/build/src/ai/assistant.d.ts +100 -0
  46. package/build/src/ai/assistant.js +487 -0
  47. package/build/src/ai/client.d.ts +49 -0
  48. package/build/src/ai/client.js +85 -0
  49. package/build/src/ai/prompt.d.ts +23 -0
  50. package/build/src/ai/prompt.js +83 -0
  51. package/build/src/ai/settings.d.ts +56 -0
  52. package/build/src/ai/settings.js +214 -0
  53. package/build/src/ai/usage.d.ts +16 -0
  54. package/build/src/ai/usage.js +33 -0
  55. package/build/src/appearance/appearance.d.ts +134 -0
  56. package/build/src/appearance/appearance.js +461 -0
  57. package/build/src/appearance/colors.d.ts +43 -0
  58. package/build/src/appearance/colors.js +99 -0
  59. package/build/src/appearance/logo.d.ts +32 -0
  60. package/build/src/appearance/logo.js +112 -0
  61. package/build/src/auth/actor_store.d.ts +5 -1
  62. package/build/src/auth/actor_store.js +8 -4
  63. package/build/src/backups/archive.d.ts +52 -0
  64. package/build/src/backups/archive.js +362 -0
  65. package/build/src/backups/cloud_backups.d.ts +133 -0
  66. package/build/src/backups/cloud_backups.js +650 -0
  67. package/build/src/backups/doctor.d.ts +7 -0
  68. package/build/src/backups/doctor.js +55 -0
  69. package/build/src/backups/providers.d.ts +81 -0
  70. package/build/src/backups/providers.js +470 -0
  71. package/build/src/collaboration/assignments.d.ts +24 -2
  72. package/build/src/collaboration/assignments.js +88 -32
  73. package/build/src/collaboration/record_collaboration.d.ts +19 -1
  74. package/build/src/collaboration/record_collaboration.js +109 -37
  75. package/build/src/collaboration/shared.d.ts +10 -2
  76. package/build/src/collaboration/shared.js +14 -1
  77. package/build/src/commands/capabilities.js +1 -1
  78. package/build/src/commands/doctor.d.ts +10 -0
  79. package/build/src/commands/doctor.js +31 -1
  80. package/build/src/commands/snapshot.d.ts +14 -0
  81. package/build/src/commands/snapshot.js +23 -0
  82. package/build/src/core/administrators.js +19 -3
  83. package/build/src/core/avatar_image.d.ts +33 -0
  84. package/build/src/core/avatar_image.js +278 -0
  85. package/build/src/core/mail_settings.d.ts +83 -0
  86. package/build/src/core/mail_settings.js +274 -0
  87. package/build/src/core/mail_test.d.ts +16 -0
  88. package/build/src/core/mail_test.js +81 -6
  89. package/build/src/core/message_templates.d.ts +8 -0
  90. package/build/src/core/message_templates.js +12 -3
  91. package/build/src/core/roles.js +24 -7
  92. package/build/src/core/saved_views.js +55 -24
  93. package/build/src/core/settings.d.ts +2 -2
  94. package/build/src/core/settings.js +15 -4
  95. package/build/src/core/ui_preferences.d.ts +6 -0
  96. package/build/src/core/ui_preferences.js +12 -1
  97. package/build/src/core/user_avatars.d.ts +79 -0
  98. package/build/src/core/user_avatars.js +186 -0
  99. package/build/src/core/user_preferences.d.ts +36 -0
  100. package/build/src/core/user_preferences.js +125 -0
  101. package/build/src/core/users.d.ts +2 -0
  102. package/build/src/core/users.js +4 -2
  103. package/build/src/database/schema.d.ts +46 -0
  104. package/build/src/database/schema.js +182 -0
  105. package/build/src/database/schema_diff.d.ts +16 -2
  106. package/build/src/database/schema_diff.js +40 -7
  107. package/build/src/integrations/api_client.js +18 -3
  108. package/build/src/integrations/imports.d.ts +16 -4
  109. package/build/src/integrations/imports.js +36 -8
  110. package/build/src/integrations/openapi.js +15 -4
  111. package/build/src/integrations/outbound.d.ts +43 -0
  112. package/build/src/integrations/outbound.js +98 -17
  113. package/build/src/integrations/print.d.ts +2 -1
  114. package/build/src/integrations/print.js +7 -3
  115. package/build/src/integrations/print_templates.js +3 -1
  116. package/build/src/integrations/xlsx.d.ts +41 -0
  117. package/build/src/integrations/xlsx.js +519 -0
  118. package/build/src/languages/ar.js +22 -0
  119. package/build/src/languages/ar_ui.d.ts +143 -14
  120. package/build/src/languages/ar_ui.js +145 -14
  121. package/build/src/languages/en.js +22 -0
  122. package/build/src/languages/en_ui.js +144 -14
  123. package/build/src/lists/dashboards.d.ts +42 -0
  124. package/build/src/lists/dashboards.js +49 -0
  125. package/build/src/mcp/tools.js +9 -104
  126. package/build/src/resource/define_resource.js +7 -0
  127. package/build/src/resource/types.d.ts +9 -0
  128. package/build/src/workflows/delegations.d.ts +15 -1
  129. package/build/src/workflows/delegations.js +49 -10
  130. package/build/src/workflows/engine.d.ts +11 -0
  131. package/build/src/workflows/engine.js +29 -7
  132. package/package.json +5 -1
@@ -3,6 +3,7 @@ import { logActivity } from '../core/activity.js';
3
3
  import { notifyWithTemplate } from '../core/message_templates.js';
4
4
  import { escapeLike } from '../core/like.js';
5
5
  import { assertTimeZone, calendarDate } from './calendar.js';
6
+ import { avatarUrlFor, avatarVersionColumn } from '../core/user_avatars.js';
6
7
  const DATE = /^\d{4}-\d{2}-\d{2}$/;
7
8
  const MAX_DAYS = 366;
8
9
  const NOTE_LIMIT = 500;
@@ -51,6 +52,20 @@ export function actionableBy(db, actorId, today) {
51
52
  .whereIn('a.on_behalf_of', delegators()))));
52
53
  });
53
54
  }
55
+ /**
56
+ * For a users query aliased `u`: members of a unit on the same organization path as one of
57
+ * `userId`'s units (that unit, an ancestor or a descendant), the tree rule of user fields.
58
+ * A user without units shares a path with nobody.
59
+ */
60
+ function sharesOrgPath(db, userId) {
61
+ return db('user_org_units as m')
62
+ .join('org_units as o', 'o.id', 'm.org_unit_id')
63
+ .whereRaw('m.user_id = u.id')
64
+ .whereExists(db('user_org_units as dm')
65
+ .join('org_units as d', 'd.id', 'dm.org_unit_id')
66
+ .where('dm.user_id', userId)
67
+ .whereRaw('(o.path <@ d.path OR o.path @> d.path)'));
68
+ }
54
69
  /** The SQL flag `delegation_active` for an assignments query aliased `a`. */
55
70
  export function delegationActive(db, today) {
56
71
  return db.raw('EXISTS (SELECT 1 FROM workflow_delegations wd WHERE wd.id = a.delegation_id AND wd.revoked_at IS NULL AND wd.starts_on <= ?::date AND wd.ends_on >= ?::date) AS delegation_active', [today, today]);
@@ -86,23 +101,32 @@ export class WorkflowDelegations {
86
101
  * Active users a delegate can be chosen from (id and display name only), at most 20.
87
102
  * The search needs two characters and matches names, or an e-mail address exactly, so
88
103
  * the picker cannot list the whole user directory or probe e-mail fragments.
104
+ * `exceptId` is the delegator: only users on the delegator's organization path (the same
105
+ * unit, an ancestor or a descendant) are offered, by name or e-mail, the rule create()
106
+ * applies to a user's own delegation. 0 (administrators managing anyone's delegation)
107
+ * offers every active user.
89
108
  */
90
109
  async candidates(search, exceptId) {
91
110
  const text = typeof search === 'string' ? search.trim().slice(0, 100) : '';
92
111
  if (text.length < 2)
93
112
  return [];
94
- const rows = await this.db('users')
95
- .whereNull('disabled_at')
96
- .whereNot('id', exceptId)
113
+ const rows = await this.db('users as u')
114
+ .whereNull('u.disabled_at')
115
+ .whereNot('u.id', exceptId)
116
+ .modify((q) => {
117
+ if (exceptId > 0)
118
+ q.whereExists(sharesOrgPath(this.db, exceptId));
119
+ })
97
120
  .where((q) => q
98
- .whereILike('full_name', `%${escapeLike(text)}%`)
99
- .orWhereRaw('lower(email) = lower(?)', [text]))
100
- .orderBy('full_name')
121
+ .whereILike('u.full_name', `%${escapeLike(text)}%`)
122
+ .orWhereRaw('lower(u.email) = lower(?)', [text]))
123
+ .orderBy('u.full_name')
101
124
  .limit(20)
102
- .select('id', 'full_name', 'email');
125
+ .select('u.id', 'u.full_name', 'u.email', avatarVersionColumn(this.db, 'u.id'));
103
126
  return rows.map((row) => ({
104
127
  id: Number(row.id),
105
128
  name: String(row.full_name ?? row.email ?? `#${row.id}`),
129
+ avatarUrl: avatarUrlFor(row.id, row.avatar_version),
106
130
  }));
107
131
  }
108
132
  /** Every delegation that has not ended, for administrators. */
@@ -126,7 +150,12 @@ export class WorkflowDelegations {
126
150
  .first('id', 'delegate_id');
127
151
  return row ? { id: Number(row.id), delegateId: Number(row.delegate_id) } : null;
128
152
  }
129
- /** Creates a delegation; the caller decides whether `createdBy` may act for the delegator. */
153
+ /**
154
+ * Creates a delegation; the caller decides whether `createdBy` may act for the delegator.
155
+ * When `createdBy` is the delegator, the delegate must be on their organization path (see
156
+ * candidates()); a caller acting for someone else (administrators) may choose any active
157
+ * user.
158
+ */
130
159
  async create(input, createdBy) {
131
160
  const delegatorId = Number(input.delegatorId);
132
161
  const delegateId = Number(input.delegateId);
@@ -162,7 +191,15 @@ export class WorkflowDelegations {
162
191
  const delegate = users.find((user) => Number(user.id) === delegateId);
163
192
  if (!delegator)
164
193
  throw new AdulaError(422, 'E_DELEGATION_USER', 'المستخدم غير صالح');
165
- if (!delegate || delegate.disabled_at)
194
+ // A user's own delegation goes to someone on their organization path, the picker's
195
+ // rule; the same answer as an unknown user, so ids cannot be probed for names.
196
+ if (!delegate ||
197
+ delegate.disabled_at ||
198
+ (createdBy === delegatorId &&
199
+ !(await trx('users as u')
200
+ .where('u.id', delegateId)
201
+ .whereExists(sharesOrgPath(trx, delegatorId))
202
+ .first('u.id'))))
166
203
  throw new AdulaError(422, 'E_DELEGATE', 'المفوَّض إليه غير موجود أو معطّل');
167
204
  const overlap = await trx('workflow_delegations')
168
205
  .where('delegator_id', delegatorId)
@@ -251,7 +288,7 @@ export class WorkflowDelegations {
251
288
  return db('workflow_delegations as d')
252
289
  .leftJoin('users as f', 'f.id', 'd.delegator_id')
253
290
  .leftJoin('users as t', 't.id', 'd.delegate_id')
254
- .select('d.*', 'f.full_name as delegator_name', 't.full_name as delegate_name');
291
+ .select('d.*', 'f.full_name as delegator_name', 't.full_name as delegate_name', avatarVersionColumn(db, 'd.delegator_id', 'delegator_avatar'), avatarVersionColumn(db, 'd.delegate_id', 'delegate_avatar'));
255
292
  }
256
293
  present(row) {
257
294
  const today = this.today();
@@ -261,8 +298,10 @@ export class WorkflowDelegations {
261
298
  id: Number(row.id),
262
299
  delegatorId: Number(row.delegator_id),
263
300
  delegatorName: row.delegator_name ? String(row.delegator_name) : null,
301
+ delegatorAvatarUrl: avatarUrlFor(row.delegator_id, row.delegator_avatar),
264
302
  delegateId: Number(row.delegate_id),
265
303
  delegateName: row.delegate_name ? String(row.delegate_name) : null,
304
+ delegateAvatarUrl: avatarUrlFor(row.delegate_id, row.delegate_avatar),
266
305
  startsOn,
267
306
  endsOn,
268
307
  note: row.note ? String(row.note) : null,
@@ -34,7 +34,11 @@ export type WorkflowRun = {
34
34
  history: {
35
35
  step: string | null;
36
36
  event: string;
37
+ /** The user who acted; null for system steps («النظام»). */
38
+ actorId?: number | null;
37
39
  actorName: string | null;
40
+ /** The actor's versioned picture URL, or null without one (ADR 050). */
41
+ actorAvatarUrl?: string | null;
38
42
  /** Set when the actor decided as a delegate (ADR 044). */
39
43
  onBehalfOfName?: string | null;
40
44
  detail: {
@@ -165,6 +169,13 @@ export declare class WorkflowEngine {
165
169
  private recipients;
166
170
  /** A role reference is its stable key; for compatibility a display name also matches. */
167
171
  private roleId;
172
+ /** permits() on a given connection: denial (403/404) is false, other errors propagate. */
173
+ private viewable;
174
+ /**
175
+ * Whether a user may view the run's record, read on `db`. Callers inside a transaction
176
+ * pass it: a second pool connection while the run is locked lets concurrent decisions,
177
+ * submissions and worker passes exhaust the pool.
178
+ */
168
179
  private canView;
169
180
  private log;
170
181
  /**
@@ -8,6 +8,7 @@ import { WorkflowDelegations, actionableBy } from './delegations.js';
8
8
  import { addDays, assertTimeZone, calendarDate } from './calendar.js';
9
9
  import { nextStep, } from './define_workflow.js';
10
10
  import { slaState } from './sla.js';
11
+ import { avatarUrlFor, avatarVersionColumn } from '../core/user_avatars.js';
11
12
  export { slaState } from './sla.js';
12
13
  const MAX_STEPS_PER_TICK = 25;
13
14
  const HOUR = 3600000;
@@ -266,7 +267,7 @@ export class WorkflowEngine {
266
267
  const users = await this.recipients(trx, run, context, assignees);
267
268
  const eligible = [];
268
269
  for (const userId of users)
269
- if (await this.canView(run.resource, run.record_id, userId))
270
+ if (await this.canView(trx, run.resource, run.record_id, userId))
270
271
  eligible.push(userId);
271
272
  // Waiting on nobody would hide the run for hours of retries; fail it where admins look.
272
273
  if (!eligible.length)
@@ -353,7 +354,9 @@ export class WorkflowEngine {
353
354
  const run = await trx('workflow_runs').where('id', runId).forUpdate().first();
354
355
  if (!run)
355
356
  throw new AdulaError(404, 'E_WORKFLOW_NOT_FOUND', 'التدفق غير موجود');
356
- if (!(await this.resources.permits(run.resource, Number(run.record_id), actor)))
357
+ // The record check runs on this transaction: a second pool connection while the run
358
+ // is locked would let concurrent decisions exhaust the pool.
359
+ if (!(await this.viewable(run, actor, trx)))
357
360
  throw new AdulaError(404, 'E_WORKFLOW_NOT_FOUND', 'التدفق غير موجود');
358
361
  // The actor's own approval first, then one they hold for a delegator (ADR 044).
359
362
  const assignment = await trx('assignments as a')
@@ -534,7 +537,7 @@ export class WorkflowEngine {
534
537
  // The requester never receives their own request (ADR 044, no self-approval).
535
538
  if (!late.has(userId) &&
536
539
  userId !== Number(run.started_by) &&
537
- (await this.canView(run.resource, run.record_id, userId)))
540
+ (await this.canView(trx, run.resource, run.record_id, userId)))
538
541
  targets.push(userId);
539
542
  }
540
543
  catch (error) {
@@ -708,7 +711,7 @@ export class WorkflowEngine {
708
711
  .where('e.run_id', row.id)
709
712
  .orderBy('e.id')
710
713
  .limit(200)
711
- .select('e.*', 'u.full_name as actor_name');
714
+ .select('e.*', 'u.full_name as actor_name', avatarVersionColumn(this.db, 'e.actor_id'));
712
715
  const mine = actor
713
716
  ? await this.db('assignments as a')
714
717
  .where({
@@ -752,7 +755,9 @@ export class WorkflowEngine {
752
755
  history: history.map((event) => ({
753
756
  step: event.step ?? null,
754
757
  event: String(event.event),
758
+ actorId: event.actor_id === null || event.actor_id === undefined ? null : Number(event.actor_id),
755
759
  actorName: event.actor_name ? String(event.actor_name) : null,
760
+ actorAvatarUrl: avatarUrlFor(event.actor_id, event.avatar_version),
756
761
  onBehalfOfName: typeof event.detail?.onBehalfOfName === 'string' ? event.detail.onBehalfOfName : null,
757
762
  detail: (event.detail ?? {}),
758
763
  at: new Date(event.created_at).toISOString(),
@@ -931,8 +936,25 @@ export class WorkflowEngine {
931
936
  const byName = await db('roles').where('name', reference).first('id');
932
937
  return byName ? Number(byName.id) : null;
933
938
  }
934
- canView(resource, id, userId) {
935
- return canView(this.db, this.resources, this.actors, resource, id, userId);
939
+ /** permits() on a given connection: denial (403/404) is false, other errors propagate. */
940
+ async viewable(run, actor, db) {
941
+ try {
942
+ await this.resources.access(String(run.resource), Number(run.record_id), actor, 'view', db);
943
+ return true;
944
+ }
945
+ catch (error) {
946
+ if (error instanceof AdulaError && (error.status === 403 || error.status === 404))
947
+ return false;
948
+ throw error;
949
+ }
950
+ }
951
+ /**
952
+ * Whether a user may view the run's record, read on `db`. Callers inside a transaction
953
+ * pass it: a second pool connection while the run is locked lets concurrent decisions,
954
+ * submissions and worker passes exhaust the pool.
955
+ */
956
+ canView(db, resource, id, userId) {
957
+ return canView(db, this.resources, this.actors, resource, id, userId);
936
958
  }
937
959
  async log(db, runId, step, event, actorId, detail) {
938
960
  await db('workflow_events').insert({
@@ -1042,7 +1064,7 @@ export class WorkflowEngine {
1042
1064
  return null;
1043
1065
  if (await this.#delegations.active(active.delegateId, db))
1044
1066
  return null;
1045
- if (!(await this.canView(run.resource, run.record_id, active.delegateId)))
1067
+ if (!(await this.canView(db, run.resource, run.record_id, active.delegateId)))
1046
1068
  return null;
1047
1069
  return active;
1048
1070
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "adula",
3
3
  "description": "Resource-driven business applications for AdonisJS 7",
4
- "version": "2.2.0",
4
+ "version": "2.3.0",
5
5
  "engines": {
6
6
  "node": ">=24.0.0"
7
7
  },
@@ -77,9 +77,13 @@
77
77
  "prettier": "@adonisjs/prettier-config",
78
78
  "dependencies": {
79
79
  "@casl/ability": "7.0.1",
80
+ "fflate": "0.8.3",
80
81
  "knex": "3.3.0",
81
82
  "otpauth": "9.5.2",
82
83
  "papaparse": "5.7.0",
84
+ "read-excel-file": "9.3.12",
85
+ "sharp": "0.35.5",
86
+ "write-excel-file": "4.1.1",
83
87
  "xstate": "5.33.2"
84
88
  },
85
89
  "repository": {