@cool-digital-solutions/interferir-models 1.2.25 → 1.2.27

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.
@@ -30,7 +30,7 @@ const aiTaskOutlineSchema = new mongoose_1.Schema({
30
30
  updatedDate: Number,
31
31
  createdDate: Number,
32
32
  }, _config_1.schemaOptions);
33
- aiTaskOutlineSchema.pre('save', function (next) {
33
+ aiTaskOutlineSchema.pre('save', async function (next) {
34
34
  const user = express_http_context_1.default.get('user');
35
35
  const notUpdatedBy = express_http_context_1.default.get('notUpdatedBy');
36
36
  if (user) {
@@ -39,13 +39,51 @@ aiTaskOutlineSchema.pre('save', function (next) {
39
39
  if (!notUpdatedBy)
40
40
  this.updatedBy = user._id;
41
41
  }
42
+ const isNewDocument = !this._id;
43
+ if (isNewDocument) {
44
+ if (this.parentId) {
45
+ const AiTaskOutline = this.constructor;
46
+ const parentDoc = await AiTaskOutline.findById(this.parentId);
47
+ if (parentDoc && !parentDoc.isEditable) {
48
+ throw new Error('Cannot add a new section under a non-editable section');
49
+ }
50
+ }
51
+ return next();
52
+ }
53
+ if (!this.isEditable) {
54
+ throw new Error('This section cannot be modified');
55
+ }
56
+ const AiTaskOutline = this.constructor;
57
+ const originalDoc = await AiTaskOutline.findById(this._id);
58
+ if (originalDoc && this.parentId && !originalDoc.parentId?.equals(this.parentId)) {
59
+ const parentDoc = await AiTaskOutline.findById(this.parentId);
60
+ if (parentDoc && !parentDoc.isEditable) {
61
+ throw new Error('Cannot move section under a non-editable section');
62
+ }
63
+ }
42
64
  next();
43
65
  });
44
- aiTaskOutlineSchema.pre('findOneAndUpdate', function (next) {
66
+ aiTaskOutlineSchema.pre('findOneAndUpdate', async function (next) {
45
67
  const user = express_http_context_1.default.get('user');
46
68
  const notUpdatedBy = express_http_context_1.default.get('notUpdatedBy');
47
- if (user && !notUpdatedBy)
69
+ if (user && !notUpdatedBy) {
48
70
  this._update.updatedBy = user._id;
71
+ }
72
+ const currentDoc = await this.model.findOne(this.getQuery());
73
+ if (!currentDoc) {
74
+ return next();
75
+ }
76
+ const isEditable = currentDoc.isEditable ?? false;
77
+ const isDeletable = currentDoc.isDeletable ?? false;
78
+ if (!isEditable || !isDeletable) {
79
+ throw new Error('This section cannot be modified');
80
+ }
81
+ if (this._update.parentId && !currentDoc.parentId?.equals(this._update.parentId)) {
82
+ const parentDoc = await this.model.findById(this._update.parentId);
83
+ if (parentDoc && !parentDoc.isEditable) {
84
+ throw new Error('Cannot move section under a non-editable section');
85
+ }
86
+ }
49
87
  next();
50
88
  });
51
89
  exports.default = aiTaskOutlineSchema;
@@ -16,19 +16,11 @@ const aiTopUseCaseSchema = new mongoose_1.Schema({
16
16
  relevancy: { type: String, required: true },
17
17
  original: { type: String, required: true },
18
18
  summary: { type: String, required: true },
19
- createdBy: { type: mongoose_1.Schema.Types.ObjectId, ref: 'User', default: null },
20
- updatedBy: { type: mongoose_1.Schema.Types.ObjectId, ref: 'User', default: null },
21
- updatedDate: Number,
22
- createdDate: Number,
23
19
  },
24
20
  sources: [
25
21
  {
26
22
  text: { type: String, required: true },
27
23
  url: { type: String, required: true },
28
- createdBy: { type: mongoose_1.Schema.Types.ObjectId, ref: 'User', default: null },
29
- updatedBy: { type: mongoose_1.Schema.Types.ObjectId, ref: 'User', default: null },
30
- updatedDate: Number,
31
- createdDate: Number,
32
24
  },
33
25
  ],
34
26
  createdBy: { type: mongoose_1.Schema.Types.ObjectId, ref: 'User', default: null },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cool-digital-solutions/interferir-models",
3
- "version": "1.2.25",
3
+ "version": "1.2.27",
4
4
  "main": "./dist/index.js",
5
5
  "files": [
6
6
  "dist/**/*"