nodejs-rigorous
Version:
Rigorous Framework
65 lines (46 loc) • 1.84 kB
JavaScript
const mongoose = require('mongoose');
const { RigorousError, errorTypes } = require('../../../factory/RigorousError/index');
const mongoRead = require('./read');
const callbackResult = require('./callback_result');
module.exports = (
collectionName,
queryRead,
attributeObjectUpdated,
params,
) => {
return new Promise((resolve, reject) => {
try {
const op = mongoose.model(collectionName)
.findOneAndUpdate(
queryRead,
attributeObjectUpdated,
{ new: true },
).select({ _id: 1 });
// Update
op.exec((err, resultUnfiltered) => {
if (resultUnfiltered === null) {
reject(new RigorousError(errorTypes.RESPONSE_ERROR_DATA_NOT_FOUND));
}
callbackResult.exec(err, resultUnfiltered)
.then((result) => {
const ids = [];
if (Array.isArray(result)) {
result.forEach((resultUnit) => {
ids.push(resultUnit.id);
});
} else {
ids.push(result.id);
}
mongoRead(collectionName, { _id: { $in: ids } }, params)
.then((resultJson) => {
return resolve(resultJson);
});
})
.catch((err2) => { return reject(err2); });
return null;
});
} catch (errGlobal) {
reject(errGlobal);
}
});
};