create-erxes 0.1.2 → 0.1.3

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/dist/index.js CHANGED
@@ -9,7 +9,7 @@ import { mkdir, readFile, readdir, symlink, writeFile } from "node:fs/promises";
9
9
  import { homedir } from "node:os";
10
10
  import { fileURLToPath } from "node:url";
11
11
  // package.json
12
- var version = "0.1.2";
12
+ var version = "0.1.3";
13
13
  // src/existing-plugins.ts
14
14
  /** The old erxes plugins a new-core plugin can be migrated from (`backend/plugins/<name>_api`). */
15
15
  const OLD_PLUGINS = [
@@ -13,10 +13,12 @@
13
13
  "@orpc/tanstack-query": "1.16.2",
14
14
  "@tabler/icons-react": "3.34.0",
15
15
  "@tanstack/react-query": "^5.107.1",
16
- "erxes-ui": "npm:@erxes/ui@4.0.0-v2.3",
16
+ "erxes-ui": "npm:@erxes/ui@4.0.0-v2.4",
17
17
  "jotai": "^3.0.2",
18
+ "nuqs": "^2.10.3",
18
19
  "react": "19.3.1",
19
20
  "react-dom": "19.3.1",
21
+ "react-hook-form": "^7.92.0",
20
22
  "react-i18next": "^18.0.1",
21
23
  "react-router-dom": "^7.18.5"
22
24
  },
@@ -39,7 +39,13 @@ data only. The inventory reads code, never a database.
39
39
  `manifest.scopes`; `contract` is a call from the UI with the member's own
40
40
  session, through `orpc` from `@erxes/core-contract/client` (members through
41
41
  erxes-ui's `SelectMember` and `MemberInline`, `ui/AGENTS.md`);
42
- `plugin-owned` lives in this plugin's tables or code.
42
+ `plugin-owned` lives in this plugin's tables or code; `drop` is left out on
43
+ purpose (segments, the client portal, brands): port none of it, and tick it
44
+ once nothing new calls it. Core has no departments or branches: a plugin
45
+ that used them gets its own `__id___teams` and `__id___team_members`,
46
+ drafted with their import from old core's `departments`, `branches` and the
47
+ members' `departmentIds` and `branchIds`. The ids stay the same, so the old
48
+ records' `departmentIds` still match.
43
49
  5. Pages. One per UI route, under `ui/src/<module>/`, built from `erxes-ui`
44
50
  the way `ui/src/items/` is. The old `ui-modules` components don't exist
45
51
  here. Member pickers and names become erxes-ui's `SelectMember` and
@@ -8,10 +8,16 @@ import { resolve } from 'node:path';
8
8
  import type * as pg from 'drizzle-orm/pg-core';
9
9
  import { z } from 'zod';
10
10
  import {
11
+ CLIENT_PORTAL,
11
12
  CORE_CALLS,
13
+ WHOLE_MODULES,
12
14
  type Equivalent,
13
15
  HELPERS,
16
+ isClientPortal,
17
+ isStructureField,
18
+ OWN_TEAMS,
14
19
  pluginCall,
20
+ PUBLIC_ROUTE,
15
21
  REGISTRATIONS,
16
22
  UNMAPPED,
17
23
  } from './core-map.ts';
@@ -98,21 +104,27 @@ const RUNTIME_ENV =
98
104
 
99
105
  // Checklist items -------------------------------------------------------------
100
106
 
101
- type Item = { section: string; text: string; gap: boolean };
107
+ type Item = { section: string; text: string; kind?: Equivalent['kind'] };
102
108
 
103
109
  const items = new Map<string, Item[]>();
104
110
 
105
- const add = (module: string, section: string, text: string, gap = false) => {
111
+ const add = (module: string, section: string, text: string, kind?: Equivalent['kind']) => {
106
112
  const list = items.get(module) ?? [];
107
113
 
108
- list.push({ section, text, gap });
114
+ list.push({ section, text, kind });
109
115
  items.set(module, list);
110
116
  };
111
117
 
118
+ /** Whether the old plugin used core's departments or branches, by call or by field. */
119
+ let usesTeams = false;
120
+ const TEAMS = `${PLUGIN}_teams`;
121
+ const TEAM_MEMBERS = `${PLUGIN}_team_members`;
122
+
112
123
  const describe = (equivalent: Equivalent) =>
113
- equivalent.kind === 'gap'
114
- ? `**gap**: ${equivalent.note ?? ''}`
115
- : `${equivalent.kind} \`${equivalent.to.replaceAll('<plugin>', PLUGIN)}\`${equivalent.note ? ` (${equivalent.note})` : ''}`;
124
+ (equivalent.kind === 'gap' || equivalent.kind === 'drop'
125
+ ? `**${equivalent.kind}**: ${equivalent.note ?? ''}`
126
+ : `${equivalent.kind} \`${equivalent.to}\`${equivalent.note ? ` (${equivalent.note})` : ''}`
127
+ ).replaceAll('<plugin>', PLUGIN);
116
128
 
117
129
  const camel = (text: string) =>
118
130
  text.replaceAll(/[_-](\w)/g, (_match, char: string) => char.toUpperCase());
@@ -138,10 +150,15 @@ for (const collection of inventory.collections) {
138
150
  }
139
151
 
140
152
  for (const op of inventory.graphql.operations) {
153
+ const portal = isClientPortal(op);
154
+
141
155
  add(
142
156
  op.module,
143
157
  'Procedures',
144
- `${op.kind} \`${op.name}\` → procedure \`${op.module}.${op.name}\` (returns \`${op.returns}\`) ${at(op)}`,
158
+ portal
159
+ ? `${op.kind} \`${op.name}\` → ${describe(CLIENT_PORTAL)} ${at(op)}`
160
+ : `${op.kind} \`${op.name}\` → procedure \`${op.module}.${op.name}\` (returns \`${op.returns}\`) ${at(op)}`,
161
+ portal ? 'drop' : undefined,
145
162
  );
146
163
  }
147
164
 
@@ -175,38 +192,66 @@ for (const call of inventory.trpcCalls) {
175
192
  const target = typeof call.pluginName === 'string' ? call.pluginName : undefined;
176
193
  const key = `${show(call.target)}.${show(call.action)}`;
177
194
 
178
- const equivalent =
179
- call.dynamic || !target
195
+ const equivalent = isClientPortal(call)
196
+ ? CLIENT_PORTAL
197
+ : call.dynamic || !target
180
198
  ? UNMAPPED
181
199
  : target === 'core'
182
- ? (CORE_CALLS[key] ?? UNMAPPED)
183
- : pluginCall(target, plugin);
200
+ ? (CORE_CALLS[key] ?? WHOLE_MODULES[show(call.target)] ?? UNMAPPED)
201
+ : pluginCall(target, plugin, show(call.action));
184
202
 
185
203
  add(
186
204
  call.module,
187
205
  'Core calls',
188
206
  `${target ?? '<dynamic>'} \`${key}\` → ${describe(equivalent)} ${at(call)}`,
189
- equivalent.kind === 'gap',
207
+ equivalent.kind,
208
+ );
209
+
210
+ if (equivalent === OWN_TEAMS) usesTeams = true;
211
+ }
212
+
213
+ // Old core's structure: the plugin keeps its own departments and branches, imported with
214
+ // their Mongo ids, so the ids its records hold stay valid.
215
+ for (const schema of inventory.schemas) {
216
+ for (const field of schema.fields.filter((entry) => isStructureField(entry.name))) {
217
+ usesTeams = true;
218
+ add(
219
+ schema.module,
220
+ 'Tables',
221
+ `\`${schema.name}.${field.name}\` → plugin-owned: ids in \`${TEAMS}\`, unchanged by the import ${at(schema)}`,
222
+ OWN_TEAMS.kind,
223
+ );
224
+ }
225
+ }
226
+
227
+ if (usesTeams) {
228
+ add(
229
+ '(plugin)',
230
+ 'Tables',
231
+ `\`${TEAMS}\` and \`${TEAM_MEMBERS}\` from old core's \`departments\`, \`branches\` and the members' \`departmentIds\` and \`branchIds\` (drafts in migration/schema-draft.ts, imported by scripts/import-mongo.ts): core has no departments or branches`,
232
+ OWN_TEAMS.kind,
190
233
  );
191
234
  }
192
235
 
193
236
  for (const call of inventory.eventCalls) {
194
- const equivalent = HELPERS[call.helper] ?? UNMAPPED;
237
+ const equivalent = isClientPortal(call) ? CLIENT_PORTAL : (HELPERS[call.helper] ?? UNMAPPED);
195
238
 
196
239
  add(
197
240
  call.module,
198
241
  'Events',
199
242
  `\`${call.helper}\`${call.type ? ` ${show(call.type)}` : ''} → ${describe(equivalent)} ${at(call)}`,
200
- equivalent.kind === 'gap',
243
+ equivalent.kind,
201
244
  );
202
245
  }
203
246
 
204
247
  for (const route of inventory.routes) {
248
+ const equivalent = isClientPortal(route) ? CLIENT_PORTAL : PUBLIC_ROUTE;
249
+
205
250
  add(
206
251
  route.module,
207
252
  'Routes',
208
- `${route.method} \`${route.path}\`${route.webhook ? ' (webhook)' : ''} → **gap**: a public route, and core can't yet tell a public request's organisation ${at(route)}`,
209
- true,
253
+ `${route.method} \`${route.path}\`${route.webhook ? ' (webhook)' : ''} → ${describe(equivalent)} ${at(route)}`,
254
+ equivalent.kind,
210
255
  );
211
256
  }
212
257
 
@@ -225,7 +270,7 @@ for (const entry of inventory.registrations) {
225
270
  '(plugin)',
226
271
  'Registrations',
227
272
  `${entry.kind} \`${show(entry.id)}\`${entry.group ? ` (${entry.group})` : ''} → ${describe(equivalent)} ${at(entry)}`,
228
- equivalent.kind === 'gap',
273
+ equivalent.kind,
229
274
  );
230
275
  }
231
276
 
@@ -250,7 +295,8 @@ const ticked = new Map(
250
295
  );
251
296
 
252
297
  const all = [...items.values()].flat();
253
- const gaps = all.filter((item) => item.gap).length;
298
+ const gaps = all.filter((item) => item.kind === 'gap').length;
299
+ const drops = all.filter((item) => item.kind === 'drop').length;
254
300
 
255
301
  const order = [
256
302
  '(plugin)',
@@ -275,8 +321,9 @@ const lines = [
275
321
  `# Migrating ${plugin} to the new core`,
276
322
  '',
277
323
  `From erxes/erxes \`${inventory.source.ref}\` (${inventory.source.sha.slice(0, 10)}), \`${plugin}_api\` and \`${plugin}_ui\`.`,
278
- `${all.length} items, ${gaps} of them gaps. Follow \`.agents/skills/plugin-migrate\`: one module at a time,`,
279
- 'tick an item once it works on the new core and its old bugs and workarounds are gone, and tick a gap once filed, ending its line with `(filed <issue url>)`.',
324
+ `${all.length} items: ${gaps} gaps and ${drops} drops. Follow \`.agents/skills/plugin-migrate\`: one module at a time,`,
325
+ 'tick an item once it works on the new core and its old bugs and workarounds are gone, tick a gap once filed, ending its line with `(filed <issue url>)`,',
326
+ 'and tick a drop once its code is left out (segments, the client portal and brands are not migrated).',
280
327
  '',
281
328
  `The old UI imports ${symbols} symbols from \`ui-modules\` in ${inventory.uiModules.files} files; the host has no \`ui-modules\`, so each page rebuilds them from \`erxes-ui\` (members through its \`SelectMember\` and \`MemberInline\`, other records through \`EntityPicker\`). erxes-ui's own old-to-new component table is \`packages/ui/MIGRATION.md\` in erxes (branch \`postgre\`).`,
282
329
  `GraphQL types (${inventory.graphql.types.length}) are listed in \`migration/inventory.json\` for reference; procedures replace them.`,
@@ -343,7 +390,12 @@ for (const collection of inventory.collections) {
343
390
 
344
391
  drafted.add(table);
345
392
 
346
- const fields = schema.fields.filter((entry) => entry.type !== 'spread' && entry.name !== '_id');
393
+ // `timestamps: true` adds createdAt and updatedAt below; a schema that also declares them gets them once.
394
+ const stamped = new Set(schema.timestamps ? ['createdAt', 'updatedAt'] : []);
395
+
396
+ const fields = schema.fields.filter(
397
+ (entry) => entry.type !== 'spread' && entry.name !== '_id' && !stamped.has(entry.name),
398
+ );
347
399
 
348
400
  const columns = fields.map((entry) => {
349
401
  const column = `${columnOf(entry.type)}${entry.required ? '.notNull()' : ''}`;
@@ -369,7 +421,9 @@ for (const collection of inventory.collections) {
369
421
  ' // The Mongo `_id`, kept as text so references survive the import.',
370
422
  ' id: text().primaryKey(),',
371
423
  ' orgId: text().notNull(),',
372
- ...spreads.map((entry) => ` // TODO spread in the old schema: ${entry.name}`),
424
+ ...spreads.map(
425
+ (entry) => ` // Spread in the old schema, list its fields here: ${entry.name}`,
426
+ ),
373
427
  ...columns,
374
428
  ...stamps,
375
429
  ' },',
@@ -399,6 +453,72 @@ for (const collection of inventory.collections) {
399
453
  );
400
454
  }
401
455
 
456
+ // The plugin's own departments and branches, in place of old core's structure.
457
+ if (usesTeams) {
458
+ tables.push(
459
+ [
460
+ "/** Old core's departments and branches, now the plugin's. Ids are the Mongo `_id`s. */",
461
+ `export const ${camel(TEAMS)} = table(`,
462
+ " 'teams',",
463
+ ' {',
464
+ ' id: text().primaryKey(),',
465
+ ' orgId: text().notNull(),',
466
+ " kind: text({ enum: ['department', 'branch'] }).notNull(),",
467
+ ' name: text(),',
468
+ ' code: text(),',
469
+ ' parentId: text(),',
470
+ ' supervisorId: text(),',
471
+ ' createdAt: timestamp({ withTimezone: true }).notNull().defaultNow(),',
472
+ ' updatedAt: timestamp({ withTimezone: true }).notNull().defaultNow(),',
473
+ ' },',
474
+ ` (t) => [index('${TEAMS}_org_idx').on(t.orgId)],`,
475
+ ');',
476
+ '',
477
+ '/**',
478
+ " * Who is in which team, from old users' departmentIds and branchIds. User ids are core's,",
479
+ ' * which keep the Mongo `_id` of members imported from old erxes. Old users may point at',
480
+ ' * removed departments: drop those rows before adding a foreign key to the teams.',
481
+ ' */',
482
+ `export const ${camel(TEAM_MEMBERS)} = table(`,
483
+ " 'team_members',",
484
+ ' {',
485
+ ' orgId: text().notNull(),',
486
+ ' teamId: text().notNull(),',
487
+ ' userId: text().notNull(),',
488
+ ' },',
489
+ ` (t) => [primaryKey({ columns: [t.teamId, t.userId] }), index('${TEAM_MEMBERS}_user_idx').on(t.userId)],`,
490
+ ');',
491
+ ].join('\n'),
492
+ );
493
+
494
+ const structure = (collection: string, kind: string) =>
495
+ [
496
+ ' {',
497
+ ` collection: '${collection}',`,
498
+ ` table: '${TEAMS}',`,
499
+ ` values: { kind: '${kind}' },`,
500
+ " columns: { _id: 'id', title: 'name', code: 'code', parentId: 'parent_id', supervisorId: 'supervisor_id', createdAt: 'created_at' },",
501
+ ' },',
502
+ ].join('\n');
503
+
504
+ const members = (field: string) =>
505
+ [
506
+ ' {',
507
+ " collection: 'users',",
508
+ ` table: '${TEAM_MEMBERS}',`,
509
+ ` unwind: '${field}',`,
510
+ ` columns: { ${field}: 'team_id', _id: 'user_id' },`,
511
+ ' },',
512
+ ].join('\n');
513
+
514
+ imports.push(
515
+ structure('departments', 'department'),
516
+ structure('branches', 'branch'),
517
+ members('departmentIds'),
518
+ members('branchIds'),
519
+ );
520
+ }
521
+
402
522
  writeFileSync(
403
523
  resolve(here, 'schema-draft.ts'),
404
524
  [
@@ -406,7 +526,7 @@ writeFileSync(
406
526
  '// schemas. Nothing imports this file: move a table into api/src/db/schema.ts when',
407
527
  '// you port its module, pick real types (integer, enums, foreign keys), then run',
408
528
  '// `pnpm db:generate` and add its core_enable_tenant_rls line (schema-change skill).',
409
- "import { boolean, doublePrecision, index, jsonb, text, timestamp } from 'drizzle-orm/pg-core';",
529
+ `import { boolean, doublePrecision, index, jsonb, ${usesTeams ? 'primaryKey, ' : ''}text, timestamp } from 'drizzle-orm/pg-core';`,
410
530
  "import { table } from '../api/src/db/table.ts';",
411
531
  '',
412
532
  tables.join('\n\n'),
@@ -419,11 +539,20 @@ writeFileSync(
419
539
  [
420
540
  '// Which Mongo field goes to which column, per collection, for scripts/import-mongo.ts.',
421
541
  '// Written by migration/checklist.ts; edit it as the tables in api/src/db/schema.ts change.',
422
- 'export const IMPORT_MAP: { collection: string; table: string; columns: Record<string, string> }[] = [',
542
+ '// `values` are columns every row gets; `unwind` makes one row per item of that array field.',
543
+ 'export const IMPORT_MAP: {',
544
+ ' collection: string;',
545
+ ' table: string;',
546
+ ' columns: Record<string, string>;',
547
+ ' values?: Record<string, string>;',
548
+ ' unwind?: string;',
549
+ '}[] = [',
423
550
  ...imports,
424
551
  '];',
425
552
  '',
426
553
  ].join('\n'),
427
554
  );
428
555
 
429
- console.info(`MIGRATION.md: ${all.length} items, ${gaps} gaps; ${tables.length} draft tables`);
556
+ console.info(
557
+ `MIGRATION.md: ${all.length} items, ${gaps} gaps, ${drops} drops; ${tables.length} draft tables`,
558
+ );
@@ -7,9 +7,10 @@
7
7
  // through orpc from @erxes/core-contract/client
8
8
  // plugin-owned the plugin keeps it in its own tables or code
9
9
  // gap core offers no equivalent yet: file it, don't work around it
10
+ // drop left out on purpose (segments, the client portal, brands): remove the code
10
11
 
11
12
  export type Equivalent = {
12
- kind: 'sdk' | 'contract' | 'plugin-owned' | 'gap';
13
+ kind: 'sdk' | 'contract' | 'plugin-owned' | 'gap' | 'drop';
13
14
  /** The procedure, function or place it moves to. */
14
15
  to: string;
15
16
  note?: string;
@@ -19,37 +20,114 @@ const sdk = (to: string, note?: string): Equivalent => ({ kind: 'sdk', to, note
19
20
  const contract = (to: string, note?: string): Equivalent => ({ kind: 'contract', to, note });
20
21
  const owned = (to: string, note?: string): Equivalent => ({ kind: 'plugin-owned', to, note });
21
22
  const gap = (note: string): Equivalent => ({ kind: 'gap', to: '', note });
23
+ const drop = (note: string): Equivalent => ({ kind: 'drop', to: '', note });
24
+
25
+ /**
26
+ * Old core's structure (departments, branches, units, positions): core has none. A plugin that
27
+ * used it keeps its own teams; migration/checklist.ts drafts the tables and the import.
28
+ */
29
+ export const OWN_TEAMS = owned(
30
+ '<plugin>_teams',
31
+ "the plugin's departments and branches; members in <plugin>_team_members, names from users.list (users:read)",
32
+ );
33
+
34
+ /** A field holding old structure ids or switches: `departmentIds`, `branchId`, `isCheckBranch`. */
35
+ export const isStructureField = (name: string) =>
36
+ /^(department|branch|unit|position)Ids?$|^isCheck(Department|Branch)$/.test(name);
37
+
38
+ const NO_SEGMENTS = drop('the new core has no segments; the plugin filters its own records');
39
+
40
+ /** What old code serving the client portal becomes: nothing, the new core has none. */
41
+ export const CLIENT_PORTAL = drop('the new core has no client portal');
42
+
43
+ /** Old core modules mapped as a whole, whatever the action: left out, or the plugin's own. */
44
+ export const WHOLE_MODULES: Record<string, Equivalent> = {
45
+ segment: NO_SEGMENTS,
46
+ segments: NO_SEGMENTS,
47
+ branches: OWN_TEAMS,
48
+ departments: OWN_TEAMS,
49
+ units: OWN_TEAMS,
50
+ positions: OWN_TEAMS,
51
+ brands: drop('the new core has no brands; leave brand ids and names out'),
52
+ clientPortal: CLIENT_PORTAL,
53
+ clientPortals: CLIENT_PORTAL,
54
+ clientPortalUsers: CLIENT_PORTAL,
55
+ cpUsers: CLIENT_PORTAL,
56
+ cpNotifications: CLIENT_PORTAL,
57
+ };
58
+
59
+ /** Starting automations for a record: they start from the events a plugin publishes. */
60
+ const START_AUTOMATIONS = sdk(
61
+ 'core_publish_event() in the same transaction',
62
+ 'declare the type in manifest.events; automations start from events',
63
+ );
64
+
65
+ /** Reading automations: plugins can't, so what the plugin needed from them is its own setting. */
66
+ const OWN_AUTOMATION = owned(
67
+ '<plugin>_settings',
68
+ "what the plugin read from an automation (a chatbot's menu, its get-started reply) is its own setting; automations act on its events",
69
+ );
70
+
71
+ /**
72
+ * An old public Express route (webhooks, OAuth callbacks): the plugin serves it and works out
73
+ * the organisation itself, before any `withOrg`.
74
+ */
75
+ export const PUBLIC_ROUTE = owned(
76
+ 'a route on the plugin API',
77
+ "organisation from the path (an id or key the plugin issued) or, for one address shared by every organisation (Meta's webhook), a lookup table listed in tools/no-org-id.sql; verify the sender's signature first",
78
+ );
79
+
80
+ /** Old code that served the client portal: files named after it, `cp*` GraphQL operations. */
81
+ export const isClientPortal = (entry: { file: string; name?: string }) =>
82
+ /client-?portal|cp-?notifications?/i.test(entry.file) || /^cp[A-Z]/.test(entry.name ?? '');
22
83
 
23
84
  /** `sendTRPCMessage({ pluginName: 'core', module, action })`, keyed `module.action`. */
24
85
  export const CORE_CALLS: Record<string, Equivalent> = {
25
86
  'customers.findOne': sdk('customers.get', 'scope customers:read; by id only'),
26
- 'customers.find': contract(
87
+ 'customers.find': sdk(
27
88
  'customers.list',
28
- 'member needs contacts.view; no plugin scope lists customers',
89
+ 'scope customers:read; search, ids, state, tagIds, companyId, filter',
90
+ ),
91
+ 'customers.findActiveCustomers': sdk('customers.list', 'scope customers:read'),
92
+ 'customers.removeCustomers': contract(
93
+ 'customers.remove',
94
+ 'members with contacts.delete only; plugins never delete contacts',
29
95
  ),
30
96
  'customers.createCustomer': sdk(
31
97
  'customers.findOrCreate',
32
- 'scope customers:write; by email or phone',
98
+ "scope customers:write; by email or phone, else customers.create (state: 'lead')",
99
+ ),
100
+ 'customers.createMessengerCustomer': sdk(
101
+ 'customers.findOrCreate',
102
+ "scope customers:write; state: 'lead', source: 'chat'",
33
103
  ),
34
- 'customers.createMessengerCustomer': sdk('customers.findOrCreate', 'scope customers:write'),
35
104
  'customers.getWidgetCustomer': sdk(
36
105
  'customers.findOrCreate',
37
106
  'scope customers:write; by email or phone',
38
107
  ),
39
- 'customers.updateCustomer': gap('plugins cannot change a customer'),
40
- 'customers.updateOne': gap('plugins cannot change a customer'),
41
- 'customers.updateMessengerCustomer': gap('plugins cannot change a customer'),
42
- 'customers.updateLocation': gap('plugins cannot change a customer'),
43
- 'customers.updateSession': gap('plugins cannot change a customer'),
44
- 'customers.markCustomerAsActive': gap('plugins cannot change a customer'),
45
- 'customers.findActiveCustomers': gap('plugins cannot list or search customers'),
108
+ 'customers.updateCustomer': sdk('customers.update', 'scope customers:write'),
109
+ 'customers.updateOne': sdk('customers.update', 'scope customers:write'),
110
+ 'customers.updateMessengerCustomer': sdk(
111
+ 'customers.update',
112
+ 'scope customers:write; messenger session data (last seen, online, location) stays in the plugin',
113
+ ),
114
+ 'customers.updateLocation': owned(
115
+ '<plugin>_customers',
116
+ "the visitor's location is messenger session data",
117
+ ),
118
+ 'customers.updateSession': owned('<plugin>_customers', 'messenger session data'),
119
+ 'customers.markCustomerAsActive': owned('<plugin>_customers', 'messenger session data'),
46
120
  'companies.findOne': sdk('companies.get', 'scope customers:read; by id only'),
47
- 'companies.find': contract('companies.list', 'member needs contacts.view'),
48
- 'companies.createCompany': gap('plugins cannot create companies'),
49
- 'companies.updateCompany': gap('plugins cannot change a company'),
50
- 'users.findOne': contract('users.get', 'member needs team.view; keep user ids on the server'),
51
- 'users.find': contract('users.list', 'member needs team.view; keep user ids on the server'),
52
- 'users.getIds': contract('users.list', 'member needs team.view'),
121
+ 'companies.find': sdk('companies.list', 'scope customers:read'),
122
+ 'companies.createCompany': sdk('companies.create', 'scope customers:write'),
123
+ 'companies.updateCompany': sdk('companies.update', 'scope customers:write'),
124
+ 'conformity.savedConformity': owned(
125
+ "customerId / companyId columns (or a join table) on the plugin's record",
126
+ 'customer ↔ company links: customers.linkCompany (customers:write)',
127
+ ),
128
+ 'users.findOne': sdk('users.get', 'scope users:read'),
129
+ 'users.find': sdk('users.list', 'scope users:read; search, ids, isActive, role'),
130
+ 'users.getIds': sdk('users.list', 'scope users:read'),
53
131
  'tags.find': owned('<plugin>_tags', "core's tags are for customers and companies (decision 9)"),
54
132
  'tags.findOne': owned(
55
133
  '<plugin>_tags',
@@ -82,21 +160,22 @@ export const CORE_CALLS: Record<string, Equivalent> = {
82
160
  'scope activities:write; customer or company targets, else manifest timelines',
83
161
  ),
84
162
  'notifications.send': sdk('notifications.create', 'scope notifications:write; team members only'),
85
- 'core.sendEmail': gap('no email API for plugins; team members: notifications.create'),
86
- 'emailDeliveries.create': gap('no email API for plugins'),
87
- 'emailDeliveries.recordHandoff': gap('no email API for plugins'),
88
- 'emailSuppression.blocked': gap('no email API for plugins'),
89
- 'cpUsers.get': gap('customer sign-in (decision 42) has no plugin procedure yet'),
90
- 'cpNotifications.create': gap('customer sign-in (decision 42) has no plugin procedure yet'),
91
- 'clientPortals.get': gap('core has no client portals'),
92
- 'segment.fetchSegment': gap('core has saved views, not segments, and none for plugins'),
93
- 'automation.find': gap(
94
- 'plugins declare manifest events and actions; they cannot read automations',
95
- ),
96
- 'brands.create': gap('core has no brands'),
97
- 'brands.updateOne': gap('core has no brands'),
98
- 'branches.findOne': gap('core has no branches'),
99
- 'departments.findWithChild': gap('core has no departments'),
163
+ 'core.sendEmail': sdk(
164
+ 'emails.send',
165
+ 'scope emails:write; plain text, up to 50 addresses, replyTo; team members: notifications.create',
166
+ ),
167
+ 'emailDeliveries.create': owned(
168
+ '<plugin>_email_deliveries',
169
+ "mail sent from the organisation's own mailbox is the plugin's to log",
170
+ ),
171
+ 'emailDeliveries.recordHandoff': owned('<plugin>_email_deliveries'),
172
+ 'emailSuppression.blocked': owned(
173
+ '<plugin>_email_suppressions',
174
+ 'bounces and opt-outs the plugin hears from its mailbox',
175
+ ),
176
+ 'automation.find': OWN_AUTOMATION,
177
+ 'branches.findOne': OWN_TEAMS,
178
+ 'departments.findWithChild': OWN_TEAMS,
100
179
  'organization.identity': owned('nothing', 'the organisation id is in every token (orgId)'),
101
180
  };
102
181
 
@@ -115,12 +194,10 @@ export const HELPERS: Record<string, Equivalent> = {
115
194
  'scope notifications:write; kind in manifest.notificationKinds',
116
195
  ),
117
196
  sendNotifications: sdk('notifications.create', 'scope notifications:write'),
118
- sendAutomationTrigger: sdk(
119
- 'core_publish_event() in the same transaction',
120
- 'declare the type in manifest.events; automations start from events',
121
- ),
122
- sendAutomationDeferredCompletion: gap(
123
- 'an automation action answers in its HTTP response; core has no deferred completion',
197
+ sendAutomationTrigger: START_AUTOMATIONS,
198
+ sendAutomationDeferredCompletion: owned(
199
+ 'the action answers at once ({ queued: true, jobId })',
200
+ 'the plugin keeps the final result on its own job row, and on the timeline with activities.create',
124
201
  ),
125
202
  };
126
203
 
@@ -136,15 +213,24 @@ export const REGISTRATIONS: Record<string, Equivalent> = {
136
213
  'manifest.actions',
137
214
  'POST <api>/automations/actions/<type>, verifyCoreCall()',
138
215
  ),
139
- 'default role': gap('core roles are its own; a plugin cannot ship a role with grants'),
140
- segment: gap('core has no segments for plugins'),
216
+ 'default role': sdk(
217
+ 'manifest.permissionSets',
218
+ 'a set of its permissions an owner adds to any role; members keep one role',
219
+ ),
220
+ segment: NO_SEGMENTS,
141
221
  'import-export': gap("core's import and export cover contacts only"),
142
222
  };
143
223
 
144
- /** A call to another plugin, or to this one through tRPC. */
145
- export const pluginCall = (target: string, self: string): Equivalent =>
146
- target === self
147
- ? owned('a direct function call')
148
- : gap(`plugins cannot call ${target}; listen to its events instead`);
224
+ /**
225
+ * A call to another plugin, or to this one through tRPC. The old automations plugin is core's
226
+ * automations now: its `trigger` is an event, and what a plugin read from it is its own setting.
227
+ */
228
+ export const pluginCall = (target: string, self: string, action: string): Equivalent => {
229
+ if (target === self) return owned('a direct function call');
230
+
231
+ if (target === 'automations') return action === 'trigger' ? START_AUTOMATIONS : OWN_AUTOMATION;
232
+
233
+ return gap(`plugins cannot call ${target}; listen to its events instead`);
234
+ };
149
235
 
150
236
  export const UNMAPPED = gap('not in migration/core-map.ts yet');
@@ -1,4 +1,10 @@
1
1
  // Which Mongo field goes to which column, per collection, for scripts/import-mongo.ts.
2
2
  // Written by migration/checklist.ts; edit it as the tables in api/src/db/schema.ts change.
3
- export const IMPORT_MAP: { collection: string; table: string; columns: Record<string, string> }[] =
4
- [];
3
+ // `values` are columns every row gets; `unwind` makes one row per item of that array field.
4
+ export const IMPORT_MAP: {
5
+ collection: string;
6
+ table: string;
7
+ columns: Record<string, string>;
8
+ values?: Record<string, string>;
9
+ unwind?: string;
10
+ }[] = [];
@@ -5,8 +5,8 @@
5
5
  // pnpm migrate:import [-- --only <collection>]
6
6
  //
7
7
  // Rows keep the Mongo `_id` as `id`, so running it again skips what it already
8
- // copied. Tables not in the database yet, and fields with no column, are skipped
9
- // and listed at the end.
8
+ // copied (rows that hit any unique key are skipped). Tables not in the database
9
+ // yet, and fields with no column, are skipped and listed at the end.
10
10
  import { parseArgs } from 'node:util';
11
11
  import { MongoClient, ObjectId } from 'mongodb';
12
12
  import { Pool } from 'pg';
@@ -54,7 +54,7 @@ const pool = new Pool({ connectionString: DATA_URL });
54
54
  const skipped: string[] = [];
55
55
 
56
56
  try {
57
- for (const { collection, table, columns } of IMPORT_MAP) {
57
+ for (const { collection, table, columns, values = {}, unwind } of IMPORT_MAP) {
58
58
  if (args.only && args.only !== collection) continue;
59
59
 
60
60
  const { rows: described } = await pool.query<{ column_name: string; data_type: string }>(
@@ -81,7 +81,8 @@ try {
81
81
  return false;
82
82
  });
83
83
 
84
- const names = ['org_id', ...fields.map(([, name]) => name)];
84
+ const constants = Object.entries(values);
85
+ const names = ['org_id', ...constants.map(([name]) => name), ...fields.map(([, name]) => name)];
85
86
  const cursor = mongo.db().collection(collection).find({}, { batchSize: BATCH });
86
87
  let copied = 0;
87
88
  let seen = 0;
@@ -98,7 +99,7 @@ try {
98
99
  await client.query("SELECT set_config('app.org_id', $1, true)", [ORG_ID]);
99
100
 
100
101
  const result = await client.query(
101
- `INSERT INTO "${table}" (${names.map((name) => `"${name}"`).join(', ')}) VALUES ${placeholders.join(', ')} ON CONFLICT (id) DO NOTHING`,
102
+ `INSERT INTO "${table}" (${names.map((name) => `"${name}"`).join(', ')}) VALUES ${placeholders.join(', ')} ON CONFLICT DO NOTHING`,
102
103
  batch.flat(),
103
104
  );
104
105
 
@@ -116,24 +117,38 @@ try {
116
117
 
117
118
  for await (const document of cursor) {
118
119
  seen += 1;
119
- batch.push([
120
- ORG_ID,
121
- ...fields.map(([field, name]) => {
122
- const column = known.get(name) ?? { array: false, json: false };
123
-
124
- return cell(document[field], column);
125
- }),
126
- ]);
127
-
128
- if (batch.length === BATCH) {
129
- await insert(batch);
130
- batch = [];
120
+
121
+ // `unwind` turns one document into a row per item of that array field (a user's departmentIds).
122
+ const listed: unknown = unwind ? document[unwind] : undefined;
123
+
124
+ const copies = unwind
125
+ ? [listed]
126
+ .flat()
127
+ .filter((item) => item !== undefined && item !== null)
128
+ .map((item) => ({ ...document, [unwind]: item }))
129
+ : [document];
130
+
131
+ for (const copy of copies) {
132
+ batch.push([
133
+ ORG_ID,
134
+ ...constants.map(([, value]) => value),
135
+ ...fields.map(([field, name]) => {
136
+ const column = known.get(name) ?? { array: false, json: false };
137
+
138
+ return cell(copy[field], column);
139
+ }),
140
+ ]);
141
+
142
+ if (batch.length === BATCH) {
143
+ await insert(batch);
144
+ batch = [];
145
+ }
131
146
  }
132
147
  }
133
148
 
134
149
  if (batch.length > 0) await insert(batch);
135
150
 
136
- console.info(`${collection} → ${table}: ${copied} of ${seen} copied`);
151
+ console.info(`${collection} → ${table}: ${copied} rows copied from ${seen} documents`);
137
152
  }
138
153
  } finally {
139
154
  await mongo.close();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-erxes",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "private": false,
5
5
  "description": "Create an erxes plugin for the new core, or migrate an old erxes plugin onto it: pnpm create erxes",
6
6
  "license": "MIT",