lib/controllers.js
function upsertDocumentController(req, res) {
                    var modelName = req.params.modelName,
                        id = req.params.documentId,
                        user = req.admin_user,
                        data = _.extend({}, req.body, req.files),
                        modelConfig = registry.models[modelName],
                        form;

                    polymorphGetDocument(modelConfig.model, modelConfig.is_single, id).then(
                        function createTheForm(document) {
                            var action = document ? 'update' : 'create';
                            var docType = document && document._doc.__t;
                            if (docType) modelConfig = registry.models[docType];
                            var FormType = modelConfig.options.form;
                            if (!user.hasPermissions(modelConfig.modelName, action)) throw new Error('unauthorized');
                            form = new FormType(modelConfig.options, modelConfig.model, document, data);
                            return form.save();
                        }
                    ).then(
                        function (document) {
                            modelConfig.options.post(document);
                            return form;
                        },
                        function (err) {
                            err.modelConfig = modelConfig;
                            err.form = form;
                            if (!form) throw err;

                            form.errors = form.errors || {};
                            if (err.name == 'ValidationError') {
                                form.errors = _.merge(form.errors, err.errors);
                            } else if (err.name === 'MongoError' && err.code == 11000) {
                                form.errors.exception = err;
                                err.name = "Duplicate";
                                var msg = '{' + err.err.split('$').pop().split('{').pop();
                                var ObjectID = /ObjectId\(\'(\w+)\'\)/.exec(msg);
                                err.fields = '\n<br />';
                                err.fields += (ObjectID.length == 2) ? '<a href="' + ObjectID[1] + '" target="_blank">Click to open duplicate document</a>' :
                                    msg;
                            } else {
                                form.errors.exception = err;
                            }
                            if (_.isEmpty(form.errors)) return form;
                            throw err;
                        }
                    ).then(
                        function (form) {
                            res._debug_form = form;
                            var response = form.instance.toJSON();
                            response.id = response.id || form.instance._id;
                            response.label = response.label || form.instance.name || form.instance.title || form.instance.toString();
                            return response;
                        }
                    ).then(
                        function (response) {
                            res.json(200, response);
                        },
                        function (err) {
                            err.form = res._debug_form;
                            res.json(422, err);
                        }
                    ).end();
                }
function upsertDocumentController(req, res) {
                    var modelName = req.params.modelName,
                        id = req.params.documentId,
                        user = req.admin_user,
                        data = _.extend({}, req.body, req.files),
                        modelConfig = registry.models[modelName],
                        form;

                    polymorphGetDocument(modelConfig.model, modelConfig.is_single, id).then(
                        function createTheForm(document) {
                            var action = document ? 'update' : 'create';
                            var docType = document && document._doc.__t;
                            if (docType) modelConfig = registry.models[docType];
                            var FormType = modelConfig.options.form;
                            if (!user.hasPermissions(modelConfig.modelName, action)) throw new Error('unauthorized');
                            form = new FormType(modelConfig.options, modelConfig.model, document, data);
                            return form.save();
                        }
                    ).then(
                        function (document) {
                            modelConfig.options.post(document);
                            return form;
                        },
                        function (err) {
                            err.modelConfig = modelConfig;
                            err.form = form;
                            if (!form) throw err;

                            form.errors = form.errors || {};
                            if (err.name == 'ValidationError') {
                                form.errors = _.merge(form.errors, err.errors);
                            } else if (err.name === 'MongoError' && err.code == 11000) {
                                form.errors.exception = err;
                                err.name = "Duplicate";
                                var msg = '{' + err.err.split('$').pop().split('{').pop();
                                var ObjectID = /ObjectId\(\'(\w+)\'\)/.exec(msg);
                                err.fields = '\n<br />';
                                err.fields += (ObjectID.length == 2) ? '<a href="' + ObjectID[1] + '" target="_blank">Click to open duplicate document</a>' :
                                    msg;
                            } else {
                                form.errors.exception = err;
                            }
                            if (_.isEmpty(form.errors)) return form;
                            throw err;
                        }
                    ).then(
                        function (form) {
                            res._debug_form = form;
                            var response = form.instance.toJSON();
                            response.id = response.id || form.instance._id;
                            response.label = response.label || form.instance.name || form.instance.title || form.instance.toString();
                            return response;
                        }
                    ).then(
                        function (response) {
                            res.json(200, response);
                        },
                        function (err) {
                            err.form = res._debug_form;
                            res.json(422, err);
                        }
                    ).end();
                }
function upsertDocumentController ( req , res ) { var modelName = req . params . modelName , id = req . params . documentId , user = req . admin_user , data = _ . extend ( { } , req . body , req . files ) , modelConfig = registry . models [ modelName ] , form ; polymorphGetDocument ( modelConfig . model , modelConfig . is_single , id ) . then ( function createTheForm ( document ) { var action = document ? 'update' : 'create' ; var docType = document && document . _doc . __t ; if ( docType ) modelConfig = registry . models [ docType ] ; var FormType = modelConfig . options . form ; if ( ! user . hasPermissions ( modelConfig . modelName , action ) ) throw new Error ( 'unauthorized' ) ; form = new FormType ( modelConfig . options , modelConfig . model , document , data ) ; return form . save ( ) ; } ) . then ( function ( document ) { modelConfig . options . post ( document ) ; return form ; } , function ( err ) { err . modelConfig = modelConfig ; err . form = form ; if ( ! form ) throw err ; form . errors = form . errors || { } ; if ( err . name == 'ValidationError' ) { form . errors = _ . merge ( form . errors , err . errors ) ; } else if ( err . name === 'MongoError' && err . code == 11000 ) { form . errors . exception = err ; err . name = "Duplicate" ; var msg = '{' + err . err . split ( '$' ) . pop ( ) . split ( '{' ) . pop ( ) ; var ObjectID = / ObjectId\(\'(\w+)\'\) / . exec ( msg ) ; err . fields = '\n<br />' ; err . fields += ( ObjectID . length == 2 ) ? '<a href="' + ObjectID [ 1 ] + '" target="_blank">Click to open duplicate document</a>' : msg ; } else { form . errors . exception = err ; } if ( _ . isEmpty ( form . errors ) ) return form ; throw err ; } ) . then ( function ( form ) { res . _debug_form = form ; var response = form . instance . toJSON ( ) ; response . id = response . id || form . instance . _id ; response . label = response . label || form . instance . name || form . instance . title || form . instance . toString ( ) ; return response ; } ) . then ( function ( response ) { res . json ( 200 , response ) ; } , function ( err ) { err . form = res . _debug_form ; res . json ( 422 , err ) ; } ) . end ( ) ; }
In use by related-model-modal
In use by related - model - modal