@openinc/parse-server-opendash 4.29.0 → 4.30.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.
@@ -1,21 +1,30 @@
1
1
  /**
2
- * Gives every source without a tag one — a Parse Dashboard job, run from
3
- * Jobs → "Schedule a job", then "Run now" on the scheduled entry.
2
+ * Gives every source without a tag one — run from the "Missing tags" button
3
+ * in the source admin view (`MissingSourceTagsButton`, @opendash/plugin-parse).
4
4
  *
5
- * Some instances have `OD3_Source` rows whose `tag` was never set. The job sets
6
- * it to `tenant-<tenant id>-<source id>`, which is unique by construction, so
7
- * the duplicate check in `hooks/Source.ts` never has anything to object to.
5
+ * Some instances have `OD3_Source` rows whose `tag` was never set. They get
6
+ * `tenant-<tenant id>-<source id>`, which is unique by construction, so the
7
+ * duplicate check in `hooks/Source.ts` never has anything to object to. A
8
+ * source without a tenant is left alone: there is nothing to build the tag
9
+ * from.
8
10
  *
9
- * Saved with the master key, so the tag permission checks in that hook pass,
10
- * and its afterSave creates the `AccessPermissions` row a tagged source gets —
11
- * exactly as if the tag had been entered by hand.
11
+ * Params: `{ dryRun: true }` only lists the sources and the tag each would
12
+ * get; without it they are saved. Either way the answer is one row per source
13
+ * without a tag, so the admin view shows the same table before and after.
12
14
  *
13
- * A source that already has a tag is left alone, so the job is safe to repeat.
14
- * A source without a tenant is skipped and its id logged: there is nothing to
15
- * build the tag from.
15
+ * Gated on `opendash:can-admin-tenants` (`Permissions.CORE.tenant_admin`): it
16
+ * reaches into every tenant's sources. That permission is registered on
17
+ * startup and held by od-admin only. The source permission
18
+ * `opendash:source:write-tag-ignore-tenant-prefix` would describe the tag
19
+ * better, but it is never registered — on most instances nobody holds it.
16
20
  *
17
- * Parameters (optional): `{"dryRun": true}` only counts what would change.
21
+ * The sources are read with the caller's session, so a source whose ACL hides
22
+ * it from the caller is not touched. They are saved with the master key: the
23
+ * generated tag ignores the tenant's tag prefix, and with a session the Source
24
+ * beforeSave would demand exactly that unregistered permission. The afterSave
25
+ * creates the `AccessPermissions` row a tagged source gets, as for a tag
26
+ * entered in the form.
18
27
  *
19
- * @param name The job name, derived from the file name.
28
+ * @param name The function name, derived from the file name.
20
29
  */
21
30
  export declare function init(name: string): Promise<void>;
@@ -5,81 +5,75 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.init = init;
7
7
  const node_1 = __importDefault(require("parse/node"));
8
- const index_js_1 = require("../types/index.js");
8
+ const index_js_1 = require("../features/permissions/index.js");
9
+ const Permissions_js_1 = require("../features/permissions/types/Permissions.js");
10
+ const index_js_2 = require("../types/index.js");
9
11
  /**
10
- * Gives every source without a tag one — a Parse Dashboard job, run from
11
- * Jobs → "Schedule a job", then "Run now" on the scheduled entry.
12
+ * Gives every source without a tag one — run from the "Missing tags" button
13
+ * in the source admin view (`MissingSourceTagsButton`, @opendash/plugin-parse).
12
14
  *
13
- * Some instances have `OD3_Source` rows whose `tag` was never set. The job sets
14
- * it to `tenant-<tenant id>-<source id>`, which is unique by construction, so
15
- * the duplicate check in `hooks/Source.ts` never has anything to object to.
15
+ * Some instances have `OD3_Source` rows whose `tag` was never set. They get
16
+ * `tenant-<tenant id>-<source id>`, which is unique by construction, so the
17
+ * duplicate check in `hooks/Source.ts` never has anything to object to. A
18
+ * source without a tenant is left alone: there is nothing to build the tag
19
+ * from.
16
20
  *
17
- * Saved with the master key, so the tag permission checks in that hook pass,
18
- * and its afterSave creates the `AccessPermissions` row a tagged source gets —
19
- * exactly as if the tag had been entered by hand.
21
+ * Params: `{ dryRun: true }` only lists the sources and the tag each would
22
+ * get; without it they are saved. Either way the answer is one row per source
23
+ * without a tag, so the admin view shows the same table before and after.
20
24
  *
21
- * A source that already has a tag is left alone, so the job is safe to repeat.
22
- * A source without a tenant is skipped and its id logged: there is nothing to
23
- * build the tag from.
25
+ * Gated on `opendash:can-admin-tenants` (`Permissions.CORE.tenant_admin`): it
26
+ * reaches into every tenant's sources. That permission is registered on
27
+ * startup and held by od-admin only. The source permission
28
+ * `opendash:source:write-tag-ignore-tenant-prefix` would describe the tag
29
+ * better, but it is never registered — on most instances nobody holds it.
24
30
  *
25
- * Parameters (optional): `{"dryRun": true}` only counts what would change.
31
+ * The sources are read with the caller's session, so a source whose ACL hides
32
+ * it from the caller is not touched. They are saved with the master key: the
33
+ * generated tag ignores the tenant's tag prefix, and with a session the Source
34
+ * beforeSave would demand exactly that unregistered permission. The afterSave
35
+ * creates the `AccessPermissions` row a tagged source gets, as for a tag
36
+ * entered in the form.
26
37
  *
27
- * @param name The job name, derived from the file name.
38
+ * @param name The function name, derived from the file name.
28
39
  */
29
40
  async function init(name) {
30
- node_1.default.Cloud.job(name, async function (request) {
31
- // "Run now" in the dashboard posts the schedule's parameters as `input`,
32
- // next to its own bookkeeping fields; a direct `POST /jobs/<name>` sends
33
- // them as the body itself.
34
- const params = request.params.input ?? request.params;
35
- const dryRun = params?.dryRun === true;
36
- const verb = dryRun ? "would be tagged" : "tagged";
37
- let checked = 0;
38
- let tagged = 0;
39
- const withoutTenant = [];
40
- const failed = [];
41
- await new node_1.default.Query(index_js_1.Source).each(async (source) => {
42
- checked++;
43
- // An empty string counts as missing too — the Source hooks treat it
44
- // the same way.
41
+ node_1.default.Cloud.define(name, async (request) => {
42
+ const sessionToken = request.user?.getSessionToken() ?? undefined;
43
+ await (0, index_js_1.requirePermission)({ sessionToken, master: request.master }, Permissions_js_1.Permissions.CORE.tenant_admin, "You are not allowed to tag sources");
44
+ const dryRun = request.params.dryRun === true;
45
+ const readOptions = request.master
46
+ ? { useMasterKey: true }
47
+ : { sessionToken };
48
+ const rows = [];
49
+ await new node_1.default.Query(index_js_2.Source).include("tenant").each(async (source) => {
50
+ // An empty string counts as missing too — the Source hooks treat it the
51
+ // same way.
45
52
  if (source.get("tag"))
46
53
  return;
47
54
  const tenant = source.get("tenant");
48
- if (!tenant?.id) {
49
- withoutTenant.push(source.id);
55
+ const row = {
56
+ objectId: source.id,
57
+ name: source.get("name") ?? null,
58
+ tenant: tenant ? (tenant.get("label") ?? tenant.id) : null,
59
+ tag: tenant ? `tenant-${tenant.id}-${source.id}` : null,
60
+ status: tenant ? "pending" : "no_tenant",
61
+ };
62
+ rows.push(row);
63
+ if (dryRun || !row.tag)
50
64
  return;
65
+ // One bad row must not stop the rest; it is reported in its row.
66
+ try {
67
+ source.set("tag", row.tag);
68
+ await source.save(null, { useMasterKey: true });
69
+ row.status = "tagged";
51
70
  }
52
- if (!dryRun) {
53
- try {
54
- source.set("tag", `tenant-${tenant.id}-${source.id}`);
55
- await source.save(null, { useMasterKey: true });
56
- }
57
- catch (error) {
58
- // One bad row must not stop the rest; it is reported at the end.
59
- failed.push(source.id);
60
- console.error(`[@openinc/parse-server-opendash][${name}] Could not tag source ${source.id}:`, error);
61
- return;
62
- }
71
+ catch (error) {
72
+ row.status = "failed";
73
+ row.error = error instanceof Error ? error.message : String(error);
74
+ console.error(`[@openinc/parse-server-opendash][${name}] Could not tag source ${source.id}:`, error);
63
75
  }
64
- tagged++;
65
- if (tagged % 100 === 0) {
66
- request.message(`${tagged} sources ${verb} so far…`);
67
- }
68
- }, { useMasterKey: true });
69
- if (withoutTenant.length > 0) {
70
- console.warn(`[@openinc/parse-server-opendash][${name}] Sources without tag and tenant, skipped: ${withoutTenant.join(", ")}`);
71
- }
72
- const summary = `${dryRun ? "Dry run — nothing saved. " : ""}` +
73
- `${checked} sources checked, ` +
74
- `${tagged} ${verb}, ` +
75
- `${withoutTenant.length} skipped without tenant, ` +
76
- `${failed.length} failed.`;
77
- console.log(`[@openinc/parse-server-opendash][${name}] ${summary}`);
78
- // Thrown so the job status reads "failed" in the dashboard; the message of
79
- // a native Error ends up in the status row just like a returned string.
80
- if (failed.length > 0) {
81
- throw new Error(`${summary} Failed ids: ${failed.join(", ")}`);
82
- }
83
- return summary;
76
+ }, readOptions);
77
+ return rows;
84
78
  });
85
79
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openinc/parse-server-opendash",
3
- "version": "4.29.0",
3
+ "version": "4.30.0",
4
4
  "description": "Parse Server Cloud Code for open.INC Stack.",
5
5
  "packageManager": "pnpm@12.8.1",
6
6
  "keywords": [