2018-12-17 15:52:52 +01:00
'use strict' ;
exports = module . exports = {
2021-04-21 12:00:07 -07:00
get ,
getByUserIdAndTitle ,
add ,
update ,
del ,
list ,
2019-03-23 09:46:04 -07:00
// exported for testing
_clear : clear
2018-12-17 15:52:52 +01:00
} ;
let assert = require ( 'assert' ) ,
2019-10-24 11:13:48 -07:00
BoxError = require ( './boxerror.js' ) ,
database = require ( './database.js' ) ;
2018-12-17 15:52:52 +01:00
2019-02-28 14:59:33 -08:00
const NOTIFICATION _FIELDS = [ 'id' , 'userId' , 'eventId' , 'title' , 'message' , 'creationTime' , 'acknowledged' ] ;
2018-12-17 15:52:52 +01:00
function postProcess ( result ) {
assert . strictEqual ( typeof result , 'object' ) ;
result . id = String ( result . id ) ;
// convert to boolean
result . acknowledged = ! ! result . acknowledged ;
}
function add ( notification , callback ) {
assert . strictEqual ( typeof notification , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2019-03-04 20:44:41 -08:00
const query = 'INSERT INTO notifications (userId, eventId, title, message, acknowledged) VALUES (?, ?, ?, ?, ?)' ;
const args = [ notification . userId , notification . eventId , notification . title , notification . message , notification . acknowledged ] ;
2018-12-17 15:52:52 +01:00
database . query ( query , args , function ( error , result ) {
2019-10-24 11:13:48 -07:00
if ( error && error . code === 'ER_NO_REFERENCED_ROW_2' ) return callback ( new BoxError ( BoxError . NOT _FOUND , 'no such eventlog entry' ) ) ;
if ( error ) return callback ( new BoxError ( BoxError . DATABASE _ERROR , error ) ) ;
2018-12-17 15:52:52 +01:00
callback ( null , String ( result . insertId ) ) ;
} ) ;
}
2019-03-04 20:58:27 -08:00
function getByUserIdAndTitle ( userId , title , callback ) {
assert . strictEqual ( typeof userId , 'string' ) ;
assert . strictEqual ( typeof title , 'string' ) ;
2019-02-06 15:46:58 +01:00
assert . strictEqual ( typeof callback , 'function' ) ;
2019-03-04 20:58:27 -08:00
database . query ( 'SELECT ' + NOTIFICATION _FIELDS + ' from notifications WHERE userId = ? AND title = ? ORDER BY creationTime LIMIT 1' , [ userId , title ] , function ( error , results ) {
2019-10-24 11:13:48 -07:00
if ( error ) return callback ( new BoxError ( BoxError . DATABASE _ERROR , error ) ) ;
2019-10-24 20:48:38 -07:00
if ( results . length === 0 ) return callback ( new BoxError ( BoxError . NOT _FOUND , 'Notification not found' ) ) ;
2019-02-06 15:46:58 +01:00
2019-03-04 20:58:27 -08:00
postProcess ( results [ 0 ] ) ;
2019-02-06 15:46:58 +01:00
2019-03-04 20:58:27 -08:00
callback ( null , results [ 0 ] ) ;
2019-02-06 15:46:58 +01:00
} ) ;
}
2018-12-17 15:52:52 +01:00
function update ( id , data , callback ) {
assert . strictEqual ( typeof id , 'string' ) ;
assert . strictEqual ( typeof data , 'object' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
let args = [ ] ;
let fields = [ ] ;
for ( let k in data ) {
fields . push ( k + ' = ?' ) ;
args . push ( data [ k ] ) ;
}
args . push ( id ) ;
database . query ( 'UPDATE notifications SET ' + fields . join ( ', ' ) + ' WHERE id = ?' , args , function ( error , result ) {
2019-10-24 11:13:48 -07:00
if ( error ) return callback ( new BoxError ( BoxError . DATABASE _ERROR , error ) ) ;
2019-10-24 20:48:38 -07:00
if ( result . affectedRows !== 1 ) return callback ( new BoxError ( BoxError . NOT _FOUND , 'Notification not found' ) ) ;
2018-12-17 15:52:52 +01:00
2019-03-04 20:58:27 -08:00
callback ( null ) ;
2018-12-17 15:52:52 +01:00
} ) ;
}
function get ( id , callback ) {
assert . strictEqual ( typeof id , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'SELECT ' + NOTIFICATION _FIELDS + ' FROM notifications WHERE id = ?' , [ id ] , function ( error , result ) {
2019-10-24 11:13:48 -07:00
if ( error ) return callback ( new BoxError ( BoxError . DATABASE _ERROR , error ) ) ;
2019-10-24 20:48:38 -07:00
if ( result . length === 0 ) return callback ( new BoxError ( BoxError . NOT _FOUND , 'Notification not found' ) ) ;
2018-12-17 15:52:52 +01:00
postProcess ( result [ 0 ] ) ;
callback ( null , result [ 0 ] ) ;
} ) ;
}
function del ( id , callback ) {
assert . strictEqual ( typeof id , 'string' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'DELETE FROM notifications WHERE id = ?' , [ id ] , function ( error , result ) {
2019-10-24 11:13:48 -07:00
if ( error ) return callback ( new BoxError ( BoxError . DATABASE _ERROR , error ) ) ;
2019-10-24 20:48:38 -07:00
if ( result . affectedRows !== 1 ) return callback ( new BoxError ( BoxError . NOT _FOUND , 'Notification not found' ) ) ;
2018-12-17 15:52:52 +01:00
callback ( null ) ;
} ) ;
}
2021-04-21 12:00:07 -07:00
function list ( filters , page , perPage , callback ) {
assert . strictEqual ( typeof filters , 'object' ) ;
2018-12-17 15:52:52 +01:00
assert . strictEqual ( typeof page , 'number' ) ;
assert . strictEqual ( typeof perPage , 'number' ) ;
assert . strictEqual ( typeof callback , 'function' ) ;
2021-04-21 12:00:07 -07:00
let args = [ ] ;
let where = [ ] ;
if ( 'userId' in filters ) {
where . push ( 'userId=?' ) ;
args . push ( filters . userId ) ;
}
2021-04-20 13:49:05 -07:00
2021-04-21 12:00:07 -07:00
if ( 'acknowledged' in filters ) {
where . push ( 'acknowledged=?' ) ;
args . push ( filters . acknowledged ) ;
2021-04-20 13:49:05 -07:00
}
2018-12-17 15:52:52 +01:00
2021-04-21 12:00:07 -07:00
let query = ` SELECT ${ NOTIFICATION _FIELDS } FROM notifications ` ;
if ( where . length ) query += ' WHERE ' + where . join ( ' AND ' ) ;
2019-05-13 22:05:58 +02:00
query += ' ORDER BY creationTime DESC LIMIT ?,?' ;
2018-12-17 15:52:52 +01:00
2021-04-20 13:49:05 -07:00
args . push ( ( page - 1 ) * perPage ) ;
args . push ( perPage ) ;
2018-12-17 15:52:52 +01:00
2021-04-20 13:49:05 -07:00
database . query ( query , args , function ( error , results ) {
2019-10-24 11:13:48 -07:00
if ( error ) return callback ( new BoxError ( BoxError . DATABASE _ERROR , error ) ) ;
2018-12-17 15:52:52 +01:00
results . forEach ( postProcess ) ;
callback ( null , results ) ;
} ) ;
}
2019-03-23 09:46:04 -07:00
function clear ( callback ) {
assert . strictEqual ( typeof callback , 'function' ) ;
database . query ( 'DELETE FROM notifications' , function ( error ) {
2019-10-24 11:13:48 -07:00
if ( error ) return callback ( new BoxError ( BoxError . DATABASE _ERROR , error ) ) ;
2019-03-23 09:46:04 -07:00
callback ( null ) ;
} ) ;
}