resumeRoutes.js

const dbCollections = require('../../config/db/dbCollections');

function resumeRoutes(app, routeOptions) {
    // List of collections available
    const DB = dbCollections(routeOptions.database);

    app.post('/api/resume/save', function(req, res) {
        const resumeData = req.body;
        const username = req.decoded.username;
        const query   = { username: username };
        const project = { _id: 0, username: 1 };
        const version = { timestamp: new Date().getTime(), data: resumeData };

        DB.ProfileClln.findOne(query, project, (err, result) => {
            if (result !== null) {
                // Add one more profile version for current user
                const update = { $push: {'versions': version } };
                DB.ProfileClln.update(query, update, (err, result) => {
                    if(err) {
                        console.error(err);
                        res.json({
                            success: false,
                            message: 'Profile Update failed'
                        });
                    } else {
                        console.log(result);
                        res.json({
                            success: true,
                            message: 'Profile Updated'
                        });
                    }
                });
            }
            else {
                // Add new record
                const record = {
                    'username': username,
                    'versions': [ version ]
                };
                DB.ProfileClln.insert(record, (err, result) => {
                    if(err) {
                        console.error(err);
                        res.json({
                            success: false,
                            message: 'Profile Save failed'
                        });
                    } else {
                        console.log(result);
                        res.json({
                            success: true,
                            message: 'Profile Saved'
                        });
                    }
                });
            }
        });
    });
}

module.exports = resumeRoutes;