Source: controller/account.js

// dependency modules
const express = require('express')
const AccountRepository = require('../repository/account')

// controller
// using to define router in controller
// params
// - db <connection>: instance of mongoose connection
// - oauth <object>: contains information about oauth provider
//   see ./lib/derault-value.js fo more information
// return express router

/**
 * accountController module
 @module accountController
 */
module.exports = function (db, oauth, auth) {
  // create oauth provider connector
  var router = express.Router()
  var accountRepo = new AccountRepository(db, oauth)

  router.route('/')
  .get(auth.authorize({
    msg_401: 'must login to perform this action',
    msg_403: 'must correct permission to perform this action'
  }),
  function (req, res) {
    res.json({
      oauth_acc: req.oauth_acc,
      app_acc: req.app_acc
    })
  })

  router.route('/me')
  .get(function (req, response) {
    accountRepo.authenticateEx(req, function (err, user) {
      if (err) {
        response.status(401).json({
          message: 'repository.account.me.error',
          data: err
        })
        return
      }

      // get information in wiser data base
      accountRepo.findByIssuer(null, user.id, function (repoErr, acc) {
        if (repoErr || !acc) {
          response.status(400).json({
            message: 'bad request',
            data: 'language code invalid, it must same as "eng"'
          })
          return
        }

        // make information
        var user_ = {
          id: acc._id,
          image: user.image.url,
          language: acc.language,
          displayName: user.displayName,
          scheduler: acc.scheduler
        }
        response.json(user_)
      })
    })
  })

  router.route('/me/language/:language')
  .put(function (req, res) {
    // verify language
    if (!req.params.language || req.params.language.length !== 2) {
      res.status(400).json({
        message: 'bad request',
        data: 'language code invalid, it must same as "en"'
      })
      return
    }

    accountRepo.authenticateEx(req, function (authErr, user) {
      if (authErr) {
        res.status(401).json({
          message: 'unauthorized',
          data: authErr
        })
        return
      }

      accountRepo.updateLanguage(user.id, req.params.language, function (repoErr) {
        if (repoErr) {
          res.status(500).json(repoErr)
          return
        }
        res.json({
          message: 'done'
        })
      })
    })
  })

  router.route('/me/scheduler/:id')
  .put(function (req, res) {
    accountRepo.authenticateEx(req, function (authErr, user) {
      if (authErr) {
        res.status(401).json({
          message: 'unauthorized',
          data: authErr
        })
        return
      }

      accountRepo.updateScheduler(user.id, req.params.id, function (repoErr) {
        if (repoErr) {
          res.status(500).json(repoErr)
          return
        }
        res.json({
          message: 'done'
        })
      })
    })
  })

  return router
}