@nocobase/actions 2.2.0 → 2.2.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.
@@ -0,0 +1,12 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { Context } from '..';
10
+ type CompoundActionName = 'firstOrCreate' | 'updateOrCreate';
11
+ export declare function compoundAction(actionName: CompoundActionName): (ctx: Context, next: any) => Promise<void>;
12
+ export {};
@@ -0,0 +1,181 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
17
+ var __export = (target, all) => {
18
+ for (var name in all)
19
+ __defProp(target, name, { get: all[name], enumerable: true });
20
+ };
21
+ var __copyProps = (to, from, except, desc) => {
22
+ if (from && typeof from === "object" || typeof from === "function") {
23
+ for (let key of __getOwnPropNames(from))
24
+ if (!__hasOwnProp.call(to, key) && key !== except)
25
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
26
+ }
27
+ return to;
28
+ };
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+ var compound_action_exports = {};
39
+ __export(compound_action_exports, {
40
+ compoundAction: () => compoundAction
41
+ });
42
+ module.exports = __toCommonJS(compound_action_exports);
43
+ var import_database = require("@nocobase/database");
44
+ var import_lodash = __toESM(require("lodash"));
45
+ var import_utils = require("../utils");
46
+ const findParamKeys = ["fields", "appends", "except", "filter", "targetCollection"];
47
+ const createParamKeys = ["values", "whitelist", "blacklist", "updateAssociationValues", "targetCollection"];
48
+ const updateParamKeys = [
49
+ "values",
50
+ "whitelist",
51
+ "blacklist",
52
+ "filter",
53
+ "updateAssociationValues",
54
+ "forceUpdate",
55
+ "targetCollection"
56
+ ];
57
+ function getTargetCollection(repository) {
58
+ return repository.targetCollection || repository.collection;
59
+ }
60
+ __name(getTargetCollection, "getTargetCollection");
61
+ function getCompoundActionRepository(ctx) {
62
+ return ctx.getCurrentRepository ? ctx.getCurrentRepository() : (0, import_utils.getRepositoryFromParams)(ctx);
63
+ }
64
+ __name(getCompoundActionRepository, "getCompoundActionRepository");
65
+ async function withCurrentTransaction(ctx, callback) {
66
+ const database = ctx.database ?? ctx.db;
67
+ if (!(database == null ? void 0 : database.sequelize)) {
68
+ return callback();
69
+ }
70
+ return database.sequelize.transaction(callback);
71
+ }
72
+ __name(withCurrentTransaction, "withCurrentTransaction");
73
+ async function applyActualActionPermission(ctx, actionName, originalParams) {
74
+ var _a;
75
+ if (((_a = ctx.permission) == null ? void 0 : _a.deferred) !== true || !["firstOrCreate", "updateOrCreate"].includes(ctx.action.actionName)) {
76
+ ctx.throw(403, "No permissions");
77
+ }
78
+ const { resourceName } = ctx.permission;
79
+ const can = ctx.can({
80
+ resource: resourceName,
81
+ action: actionName,
82
+ rawResourceName: ctx.action.resourceName
83
+ });
84
+ if (!can || typeof can !== "object") {
85
+ ctx.throw(403, "No permissions");
86
+ }
87
+ const resolved = await ctx.acl.resolveActionParams(ctx, {
88
+ actionName,
89
+ params: originalParams,
90
+ resourceName: ctx.action.resourceName,
91
+ useCurrentRepository: true
92
+ });
93
+ ctx.action.params = resolved.mergedParams;
94
+ ctx.permission.can = can;
95
+ ctx.permission.parsedParams = resolved.parsedParams;
96
+ ctx.permission.rawParams = resolved.rawParams;
97
+ ctx.permission.mergedParams = import_lodash.default.cloneDeep(resolved.mergedParams);
98
+ ctx.permission.actualActionName = actionName;
99
+ ctx.permission.deferred = false;
100
+ }
101
+ __name(applyActualActionPermission, "applyActualActionPermission");
102
+ function compoundAction(actionName) {
103
+ return /* @__PURE__ */ __name(async function compoundActionHandler(ctx, next) {
104
+ var _a;
105
+ const repository = getCompoundActionRepository(ctx);
106
+ if (!ctx.acl) {
107
+ const compoundParams = {
108
+ filterKeys: ctx.action.params.filterKeys,
109
+ values: ctx.action.params.values,
110
+ whitelist: ctx.action.params.whitelist,
111
+ blacklist: ctx.action.params.blacklist,
112
+ updateAssociationValues: ctx.action.params.updateAssociationValues,
113
+ targetCollection: ctx.action.params.targetCollection,
114
+ context: ctx
115
+ };
116
+ ctx.body = actionName === "firstOrCreate" ? await repository.firstOrCreate(compoundParams) : await repository.updateOrCreate(compoundParams);
117
+ ctx.status = 200;
118
+ await next();
119
+ return;
120
+ }
121
+ if (((_a = ctx.permission) == null ? void 0 : _a.deferred) !== true || ctx.action.actionName !== actionName) {
122
+ ctx.throw(403, "No permissions");
123
+ }
124
+ const originalParams = import_lodash.default.cloneDeep(ctx.action.params);
125
+ const { filterKeys, values } = originalParams;
126
+ const filter = import_database.Repository.valuesToFilter(values, filterKeys);
127
+ ctx.body = await withCurrentTransaction(ctx, async (transaction) => {
128
+ const instance = await repository.findOne({ filter, transaction, context: ctx });
129
+ if (!instance) {
130
+ await applyActualActionPermission(ctx, "create", originalParams);
131
+ return repository.create({
132
+ values: ctx.action.params.values,
133
+ ...import_lodash.default.pick(
134
+ ctx.action.params,
135
+ createParamKeys.filter((key) => key !== "values")
136
+ ),
137
+ transaction,
138
+ context: ctx
139
+ });
140
+ }
141
+ const targetCollection = getTargetCollection(repository);
142
+ const targetKey = targetCollection.filterTargetKey || targetCollection.model.primaryKeyAttribute;
143
+ const filterByTk = instance.get(targetKey);
144
+ if (actionName === "firstOrCreate") {
145
+ await applyActualActionPermission(ctx, "get", originalParams);
146
+ const result2 = await repository.findOne({
147
+ ...import_lodash.default.pick(ctx.action.params, findParamKeys),
148
+ filterByTk,
149
+ transaction,
150
+ context: ctx
151
+ });
152
+ if (!result2) {
153
+ ctx.throw(403, "No permissions");
154
+ }
155
+ return result2;
156
+ }
157
+ await applyActualActionPermission(ctx, "update", originalParams);
158
+ const result = await repository.update({
159
+ values: ctx.action.params.values,
160
+ ...import_lodash.default.pick(
161
+ ctx.action.params,
162
+ updateParamKeys.filter((key) => key !== "values")
163
+ ),
164
+ filterByTk,
165
+ transaction,
166
+ context: ctx
167
+ });
168
+ if (!result || Array.isArray(result) && result.length === 0) {
169
+ ctx.throw(403, "No permissions");
170
+ }
171
+ return result;
172
+ });
173
+ ctx.status = 200;
174
+ await next();
175
+ }, "compoundActionHandler");
176
+ }
177
+ __name(compoundAction, "compoundAction");
178
+ // Annotate the CommonJS export names for ESM import in node:
179
+ 0 && (module.exports = {
180
+ compoundAction
181
+ });
@@ -29,11 +29,8 @@ __export(first_or_create_exports, {
29
29
  firstOrCreate: () => firstOrCreate
30
30
  });
31
31
  module.exports = __toCommonJS(first_or_create_exports);
32
- var import_proxy_to_repository = require("./proxy-to-repository");
33
- const firstOrCreate = (0, import_proxy_to_repository.proxyToRepository)(
34
- ["values", "filterKeys", "whitelist", "blacklist", "updateAssociationValues", "targetCollection"],
35
- "firstOrCreate"
36
- );
32
+ var import_compound_action = require("./compound-action");
33
+ const firstOrCreate = (0, import_compound_action.compoundAction)("firstOrCreate");
37
34
  // Annotate the CommonJS export names for ESM import in node:
38
35
  0 && (module.exports = {
39
36
  firstOrCreate
@@ -29,11 +29,8 @@ __export(update_or_create_exports, {
29
29
  updateOrCreate: () => updateOrCreate
30
30
  });
31
31
  module.exports = __toCommonJS(update_or_create_exports);
32
- var import_proxy_to_repository = require("./proxy-to-repository");
33
- const updateOrCreate = (0, import_proxy_to_repository.proxyToRepository)(
34
- ["values", "filterKeys", "whitelist", "blacklist", "updateAssociationValues", "targetCollection"],
35
- "updateOrCreate"
36
- );
32
+ var import_compound_action = require("./compound-action");
33
+ const updateOrCreate = (0, import_compound_action.compoundAction)("updateOrCreate");
37
34
  // Annotate the CommonJS export names for ESM import in node:
38
35
  0 && (module.exports = {
39
36
  updateOrCreate
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@nocobase/actions",
3
- "version": "2.2.0",
3
+ "version": "2.2.2",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./lib/index.js",
7
7
  "types": "./lib/index.d.ts",
8
8
  "dependencies": {
9
- "@nocobase/cache": "2.2.0",
10
- "@nocobase/database": "2.2.0",
11
- "@nocobase/resourcer": "2.2.0"
9
+ "@nocobase/cache": "2.2.2",
10
+ "@nocobase/database": "2.2.2",
11
+ "@nocobase/resourcer": "2.2.2"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
15
15
  "url": "git+https://github.com/nocobase/nocobase.git",
16
16
  "directory": "packages/actions"
17
17
  },
18
- "gitHead": "d5052cae37177054c236f1bf7bba3aed6ef029d0"
18
+ "gitHead": "336230738dda76255e363a0a68fcbec775c81564"
19
19
  }