/** * Checks the validity of references (e.g. to addresses) */ 'use strict'; var mongodb = require('mongodb'); var Q = require('q'); var mainDB = require(global.pathPrefix + 'mainDB.js'); var utils = require(global.pathPrefix + 'utils.js'); const ERRORS = { INVALID_ADDRESS: 'BRIDGE: INVALID ADDRESS', INVALID_ACCOUNT: 'BRIDGE: INVALID ACCOUNT', INVALID_CLIENT: 'BRIDGE: INVALID_CLIENT', INVALID_DEVICE: 'BRIDGE: INVALID_DEVICE' }; module.exports = { isValidAddressRef: isValidAddressRef, getAccount: getAccount, getEmailAddress: getEmailAddress, getClient: getClient, getClientByEmail: getClientByEmail, getDevice: getDevice, ERRORS: ERRORS }; /** * Function to check that the given address reference is valid for the client * * @param {string} clientID - The client's unique id * @param {string} addressRef - The address id to check * @param {string} caller - The calling function's name (for debug) * * @returns {promise} - Promise that resolves to the found address * or rejects with an error code */ function isValidAddressRef(clientID, addressRef, caller) { var addrQuery = { _id: mongodb.ObjectID(addressRef), // The id given ClientID: clientID // Must be *my* address }; var options = { fields: {}, // Don't want any fields, just checking existence comment: caller }; return Q.nfcall( mainDB.findOneObject, mainDB.collectionAddresses, addrQuery, options, false ).then(function(item) { if (item === null) { // // Didn't find a matching address (doesn't exist, or // doesn't belong to client). // return Q.reject({name: ERRORS.INVALID_ADDRESS}); } else { // // Item was found so it exists and belongs to this client // return Q.resolve(item); } }); } /** * Function that gets an account from an ID, if it belongs to the specified * client. It finds only active accounts by default, but can optionally find * deleted accounts. * * @param {string} accountID - The id of the account to find * @param {string} clientID - The client's unique ID * @param {?bool} includeDeleted - True to include deleted accounts * * @returns {Promise} - Promise that resolves to the account or rejects with error */ function getAccount(accountID, clientID, includeDeleted) { var query = { _id: mongodb.ObjectID(accountID), ClientID: clientID }; var options = { comment: 'references:getAccount' }; if (!includeDeleted) { query.AccountStatus = { $bitsAllClear: utils.AccountDeleted }; } return Q.nfcall( mainDB.findOneObject, mainDB.collectionAccount, query, options, false // Don't suppress errors ).then(function(account) { if (!account) { return Q.reject({name: ERRORS.INVALID_ACCOUNT}); } else { return account; } }); } /** * Returns a promise for the email address for a client based on their ID * * @param {String} clientID - The unique id for the client * * @return {Promise} - Promise for the email address if found */ function getEmailAddress(clientID) { return getClient(clientID).then((client) => client.ClientName); } /** * Returns a promise for the client object based on their ID * * @param {String} clientID - The unique id for the client * * @return {Promise} - Promise for the client object if found */ function getClient(clientID) { var query = { ClientID: clientID }; var options = { comment: 'references:getClient' }; return Q.nfcall( mainDB.findOneObject, mainDB.collectionClient, query, options, false // Don't suppress errors ).then(function(client) { if (!client) { return Q.reject({name: ERRORS.INVALID_CLIENT}); } else { return client; } }); } /** * Returns a promise for the client object based on their ID * * @param {String} email - The email for the client * @returns {Promise} - Promise for the client object if found */ function getClientByEmail(email) { var query = { ClientName: email }; var options = { comment: 'references:getClientByEmail' }; return Q.nfcall( mainDB.findOneObject, mainDB.collectionClient, query, options, false // Don't suppress errors ).then(function(client) { if (!client) { return Q.reject({name: ERRORS.INVALID_CLIENT}); } else { return client; } }); } /** * Returns a promise for the active device belong to a client * * @param {string} deviceNumber - The number of the device to find * @param {string} clientID - The client's unique ID * @param {?bool} includeInactive - True to include devices that are barred/disabled/etc * * @returns {Promise} - Promise that resolves to the account or rejects with error */ function getDevice(deviceNumber, clientID, includeInactive) { const query = { ClientID: clientID, DeviceNumber: deviceNumber }; const options = {}; if (!includeInactive) { /* Expected use of bitwise & */ /* jshint -W016 */ query.DeviceStatus = { $bitsAllSet: utils.DeviceFullyRegistered, $bitsAllClear: utils.DeviceSuspendedMask & utils.DeviceBarredMask }; /* jshint +W016 */ } return Q.nfcall( mainDB.findOneObject, mainDB.collectionDevice, query, options, false // Don't suppress errors ).then(function(device) { if (!device) { return Q.reject({name: ERRORS.INVALID_DEVICE}); } else { return device; } }); }