2018-12-17 16:37:19 +01:00
'use strict' ;
exports = module . exports = {
2021-05-28 14:34:18 -07:00
load ,
2021-04-20 13:49:05 -07:00
get ,
list ,
2021-04-21 12:00:07 -07:00
update
2018-12-17 16:37:19 +01:00
} ;
2021-06-01 09:35:20 -07:00
const 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 ,
2021-05-28 14:34:18 -07:00
notifications = require ( '../notifications.js' ) ,
safe = require ( 'safetydance' ) ;
2019-10-22 12:59:26 -07:00
2021-05-28 14:34:18 -07:00
async function load ( req , res , next ) {
assert . strictEqual ( typeof req . params . notificationId , 'string' ) ;
2018-12-17 16:37:19 +01:00
2021-05-28 14:34:18 -07:00
const [ error , result ] = await safe ( notifications . get ( req . params . notificationId ) ) ;
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
if ( ! result ) return next ( new HttpError ( 404 , 'Notification not found' ) ) ;
2018-12-17 16:37:19 +01:00
2021-05-28 14:34:18 -07:00
req . resource = result ;
next ( ) ;
2018-12-17 16:37:19 +01:00
}
function get ( req , res , next ) {
2021-05-28 14:34:18 -07:00
assert . strictEqual ( typeof req . resource , 'object' ) ;
2018-12-17 16:37:19 +01:00
2021-05-28 14:34:18 -07:00
next ( new HttpSuccess ( 200 , req . resource ) ) ;
2018-12-17 16:37:19 +01:00
}
2021-05-28 14:34:18 -07:00
async function list ( req , res , next ) {
2021-04-20 13:49:05 -07:00
const page = typeof req . query . page !== 'undefined' ? parseInt ( req . query . page ) : 1 ;
2018-12-17 16:37:19 +01:00
if ( ! page || page < 0 ) return next ( new HttpError ( 400 , 'page query param has to be a postive number' ) ) ;
2021-04-20 13:49:05 -07:00
const perPage = typeof req . query . per _page !== 'undefined' ? parseInt ( req . query . per _page ) : 25 ;
2018-12-17 16:37:19 +01:00
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
if ( req . query . acknowledged && ! ( req . query . acknowledged === 'true' || req . query . acknowledged === 'false' ) ) return next ( new HttpError ( 400 , 'acknowledged must be a true or false' ) ) ;
2018-12-17 16:37:19 +01:00
2021-05-29 12:11:28 -07:00
let filters = { } ;
if ( req . query . acknowledged ) filters . acknowledged = req . query . acknowledged === 'true' ;
2021-04-20 13:49:05 -07:00
2021-05-29 12:11:28 -07:00
const [ error , result ] = await safe ( notifications . list ( filters , page , perPage ) ) ;
2021-05-28 14:34:18 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
next ( new HttpSuccess ( 200 , { notifications : result } ) ) ;
2018-12-17 16:37:19 +01:00
}
2021-05-28 14:34:18 -07:00
async function update ( req , res , next ) {
assert . strictEqual ( typeof req . resource , 'object' ) ;
2021-04-21 12:00:07 -07:00
assert . strictEqual ( typeof req . body , 'object' ) ;
2018-12-17 16:37:19 +01:00
2021-04-21 12:00:07 -07:00
if ( typeof req . body . acknowledged !== 'boolean' ) return next ( new HttpError ( 400 , 'acknowledged must be a booliean' ) ) ;
2021-05-28 14:34:18 -07:00
const [ error ] = await safe ( notifications . update ( req . resource , { acknowledged : req . body . acknowledged } ) ) ;
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
next ( new HttpSuccess ( 204 , { } ) ) ;
2018-12-17 16:37:19 +01:00
}