MCPcopy Create free account
hub / github.com/CloudBoost/cloudboost / _save

Function _save

services/cloudObjects.js:213–270  ·  view source on GitHub ↗
(appId, collectionName, document, accessList, isMasterKey, reqType, opts, encryption_key)

Source from the content-addressed store, hash-verified

211
212
213async function _save(appId, collectionName, document, accessList, isMasterKey, reqType, opts, encryption_key) {
214 const deferred = q.defer();
215 try {
216 const docToSave = document;
217
218 const promises = [];
219 // To keep track of documents whether the document is save or update, this keeps track by id document with value
220 // as "save" or "update"
221 let unModDoc = [];
222 /* reqType keeps track of the collections which are for save and which are for update.
223 * It stores the id of collections for save in save array and update in update array */
224 if (!reqType) {
225 reqType = {
226 save: [],
227 update: [],
228 };
229
230 document = _generateId(document, reqType);
231 }
232 const parentId = document._id;
233
234 document = _getModifiedDocs(document, unModDoc);
235 if (document && Object.keys(document).length > 0) {
236 let listOfDocs = await customHelper.checkWriteAclAndUpdateVersion(appId, document, accessList, isMasterKey);
237 let obj = _seperateDocs(listOfDocs);
238 listOfDocs = obj.newDoc;
239 obj = obj.oldDoc;
240 const newListOfDocs = await _validateSchema(appId, listOfDocs, accessList, isMasterKey, encryption_key)
241 const mongoDocs = newListOfDocs.map(doc => Object.assign({}, doc));
242
243 promises.push(mongoService.document.save(appId, mongoDocs));
244 q.allSettled(promises).then((array) => {
245 if (array[0].state === 'fulfilled') {
246 _sendNotification(appId, array[0], reqType);
247 unModDoc = _merge(parentId, array[0].value, unModDoc);
248 deferred.resolve(unModDoc);
249 } else {
250 _rollBack(appId, array, listOfDocs, obj).then((res) => {
251 winston.log('error', res);
252 deferred.reject('Unable to Save the document at this time');
253 }, (err) => {
254 winston.log('error', err);
255 deferred.reject(err);
256 });
257 }
258 });
259 } else {
260 deferred.resolve(docToSave);
261 }
262 } catch (err) {
263 winston.log('error', {
264 error: String(err),
265 stack: new Error().stack,
266 });
267 deferred.reject(err);
268 }
269 return deferred.promise;
270}

Callers 1

saveFunction · 0.70

Calls 8

_generateIdFunction · 0.85
_getModifiedDocsFunction · 0.85
_seperateDocsFunction · 0.85
_validateSchemaFunction · 0.85
_mergeFunction · 0.85
_rollBackFunction · 0.85
_sendNotificationFunction · 0.70
saveMethod · 0.45

Tested by

no test coverage detected