All files / lib/controllers notification.ts

19.15% Statements 9/47
0% Branches 0/8
12.5% Functions 1/8
19.15% Lines 9/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100          16x 16x 16x   16x                     16x                                                                               16x                       16x                         16x                       16x    
import Crowi from 'server/crowi'
import ApiResponse from '../util/apiResponse'
 
export default (crowi: Crowi) => {
  // const debug = Debug('crowi:routes:notification')
  const Notification = crowi.model('Notification')
  const actions = {} as any
  actions.api = {} as any
 
  actions.notificationPage = function(req, res) {
    return res.render('notification', {})
  }
 
  /**
   * @api {get} /notifications.list
   * @apiName ListNotifications
   * @apiGroup Notification
   *
   * @apiParam {String} linit
   */
  actions.api.list = function(req, res) {
    const user = req.user
 
    let limit = 10
    if (req.query.limit) {
      limit = parseInt(req.query.limit, 10)
    }
 
    let offset = 0
    if (req.query.offset) {
      offset = parseInt(req.query.offset, 10)
    }
 
    const requestLimit = limit + 1
 
    Notification.findLatestNotificationsByUser(user, requestLimit, offset)
      .then(function(notifications) {
        let hasPrev = false
        if (offset > 0) {
          hasPrev = true
        }
 
        let hasNext = false
        if (notifications.length > limit) {
          hasNext = true
        }
 
        const result = {
          notifications: notifications.slice(0, limit),
          hasPrev: hasPrev,
          hasNext: hasNext,
        }
 
        return res.json(ApiResponse.success(result))
      })
      .catch(function(err) {
        return res.json(ApiResponse.error(err))
      })
  }
 
  actions.api.read = function(req, res) {
    const user = req.user
 
    try {
      const notification = Notification.read(user)
      const result = { notification }
      return res.json(ApiResponse.success(result))
    } catch (err) {
      return res.json(ApiResponse.error(err))
    }
  }
 
  actions.api.open = async function(req, res) {
    const user = req.user
    const id = req.body.id
 
    try {
      const notification = await Notification.open(user, id)
      const result = { notification }
      return res.json(ApiResponse.success(result))
    } catch (err) {
      return res.json(ApiResponse.error(err))
    }
  }
 
  actions.api.status = async function(req, res) {
    const user = req.user
 
    try {
      const count = await Notification.getUnreadCountByUser(user)
      const result = { count }
      return res.json(ApiResponse.success(result))
    } catch (err) {
      return res.json(ApiResponse.error(err))
    }
  }
 
  return actions
}