@mfdaves/okf-odoo 0.0.0-stage → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/catalog.js ADDED
@@ -0,0 +1,704 @@
1
+ "use strict";
2
+
3
+ // Reads the technical catalog of an Odoo database into one normalised shape
4
+ // for Odoo 18, 19 and 20. Every value comes from a field named in the
5
+ // allowlist of the server's version; nothing else a response carries reaches
6
+ // the catalog.
7
+
8
+ const { OdooError } = require("./odoo");
9
+
10
+ const SECTIONS = Object.freeze([
11
+ "modules",
12
+ "models",
13
+ "access",
14
+ "groups",
15
+ "menus",
16
+ "crons",
17
+ "reports",
18
+ "serverActions",
19
+ ]);
20
+ const DEFAULT_SECTIONS = Object.freeze(SECTIONS.filter((section) => section !== "serverActions"));
21
+ // These are rendered on model concepts, so they need the models section.
22
+ const MODEL_SECTIONS = Object.freeze(["access", "crons", "reports", "serverActions"]);
23
+
24
+ // The allowlist: the only fields ever requested, per Odoo model, as of Odoo 19.
25
+ const BASE_FIELDS = Object.freeze({
26
+ "ir.module.module": [
27
+ "name",
28
+ "shortdesc",
29
+ "summary",
30
+ "latest_version",
31
+ "installed_version",
32
+ "category_id",
33
+ "application",
34
+ "auto_install",
35
+ "license",
36
+ ],
37
+ "ir.module.module.dependency": ["module_id", "name"],
38
+ "ir.model": ["model", "name", "modules", "transient", "abstract", "state"],
39
+ "ir.model.data": ["module", "name", "res_id"],
40
+ "ir.model.fields": [
41
+ "model_id",
42
+ "name",
43
+ "field_description",
44
+ "ttype",
45
+ "relation",
46
+ "relation_field",
47
+ "required",
48
+ "readonly",
49
+ "store",
50
+ "index",
51
+ "translate",
52
+ "related",
53
+ "help",
54
+ "state",
55
+ "modules",
56
+ "groups",
57
+ "company_dependent",
58
+ "on_delete",
59
+ "selectable",
60
+ ],
61
+ "ir.model.fields.selection": ["field_id", "value", "name", "sequence"],
62
+ "ir.access": ["name", "model_id", "group_id", "operation", "domain", "kind"],
63
+ "ir.model.access": [
64
+ "name",
65
+ "model_id",
66
+ "group_id",
67
+ "perm_read",
68
+ "perm_write",
69
+ "perm_create",
70
+ "perm_unlink",
71
+ ],
72
+ "ir.rule": [
73
+ "name",
74
+ "model_id",
75
+ "groups",
76
+ "domain_force",
77
+ "perm_read",
78
+ "perm_write",
79
+ "perm_create",
80
+ "perm_unlink",
81
+ ],
82
+ "res.groups": ["name", "full_name", "comment", "share", "privilege_id", "implied_ids"],
83
+ "ir.ui.menu": ["name", "parent_id", "action", "sequence", "group_ids"],
84
+ "ir.actions.act_window": ["name", "res_model"],
85
+ "ir.cron": ["cron_name", "model_id", "interval_number", "interval_type", "active", "priority"],
86
+ "ir.actions.report": ["name", "model", "report_type", "report_name", "group_ids"],
87
+ "ir.actions.server": ["name", "model_id", "state", "binding_model_id", "group_ids"],
88
+ });
89
+
90
+ function withoutModels(fields, ...models) {
91
+ return Object.fromEntries(Object.entries(fields).filter(([model]) => !models.includes(model)));
92
+ }
93
+
94
+ function renamed(list, from, to) {
95
+ return list.map((field) => (field === from ? to : field)).filter(Boolean);
96
+ }
97
+
98
+ // What differs between the supported versions, measured on live servers.
99
+ // Odoo 18 has no ir.model.abstract, groups by application category, and
100
+ // groups_id where 19 has group_ids; Odoo 20 replaces ir.model.access and
101
+ // ir.rule with ir.access. Odoo 18 and 19 leave ir.model.fields.groups empty
102
+ // for fields defined in code, so their field groups say nothing.
103
+ const VERSIONS = Object.freeze({
104
+ 18: Object.freeze({
105
+ accessModel: "ir.model.access",
106
+ fieldGroups: false,
107
+ groupCategory: "category_id",
108
+ groupsField: "groups_id",
109
+ fields: Object.freeze({
110
+ ...withoutModels(BASE_FIELDS, "ir.access"),
111
+ "ir.model": renamed(BASE_FIELDS["ir.model"], "abstract", null),
112
+ "res.groups": renamed(BASE_FIELDS["res.groups"], "privilege_id", "category_id"),
113
+ "ir.ui.menu": renamed(BASE_FIELDS["ir.ui.menu"], "group_ids", "groups_id"),
114
+ "ir.actions.report": renamed(BASE_FIELDS["ir.actions.report"], "group_ids", "groups_id"),
115
+ "ir.actions.server": renamed(BASE_FIELDS["ir.actions.server"], "group_ids", "groups_id"),
116
+ }),
117
+ }),
118
+ 19: Object.freeze({
119
+ accessModel: "ir.model.access",
120
+ fieldGroups: false,
121
+ groupCategory: "privilege_id",
122
+ groupsField: "group_ids",
123
+ fields: Object.freeze(withoutModels(BASE_FIELDS, "ir.access")),
124
+ }),
125
+ 20: Object.freeze({
126
+ accessModel: "ir.access",
127
+ fieldGroups: true,
128
+ groupCategory: "privilege_id",
129
+ groupsField: "group_ids",
130
+ fields: Object.freeze(withoutModels(BASE_FIELDS, "ir.model.access", "ir.rule")),
131
+ }),
132
+ });
133
+ const SUPPORTED_VERSIONS = Object.freeze(Object.keys(VERSIONS));
134
+
135
+ function fieldsFor(version) {
136
+ const shape = VERSIONS[version];
137
+ if (!shape) throw new TypeError(`Unsupported Odoo version: ${version}.`);
138
+ return shape.fields;
139
+ }
140
+
141
+ class CatalogError extends Error {
142
+ constructor(code, section, cause) {
143
+ const reason =
144
+ code === "access_denied"
145
+ ? `The API key's user may not read the ${section} section.`
146
+ : `Reading the ${section} section failed: ${cause ? cause.message : code}`;
147
+ super(reason);
148
+ this.name = "CatalogError";
149
+ this.code = code;
150
+ this.section = section;
151
+ }
152
+ }
153
+
154
+ function text(value) {
155
+ return typeof value === "string" && value.trim() ? value.trim() : null;
156
+ }
157
+
158
+ function many2oneId(value) {
159
+ return Array.isArray(value) && Number.isInteger(value[0]) ? value[0] : null;
160
+ }
161
+
162
+ function many2oneName(value) {
163
+ return Array.isArray(value) ? text(value[1]) : null;
164
+ }
165
+
166
+ function ids(value) {
167
+ return Array.isArray(value) ? value.filter(Number.isInteger) : [];
168
+ }
169
+
170
+ function moduleList(value) {
171
+ return [
172
+ ...new Set(
173
+ String(value || "")
174
+ .split(",")
175
+ .map((name) => name.trim())
176
+ .filter(Boolean),
177
+ ),
178
+ ].sort();
179
+ }
180
+
181
+ function compare(left, right) {
182
+ return left < right ? -1 : left > right ? 1 : 0;
183
+ }
184
+
185
+ // Orders by the given keys, then by the whole entry, so the result never
186
+ // depends on the order Odoo returned the rows in.
187
+ function byKey(...keys) {
188
+ return (left, right) => {
189
+ for (const key of keys) {
190
+ const order = compare(key(left), key(right));
191
+ if (order) return order;
192
+ }
193
+ return compare(JSON.stringify(left), JSON.stringify(right));
194
+ };
195
+ }
196
+
197
+ function patternMatcher(patterns) {
198
+ const expressions = (patterns || []).map(
199
+ (pattern) => new RegExp(`^${pattern.split("*").map(escapeRegExp).join(".*")}$`),
200
+ );
201
+ return (name) => expressions.some((expression) => expression.test(name));
202
+ }
203
+
204
+ function escapeRegExp(value) {
205
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
206
+ }
207
+
208
+ function operationsFromPerms(row) {
209
+ return [
210
+ row.perm_create ? "c" : "",
211
+ row.perm_read ? "r" : "",
212
+ row.perm_write ? "u" : "",
213
+ row.perm_unlink ? "d" : "",
214
+ ].join("");
215
+ }
216
+
217
+ // Odoo 19 stores a boolean (indexed or not); Odoo 20 names the index kind.
218
+ function indexKind(value) {
219
+ if (value === true) return "indexed";
220
+ return text(value);
221
+ }
222
+
223
+ function normalizeOptions(options = {}) {
224
+ const sections = Array.isArray(options.sections) ? options.sections : DEFAULT_SECTIONS;
225
+ return {
226
+ sections: new Set(sections),
227
+ include: options.modules?.include?.length ? options.modules.include : null,
228
+ exclude: options.modules?.exclude || [],
229
+ otherModules: options.otherModules || "summary",
230
+ lang: options.lang || "en_US",
231
+ };
232
+ }
233
+
234
+ async function readCatalog(client, options = {}) {
235
+ if (!client || typeof client.searchRead !== "function") {
236
+ throw new TypeError("An Odoo client with searchRead() is required.");
237
+ }
238
+ const config = normalizeOptions(options);
239
+ const has = (section) => config.sections.has(section);
240
+ const orphan = MODEL_SECTIONS.find((section) => has(section) && !has("models"));
241
+ if (orphan) throw new TypeError(`The ${orphan} section requires the models section.`);
242
+ const context = { lang: config.lang };
243
+ let shape = null;
244
+ const read = (model, domain, extra = {}) =>
245
+ client.searchRead(model, {
246
+ domain,
247
+ fields: (shape ? shape.fields : BASE_FIELDS)[model],
248
+ context: { ...context, ...extra },
249
+ });
250
+ const included = config.include ? patternMatcher(config.include) : () => true;
251
+ const excluded = patternMatcher(config.exclude);
252
+ const selectedModule = (name) => included(name) && !excluded(name);
253
+
254
+ async function inSection(section, work) {
255
+ try {
256
+ return await work();
257
+ } catch (error) {
258
+ if (error instanceof CatalogError) throw error;
259
+ if (error instanceof OdooError) throw new CatalogError(error.code, section, error);
260
+ throw error;
261
+ }
262
+ }
263
+
264
+ // Lookups shared by several sections, read once by whichever needs them first.
265
+ const memo = new Map();
266
+ const once = (key, work) => {
267
+ if (!memo.has(key)) memo.set(key, work());
268
+ return memo.get(key);
269
+ };
270
+ const xmlids = (model) =>
271
+ once(`xmlids:${model}`, async () => {
272
+ const rows = await read("ir.model.data", [["model", "=", model]]);
273
+ const byId = new Map();
274
+ rows
275
+ .slice()
276
+ .sort(byKey((row) => row.id))
277
+ .forEach((row) => {
278
+ const resId = Number(row.res_id);
279
+ if (!byId.has(resId)) byId.set(resId, `${row.module}.${row.name}`);
280
+ });
281
+ return byId;
282
+ });
283
+ const groupIndex = () =>
284
+ once("groups", async () => {
285
+ const [rows, keys] = await Promise.all([read("res.groups", []), xmlids("res.groups")]);
286
+ const index = new Map();
287
+ rows.forEach((row) => {
288
+ const xmlid = keys.get(row.id) || null;
289
+ index.set(row.id, {
290
+ key: xmlid || `group-${row.id}`,
291
+ xmlid,
292
+ name: text(row.name) || `Group ${row.id}`,
293
+ fullName: text(row.full_name) || text(row.name) || `Group ${row.id}`,
294
+ privilege: many2oneName(row[shape.groupCategory]),
295
+ comment: text(row.comment),
296
+ share: row.share === true,
297
+ impliedIds: ids(row.implied_ids),
298
+ });
299
+ });
300
+ return index;
301
+ });
302
+ const groupRefs = async (value) => {
303
+ const index = await groupIndex();
304
+ return ids(value)
305
+ .map((id) => index.get(id))
306
+ .filter(Boolean)
307
+ .map((group) => ({ key: group.key, name: group.fullName }))
308
+ .sort(byKey((group) => group.key));
309
+ };
310
+
311
+ // The installed version of base names the server's major version, which
312
+ // decides the allowlist. Any internal user can read it.
313
+ const version = await inSection("version", async () => {
314
+ const [base] = await read("ir.module.module", [["name", "=", "base"]]);
315
+ const major = /^(\d+)\./.exec(String(base?.latest_version || ""))?.[1] || "unknown";
316
+ if (!VERSIONS[major]) throw new CatalogError("unsupported_odoo_version", "version");
317
+ return major;
318
+ });
319
+ shape = VERSIONS[version];
320
+
321
+ const catalog = {
322
+ sections: SECTIONS.filter(has),
323
+ odooVersion: version,
324
+ accessModel: shape.accessModel,
325
+ fieldGroups: shape.fieldGroups,
326
+ modules: [],
327
+ modelIndex: [],
328
+ models: [],
329
+ groups: [],
330
+ apps: [],
331
+ };
332
+
333
+ // Models, their fields and the module that defines each of them.
334
+ let modelById = new Map();
335
+ let selectedById = new Map();
336
+ if (has("models")) {
337
+ await inSection("models", async () => {
338
+ const [rows, data] = await Promise.all([
339
+ read("ir.model", []),
340
+ read("ir.model.data", [["model", "=", "ir.model"]]),
341
+ ]);
342
+ const owners = new Map();
343
+ data
344
+ .slice()
345
+ .sort(byKey((row) => row.id))
346
+ .forEach((row) => {
347
+ const resId = Number(row.res_id);
348
+ if (!owners.has(resId)) owners.set(resId, []);
349
+ owners.get(resId).push(String(row.module));
350
+ });
351
+ const abstractKnown = shape.fields["ir.model"].includes("abstract");
352
+ catalog.modelIndex = rows
353
+ .map((row) => {
354
+ const modules = moduleList(row.modules);
355
+ // The first module to register the model defines it; later ones extend it.
356
+ const definedIn = (owners.get(row.id) || []).find((name) => modules.includes(name));
357
+ return {
358
+ id: row.id,
359
+ model: String(row.model),
360
+ name: text(row.name) || String(row.model),
361
+ // Odoo 18 does not record which models are abstract.
362
+ kind: row.transient
363
+ ? "transient"
364
+ : !abstractKnown
365
+ ? null
366
+ : row.abstract
367
+ ? "abstract"
368
+ : "model",
369
+ custom: row.state === "manual",
370
+ modules,
371
+ definedIn: definedIn || null,
372
+ };
373
+ })
374
+ .sort(byKey((model) => model.model));
375
+ modelById = new Map(catalog.modelIndex.map((model) => [model.id, model]));
376
+
377
+ const selected = catalog.modelIndex.filter((model) =>
378
+ model.modules.length ? model.modules.some(selectedModule) : !config.include,
379
+ );
380
+ selectedById = new Map(
381
+ selected.map((model) => [
382
+ model.id,
383
+ {
384
+ model: model.model,
385
+ name: model.name,
386
+ kind: model.kind,
387
+ custom: model.custom,
388
+ modules: model.modules,
389
+ definedIn: model.definedIn,
390
+ fields: [],
391
+ access: [],
392
+ crons: [],
393
+ reports: [],
394
+ serverActions: [],
395
+ },
396
+ ]),
397
+ );
398
+ catalog.models = [...selectedById.values()];
399
+ if (!selected.length) return;
400
+
401
+ const fieldRows = await read("ir.model.fields", [
402
+ ["model_id", "in", selected.map((model) => model.id)],
403
+ ]);
404
+ const selectionIds = fieldRows
405
+ .filter((row) => row.ttype === "selection")
406
+ .map((row) => row.id);
407
+ const selectionRows = selectionIds.length
408
+ ? await read("ir.model.fields.selection", [["field_id", "in", selectionIds]])
409
+ : [];
410
+ const selections = new Map();
411
+ selectionRows
412
+ .slice()
413
+ .sort(
414
+ byKey(
415
+ (row) => Number(row.sequence) || 0,
416
+ (row) => row.id,
417
+ ),
418
+ )
419
+ .forEach((row) => {
420
+ const fieldId = many2oneId(row.field_id);
421
+ if (!selections.has(fieldId)) selections.set(fieldId, []);
422
+ selections.get(fieldId).push({ value: String(row.value), label: text(row.name) });
423
+ });
424
+ for (const row of fieldRows) {
425
+ const model = selectedById.get(many2oneId(row.model_id));
426
+ if (!model) continue;
427
+ model.fields.push({
428
+ name: String(row.name),
429
+ label: text(row.field_description),
430
+ type: String(row.ttype),
431
+ relation: text(row.relation),
432
+ relationField: text(row.relation_field),
433
+ required: row.required === true,
434
+ readonly: row.readonly === true,
435
+ stored: row.store === true,
436
+ index: indexKind(row.index),
437
+ // A boolean on Odoo 18, the kind of translation on 19 and 20.
438
+ translate: row.translate === true ? "translated" : text(row.translate),
439
+ related: text(row.related),
440
+ help: text(row.help),
441
+ custom: row.state === "manual",
442
+ companyDependent: row.company_dependent === true,
443
+ // Odoo reflects "stored or has a search method" as selectable.
444
+ searchable: row.selectable === true,
445
+ onDelete: text(row.on_delete),
446
+ modules: moduleList(row.modules),
447
+ groups: await groupRefs(row.groups),
448
+ selection: selections.get(row.id) || [],
449
+ });
450
+ }
451
+ for (const model of catalog.models) model.fields.sort(byKey((field) => field.name));
452
+ });
453
+ }
454
+
455
+ if (has("modules")) {
456
+ await inSection("modules", async () => {
457
+ const rows = await read("ir.module.module", [
458
+ ["state", "in", ["installed", "to upgrade", "to remove"]],
459
+ ]);
460
+ const dependencies = rows.length
461
+ ? await read("ir.module.module.dependency", [
462
+ ["module_id", "in", rows.map((row) => row.id)],
463
+ ])
464
+ : [];
465
+ const dependsOn = new Map();
466
+ dependencies.forEach((row) => {
467
+ const moduleId = many2oneId(row.module_id);
468
+ if (!dependsOn.has(moduleId)) dependsOn.set(moduleId, new Set());
469
+ dependsOn.get(moduleId).add(String(row.name));
470
+ });
471
+ catalog.modules = rows
472
+ .filter((row) => !excluded(String(row.name)))
473
+ .map((row) => ({
474
+ name: String(row.name),
475
+ title: text(row.shortdesc) || String(row.name),
476
+ summary: text(row.summary),
477
+ version: text(row.latest_version),
478
+ diskVersion: text(row.installed_version),
479
+ category: many2oneName(row.category_id),
480
+ application: row.application === true,
481
+ autoInstall: row.auto_install === true,
482
+ license: text(row.license),
483
+ dependencies: [...(dependsOn.get(row.id) || [])].sort(),
484
+ selected: selectedModule(String(row.name)),
485
+ }))
486
+ .filter((module) => module.selected || config.otherModules === "summary")
487
+ .sort(byKey((module) => module.name));
488
+ });
489
+ }
490
+
491
+ if (has("access")) {
492
+ await inSection("access", async () => {
493
+ const domain = [["model_id", "in", [...selectedById.keys()]]];
494
+ const entries = [];
495
+ if (catalog.accessModel === "ir.access") {
496
+ for (const row of await read("ir.access", domain)) {
497
+ entries.push({
498
+ modelId: many2oneId(row.model_id),
499
+ name: text(row.name),
500
+ kind: text(row.kind) || "permission",
501
+ groups: await groupRefs(row.group_id ? [many2oneId(row.group_id)] : []),
502
+ operations: text(row.operation) || "",
503
+ domain: text(row.domain),
504
+ });
505
+ }
506
+ } else {
507
+ for (const row of await read("ir.model.access", domain)) {
508
+ entries.push({
509
+ modelId: many2oneId(row.model_id),
510
+ name: text(row.name),
511
+ kind: "permission",
512
+ groups: await groupRefs(row.group_id ? [many2oneId(row.group_id)] : []),
513
+ operations: operationsFromPerms(row),
514
+ domain: null,
515
+ });
516
+ }
517
+ for (const row of await read("ir.rule", domain)) {
518
+ entries.push({
519
+ modelId: many2oneId(row.model_id),
520
+ name: text(row.name),
521
+ kind: "restriction",
522
+ groups: await groupRefs(row.groups),
523
+ operations: operationsFromPerms(row),
524
+ domain: text(row.domain_force),
525
+ });
526
+ }
527
+ }
528
+ for (const { modelId, ...entry } of entries) {
529
+ selectedById.get(modelId)?.access.push(entry);
530
+ }
531
+ for (const model of catalog.models) {
532
+ model.access.sort(
533
+ byKey(
534
+ (entry) => entry.kind,
535
+ (entry) => entry.groups.map((group) => group.key).join(","),
536
+ (entry) => entry.name || "",
537
+ (entry) => entry.operations,
538
+ (entry) => entry.domain || "",
539
+ ),
540
+ );
541
+ }
542
+ });
543
+ }
544
+
545
+ if (has("groups")) {
546
+ await inSection("groups", async () => {
547
+ const index = await groupIndex();
548
+ catalog.groups = [...index.values()]
549
+ .map(({ impliedIds, ...group }) => ({
550
+ ...group,
551
+ implies: impliedIds
552
+ .map((id) => index.get(id))
553
+ .filter(Boolean)
554
+ .map((implied) => ({ key: implied.key, name: implied.fullName }))
555
+ .sort(byKey((implied) => implied.key)),
556
+ }))
557
+ .sort(byKey((group) => group.key));
558
+ });
559
+ }
560
+
561
+ if (has("menus")) {
562
+ await inSection("menus", async () => {
563
+ const [rows, keys] = await Promise.all([
564
+ read("ir.ui.menu", [], { "ir.ui.menu.full_list": true }),
565
+ xmlids("ir.ui.menu"),
566
+ ]);
567
+ const windowIds = rows
568
+ .map((row) => /^ir\.actions\.act_window,(\d+)$/.exec(String(row.action || "")))
569
+ .filter(Boolean)
570
+ .map((match) => Number(match[1]));
571
+ const windows = new Map(
572
+ (windowIds.length
573
+ ? await read("ir.actions.act_window", [["id", "in", [...new Set(windowIds)]]])
574
+ : []
575
+ ).map((row) => [row.id, row]),
576
+ );
577
+ const children = new Map();
578
+ rows.forEach((row) => {
579
+ const parent = many2oneId(row.parent_id);
580
+ if (!children.has(parent)) children.set(parent, []);
581
+ children.get(parent).push(row);
582
+ });
583
+ const ordered = (list) =>
584
+ (list || []).slice().sort(
585
+ byKey(
586
+ (row) => Number(row.sequence) || 0,
587
+ (row) => String(row.name),
588
+ (row) => row.id,
589
+ ),
590
+ );
591
+ const describeAction = (value) => {
592
+ const match = /^([a-z_.]+),(\d+)$/.exec(String(value || ""));
593
+ if (!match) return null;
594
+ const window = match[1] === "ir.actions.act_window" ? windows.get(Number(match[2])) : null;
595
+ return {
596
+ type: match[1],
597
+ name: window ? text(window.name) : null,
598
+ model: window ? text(window.res_model) : null,
599
+ };
600
+ };
601
+ for (const root of ordered(children.get(null))) {
602
+ const menus = [];
603
+ const walk = async (row, path, depth) => {
604
+ for (const child of ordered(children.get(row.id))) {
605
+ const childPath = [...path, text(child.name) || `Menu ${child.id}`];
606
+ menus.push({
607
+ path: childPath,
608
+ depth,
609
+ action: describeAction(child.action),
610
+ groups: await groupRefs(child[shape.groupsField]),
611
+ });
612
+ await walk(child, childPath, depth + 1);
613
+ }
614
+ };
615
+ await walk(root, [], 1);
616
+ const xmlid = keys.get(root.id) || null;
617
+ catalog.apps.push({
618
+ key: xmlid || `menu-${root.id}`,
619
+ xmlid,
620
+ name: text(root.name) || `Menu ${root.id}`,
621
+ sequence: Number(root.sequence) || 0,
622
+ action: describeAction(root.action),
623
+ groups: await groupRefs(root[shape.groupsField]),
624
+ menus,
625
+ });
626
+ }
627
+ });
628
+ }
629
+
630
+ const modelName = (value) => modelById.get(many2oneId(value))?.model || null;
631
+ const selectedByName = new Map(catalog.models.map((model) => [model.model, model]));
632
+ const attach = (model, list, entry) => selectedByName.get(model)?.[list].push(entry);
633
+
634
+ if (has("crons")) {
635
+ await inSection("crons", async () => {
636
+ for (const row of await read("ir.cron", [], { active_test: false })) {
637
+ attach(modelName(row.model_id), "crons", {
638
+ name: text(row.cron_name) || `Scheduled job ${row.id}`,
639
+ interval: `${Number(row.interval_number) || 0} ${String(row.interval_type || "")}`.trim(),
640
+ active: row.active === true,
641
+ priority: Number(row.priority) || 0,
642
+ });
643
+ }
644
+ for (const model of catalog.models) model.crons.sort(byKey((cron) => cron.name));
645
+ });
646
+ }
647
+
648
+ if (has("reports")) {
649
+ await inSection("reports", async () => {
650
+ for (const row of await read("ir.actions.report", [])) {
651
+ attach(text(row.model), "reports", {
652
+ name: text(row.name) || String(row.report_name),
653
+ reportName: text(row.report_name),
654
+ type: text(row.report_type),
655
+ groups: await groupRefs(row[shape.groupsField]),
656
+ });
657
+ }
658
+ for (const model of catalog.models) {
659
+ model.reports.sort(
660
+ byKey(
661
+ (report) => report.reportName || "",
662
+ (report) => report.name,
663
+ ),
664
+ );
665
+ }
666
+ });
667
+ }
668
+
669
+ if (has("serverActions")) {
670
+ await inSection("serverActions", async () => {
671
+ for (const row of await read("ir.actions.server", [["usage", "=", "ir_actions_server"]])) {
672
+ attach(modelName(row.model_id), "serverActions", {
673
+ name: text(row.name) || `Server action ${row.id}`,
674
+ type: text(row.state),
675
+ bound: many2oneId(row.binding_model_id) !== null,
676
+ groups: await groupRefs(row[shape.groupsField]),
677
+ });
678
+ }
679
+ for (const model of catalog.models) {
680
+ model.serverActions.sort(
681
+ byKey(
682
+ (action) => action.name,
683
+ (action) => action.type || "",
684
+ ),
685
+ );
686
+ }
687
+ });
688
+ }
689
+
690
+ catalog.modelIndex = catalog.modelIndex.map(({ id, ...model }) => model);
691
+ return catalog;
692
+ }
693
+
694
+ module.exports = {
695
+ CatalogError,
696
+ DEFAULT_SECTIONS,
697
+ BASE_FIELDS,
698
+ SUPPORTED_VERSIONS,
699
+ VERSIONS,
700
+ fieldsFor,
701
+ MODEL_SECTIONS,
702
+ SECTIONS,
703
+ readCatalog,
704
+ };