@ruiapp/rapid-core 0.1.59 → 0.1.60
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
|
@@ -6,9 +6,9 @@ var lodash = require('lodash');
|
|
|
6
6
|
var events = require('events');
|
|
7
7
|
var Router = require('koa-tree-router');
|
|
8
8
|
var qs = require('qs');
|
|
9
|
+
var dayjs = require('dayjs');
|
|
9
10
|
var jsonwebtoken = require('jsonwebtoken');
|
|
10
11
|
var crypto = require('crypto');
|
|
11
|
-
var dayjs = require('dayjs');
|
|
12
12
|
var bcrypt = require('bcrypt');
|
|
13
13
|
var path = require('path');
|
|
14
14
|
var fs = require('fs');
|
|
@@ -38,8 +38,8 @@ function _interopNamespace(e) {
|
|
|
38
38
|
|
|
39
39
|
var Router__default = /*#__PURE__*/_interopDefaultLegacy(Router);
|
|
40
40
|
var qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);
|
|
41
|
-
var crypto__default = /*#__PURE__*/_interopDefaultLegacy(crypto);
|
|
42
41
|
var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
|
|
42
|
+
var crypto__default = /*#__PURE__*/_interopDefaultLegacy(crypto);
|
|
43
43
|
var bcrypt__default = /*#__PURE__*/_interopDefaultLegacy(bcrypt);
|
|
44
44
|
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
45
45
|
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
@@ -1965,6 +1965,10 @@ function getEntityPartChanges(before, after) {
|
|
|
1965
1965
|
return changed;
|
|
1966
1966
|
}
|
|
1967
1967
|
|
|
1968
|
+
function getNowStringWithTimezone() {
|
|
1969
|
+
return dayjs__default["default"]().format("YYYY-MM-DD HH:mm:ss.SSSZ");
|
|
1970
|
+
}
|
|
1971
|
+
|
|
1968
1972
|
function convertEntityOrderByToRowOrderBy(server, model, baseModel, orderByList) {
|
|
1969
1973
|
if (!orderByList) {
|
|
1970
1974
|
return null;
|
|
@@ -2413,6 +2417,17 @@ async function createEntity(server, dataAccessor, options, plugin) {
|
|
|
2413
2417
|
throw newEntityOperationError("Create base entity directly is not allowed.");
|
|
2414
2418
|
}
|
|
2415
2419
|
const { entity, routeContext } = options;
|
|
2420
|
+
const userId = options.routeContext.state?.userId;
|
|
2421
|
+
if (userId) {
|
|
2422
|
+
const createdByProperty = getEntityPropertyByCode(server, model, "createdBy");
|
|
2423
|
+
if (createdByProperty) {
|
|
2424
|
+
entity.createdBy = userId;
|
|
2425
|
+
}
|
|
2426
|
+
}
|
|
2427
|
+
const createdAtProperty = getEntityPropertyByCode(server, model, "createdAt");
|
|
2428
|
+
if (createdAtProperty) {
|
|
2429
|
+
entity.createdAt = getNowStringWithTimezone();
|
|
2430
|
+
}
|
|
2416
2431
|
await server.beforeCreateEntity(model, options);
|
|
2417
2432
|
await server.emitEvent({
|
|
2418
2433
|
eventName: "entity.beforeCreate",
|
|
@@ -2580,7 +2595,7 @@ async function createEntity(server, dataAccessor, options, plugin) {
|
|
|
2580
2595
|
}
|
|
2581
2596
|
async function updateEntityById(server, dataAccessor, options, plugin) {
|
|
2582
2597
|
const model = dataAccessor.getModel();
|
|
2583
|
-
const { id,
|
|
2598
|
+
const { id, routeContext } = options;
|
|
2584
2599
|
if (!id) {
|
|
2585
2600
|
throw new Error("Id is required when updating an entity.");
|
|
2586
2601
|
}
|
|
@@ -2591,11 +2606,23 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
|
|
|
2591
2606
|
if (!entity) {
|
|
2592
2607
|
throw new Error(`${model.namespace}.${model.singularCode} with id "${id}" was not found.`);
|
|
2593
2608
|
}
|
|
2609
|
+
let { entityToSave } = options;
|
|
2594
2610
|
let changes = getEntityPartChanges(entity, entityToSave);
|
|
2595
2611
|
if (!changes && !options.operation) {
|
|
2596
2612
|
return entity;
|
|
2597
2613
|
}
|
|
2598
|
-
|
|
2614
|
+
entityToSave = changes || {};
|
|
2615
|
+
const userId = options.routeContext.state?.userId;
|
|
2616
|
+
if (userId) {
|
|
2617
|
+
const updatedByProperty = getEntityPropertyByCode(server, model, "updatedBy");
|
|
2618
|
+
if (updatedByProperty) {
|
|
2619
|
+
entityToSave.updatedBy = userId;
|
|
2620
|
+
}
|
|
2621
|
+
}
|
|
2622
|
+
const updatedAtProperty = getEntityPropertyByCode(server, model, "updatedAt");
|
|
2623
|
+
if (updatedAtProperty) {
|
|
2624
|
+
entityToSave.updatedAt = getNowStringWithTimezone();
|
|
2625
|
+
}
|
|
2599
2626
|
await server.beforeUpdateEntity(model, options, entity);
|
|
2600
2627
|
await server.emitEvent({
|
|
2601
2628
|
eventName: "entity.beforeUpdate",
|
|
@@ -2603,14 +2630,14 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
|
|
|
2603
2630
|
namespace: model.namespace,
|
|
2604
2631
|
modelSingularCode: model.singularCode,
|
|
2605
2632
|
before: entity,
|
|
2606
|
-
changes:
|
|
2633
|
+
changes: entityToSave,
|
|
2607
2634
|
operation: options.operation,
|
|
2608
2635
|
stateProperties: options.stateProperties,
|
|
2609
2636
|
},
|
|
2610
2637
|
sender: plugin,
|
|
2611
2638
|
routeContext: options.routeContext,
|
|
2612
2639
|
});
|
|
2613
|
-
changes = getEntityPartChanges(entity,
|
|
2640
|
+
changes = getEntityPartChanges(entity, entityToSave);
|
|
2614
2641
|
const oneRelationPropertiesToUpdate = [];
|
|
2615
2642
|
const manyRelationPropertiesToUpdate = [];
|
|
2616
2643
|
lodash.keys(changes).forEach((propertyCode) => {
|
|
@@ -3934,10 +3961,6 @@ async function handler$j(plugin, ctx, options) {
|
|
|
3934
3961
|
const { defaultInput, fixedInput } = options;
|
|
3935
3962
|
const mergedInput = mergeInput(defaultInput, input, fixedInput);
|
|
3936
3963
|
logger.debug(`Running ${code$j} handler...`, { defaultInput, fixedInput, mergedInput });
|
|
3937
|
-
const userId = ctx.routerContext.state?.userId;
|
|
3938
|
-
if (userId) {
|
|
3939
|
-
input.createdBy = userId;
|
|
3940
|
-
}
|
|
3941
3964
|
const entityManager = server.getEntityManager(options.singularCode);
|
|
3942
3965
|
const output = await entityManager.createEntity({
|
|
3943
3966
|
entity: input,
|
package/package.json
CHANGED
|
@@ -26,7 +26,7 @@ import { filter, find, first, forEach, isArray, isObject, keys, map, reject, uni
|
|
|
26
26
|
import { getEntityPropertiesIncludingBase, getEntityProperty, getEntityPropertyByCode } from "./metaHelper";
|
|
27
27
|
import { ColumnQueryOptions, CountRowOptions, FindRowOptions, FindRowOrderByOptions, RowFilterOptions } from "./dataAccessTypes";
|
|
28
28
|
import { newEntityOperationError } from "~/utilities/errorUtility";
|
|
29
|
-
import {
|
|
29
|
+
import { getNowStringWithTimezone } from "~/utilities/timeUtility";
|
|
30
30
|
|
|
31
31
|
function convertEntityOrderByToRowOrderBy(server: IRpdServer, model: RpdDataModel, baseModel?: RpdDataModel, orderByList?: FindEntityOrderByOptions[]) {
|
|
32
32
|
if (!orderByList) {
|
|
@@ -514,6 +514,18 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
|
|
|
514
514
|
|
|
515
515
|
const { entity, routeContext } = options;
|
|
516
516
|
|
|
517
|
+
const userId = options.routeContext.state?.userId;
|
|
518
|
+
if (userId) {
|
|
519
|
+
const createdByProperty = getEntityPropertyByCode(server, model, "createdBy");
|
|
520
|
+
if (createdByProperty) {
|
|
521
|
+
entity.createdBy = userId;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
const createdAtProperty = getEntityPropertyByCode(server, model, "createdAt");
|
|
525
|
+
if (createdAtProperty) {
|
|
526
|
+
entity.createdAt = getNowStringWithTimezone();
|
|
527
|
+
}
|
|
528
|
+
|
|
517
529
|
await server.beforeCreateEntity(model, options);
|
|
518
530
|
|
|
519
531
|
await server.emitEvent({
|
|
@@ -693,7 +705,7 @@ async function createEntity(server: IRpdServer, dataAccessor: IRpdDataAccessor,
|
|
|
693
705
|
|
|
694
706
|
async function updateEntityById(server: IRpdServer, dataAccessor: IRpdDataAccessor, options: UpdateEntityByIdOptions, plugin?: RapidPlugin) {
|
|
695
707
|
const model = dataAccessor.getModel();
|
|
696
|
-
const { id,
|
|
708
|
+
const { id, routeContext } = options;
|
|
697
709
|
if (!id) {
|
|
698
710
|
throw new Error("Id is required when updating an entity.");
|
|
699
711
|
}
|
|
@@ -706,12 +718,26 @@ async function updateEntityById(server: IRpdServer, dataAccessor: IRpdDataAccess
|
|
|
706
718
|
throw new Error(`${model.namespace}.${model.singularCode} with id "${id}" was not found.`);
|
|
707
719
|
}
|
|
708
720
|
|
|
721
|
+
let { entityToSave } = options;
|
|
709
722
|
let changes = getEntityPartChanges(entity, entityToSave);
|
|
710
723
|
if (!changes && !options.operation) {
|
|
711
724
|
return entity;
|
|
712
725
|
}
|
|
713
726
|
|
|
714
|
-
|
|
727
|
+
entityToSave = changes || {};
|
|
728
|
+
|
|
729
|
+
const userId = options.routeContext.state?.userId;
|
|
730
|
+
if (userId) {
|
|
731
|
+
const updatedByProperty = getEntityPropertyByCode(server, model, "updatedBy");
|
|
732
|
+
if (updatedByProperty) {
|
|
733
|
+
entityToSave.updatedBy = userId;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
const updatedAtProperty = getEntityPropertyByCode(server, model, "updatedAt");
|
|
737
|
+
if (updatedAtProperty) {
|
|
738
|
+
entityToSave.updatedAt = getNowStringWithTimezone();
|
|
739
|
+
}
|
|
740
|
+
|
|
715
741
|
await server.beforeUpdateEntity(model, options, entity);
|
|
716
742
|
|
|
717
743
|
await server.emitEvent({
|
|
@@ -720,7 +746,7 @@ async function updateEntityById(server: IRpdServer, dataAccessor: IRpdDataAccess
|
|
|
720
746
|
namespace: model.namespace,
|
|
721
747
|
modelSingularCode: model.singularCode,
|
|
722
748
|
before: entity,
|
|
723
|
-
changes:
|
|
749
|
+
changes: entityToSave,
|
|
724
750
|
operation: options.operation,
|
|
725
751
|
stateProperties: options.stateProperties,
|
|
726
752
|
},
|
|
@@ -728,7 +754,7 @@ async function updateEntityById(server: IRpdServer, dataAccessor: IRpdDataAccess
|
|
|
728
754
|
routeContext: options.routeContext,
|
|
729
755
|
});
|
|
730
756
|
|
|
731
|
-
changes = getEntityPartChanges(entity,
|
|
757
|
+
changes = getEntityPartChanges(entity, entityToSave);
|
|
732
758
|
|
|
733
759
|
const oneRelationPropertiesToUpdate: RpdDataModelProperty[] = [];
|
|
734
760
|
const manyRelationPropertiesToUpdate: RpdDataModelProperty[] = [];
|
|
@@ -12,11 +12,6 @@ export async function handler(plugin: RapidPlugin, ctx: ActionHandlerContext, op
|
|
|
12
12
|
const mergedInput = mergeInput(defaultInput, input, fixedInput);
|
|
13
13
|
logger.debug(`Running ${code} handler...`, { defaultInput, fixedInput, mergedInput });
|
|
14
14
|
|
|
15
|
-
const userId = ctx.routerContext.state?.userId;
|
|
16
|
-
if (userId) {
|
|
17
|
-
input.createdBy = userId;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
15
|
const entityManager = server.getEntityManager(options.singularCode);
|
|
21
16
|
const output = await entityManager.createEntity(
|
|
22
17
|
{
|