create-erxes 0.1.8 → 0.1.9
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 +1 -1
- package/dist/templates/base/.agents/skills/ui-patterns/SKILL.md +8 -7
- package/dist/templates/base/api/AGENTS.md +6 -0
- package/dist/templates/base/api/package.json +2 -2
- package/dist/templates/base/api/src/__tests__/server.test.ts +11 -0
- package/dist/templates/base/api/src/core.ts +5 -2
- package/dist/templates/base/api/src/jobs.ts +40 -0
- package/dist/templates/base/api/src/server.ts +22 -1
- package/dist/templates/base/tools/check-rls.sql +2 -1
- package/dist/templates/base/ui/package.json +1 -1
- package/package.json +1 -1
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.
|
|
12
|
+
var version = "0.1.9";
|
|
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 = [
|
|
@@ -96,13 +96,14 @@ gives it its fields, and `api/src/modules/items/itemFilter.ts` applies them.
|
|
|
96
96
|
draft with `openNew(contentType, defaults)`. `detailPath(id)` is a route of
|
|
97
97
|
the plugin's that renders `<RecordPage contentType contentId />`
|
|
98
98
|
(`ui/src/__Pascal__.tsx`): the expanded record.
|
|
99
|
-
- A plugin whose records belong to customers or companies lists them on
|
|
100
|
-
|
|
101
|
-
The component gets the host record
|
|
102
|
-
a record clicked there opens with
|
|
103
|
-
|
|
104
|
-
plugin
|
|
105
|
-
|
|
99
|
+
- A plugin whose records belong to customers or companies lists them on those
|
|
100
|
+
customers' and companies' panels with `CONFIG.relationTabs`:
|
|
101
|
+
`{ name, title, icon, hosts?, component }`. The component gets the host record
|
|
102
|
+
and its `customerIds` and `companyIds`; a record clicked there opens with
|
|
103
|
+
`useOpenRecord().open` on top in the same panel, and Back returns. Another
|
|
104
|
+
plugin's record (an order, a task) shows the tab only when `hosts` names its
|
|
105
|
+
type and the type gives `contacts`: name it only when a member working on that
|
|
106
|
+
record needs the list, never by default.
|
|
106
107
|
|
|
107
108
|
## Forms
|
|
108
109
|
|
|
@@ -23,6 +23,12 @@ Node 26 runs the TypeScript directly; imports carry `.ts` and `#/` is `src/`.
|
|
|
23
23
|
`entityId`. It appears on the record's Activity tab, and on the customer's or
|
|
24
24
|
company's too when it names `subjectType` and `subjectId`. A retried event
|
|
25
25
|
adds nothing. `activities.create` is for entries that are not events.
|
|
26
|
+
- Background work is a job in `src/jobs.ts`, which the manifest declares
|
|
27
|
+
(`jobs`, with an optional `cron` in UTC and `retry`). Queue one inside the
|
|
28
|
+
change that needs it, `enqueueJob(context.tx, { orgId, name, payload })` from
|
|
29
|
+
`@erxes/plugin-sdk`, so it exists only if the change commits. Core POSTs it
|
|
30
|
+
to `/api/jobs/<name>`, which `src/server.ts` checks with `verifyCoreJob`, and
|
|
31
|
+
retries until it answers 2xx. A job may run twice: key its effect by `jobId`.
|
|
26
32
|
- Tests: `vite-plus/test`, run with `call(procedure, input, { context: { member } })`.
|
|
27
33
|
`src/test/setup.ts` gives each file its own database, owned by a role without
|
|
28
34
|
BYPASSRLS, so row-level security is real in tests.
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"db:check": "drizzle-kit check"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@erxes/core-contract": "0.1.0-v2.
|
|
25
|
-
"@erxes/plugin-sdk": "0.1.0-v2.
|
|
24
|
+
"@erxes/core-contract": "0.1.0-v2.7",
|
|
25
|
+
"@erxes/plugin-sdk": "0.1.0-v2.6",
|
|
26
26
|
"@opentelemetry/instrumentation-express": "^0.73.0",
|
|
27
27
|
"@opentelemetry/instrumentation-http": "^0.301.1",
|
|
28
28
|
"@opentelemetry/instrumentation-pg": "^0.78.0",
|
|
@@ -35,6 +35,17 @@ describe('server', () => {
|
|
|
35
35
|
).toBe(401);
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
+
it("refuses a job without core's token", async () => {
|
|
39
|
+
const job = { jobId: 'j1', orgId: 'o1', name: 'items.count', attempt: 1, payload: {} };
|
|
40
|
+
|
|
41
|
+
const response = await fetch(`${base}/api/jobs/items.count`, {
|
|
42
|
+
method: 'POST',
|
|
43
|
+
body: JSON.stringify(job),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(response.status).toBe(401);
|
|
47
|
+
});
|
|
48
|
+
|
|
38
49
|
it('lets web call it from the browser', async () => {
|
|
39
50
|
const response = await fetch(`${base}/api/rpc/items/list`, {
|
|
40
51
|
method: 'OPTIONS',
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PluginManifest } from '@erxes/core-contract';
|
|
2
2
|
import { createCoreClient } from '@erxes/plugin-sdk';
|
|
3
3
|
import { config, PLUGIN } from '#/config.ts';
|
|
4
|
+
import { manifestJobs } from '#/jobs.ts';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Core, called as this plugin: `core.forOrg(orgId).customers.get({ id })`.
|
|
@@ -20,8 +21,9 @@ export const PERMISSIONS = {
|
|
|
20
21
|
} as const;
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
|
-
* What the plugin adds to core, sent on every start.
|
|
24
|
-
* starts with `__id__
|
|
24
|
+
* What the plugin adds to core, sent on every start. Every permission,
|
|
25
|
+
* event and kind it names starts with `__id__.`; job names are its own.
|
|
26
|
+
* A new scope waits for the owner's approval.
|
|
25
27
|
*/
|
|
26
28
|
export const manifest = {
|
|
27
29
|
title: '__title__',
|
|
@@ -34,4 +36,5 @@ export const manifest = {
|
|
|
34
36
|
{ action: PERMISSIONS.view, label: 'View items', group: '__title__' },
|
|
35
37
|
{ action: PERMISSIONS.manage, label: 'Create and change items', group: '__title__' },
|
|
36
38
|
],
|
|
39
|
+
jobs: manifestJobs,
|
|
37
40
|
} satisfies PluginManifest;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { PluginJobDelivery } from '@erxes/core-contract';
|
|
2
|
+
import { withOrg } from '#/db/client.ts';
|
|
3
|
+
import { items } from '#/db/schema.ts';
|
|
4
|
+
import { logger } from '#/logger.ts';
|
|
5
|
+
|
|
6
|
+
interface Job {
|
|
7
|
+
/** A cron schedule in UTC: core runs the job for each organisation that has the plugin. */
|
|
8
|
+
cron?: string;
|
|
9
|
+
/** Throws to retry. A job can run more than once, so make it safe to repeat. */
|
|
10
|
+
run: (job: PluginJobDelivery) => Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The plugin's jobs, by name. Core POSTs each to `/api/jobs/<name>` and
|
|
15
|
+
* retries until it answers 2xx. Queue one in a procedure's transaction:
|
|
16
|
+
* `await enqueueJob(context.tx, { orgId, name: 'items.count', payload: {} })`.
|
|
17
|
+
*/
|
|
18
|
+
const JOBS: Record<string, Job> = {
|
|
19
|
+
'items.count': {
|
|
20
|
+
cron: '0 3 * * *',
|
|
21
|
+
run: async (job) => {
|
|
22
|
+
const count = await withOrg(job.orgId, (tx) => tx.$count(items));
|
|
23
|
+
|
|
24
|
+
logger.info({ orgId: job.orgId, count }, 'items counted');
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** The manifest's `jobs`. */
|
|
30
|
+
export const manifestJobs = Object.entries(JOBS).map(([name, { cron }]) =>
|
|
31
|
+
cron ? { name, cron } : { name },
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
export const runJob = async (job: PluginJobDelivery) => {
|
|
35
|
+
const found = JOBS[job.name];
|
|
36
|
+
|
|
37
|
+
if (!found) throw new Error(`No job ${job.name}`);
|
|
38
|
+
|
|
39
|
+
await found.run(job);
|
|
40
|
+
};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { bearerOf, verifyPluginSession } from '@erxes/plugin-sdk';
|
|
1
|
+
import { bearerOf, verifyCoreJob, verifyPluginSession } from '@erxes/plugin-sdk';
|
|
2
2
|
import { onError, ORPCError } from '@orpc/server';
|
|
3
3
|
import { RPCHandler } from '@orpc/server/node';
|
|
4
4
|
import cors from 'cors';
|
|
5
5
|
import express from 'express';
|
|
6
6
|
import { pinoHttp } from 'pino-http';
|
|
7
7
|
import { config, PLUGIN } from '#/config.ts';
|
|
8
|
+
import { runJob } from '#/jobs.ts';
|
|
8
9
|
import { logger } from '#/logger.ts';
|
|
9
10
|
import { router } from '#/router.ts';
|
|
10
11
|
|
|
@@ -52,6 +53,26 @@ export const createApp = () => {
|
|
|
52
53
|
if (!matched) next();
|
|
53
54
|
});
|
|
54
55
|
|
|
56
|
+
// Core's jobs (`api/src/jobs.ts`). The token signs the raw body, so it stays text.
|
|
57
|
+
app.post('/api/jobs/:name', express.text({ type: '*/*' }), async (req, res) => {
|
|
58
|
+
const body = typeof req.body === 'string' ? req.body : '';
|
|
59
|
+
|
|
60
|
+
const job = await verifyCoreJob(req.headers.authorization, body, {
|
|
61
|
+
coreUrl: config.coreUrl,
|
|
62
|
+
plugin: PLUGIN,
|
|
63
|
+
}).catch(() => null);
|
|
64
|
+
|
|
65
|
+
if (!job) {
|
|
66
|
+
res.sendStatus(401);
|
|
67
|
+
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// A throw answers 500 and core retries it.
|
|
72
|
+
await runJob(job);
|
|
73
|
+
res.sendStatus(204);
|
|
74
|
+
});
|
|
75
|
+
|
|
55
76
|
if (config.uiDir) app.use('/ui', express.static(config.uiDir));
|
|
56
77
|
|
|
57
78
|
return app;
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
-- checks: its tables have no row level security, a job carries its
|
|
21
21
|
-- organisation in its data, and failed jobs are read by the scope's
|
|
22
22
|
-- organisation. So no plugin role and not PUBLIC may use it; plugins queue
|
|
23
|
-
-- work through core_events and its insert trigger
|
|
23
|
+
-- work through core_events and its insert trigger, and their own jobs
|
|
24
|
+
-- through core_enqueue_job().
|
|
24
25
|
create temp table no_org_id (table_name text primary key, tenant_key text, reason text not null);
|
|
25
26
|
insert into no_org_id values (format('__drizzle_migrations_%s', :'scope'), null, 'The migration log.');
|
|
26
27
|
\ir no-org-id.sql
|