@researchdatabox/sails-hook-redbox-storage-mongo 1.4.9 → 1.4.11

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.
@@ -151,8 +151,14 @@ var Services;
151
151
  response.success = true;
152
152
  }
153
153
  catch (err) {
154
- sails.log.error(`${this.logHeader} Failed to save update to MongoDB:`);
155
- sails.log.error(JSON.stringify(err));
154
+ sails.log.error(`${this.logHeader} Failed to save update to MongoDB: ${JSON.stringify({
155
+ error: err,
156
+ response: response,
157
+ brand: brand,
158
+ oid: oid,
159
+ record: record,
160
+ user: user,
161
+ })}`);
156
162
  response.success = false;
157
163
  response.message = err;
158
164
  }
@@ -199,28 +205,24 @@ var Services;
199
205
  provideUserAccessAndRemovePendingAccess(oid, userid, pendingValue) {
200
206
  const batchFn = () => __awaiter(this, void 0, void 0, function* () {
201
207
  const metadata = yield this.getMeta(oid);
202
- var pendingEditArray = metadata['authorization']['editPending'];
203
- var editArray = metadata['authorization']['edit'];
204
- for (var i = 0; i < pendingEditArray.length; i++) {
205
- if (pendingEditArray[i] == pendingValue) {
206
- pendingEditArray = pendingEditArray.filter(e => e !== pendingValue);
207
- editArray = editArray.filter(e => e !== userid);
208
- editArray.push(userid);
209
- }
210
- }
211
- metadata['authorization']['editPending'] = pendingEditArray;
212
- metadata['authorization']['edit'] = editArray;
213
- var pendingViewArray = metadata['authorization']['viewPending'];
214
- var viewArray = metadata['authorization']['view'];
215
- for (var i = 0; i < pendingViewArray.length; i++) {
216
- if (pendingViewArray[i] == pendingValue) {
217
- pendingViewArray = pendingViewArray.filter(e => e !== pendingValue);
218
- viewArray = viewArray.filter(e => e !== userid);
219
- viewArray.push(userid);
220
- }
221
- }
222
- metadata['authorization']['viewPending'] = pendingViewArray;
223
- metadata['authorization']['view'] = viewArray;
208
+ const pendingEditArray = _.get(metadata, 'authorization.editPending', []);
209
+ const editArray = _.get(metadata, 'authorization.edit', []);
210
+ const pendingEditArrayFiltered = pendingEditArray.filter(value => value !== pendingValue);
211
+ const pendingEditFound = pendingEditArray.length > pendingEditArrayFiltered.length;
212
+ if (pendingEditFound && !editArray.includes(userid)) {
213
+ editArray.push(userid);
214
+ }
215
+ _.set(metadata, 'authorization.editPending', pendingEditArrayFiltered);
216
+ _.set(metadata, 'authorization.edit', editArray);
217
+ const pendingViewArray = _.get(metadata, 'authorization.viewPending', []);
218
+ const viewArray = _.get(metadata, 'authorization.view', []);
219
+ const pendingViewArrayFiltered = pendingViewArray.filter(value => value !== pendingValue);
220
+ const pendingViewFound = pendingViewArray.length > pendingViewArrayFiltered.length;
221
+ if (pendingViewFound && !viewArray.includes(userid)) {
222
+ viewArray.push(userid);
223
+ }
224
+ _.set(metadata, 'authorization.viewPending', pendingViewArrayFiltered);
225
+ _.set(metadata, 'authorization.view', viewArray);
224
226
  try {
225
227
  yield this.updateMeta(null, oid, metadata);
226
228
  }
@@ -664,9 +666,11 @@ var Services;
664
666
  updateDatastream(oid, record, newMetadata, fileRoot, fileIdsAdded) {
665
667
  return FormsService.getFormByName(record.metaMetadata.form, true)
666
668
  .flatMap(form => {
669
+ form = form !== null && form !== void 0 ? form : { attachmentFields: [] };
670
+ const typedForm = form;
667
671
  const reqs = [];
668
672
  record.metaMetadata.attachmentFields = form.attachmentFields;
669
- _.each(form.attachmentFields, (attField) => __awaiter(this, void 0, void 0, function* () {
673
+ _.each(typedForm.attachmentFields, (attField) => __awaiter(this, void 0, void 0, function* () {
670
674
  const oldAttachments = record.metadata[attField];
671
675
  const newAttachments = newMetadata[attField];
672
676
  const removeIds = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@researchdatabox/sails-hook-redbox-storage-mongo",
3
- "version": "1.4.9",
3
+ "version": "1.4.11",
4
4
  "description": "A MongoDB storage plugin for ReDBox.",
5
5
  "main": "index.js",
6
6
  "sails": {
@@ -162,8 +162,14 @@ export module Services {
162
162
  await Record.updateOne({ redboxOid: oid }).set(record);
163
163
  response.success = true;
164
164
  } catch (err) {
165
- sails.log.error(`${this.logHeader} Failed to save update to MongoDB:`);
166
- sails.log.error(JSON.stringify(err));
165
+ sails.log.error(`${this.logHeader} Failed to save update to MongoDB: ${JSON.stringify({
166
+ error: err,
167
+ response: response,
168
+ brand: brand,
169
+ oid: oid,
170
+ record: record,
171
+ user: user,
172
+ })}`);
167
173
  response.success = false;
168
174
  response.message = err;
169
175
  }
@@ -208,33 +214,55 @@ export module Services {
208
214
  return response;
209
215
  }
210
216
 
217
+ /**
218
+ * If pendingValue is in editPending or viewPending, remove all instances from the pending arrays and
219
+ * put the userid in the matching access / non-pending array, then save the changes to the storage.
220
+ *
221
+ * Implementation Note: This is a sync method, but it calls async methods.
222
+ * This means that the storage will not be updated when this method returns, but at some point later.
223
+ *
224
+ * @param oid The record identifier.
225
+ * @param userid The value to put in the matching access / non-pending arrays.
226
+ * @param pendingValue The value to find in the pending arrays.
227
+ */
211
228
  public provideUserAccessAndRemovePendingAccess(oid, userid, pendingValue): void {
212
229
  const batchFn = async () => {
213
230
  const metadata = await this.getMeta(oid);
214
- // remove pending edit access and add real edit access with userid
215
- var pendingEditArray = metadata['authorization']['editPending'];
216
- var editArray = metadata['authorization']['edit'];
217
- for (var i = 0; i < pendingEditArray.length; i++) {
218
- if (pendingEditArray[i] == pendingValue) {
219
- pendingEditArray = pendingEditArray.filter(e => e !== pendingValue);
220
- editArray = editArray.filter(e => e !== userid);
221
- editArray.push(userid);
222
- }
231
+
232
+ // Update edit authorization - remove pending edit access and add real edit access with userid
233
+ const pendingEditArray: string[] = _.get(metadata, 'authorization.editPending', []);
234
+ const editArray: string[] = _.get(metadata, 'authorization.edit', []);
235
+
236
+ // remove all items matching pendingValue from the pendingEditArray
237
+ const pendingEditArrayFiltered = pendingEditArray.filter(value => value !== pendingValue);
238
+ const pendingEditFound = pendingEditArray.length > pendingEditArrayFiltered.length;
239
+
240
+ // add the item to the editArray if it existed in the pendingEditArray, and it is not in the editArray
241
+ if (pendingEditFound && !editArray.includes(userid)) {
242
+ editArray.push(userid);
223
243
  }
224
- metadata['authorization']['editPending'] = pendingEditArray;
225
- metadata['authorization']['edit'] = editArray;
226
-
227
- var pendingViewArray = metadata['authorization']['viewPending'];
228
- var viewArray = metadata['authorization']['view'];
229
- for (var i = 0; i < pendingViewArray.length; i++) {
230
- if (pendingViewArray[i] == pendingValue) {
231
- pendingViewArray = pendingViewArray.filter(e => e !== pendingValue);
232
- viewArray = viewArray.filter(e => e !== userid);
233
- viewArray.push(userid);
234
- }
244
+
245
+ // update the metadata with the modified arrays
246
+ _.set(metadata, 'authorization.editPending', pendingEditArrayFiltered);
247
+ _.set(metadata, 'authorization.edit', editArray);
248
+
249
+ // Update view authorization - remove pending view access and add real view access with userid
250
+ const pendingViewArray: string[] = _.get(metadata, 'authorization.viewPending', []);
251
+ const viewArray: string[] = _.get(metadata, 'authorization.view', []);
252
+
253
+ // remove all items matching pendingValue from the pendingViewArray
254
+ const pendingViewArrayFiltered = pendingViewArray.filter(value => value !== pendingValue);
255
+ const pendingViewFound = pendingViewArray.length > pendingViewArrayFiltered.length;
256
+
257
+ // add the item to the viewArray if it existed in the pendingViewArray, and it is not in the viewArray
258
+ if (pendingViewFound && !viewArray.includes(userid)) {
259
+ viewArray.push(userid);
235
260
  }
236
- metadata['authorization']['viewPending'] = pendingViewArray;
237
- metadata['authorization']['view'] = viewArray;
261
+
262
+ // update the metadata with the modified arrays
263
+ _.set(metadata, 'authorization.viewPending', pendingViewArrayFiltered);
264
+ _.set(metadata, 'authorization.view', viewArray);
265
+
238
266
  try {
239
267
  await this.updateMeta(null, oid, metadata);
240
268
  } catch (err) {
@@ -242,6 +270,8 @@ export module Services {
242
270
  sails.log.error(JSON.stringify(err));
243
271
  }
244
272
  };
273
+
274
+ // TODO: Fix this so that the method only returns after the storage is updated.
245
275
  batchFn();
246
276
  }
247
277
 
@@ -706,9 +736,13 @@ export module Services {
706
736
  // loop thru the attachment fields and determine if we need to add or remove
707
737
  return FormsService.getFormByName(record.metaMetadata.form, true)
708
738
  .flatMap(form => {
739
+ // For any generated, view-only forms, the form may be null, add a coalescence to avoid breaking
740
+ // the attachment update process.
741
+ form = form ?? { attachmentFields: [] };
742
+ const typedForm = form as { attachmentFields: string[] };
709
743
  const reqs = [];
710
744
  record.metaMetadata.attachmentFields = form.attachmentFields;
711
- _.each(form.attachmentFields, async (attField) => {
745
+ _.each(typedForm.attachmentFields, async (attField) => {
712
746
  const oldAttachments = record.metadata[attField];
713
747
  const newAttachments = newMetadata[attField];
714
748
  const removeIds = [];