2018-12-17 16:37:19 +01:00
'use strict' ;
exports = module . exports = {
verifyOwnership : verifyOwnership ,
get : get ,
list : list ,
ack : ack
} ;
let assert = require ( 'assert' ) ,
2019-10-22 12:59:26 -07:00
BoxError = require ( '../boxerror.js' ) ,
2018-12-17 16:37:19 +01:00
HttpError = require ( 'connect-lastmile' ) . HttpError ,
HttpSuccess = require ( 'connect-lastmile' ) . HttpSuccess ,
2019-10-22 12:59:26 -07:00
notifications = require ( '../notifications.js' ) ;
2018-12-17 16:37:19 +01:00
function verifyOwnership ( req , res , next ) {
if ( ! req . params . notificationId ) return next ( ) ; // skip for listing
notifications . get ( req . params . notificationId , function ( error , result ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-12-17 16:37:19 +01:00
2019-03-23 14:07:37 -07:00
if ( result . userId !== req . user . id ) return next ( new HttpError ( 403 , 'User is not owner' ) ) ;
2018-12-17 16:37:19 +01:00
req . notification = result ;
next ( ) ;
} ) ;
}
function get ( req , res , next ) {
assert . strictEqual ( typeof req . notification , 'object' ) ;
next ( new HttpSuccess ( 200 , { notification : req . notification } ) ) ;
}
function list ( req , res , next ) {
var page = typeof req . query . page !== 'undefined' ? parseInt ( req . query . page ) : 1 ;
if ( ! page || page < 0 ) return next ( new HttpError ( 400 , 'page query param has to be a postive number' ) ) ;
var perPage = typeof req . query . per _page !== 'undefined' ? parseInt ( req . query . per _page ) : 25 ;
if ( ! perPage || perPage < 0 ) return next ( new HttpError ( 400 , 'per_page query param has to be a postive number' ) ) ;
2019-01-07 16:18:18 +01:00
var acknowledged = null ;
if ( req . query . acknowledged && ! ( req . query . acknowledged === 'true' || req . query . acknowledged === 'false' ) ) return next ( new HttpError ( 400 , 'acknowledged must be a true or false' ) ) ;
2019-01-08 13:38:30 +01:00
else if ( req . query . acknowledged ) acknowledged = req . query . acknowledged === 'true' ? true : false ;
2018-12-17 16:37:19 +01:00
2019-01-07 16:18:18 +01:00
notifications . getAllPaged ( req . user . id , acknowledged , page , perPage , function ( error , result ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-12-17 16:37:19 +01:00
next ( new HttpSuccess ( 200 , { notifications : result } ) ) ;
} ) ;
}
function ack ( req , res , next ) {
assert . strictEqual ( typeof req . params . notificationId , 'string' ) ;
notifications . ack ( req . params . notificationId , function ( error ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-12-17 16:37:19 +01:00
next ( new HttpSuccess ( 204 , { } ) ) ;
} ) ;
}