@ruiapp/rapid-core 0.1.36 → 0.1.37

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
@@ -3339,12 +3339,12 @@ async function handler$h(plugin, ctx, options) {
3339
3339
  if (operation) {
3340
3340
  delete mergedInput.$operation;
3341
3341
  }
3342
- const stateProperty = mergedInput.$stateProperty;
3343
- if (stateProperty) {
3344
- delete mergedInput.$stateProperty;
3342
+ const stateProperties = mergedInput.$stateProperties;
3343
+ if (stateProperties) {
3344
+ delete mergedInput.$stateProperties;
3345
3345
  }
3346
3346
  const entityManager = server.getEntityManager(options.singularCode);
3347
- const output = await entityManager.updateEntityById({ id: mergedInput.id, entityToSave: mergedInput, operation, stateProperty }, plugin);
3347
+ const output = await entityManager.updateEntityById({ id: mergedInput.id, entityToSave: mergedInput, operation, stateProperties }, plugin);
3348
3348
  ctx.output = output;
3349
3349
  }
3350
3350
 
@@ -5259,36 +5259,45 @@ class StateMachinePlugin {
5259
5259
  */
5260
5260
  async beforeUpdateEntity(server, model, options, currentEntity) {
5261
5261
  const entity = options.entityToSave;
5262
+ const stateMachineEnabledProperties = [];
5262
5263
  for (const property of model.properties) {
5263
5264
  const isStateMachineEnabled = lodash.get(property.config, "stateMachine.enabled", false);
5264
- const isTransferControlEnabled = lodash.get(property.config, "stateMachine.transferControl", false);
5265
- if (isStateMachineEnabled && isTransferControlEnabled && !isNullOrUndefined(entity[property.code])) {
5266
- throw new Error(`You're not allowed to change '${property.code}' property directly when transfer control is enabled, do an operation instead.`);
5265
+ if (isStateMachineEnabled) {
5266
+ stateMachineEnabledProperties.push(property);
5267
+ const isTransferControlEnabled = lodash.get(property.config, "stateMachine.transferControl", false);
5268
+ if (isTransferControlEnabled && !isNullOrUndefined(entity[property.code])) {
5269
+ throw new Error(`You're not allowed to change '${property.code}' property directly when transfer control is enabled, do an operation instead.`);
5270
+ }
5267
5271
  }
5268
5272
  }
5269
5273
  if (!options.operation) {
5270
5274
  return;
5271
5275
  }
5272
- const stateMachineEnabledProperties = lodash.filter(model.properties, (property) => lodash.get(property.config, "stateMachine.enabled", false));
5273
- let stateMachineEnabledProperty = lodash.first(stateMachineEnabledProperties);
5274
- if (options.stateProperty) {
5275
- stateMachineEnabledProperty = lodash.find(stateMachineEnabledProperties, (property) => property.code === options.stateProperty);
5276
+ let statePropertiesToUpdate;
5277
+ const statePropertyCodes = options.stateProperties;
5278
+ if (statePropertyCodes && statePropertyCodes.length) {
5279
+ statePropertiesToUpdate = lodash.filter(stateMachineEnabledProperties, (property) => statePropertyCodes.includes(property.code));
5280
+ }
5281
+ else {
5282
+ statePropertiesToUpdate = stateMachineEnabledProperties;
5276
5283
  }
5277
- if (!stateMachineEnabledProperty) {
5284
+ if (!statePropertiesToUpdate.length) {
5278
5285
  throw new Error(`State machine property not found.`);
5279
5286
  }
5280
- const machineConfig = lodash.get(stateMachineEnabledProperty.config, "stateMachine.config", null);
5281
- if (!machineConfig) {
5282
- throw new Error(`State machine of property '${stateMachineEnabledProperty.code}' not configured.`);
5287
+ for (const statePropertyToUpdate of statePropertiesToUpdate) {
5288
+ const machineConfig = lodash.get(statePropertyToUpdate.config, "stateMachine.config", null);
5289
+ if (!machineConfig) {
5290
+ throw new Error(`State machine of property '${statePropertyToUpdate.code}' not configured.`);
5291
+ }
5292
+ machineConfig.id = getStateMachineCode(model, statePropertyToUpdate);
5293
+ const nextSnapshot = await getStateMachineNextSnapshot(server, {
5294
+ machineConfig,
5295
+ context: {},
5296
+ currentState: currentEntity[statePropertyToUpdate.code],
5297
+ event: options.operation,
5298
+ });
5299
+ entity[statePropertyToUpdate.code] = nextSnapshot.value;
5283
5300
  }
5284
- machineConfig.id = getStateMachineCode(model, stateMachineEnabledProperty);
5285
- const nextSnapshot = await getStateMachineNextSnapshot(server, {
5286
- machineConfig,
5287
- context: {},
5288
- currentState: currentEntity[stateMachineEnabledProperty.code],
5289
- event: options.operation,
5290
- });
5291
- entity[stateMachineEnabledProperty.code] = nextSnapshot.value;
5292
5301
  }
5293
5302
  }
5294
5303
  function getStateMachineCode(model, property) {
package/dist/types.d.ts CHANGED
@@ -355,7 +355,7 @@ export interface UpdateEntityByIdOptions {
355
355
  id: any;
356
356
  entityToSave: any;
357
357
  operation?: any;
358
- stateProperty?: string;
358
+ stateProperties?: string[];
359
359
  }
360
360
  export interface DeleteEntityOptions {
361
361
  filters?: EntityFilterOptions[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.1.36",
3
+ "version": "0.1.37",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -21,12 +21,12 @@ export async function handler(
21
21
  delete mergedInput.$operation;
22
22
  }
23
23
 
24
- const stateProperty = mergedInput.$stateProperty;
25
- if (stateProperty) {
26
- delete mergedInput.$stateProperty;
24
+ const stateProperties = mergedInput.$stateProperties;
25
+ if (stateProperties) {
26
+ delete mergedInput.$stateProperties;
27
27
  }
28
28
 
29
29
  const entityManager = server.getEntityManager(options.singularCode);
30
- const output = await entityManager.updateEntityById({ id: mergedInput.id, entityToSave: mergedInput, operation, stateProperty }, plugin);
30
+ const output = await entityManager.updateEntityById({ id: mergedInput.id, entityToSave: mergedInput, operation, stateProperties }, plugin);
31
31
  ctx.output = output;
32
32
  }
@@ -127,12 +127,17 @@ class StateMachinePlugin implements RapidPlugin {
127
127
  async beforeUpdateEntity(server: IRpdServer, model: RpdDataModel, options: UpdateEntityByIdOptions, currentEntity: any) {
128
128
  const entity = options.entityToSave;
129
129
 
130
+ const stateMachineEnabledProperties: RpdDataModelProperty[] = [];
130
131
  for (const property of model.properties) {
131
132
  const isStateMachineEnabled = get(property.config, "stateMachine.enabled", false);
132
- const isTransferControlEnabled = get(property.config, "stateMachine.transferControl", false);
133
- if (isStateMachineEnabled && isTransferControlEnabled && !isNullOrUndefined(entity[property.code])
134
- ) {
135
- throw new Error(`You're not allowed to change '${property.code}' property directly when transfer control is enabled, do an operation instead.`);
133
+
134
+ if (isStateMachineEnabled) {
135
+ stateMachineEnabledProperties.push(property);
136
+
137
+ const isTransferControlEnabled = get(property.config, "stateMachine.transferControl", false);
138
+ if (isTransferControlEnabled && !isNullOrUndefined(entity[property.code])) {
139
+ throw new Error(`You're not allowed to change '${property.code}' property directly when transfer control is enabled, do an operation instead.`);
140
+ }
136
141
  }
137
142
  }
138
143
 
@@ -140,32 +145,35 @@ class StateMachinePlugin implements RapidPlugin {
140
145
  return;
141
146
  }
142
147
 
143
- const stateMachineEnabledProperties = filter(model.properties, (property) => get(property.config, "stateMachine.enabled", false)) as RpdDataModelProperty[];
144
- let stateMachineEnabledProperty = first(stateMachineEnabledProperties);
145
- if (options.stateProperty) {
146
- stateMachineEnabledProperty = find(stateMachineEnabledProperties, (property) => property.code === options.stateProperty);
148
+ let statePropertiesToUpdate: RpdDataModelProperty[];
149
+ const statePropertyCodes = options.stateProperties;
150
+ if (statePropertyCodes && statePropertyCodes.length) {
151
+ statePropertiesToUpdate = filter(stateMachineEnabledProperties, (property) => statePropertyCodes.includes(property.code));
152
+ } else {
153
+ statePropertiesToUpdate = stateMachineEnabledProperties;
147
154
  }
148
155
 
149
- if (!stateMachineEnabledProperty) {
156
+ if (!statePropertiesToUpdate.length) {
150
157
  throw new Error(`State machine property not found.`);
151
158
  }
152
159
 
153
- const machineConfig = get(stateMachineEnabledProperty.config, "stateMachine.config", null);
154
- if (!machineConfig) {
155
- throw new Error(`State machine of property '${stateMachineEnabledProperty.code}' not configured.`);
160
+ for (const statePropertyToUpdate of statePropertiesToUpdate) {
161
+ const machineConfig = get(statePropertyToUpdate.config, "stateMachine.config", null);
162
+ if (!machineConfig) {
163
+ throw new Error(`State machine of property '${statePropertyToUpdate.code}' not configured.`);
164
+ }
165
+ machineConfig.id = getStateMachineCode(model, statePropertyToUpdate);
166
+
167
+ const nextSnapshot = await getStateMachineNextSnapshot(server, {
168
+ machineConfig,
169
+ context: {},
170
+ currentState: currentEntity[statePropertyToUpdate.code],
171
+ event: options.operation,
172
+ });
173
+
174
+ entity[statePropertyToUpdate.code] = nextSnapshot.value;
156
175
  }
157
- machineConfig.id = getStateMachineCode(model, stateMachineEnabledProperty);
158
-
159
- const nextSnapshot = await getStateMachineNextSnapshot(server, {
160
- machineConfig,
161
- context: {},
162
- currentState: currentEntity[stateMachineEnabledProperty.code],
163
- event: options.operation,
164
- });
165
-
166
- entity[stateMachineEnabledProperty.code] = nextSnapshot.value;
167
176
  }
168
-
169
177
  }
170
178
 
171
179
  function getStateMachineCode(model: RpdDataModel, property: RpdDataModelProperty) {
package/src/types.ts CHANGED
@@ -474,7 +474,7 @@ export interface UpdateEntityByIdOptions {
474
474
  id: any;
475
475
  entityToSave: any;
476
476
  operation?: any;
477
- stateProperty?: string;
477
+ stateProperties?: string[];
478
478
  }
479
479
 
480
480
  export interface DeleteEntityOptions {