refactored user.ts
This commit is contained in:
@ -22,6 +22,22 @@ router.get('/samples', (req, res, next) => {
|
||||
})
|
||||
});
|
||||
|
||||
router.get('/samples/:group(new|deleted)', (req, res, next) => {
|
||||
if (!req.auth(res, ['maintain', 'admin'], 'basic')) return;
|
||||
|
||||
let status;
|
||||
switch (req.params.group) {
|
||||
case 'new': status = 0;
|
||||
break;
|
||||
case 'deleted': status = -1;
|
||||
break;
|
||||
}
|
||||
SampleModel.find({status: status}).lean().exec((err, data) => {
|
||||
if (err) return next(err);
|
||||
res.json(_.compact(data.map(e => SampleValidate.output(e)))); // validate all and filter null values from validation errors
|
||||
})
|
||||
});
|
||||
|
||||
router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
if (!req.auth(res, ['write', 'maintain', 'dev', 'admin'], 'basic')) return;
|
||||
|
||||
@ -33,6 +49,7 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
if (!sampleData) {
|
||||
return res.status(404).json({status: 'Not found'});
|
||||
}
|
||||
|
||||
// only maintain and admin are allowed to edit other user's data
|
||||
if (sampleData.user_id.toString() !== req.authDetails.id && !req.auth(res, ['maintain', 'admin'], 'basic')) return;
|
||||
|
||||
@ -48,12 +65,12 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
if (sampleData.note_id !== null) { // old notes data exists
|
||||
const data = await NoteModel.findById(sampleData.note_id).lean().exec().catch(err => {next(err);}) as any;
|
||||
if (data instanceof Error) return;
|
||||
newNotes = !_.isEqual(_.pick(IdValidate.stringify(data), _.keys(sample.notes)), sample.notes);
|
||||
newNotes = !_.isEqual(_.pick(IdValidate.stringify(data), _.keys(sample.notes)), sample.notes); // check if notes were changed
|
||||
if (newNotes) {
|
||||
if (data.hasOwnProperty('custom_fields')) { // update note_fields
|
||||
customFieldsChange(Object.keys(data.custom_fields), -1);
|
||||
}
|
||||
NoteModel.findByIdAndDelete(sampleData.note_id).lean().exec(err => { // delete old notes
|
||||
await NoteModel.findByIdAndDelete(sampleData.note_id).lean().exec(err => { // delete old notes
|
||||
if (err) return console.error(err);
|
||||
});
|
||||
}
|
||||
@ -74,7 +91,8 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
if (!_.isEqual(_.pick(IdValidate.stringify(sampleData), _.keys(sample)), _.omit(sample, ['notes']))) {
|
||||
sample.status = 0;
|
||||
}
|
||||
SampleModel.findByIdAndUpdate(req.params.id, sample, {new: true}).lean().exec((err, data) => {
|
||||
|
||||
await SampleModel.findByIdAndUpdate(req.params.id, sample, {new: true}).lean().exec((err, data) => {
|
||||
if (err) return next(err);
|
||||
res.json(SampleValidate.output(data));
|
||||
});
|
||||
@ -90,12 +108,13 @@ router.delete('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
if (!sampleData) {
|
||||
return res.status(404).json({status: 'Not found'});
|
||||
}
|
||||
|
||||
// only maintain and admin are allowed to edit other user's data
|
||||
if (sampleData.user_id.toString() !== req.authDetails.id && !req.auth(res, ['maintain', 'admin'], 'basic')) return;
|
||||
|
||||
SampleModel.findByIdAndUpdate(req.params.id, {status: -1}).lean().exec(err => { // set sample status
|
||||
await SampleModel.findByIdAndUpdate(req.params.id, {status: -1}).lean().exec(err => { // set sample status
|
||||
if (err) return next(err);
|
||||
if (sampleData.note_id !== null) {
|
||||
if (sampleData.note_id !== null) { // handle notes
|
||||
NoteModel.findById(sampleData.note_id).lean().exec((err, data: any) => { // find notes to update note_fields
|
||||
if (err) return next(err);
|
||||
if (data.hasOwnProperty('custom_fields')) { // update note_fields
|
||||
@ -124,15 +143,15 @@ router.post('/sample/new', async (req, res, next) => {
|
||||
customFieldsChange(Object.keys(sample.notes.custom_fields), 1);
|
||||
}
|
||||
|
||||
sample.status = 0;
|
||||
sample.status = 0; // set status to new
|
||||
sample.number = await numberGenerate(sample, req, res, next);
|
||||
if (!sample.number) return;
|
||||
new NoteModel(sample.notes).save((err, data) => {
|
||||
|
||||
await new NoteModel(sample.notes).save((err, data) => { // save notes
|
||||
if (err) return next(err);
|
||||
delete sample.notes;
|
||||
sample.note_id = data._id;
|
||||
sample.user_id = req.authDetails.id;
|
||||
console.log(sample);
|
||||
new SampleModel(sample).save((err, data) => {
|
||||
if (err) return next(err);
|
||||
res.json(SampleValidate.output(data.toObject()));
|
||||
@ -153,7 +172,7 @@ router.get('/sample/notes/fields', (req, res, next) => {
|
||||
module.exports = router;
|
||||
|
||||
|
||||
async function numberGenerate (sample, req, res, next) { // validate number, returns false if invalid
|
||||
async function numberGenerate (sample, req, res, next) { // generate number, returns false on error
|
||||
const sampleData = await SampleModel
|
||||
.find({number: new RegExp('^' + req.authDetails.location + '[0-9]+$', 'm')})
|
||||
.lean()
|
||||
@ -180,7 +199,8 @@ async function materialCheck (sample, res, next, id = sample.material_id) { //
|
||||
function sampleRefCheck (sample, res, next) { // validate sample_references, resolves false for invalid reference
|
||||
return new Promise(resolve => {
|
||||
if (sample.notes.sample_references.length > 0) { // there are sample_references
|
||||
let referencesCount = sample.notes.sample_references.length;
|
||||
let referencesCount = sample.notes.sample_references.length; // count to keep track of running async operations
|
||||
|
||||
sample.notes.sample_references.forEach(reference => {
|
||||
SampleModel.findById(reference.id).lean().exec((err, data) => {
|
||||
if (err) {next(err); resolve(false)}
|
||||
@ -189,7 +209,7 @@ function sampleRefCheck (sample, res, next) { // validate sample_references, re
|
||||
return resolve(false);
|
||||
}
|
||||
referencesCount --;
|
||||
if (referencesCount <= 0) {
|
||||
if (referencesCount <= 0) { // all async requests done
|
||||
resolve(true);
|
||||
}
|
||||
});
|
||||
@ -201,7 +221,7 @@ function sampleRefCheck (sample, res, next) { // validate sample_references, re
|
||||
});
|
||||
}
|
||||
|
||||
function customFieldsChange (fields, amount) {
|
||||
function customFieldsChange (fields, amount) { // update custom_fields and respective quantities
|
||||
fields.forEach(field => {
|
||||
NoteFieldModel.findOneAndUpdate({name: field}, {$inc: {qty: amount}}, {new: true}).lean().exec((err, data: any) => { // check if field exists
|
||||
if (err) return console.error(err);
|
||||
|
Reference in New Issue
Block a user