@monorise/core 3.0.3 → 3.1.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.
package/dist/index.d.ts CHANGED
@@ -53,6 +53,19 @@ declare abstract class Repository {
53
53
  ExpressionAttributeNames: Record<string, string>;
54
54
  ExpressionAttributeValues: Record<string, AttributeValue>;
55
55
  };
56
+ toAdjustUpdate(adjustments: Record<string, number>, constraints?: {
57
+ [field: string]: {
58
+ min?: number;
59
+ max?: number;
60
+ minField?: string;
61
+ maxField?: string;
62
+ };
63
+ }, prefix?: string): {
64
+ UpdateExpression: string;
65
+ ConditionExpression?: string;
66
+ ExpressionAttributeNames: Record<string, string>;
67
+ ExpressionAttributeValues: Record<string, AttributeValue>;
68
+ };
56
69
  }
57
70
 
58
71
  declare class Entity$1<T extends Entity$2> extends Item$1 {
@@ -119,6 +132,12 @@ declare class EntityRepository extends Repository {
119
132
  ExpressionAttributeNames?: Record<string, string>;
120
133
  ExpressionAttributeValues?: Record<string, AttributeValue>;
121
134
  }): Promise<Entity$1<T>>;
135
+ adjustEntity<T extends Entity$2>(entityType: T, entityId: string, adjustments: Record<string, number>, constraints?: {
136
+ [field: string]: {
137
+ min?: number;
138
+ max?: number;
139
+ };
140
+ }): Promise<Entity$1<T>>;
122
141
  deleteEntity<T extends Entity$2>(entityType: T, entityId: string): Promise<void>;
123
142
  queryEntities<T extends Entity$2>(entityType: T, query: string): Promise<{
124
143
  items: Entity$1<T>[];
@@ -465,7 +484,7 @@ new EventBridgeClient();
465
484
  // middlewares/general-error-handler.ts
466
485
  createFactory();
467
486
 
468
- // ../../node_modules/zod/dist/esm/v3/external.js
487
+ // ../../node_modules/zod/v3/external.js
469
488
  var external_exports = {};
470
489
  __export(external_exports, {
471
490
  BRAND: () => BRAND,
@@ -577,7 +596,7 @@ __export(external_exports, {
577
596
  void: () => voidType
578
597
  });
579
598
 
580
- // ../../node_modules/zod/dist/esm/v3/helpers/util.js
599
+ // ../../node_modules/zod/v3/helpers/util.js
581
600
  var util;
582
601
  (function(util2) {
583
602
  util2.assertEqual = (_) => {
@@ -707,7 +726,7 @@ var getParsedType = (data) => {
707
726
  }
708
727
  };
709
728
 
710
- // ../../node_modules/zod/dist/esm/v3/ZodError.js
729
+ // ../../node_modules/zod/v3/ZodError.js
711
730
  var ZodIssueCode = util.arrayToEnum([
712
731
  "invalid_type",
713
732
  "invalid_literal",
@@ -807,8 +826,9 @@ var ZodError = class _ZodError extends Error {
807
826
  const formErrors = [];
808
827
  for (const sub of this.issues) {
809
828
  if (sub.path.length > 0) {
810
- fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
811
- fieldErrors[sub.path[0]].push(mapper(sub));
829
+ const firstEl = sub.path[0];
830
+ fieldErrors[firstEl] = fieldErrors[firstEl] || [];
831
+ fieldErrors[firstEl].push(mapper(sub));
812
832
  } else {
813
833
  formErrors.push(mapper(sub));
814
834
  }
@@ -824,7 +844,7 @@ ZodError.create = (issues) => {
824
844
  return error;
825
845
  };
826
846
 
827
- // ../../node_modules/zod/dist/esm/v3/locales/en.js
847
+ // ../../node_modules/zod/v3/locales/en.js
828
848
  var errorMap = (issue, _ctx) => {
829
849
  let message;
830
850
  switch (issue.code) {
@@ -886,6 +906,8 @@ var errorMap = (issue, _ctx) => {
886
906
  message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
887
907
  else if (issue.type === "number")
888
908
  message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
909
+ else if (issue.type === "bigint")
910
+ message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
889
911
  else if (issue.type === "date")
890
912
  message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
891
913
  else
@@ -925,7 +947,7 @@ var errorMap = (issue, _ctx) => {
925
947
  };
926
948
  var en_default = errorMap;
927
949
 
928
- // ../../node_modules/zod/dist/esm/v3/errors.js
950
+ // ../../node_modules/zod/v3/errors.js
929
951
  var overrideErrorMap = en_default;
930
952
  function setErrorMap(map) {
931
953
  overrideErrorMap = map;
@@ -934,7 +956,7 @@ function getErrorMap() {
934
956
  return overrideErrorMap;
935
957
  }
936
958
 
937
- // ../../node_modules/zod/dist/esm/v3/helpers/parseUtil.js
959
+ // ../../node_modules/zod/v3/helpers/parseUtil.js
938
960
  var makeIssue = (params) => {
939
961
  const { data, path, errorMaps, issueData } = params;
940
962
  const fullPath = [...path, ...issueData.path || []];
@@ -1043,14 +1065,14 @@ var isDirty = (x) => x.status === "dirty";
1043
1065
  var isValid = (x) => x.status === "valid";
1044
1066
  var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
1045
1067
 
1046
- // ../../node_modules/zod/dist/esm/v3/helpers/errorUtil.js
1068
+ // ../../node_modules/zod/v3/helpers/errorUtil.js
1047
1069
  var errorUtil;
1048
1070
  (function(errorUtil2) {
1049
1071
  errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
1050
1072
  errorUtil2.toString = (message) => typeof message === "string" ? message : message == null ? void 0 : message.message;
1051
1073
  })(errorUtil || (errorUtil = {}));
1052
1074
 
1053
- // ../../node_modules/zod/dist/esm/v3/types.js
1075
+ // ../../node_modules/zod/v3/types.js
1054
1076
  var ParseInputLazyPath = class {
1055
1077
  constructor(parent, value, path, key) {
1056
1078
  this._cachedPath = [];
@@ -1450,6 +1472,8 @@ function isValidJWT(jwt, alg) {
1450
1472
  return false;
1451
1473
  try {
1452
1474
  const [header] = jwt.split(".");
1475
+ if (!header)
1476
+ return false;
1453
1477
  const base64 = header.replace(/-/g, "+").replace(/_/g, "/").padEnd(header.length + (4 - header.length % 4) % 4, "=");
1454
1478
  const decoded = JSON.parse(atob(base64));
1455
1479
  if (typeof decoded !== "object" || decoded === null)
@@ -4447,6 +4471,12 @@ declare class EntityService {
4447
4471
  mutualId?: string;
4448
4472
  };
4449
4473
  }) => Promise<Entity<T>>;
4474
+ adjustEntity: <T extends Entity$2>({ entityType, entityId, adjustments, accountId, }: {
4475
+ entityType: T;
4476
+ entityId: string;
4477
+ adjustments: Record<string, number>;
4478
+ accountId?: string;
4479
+ }) => Promise<Entity<T>>;
4450
4480
  updateEntity: <T extends Entity$2>({ entityType, entityId, entityPayload, accountId, }: {
4451
4481
  entityType: T;
4452
4482
  entityId: string;
@@ -4513,6 +4543,14 @@ declare class UpdateEntityController {
4513
4543
  }, hono_utils_http_status.ContentfulStatusCode, "json">>;
4514
4544
  }
4515
4545
 
4546
+ declare class AdjustEntityController {
4547
+ private entityService;
4548
+ constructor(entityService: EntityService);
4549
+ controller: hono.MiddlewareHandler<any, string, {}, Response & hono.TypedResponse<{
4550
+ [x: string]: hono_utils_types.JSONValue;
4551
+ }, hono_utils_http_status.ContentfulStatusCode, "json">>;
4552
+ }
4553
+
4516
4554
  declare class UpsertEntityController {
4517
4555
  private EntityConfig;
4518
4556
  private entityRepository;
@@ -4668,6 +4706,7 @@ declare class DependencyContainer {
4668
4706
  get createEntityController(): CreateEntityController;
4669
4707
  get upsertEntityController(): UpsertEntityController;
4670
4708
  get updateEntityController(): UpdateEntityController;
4709
+ get adjustEntityController(): AdjustEntityController;
4671
4710
  get deleteEntityController(): DeleteEntityController;
4672
4711
  get listEntitiesByEntityController(): ListEntitiesByEntityController;
4673
4712
  get getMutualController(): GetMutualController;
package/dist/index.js CHANGED
@@ -107,6 +107,10 @@ var setupCommonRoutes = (container) => {
107
107
  "/entity/:entityType/unique/:uniqueField/:uniqueFieldValue",
108
108
  container.getEntityByUniqueFieldController.controller
109
109
  );
110
+ app.post(
111
+ "/entity/:entityType/:entityId/adjust",
112
+ container.adjustEntityController.controller
113
+ );
110
114
  app.get(
111
115
  "/entity/:entityType/:entityId",
112
116
  container.getEntityController.controller
@@ -250,6 +254,47 @@ var Repository = class {
250
254
  };
251
255
  return updateAttributes;
252
256
  }
257
+ toAdjustUpdate(adjustments, constraints, prefix = "data") {
258
+ const parts = [];
259
+ const conditionParts = [];
260
+ const expressionAttributeNames = {};
261
+ const expressionAttributeValues = {};
262
+ expressionAttributeNames[`#${prefix}`] = prefix;
263
+ for (const field of Object.keys(adjustments)) {
264
+ const namePlaceholder = `#${field}`;
265
+ const valuePlaceholder = `:${field}`;
266
+ const fieldExpr = `if_not_exists(#${prefix}.${namePlaceholder}, :zero)`;
267
+ parts.push(
268
+ `#${prefix}.${namePlaceholder} = ${fieldExpr} + ${valuePlaceholder}`
269
+ );
270
+ expressionAttributeNames[namePlaceholder] = field;
271
+ expressionAttributeValues[valuePlaceholder] = adjustments[field];
272
+ if (constraints == null ? void 0 : constraints[field]) {
273
+ const constraint = constraints[field];
274
+ const delta = adjustments[field];
275
+ const currentFieldRef = `#${prefix}.${namePlaceholder}`;
276
+ if (constraint.min !== void 0 && delta < 0) {
277
+ const thresholdPlaceholder = `:${field}_min_threshold`;
278
+ conditionParts.push(`${currentFieldRef} >= ${thresholdPlaceholder}`);
279
+ expressionAttributeValues[thresholdPlaceholder] = constraint.min - delta;
280
+ }
281
+ if (constraint.max !== void 0 && delta > 0) {
282
+ const thresholdPlaceholder = `:${field}_max_threshold`;
283
+ conditionParts.push(`${currentFieldRef} <= ${thresholdPlaceholder}`);
284
+ expressionAttributeValues[thresholdPlaceholder] = constraint.max - delta;
285
+ }
286
+ }
287
+ }
288
+ expressionAttributeValues[":zero"] = 0;
289
+ return __spreadProps(__spreadValues({
290
+ UpdateExpression: `SET ${parts.join(", ")}`
291
+ }, conditionParts.length > 0 && {
292
+ ConditionExpression: conditionParts.join(" AND ")
293
+ }), {
294
+ ExpressionAttributeNames: expressionAttributeNames,
295
+ ExpressionAttributeValues: marshall(expressionAttributeValues)
296
+ });
297
+ }
253
298
  };
254
299
 
255
300
  // data/Entity.ts
@@ -751,6 +796,25 @@ var EntityRepository = class extends Repository {
751
796
  }
752
797
  });
753
798
  }
799
+ adjustEntity(entityType, entityId, adjustments, constraints) {
800
+ return __async(this, null, function* () {
801
+ const entity = new Entity(entityType, entityId);
802
+ const { UpdateExpression, ConditionExpression, ExpressionAttributeNames, ExpressionAttributeValues } = this.toAdjustUpdate(adjustments, constraints);
803
+ const updatedAtExpression = ", #updatedAt = :updatedAt";
804
+ ExpressionAttributeNames["#updatedAt"] = "updatedAt";
805
+ ExpressionAttributeValues[":updatedAt"] = { S: (/* @__PURE__ */ new Date()).toISOString() };
806
+ const resp = yield this.dynamodbClient.updateItem(__spreadProps(__spreadValues({
807
+ TableName: this.TABLE_NAME,
808
+ Key: entity.keys(),
809
+ UpdateExpression: UpdateExpression + updatedAtExpression
810
+ }, ConditionExpression && { ConditionExpression }), {
811
+ ExpressionAttributeNames,
812
+ ExpressionAttributeValues,
813
+ ReturnValues: "ALL_NEW"
814
+ }));
815
+ return Entity.fromItem(resp.Attributes);
816
+ });
817
+ }
754
818
  deleteEntity(entityType, entityId) {
755
819
  return __async(this, null, function* () {
756
820
  try {
@@ -2850,6 +2914,7 @@ var CreateEntityController = class {
2850
2914
  const entity = yield this.entityService.createEntity({
2851
2915
  entityType,
2852
2916
  entityPayload: body,
2917
+ entityId: body.entityId,
2853
2918
  accountId,
2854
2919
  options: {
2855
2920
  createAndUpdateDatetime: body.createdAt
@@ -2975,7 +3040,7 @@ var GetEntityController = class {
2975
3040
  // controllers/entity/list-entities.controller.ts
2976
3041
  import { createMiddleware as createMiddleware7 } from "hono/factory";
2977
3042
 
2978
- // ../../node_modules/zod/dist/esm/v3/external.js
3043
+ // ../../node_modules/zod/v3/external.js
2979
3044
  var external_exports = {};
2980
3045
  __export(external_exports, {
2981
3046
  BRAND: () => BRAND,
@@ -3087,7 +3152,7 @@ __export(external_exports, {
3087
3152
  void: () => voidType
3088
3153
  });
3089
3154
 
3090
- // ../../node_modules/zod/dist/esm/v3/helpers/util.js
3155
+ // ../../node_modules/zod/v3/helpers/util.js
3091
3156
  var util;
3092
3157
  (function(util2) {
3093
3158
  util2.assertEqual = (_) => {
@@ -3217,7 +3282,7 @@ var getParsedType = (data) => {
3217
3282
  }
3218
3283
  };
3219
3284
 
3220
- // ../../node_modules/zod/dist/esm/v3/ZodError.js
3285
+ // ../../node_modules/zod/v3/ZodError.js
3221
3286
  var ZodIssueCode = util.arrayToEnum([
3222
3287
  "invalid_type",
3223
3288
  "invalid_literal",
@@ -3317,8 +3382,9 @@ var ZodError = class _ZodError extends Error {
3317
3382
  const formErrors = [];
3318
3383
  for (const sub of this.issues) {
3319
3384
  if (sub.path.length > 0) {
3320
- fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
3321
- fieldErrors[sub.path[0]].push(mapper(sub));
3385
+ const firstEl = sub.path[0];
3386
+ fieldErrors[firstEl] = fieldErrors[firstEl] || [];
3387
+ fieldErrors[firstEl].push(mapper(sub));
3322
3388
  } else {
3323
3389
  formErrors.push(mapper(sub));
3324
3390
  }
@@ -3334,7 +3400,7 @@ ZodError.create = (issues) => {
3334
3400
  return error;
3335
3401
  };
3336
3402
 
3337
- // ../../node_modules/zod/dist/esm/v3/locales/en.js
3403
+ // ../../node_modules/zod/v3/locales/en.js
3338
3404
  var errorMap = (issue, _ctx) => {
3339
3405
  let message;
3340
3406
  switch (issue.code) {
@@ -3396,6 +3462,8 @@ var errorMap = (issue, _ctx) => {
3396
3462
  message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
3397
3463
  else if (issue.type === "number")
3398
3464
  message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
3465
+ else if (issue.type === "bigint")
3466
+ message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
3399
3467
  else if (issue.type === "date")
3400
3468
  message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
3401
3469
  else
@@ -3435,7 +3503,7 @@ var errorMap = (issue, _ctx) => {
3435
3503
  };
3436
3504
  var en_default = errorMap;
3437
3505
 
3438
- // ../../node_modules/zod/dist/esm/v3/errors.js
3506
+ // ../../node_modules/zod/v3/errors.js
3439
3507
  var overrideErrorMap = en_default;
3440
3508
  function setErrorMap(map) {
3441
3509
  overrideErrorMap = map;
@@ -3444,7 +3512,7 @@ function getErrorMap() {
3444
3512
  return overrideErrorMap;
3445
3513
  }
3446
3514
 
3447
- // ../../node_modules/zod/dist/esm/v3/helpers/parseUtil.js
3515
+ // ../../node_modules/zod/v3/helpers/parseUtil.js
3448
3516
  var makeIssue = (params) => {
3449
3517
  const { data, path, errorMaps, issueData } = params;
3450
3518
  const fullPath = [...path, ...issueData.path || []];
@@ -3553,14 +3621,14 @@ var isDirty = (x) => x.status === "dirty";
3553
3621
  var isValid = (x) => x.status === "valid";
3554
3622
  var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
3555
3623
 
3556
- // ../../node_modules/zod/dist/esm/v3/helpers/errorUtil.js
3624
+ // ../../node_modules/zod/v3/helpers/errorUtil.js
3557
3625
  var errorUtil;
3558
3626
  (function(errorUtil2) {
3559
3627
  errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
3560
3628
  errorUtil2.toString = (message) => typeof message === "string" ? message : message == null ? void 0 : message.message;
3561
3629
  })(errorUtil || (errorUtil = {}));
3562
3630
 
3563
- // ../../node_modules/zod/dist/esm/v3/types.js
3631
+ // ../../node_modules/zod/v3/types.js
3564
3632
  var ParseInputLazyPath = class {
3565
3633
  constructor(parent, value, path, key) {
3566
3634
  this._cachedPath = [];
@@ -3960,6 +4028,8 @@ function isValidJWT(jwt, alg) {
3960
4028
  return false;
3961
4029
  try {
3962
4030
  const [header] = jwt.split(".");
4031
+ if (!header)
4032
+ return false;
3963
4033
  const base64 = header.replace(/-/g, "+").replace(/_/g, "/").padEnd(header.length + (4 - header.length % 4) % 4, "=");
3964
4034
  const decoded = JSON.parse(atob(base64));
3965
4035
  if (typeof decoded !== "object" || decoded === null)
@@ -6955,7 +7025,7 @@ var ListEntitiesController = class {
6955
7025
  }
6956
7026
  const results = yield this.entityRepository.listEntities(__spreadValues({
6957
7027
  entityType,
6958
- limit: Number(limit),
7028
+ limit: limit ? Number(limit) : void 0,
6959
7029
  options: {
6960
7030
  lastKey
6961
7031
  }
@@ -7028,15 +7098,63 @@ var UpdateEntityController = class {
7028
7098
  }
7029
7099
  };
7030
7100
 
7031
- // controllers/entity/upsert-entity.controller.ts
7101
+ // controllers/entity/adjust-entity.controller.ts
7032
7102
  import { createMiddleware as createMiddleware9 } from "hono/factory";
7033
7103
  import httpStatus9 from "http-status";
7104
+ var AdjustEntityController = class {
7105
+ constructor(entityService) {
7106
+ this.entityService = entityService;
7107
+ this.controller = createMiddleware9((c) => __async(this, null, function* () {
7108
+ var _a;
7109
+ const accountId = c.req.header("account-id") || "";
7110
+ const { entityType, entityId } = c.req.param();
7111
+ const body = yield c.req.json();
7112
+ for (const [key, value] of Object.entries(body)) {
7113
+ if (typeof value !== "number") {
7114
+ c.status(httpStatus9.BAD_REQUEST);
7115
+ return c.json({
7116
+ code: "API_VALIDATION_ERROR",
7117
+ message: `Field "${key}" must be a number, got ${typeof value}`
7118
+ });
7119
+ }
7120
+ }
7121
+ try {
7122
+ const entity = yield this.entityService.adjustEntity({
7123
+ entityType,
7124
+ entityId,
7125
+ adjustments: body,
7126
+ accountId
7127
+ });
7128
+ c.status(httpStatus9.OK);
7129
+ return c.json(entity.toJSON());
7130
+ } catch (err) {
7131
+ if (err instanceof StandardError && err.code === StandardErrorCode.ENTITY_IS_UNDEFINED) {
7132
+ c.status(httpStatus9.NOT_FOUND);
7133
+ return c.json(__spreadValues({}, err.toJSON()));
7134
+ }
7135
+ if ((err == null ? void 0 : err.name) === "ConditionalCheckFailedException" || ((_a = err == null ? void 0 : err.__type) == null ? void 0 : _a.includes("ConditionalCheckFailed"))) {
7136
+ c.status(httpStatus9.CONFLICT);
7137
+ return c.json({
7138
+ code: "ADJUSTMENT_CONSTRAINT_VIOLATED",
7139
+ message: "Adjustment would violate entity constraints"
7140
+ });
7141
+ }
7142
+ console.log("====ADJUST_ENTITY_CONTROLLER_ERROR", err);
7143
+ throw err;
7144
+ }
7145
+ }));
7146
+ }
7147
+ };
7148
+
7149
+ // controllers/entity/upsert-entity.controller.ts
7150
+ import { createMiddleware as createMiddleware10 } from "hono/factory";
7151
+ import httpStatus10 from "http-status";
7034
7152
  var UpsertEntityController = class {
7035
7153
  constructor(EntityConfig, entityRepository, publishEvent2) {
7036
7154
  this.EntityConfig = EntityConfig;
7037
7155
  this.entityRepository = entityRepository;
7038
7156
  this.publishEvent = publishEvent2;
7039
- this.controller = createMiddleware9((c) => __async(this, null, function* () {
7157
+ this.controller = createMiddleware10((c) => __async(this, null, function* () {
7040
7158
  var _a, _b;
7041
7159
  const accountId = c.req.header("account-id");
7042
7160
  const { entityType, entityId } = c.req.param();
@@ -7092,7 +7210,7 @@ var UpsertEntityController = class {
7092
7210
  return c.json(entity);
7093
7211
  } catch (err) {
7094
7212
  if (err instanceof ZodError) {
7095
- c.status(httpStatus9.BAD_REQUEST);
7213
+ c.status(httpStatus10.BAD_REQUEST);
7096
7214
  return c.json({
7097
7215
  code: "API_VALIDATION_ERROR",
7098
7216
  message: "API validation failed",
@@ -7100,7 +7218,7 @@ var UpsertEntityController = class {
7100
7218
  });
7101
7219
  }
7102
7220
  if (err instanceof StandardError && err.code === StandardErrorCode.EMAIL_EXISTS) {
7103
- c.status(httpStatus9.BAD_REQUEST);
7221
+ c.status(httpStatus10.BAD_REQUEST);
7104
7222
  return c.json(__spreadValues({}, err.toJSON()));
7105
7223
  }
7106
7224
  throw err;
@@ -7110,12 +7228,12 @@ var UpsertEntityController = class {
7110
7228
  };
7111
7229
 
7112
7230
  // controllers/mutual/create-mutual.controller.ts
7113
- import { createMiddleware as createMiddleware10 } from "hono/factory";
7114
- import httpStatus10 from "http-status";
7231
+ import { createMiddleware as createMiddleware11 } from "hono/factory";
7232
+ import httpStatus11 from "http-status";
7115
7233
  var CreateMutualController = class {
7116
7234
  constructor(mutualService) {
7117
7235
  this.mutualService = mutualService;
7118
- this.controller = createMiddleware10((c) => __async(this, null, function* () {
7236
+ this.controller = createMiddleware11((c) => __async(this, null, function* () {
7119
7237
  const accountId = c.req.header("account-id");
7120
7238
  const { byEntityType, byEntityId, entityType, entityId } = c.req.param();
7121
7239
  const asEntity = c.req.query("asEntity");
@@ -7135,7 +7253,7 @@ var CreateMutualController = class {
7135
7253
  return c.json(mutual);
7136
7254
  } catch (err) {
7137
7255
  if (err instanceof ZodError) {
7138
- c.status(httpStatus10.BAD_REQUEST);
7256
+ c.status(httpStatus11.BAD_REQUEST);
7139
7257
  return c.json({
7140
7258
  code: "API_VALIDATION_ERROR",
7141
7259
  message: "API validation failed",
@@ -7143,11 +7261,11 @@ var CreateMutualController = class {
7143
7261
  });
7144
7262
  }
7145
7263
  if (err instanceof StandardError && err.code === StandardErrorCode.MUTUAL_EXISTS) {
7146
- c.status(httpStatus10.BAD_REQUEST);
7264
+ c.status(httpStatus11.BAD_REQUEST);
7147
7265
  return c.json(__spreadValues({}, err.toJSON()));
7148
7266
  }
7149
7267
  if (err instanceof StandardError && err.code === StandardErrorCode.ENTITY_IS_UNDEFINED) {
7150
- c.status(httpStatus10.BAD_REQUEST);
7268
+ c.status(httpStatus11.BAD_REQUEST);
7151
7269
  return c.json(__spreadValues({}, err.toJSON()));
7152
7270
  }
7153
7271
  throw err;
@@ -7157,12 +7275,12 @@ var CreateMutualController = class {
7157
7275
  };
7158
7276
 
7159
7277
  // controllers/mutual/delete-mutual.controller.ts
7160
- import { createMiddleware as createMiddleware11 } from "hono/factory";
7161
- import httpStatus11 from "http-status";
7278
+ import { createMiddleware as createMiddleware12 } from "hono/factory";
7279
+ import httpStatus12 from "http-status";
7162
7280
  var DeleteMutualController = class {
7163
7281
  constructor(mutualService) {
7164
7282
  this.mutualService = mutualService;
7165
- this.controller = createMiddleware11((c) => __async(this, null, function* () {
7283
+ this.controller = createMiddleware12((c) => __async(this, null, function* () {
7166
7284
  const accountId = c.req.header("account-id");
7167
7285
  const { byEntityType, byEntityId, entityType, entityId } = c.req.param();
7168
7286
  try {
@@ -7176,7 +7294,7 @@ var DeleteMutualController = class {
7176
7294
  return c.json(mutual);
7177
7295
  } catch (err) {
7178
7296
  if (err instanceof StandardError && err.code === StandardErrorCode.MUTUAL_NOT_FOUND) {
7179
- c.status(httpStatus11.BAD_REQUEST);
7297
+ c.status(httpStatus12.BAD_REQUEST);
7180
7298
  return c.json(__spreadValues({}, err.toJSON()));
7181
7299
  }
7182
7300
  throw err;
@@ -7186,12 +7304,12 @@ var DeleteMutualController = class {
7186
7304
  };
7187
7305
 
7188
7306
  // controllers/mutual/get-mutual.controller.ts
7189
- import { createMiddleware as createMiddleware12 } from "hono/factory";
7190
- import httpStatus12 from "http-status";
7307
+ import { createMiddleware as createMiddleware13 } from "hono/factory";
7308
+ import httpStatus13 from "http-status";
7191
7309
  var GetMutualController = class {
7192
7310
  constructor(associateRepository) {
7193
7311
  this.associateRepository = associateRepository;
7194
- this.controller = createMiddleware12((c) => __async(this, null, function* () {
7312
+ this.controller = createMiddleware13((c) => __async(this, null, function* () {
7195
7313
  const { byEntityType, byEntityId, entityType, entityId } = c.req.param();
7196
7314
  try {
7197
7315
  const associate = yield this.associateRepository.getMutual(
@@ -7203,7 +7321,7 @@ var GetMutualController = class {
7203
7321
  return c.json(associate);
7204
7322
  } catch (err) {
7205
7323
  if (err instanceof StandardError && err.code === StandardErrorCode.MUTUAL_IS_UNDEFINED) {
7206
- c.status(httpStatus12.NOT_FOUND);
7324
+ c.status(httpStatus13.NOT_FOUND);
7207
7325
  return c.json({
7208
7326
  code: "MUTUAL_NOT_FOUND",
7209
7327
  message: "Mutual not found"
@@ -7216,11 +7334,11 @@ var GetMutualController = class {
7216
7334
  };
7217
7335
 
7218
7336
  // controllers/mutual/list-entities-by-entity.controller.ts
7219
- import { createMiddleware as createMiddleware13 } from "hono/factory";
7337
+ import { createMiddleware as createMiddleware14 } from "hono/factory";
7220
7338
  var ListEntitiesByEntityController = class {
7221
7339
  constructor(associateRepository) {
7222
7340
  this.associateRepository = associateRepository;
7223
- this.controller = createMiddleware13((c) => __async(this, null, function* () {
7341
+ this.controller = createMiddleware14((c) => __async(this, null, function* () {
7224
7342
  const { byEntityType, byEntityId, entityType } = c.req.param();
7225
7343
  const querySchema3 = external_exports.object({
7226
7344
  chainEntityQuery: external_exports.string().optional(),
@@ -7278,12 +7396,12 @@ var ListEntitiesByEntityController = class {
7278
7396
  };
7279
7397
 
7280
7398
  // controllers/mutual/update-mutual.controller.ts
7281
- import { createMiddleware as createMiddleware14 } from "hono/factory";
7282
- import httpStatus13 from "http-status";
7399
+ import { createMiddleware as createMiddleware15 } from "hono/factory";
7400
+ import httpStatus14 from "http-status";
7283
7401
  var UpdateMutualController = class {
7284
7402
  constructor(mutualService) {
7285
7403
  this.mutualService = mutualService;
7286
- this.controller = createMiddleware14((c) => __async(this, null, function* () {
7404
+ this.controller = createMiddleware15((c) => __async(this, null, function* () {
7287
7405
  const accountId = c.req.header("account-id");
7288
7406
  const { byEntityType, byEntityId, entityType, entityId } = c.req.param();
7289
7407
  const body = yield c.req.json();
@@ -7302,7 +7420,7 @@ var UpdateMutualController = class {
7302
7420
  return c.json(mutual);
7303
7421
  } catch (err) {
7304
7422
  if (err instanceof ZodError) {
7305
- c.status(httpStatus13.BAD_REQUEST);
7423
+ c.status(httpStatus14.BAD_REQUEST);
7306
7424
  return c.json({
7307
7425
  code: "API_VALIDATION_ERROR",
7308
7426
  message: "API validation failed",
@@ -7310,7 +7428,7 @@ var UpdateMutualController = class {
7310
7428
  });
7311
7429
  }
7312
7430
  if (err instanceof StandardError && err.code === StandardErrorCode.MUTUAL_NOT_FOUND) {
7313
- c.status(httpStatus13.BAD_REQUEST);
7431
+ c.status(httpStatus14.BAD_REQUEST);
7314
7432
  return c.json(__spreadValues({}, err.toJSON()));
7315
7433
  }
7316
7434
  throw err;
@@ -7370,6 +7488,51 @@ var EntityService = class {
7370
7488
  );
7371
7489
  return entity;
7372
7490
  });
7491
+ this.adjustEntity = (_0) => __async(this, [_0], function* ({
7492
+ entityType,
7493
+ entityId,
7494
+ adjustments,
7495
+ accountId
7496
+ }) {
7497
+ var _a, _b, _c, _d;
7498
+ const rawConstraints = (_a = this.EntityConfig[entityType]) == null ? void 0 : _a.adjustmentConstraints;
7499
+ let resolvedConstraints = rawConstraints;
7500
+ if (rawConstraints) {
7501
+ const hasDynamicFields = Object.values(rawConstraints).some(
7502
+ (c) => c.minField || c.maxField
7503
+ );
7504
+ if (hasDynamicFields) {
7505
+ const currentEntity = yield this.entityRepository.getEntity(entityType, entityId);
7506
+ const data = (_b = currentEntity == null ? void 0 : currentEntity.data) != null ? _b : {};
7507
+ resolvedConstraints = {};
7508
+ for (const [field, constraint] of Object.entries(rawConstraints)) {
7509
+ const resolved = {};
7510
+ if (constraint.min !== void 0) resolved.min = constraint.min;
7511
+ if (constraint.max !== void 0) resolved.max = constraint.max;
7512
+ if (constraint.minField) resolved.min = (_c = data[constraint.minField]) != null ? _c : 0;
7513
+ if (constraint.maxField) resolved.max = (_d = data[constraint.maxField]) != null ? _d : Number.MAX_SAFE_INTEGER;
7514
+ resolvedConstraints[field] = resolved;
7515
+ }
7516
+ }
7517
+ }
7518
+ const entity = yield this.entityRepository.adjustEntity(
7519
+ entityType,
7520
+ entityId,
7521
+ adjustments,
7522
+ resolvedConstraints
7523
+ );
7524
+ yield this.publishEvent({
7525
+ event: EVENT.CORE.ENTITY_UPDATED,
7526
+ payload: {
7527
+ entityType,
7528
+ entityId,
7529
+ data: entity.data,
7530
+ updatedByAccountId: accountId,
7531
+ publishedAt: entity.updatedAt || (/* @__PURE__ */ new Date()).toISOString()
7532
+ }
7533
+ });
7534
+ return entity;
7535
+ });
7373
7536
  this.updateEntity = (_0) => __async(this, [_0], function* ({
7374
7537
  entityType,
7375
7538
  entityId,
@@ -7652,7 +7815,7 @@ var MutualService = class {
7652
7815
  };
7653
7816
 
7654
7817
  // controllers/tag/list-tags.controller.ts
7655
- import { createMiddleware as createMiddleware15 } from "hono/factory";
7818
+ import { createMiddleware as createMiddleware16 } from "hono/factory";
7656
7819
  var querySchema2 = external_exports.object({
7657
7820
  group: external_exports.string().optional(),
7658
7821
  query: external_exports.string().optional(),
@@ -7664,7 +7827,7 @@ var querySchema2 = external_exports.object({
7664
7827
  var ListTagsController = class {
7665
7828
  constructor(tagRepository) {
7666
7829
  this.tagRepository = tagRepository;
7667
- this.controller = createMiddleware15((c) => __async(this, null, function* () {
7830
+ this.controller = createMiddleware16((c) => __async(this, null, function* () {
7668
7831
  const errorContext = {};
7669
7832
  try {
7670
7833
  errorContext.params = c.req.param();
@@ -7860,6 +8023,12 @@ var DependencyContainer = class {
7860
8023
  this.entityService
7861
8024
  );
7862
8025
  }
8026
+ get adjustEntityController() {
8027
+ return this.createCachedInstance(
8028
+ AdjustEntityController,
8029
+ this.entityService
8030
+ );
8031
+ }
7863
8032
  get deleteEntityController() {
7864
8033
  return this.createCachedInstance(
7865
8034
  DeleteEntityController,