PUT finished
This commit is contained in:
@ -46,25 +46,31 @@ router.put('/sample/' + IdValidate.parameter(), (req, res, next) => {
|
||||
if (!await materialCheck(sample, res, next, sampleData.material_id)) return;
|
||||
}
|
||||
|
||||
if (sample.hasOwnProperty('notes') && sampleData.note_id !== null) { // deal with old notes data
|
||||
NoteModel.findById(sampleData.note_id).lean().exec((err, data: any) => {
|
||||
if (err) return console.error(err);
|
||||
if (data.hasOwnProperty('custom_fields')) { // update note_fields
|
||||
customFieldsChange(Object.keys(data.custom_fields), -1);
|
||||
if (sample.hasOwnProperty('notes')) {
|
||||
let newNotes = true;
|
||||
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);
|
||||
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
|
||||
if (err) return console.error(err);
|
||||
});
|
||||
}
|
||||
NoteModel.findByIdAndDelete(sampleData.note_id).lean().exec(err => { // delete old notes
|
||||
if (err) return console.error(err);
|
||||
})
|
||||
});
|
||||
}
|
||||
if (sample.hasOwnProperty('notes') && Object.keys(sample.notes).length > 0) { // save new notes
|
||||
if (!await sampleRefCheck(sample, res, next)) return;
|
||||
if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) { // new custom_fields
|
||||
customFieldsChange(Object.keys(sample.notes.custom_fields), 1);
|
||||
}
|
||||
let data = await new NoteModel(sample.notes).save().catch(err => { return next(err)}); // save new notes
|
||||
delete sample.notes;
|
||||
sample.note_id = data._id;
|
||||
|
||||
if (_.keys(sample.notes).length > 0 && newNotes) { // save new notes
|
||||
if (!await sampleRefCheck(sample, res, next)) return;
|
||||
if (sample.notes.hasOwnProperty('custom_fields') && Object.keys(sample.notes.custom_fields).length > 0) { // new custom_fields
|
||||
customFieldsChange(Object.keys(sample.notes.custom_fields), 1);
|
||||
}
|
||||
let data = await new NoteModel(sample.notes).save().catch(err => { return next(err)}); // save new notes
|
||||
delete sample.notes;
|
||||
sample.note_id = data._id;
|
||||
}
|
||||
}
|
||||
|
||||
// check for changes
|
||||
@ -160,9 +166,7 @@ async function numberCheck (sample, res, next) { // validate number, returns fa
|
||||
|
||||
async function materialCheck (sample, res, next, id = sample.material_id) { // validate material_id and color, returns false if invalid
|
||||
const materialData = await MaterialModel.findById(id).lean().exec().catch(err => {next(err); return false;}) as any;
|
||||
if (materialData instanceof Error) {
|
||||
return false;
|
||||
}
|
||||
if (materialData instanceof Error) return false;
|
||||
if (!materialData) { // could not find material_id
|
||||
res.status(400).json({status: 'Material not available'});
|
||||
return false;
|
||||
|
Reference in New Issue
Block a user