Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nethlink. added get and post #309

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ function setCompUtil(comp) {
* 1. [`user/me`](#meget)
* 1. [`user/endpoints/all`](#userendpointsallget)
* 1. [`user/paramurl`](#paramurlget)
* 1. [`user/nethlink`](#nethlinkget)
*
* ---
*
Expand Down Expand Up @@ -582,6 +583,7 @@ function setCompUtil(comp) {
* @param {string} presencelist_onbusy To get the list of possible conditional presence on busy status
* @param {string} presencelist_onunavailable To get the list of possible conditional presence on unavailable status
* @param {string} all_avatars To get the all user settings
* @param {string} nethlink Set the nethlink object
*/
'get': [
'me',
Expand All @@ -593,7 +595,8 @@ function setCompUtil(comp) {
'presence_onbusy',
'presencelist_onbusy',
'presence_onunavailable',
'presencelist_onunavailable'
'presencelist_onunavailable',
'nethlink'
],

/**
Expand All @@ -608,14 +611,16 @@ function setCompUtil(comp) {
* @param {string} presence_onbusy Set a conditional presence status on busy for the user
* @param {string} presence_onunavailable Set a conditional presence status on unavailable for the user
* @param {string} mobile Associate a mobile phone number to the user
* @param {string} nethlink Set the nethlink object
*/
'post': [
'presence',
'settings',
'default_device',
'presence_onbusy',
'presence_onunavailable',
'mobile'
'mobile',
'nethlink'
],
'head': [],

Expand Down Expand Up @@ -1002,6 +1007,33 @@ function setCompUtil(comp) {
}
},


/**
* Manages GET and POST requests to get/set the nethlink object
*
* GET nethlink
* POST nethlink
*
* @method nethlink
* @param {object} req The client request
* @param {object} res The client response
* @param {function} next Function to run the next handler in the chain
*/
nethlink: function(req, res, next) {
try {
if (req.method.toLowerCase() === 'get') {
nethlinkGet(req, res, next);
} else if (req.method.toLowerCase() === 'post') {
nethlinkPost(req, res, next);
} else {
logger.log.warn(IDLOG, 'unknown requested method ' + req.method);
}
} catch (err) {
logger.log.error(IDLOG, err.stack);
compUtil.net.sendHttp500(IDLOG, res, err.toString());
}
},

/**
* Save/Delete the user settings by the following REST API:
*
Expand Down Expand Up @@ -1138,6 +1170,7 @@ function setCompUtil(comp) {
exports.setting = user.setting;
exports.paramurl = user.paramurl;
exports.presence = user.presence;
exports.nethlink = user.nethlink;
exports.settings = user.settings;
exports.setLogger = setLogger;
exports.endpoints = user.endpoints;
Expand Down Expand Up @@ -1523,3 +1556,76 @@ function presenceSet(req, res, next) {
compUtil.net.sendHttp500(IDLOG, res, error.toString());
}
}

/**
* Associate a connection nethlink timestamp to the user with the following REST API:
*
* POST nethlink
*
* @method nethlinkPost
* @param {object} req The client request
* @param {object} res The client response
* @param {function} next Function to run the next handler in the chain
*/
function nethlinkPost(req, res, next) {
try {
var username = req.headers.authorization_user;
var extension = req.params.extension;
var actualDate = new Date()

compUser.setNethLinkTimeStamp(username, extension, actualDate, function(err) {
try {
if (err) {
logger.log.error(IDLOG, 'setting timestamp "' + actualDate + '" to user "' + username + '"');
compUtil.net.sendHttp500(IDLOG, res, err.toString());
return;
}
logger.log.info(IDLOG, 'timestamp "' + actualDate + '" has been set successfully to user "' + username + '" ');
compUtil.net.sendHttp200(IDLOG, res);

} catch (err1) {
logger.log.error(IDLOG, err1.stack);
compUtil.net.sendHttp500(IDLOG, res, err1.toString());
}
}
);
} catch (err) {
logger.log.error(IDLOG, err.stack);
compUtil.net.sendHttp500(IDLOG, res, err.toString());
}
}

/**
* Associate a connection nethlink timestamp to the user with the following REST API:
*
* GET nethlink
*
* @method nethlinkGet
* @param {object} req The client request
* @param {object} res The client response
* @param {function} next Function to run the next handler in the chain
*/
function nethlinkGet(req, res, next) {
try {
var username = req.headers.authorization_user;

compUser.getNethLinkTimeStamp(username, function(err, results) {
try {
if (err) {
throw err;
}

logger.log.info(IDLOG, 'timestamp has been reading from user ' + username);
res.send(200, results);

} catch (err1) {
logger.log.error(IDLOG, err1.stack);
compUtil.net.sendHttp500(IDLOG, res, err1.toString());
}
}
);
} catch (err) {
logger.log.error(IDLOG, err.stack);
compUtil.net.sendHttp500(IDLOG, res, err.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ const migratedTables = [
'pin',
'queue_log',
'phonebook',
'cti_phonebook'
'cti_phonebook',
'user_nethlink'
];

/**
Expand All @@ -144,6 +145,7 @@ const migratedTables = [
INCOMING: "incoming",
VOICEMAIL: "voicemail",
PHONEBOOK: "phonebook",
USER_NETHLINK: 'user_nethlink',
QUEUE_LOG: "queue_log",
REST_USERS: "rest_users",
SMS_HISTORY: "sms_history",
Expand All @@ -167,6 +169,7 @@ const migratedTables = [
INCOMING: 'incoming',
VOICEMAIL: 'voicemail',
PHONEBOOK: 'phonebook',
USER_NETHLINK: 'user_nethlink',
QUEUE_LOG: 'queue_log',
REST_USERS: 'rest_users',
SMS_HISTORY: 'sms_history',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,86 @@ function setUserMobilePhoneNumber(username, pnumber, cb) {
}
}

/**
* Store the NethCTI link timestamp associated with the user.
* @method setUserNethlinkTimestamp
* @param {string} username The username
* @param {string} extension The nethlink extension
* @param {string} actualDate The actual date
* @param {funcion} cb The callback function
*/
function setUserNethlinkTimestamp(username, extension, actualDate, cb) {

try {
let query = `REPLACE INTO user_nethlink (user, extension, timestamp) VALUES (?, ?, ?)`;
let values = [username, extension, actualDate];

compDbconnMain.dbConn['user_nethlink'].query(
query,
values,
(err, results, fields) => {
try {
if (err) {
logger.log.error(IDLOG, 'saving timestamp information: ' + err.toString());
cb(err.toString());
return;
}
logger.log.info(IDLOG, 'user timestamp saved successfully');
cb();
} catch (error) {
logger.log.error(IDLOG, error.stack);
cb(error);
}
}
);
compDbconnMain.incNumExecQueries();

} catch (err) {
logger.log.error(IDLOG, err.stack);
cb(err);
}
}

/**
* Store NethLink timestamp associated with the username.
* @method getUserNethlinkTimestamp
* @param {string} username The nethlink username
*/
function getUserNethlinkTimestamp(username, cb) {

try {
if (typeof cb !== 'function') {
throw new Error('wrong parameters: ' + JSON.stringify(arguments));
}
let query = `SELECT timestamp FROM user_nethlink WHERE user = ?`;
let values = [username];

compDbconnMain.dbConn['user_nethlink'].query(
query,
values,
(err, results, fields) => {

try {
logger.log.info(IDLOG, 'user timestamp saved successfully');
cb(null, results);
} catch (error) {
logger.log.error(IDLOG, 'saving timestamp information: ' + err.toString());
cb(err.toString());
}

}
);
compDbconnMain.incNumExecQueries();

} catch (err) {
logger.log.error(IDLOG, err.stack);
cb(err);
}
}

apiList.setUserMobilePhoneNumber = setUserMobilePhoneNumber;
apiList.setUserNethlinkTimestamp = setUserNethlinkTimestamp;
apiList.getUserNethlinkTimestamp = getUserNethlinkTimestamp;

// public interface
exports.apiList = apiList;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = function(sequelize, DataTypes) {
return sequelize.define('user_nethlink', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
user: DataTypes.STRING,
extension: DataTypes.STRING,
timestamp: DataTypes.DATE,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ module.exports = function(options, imports, register) {
getConfigurations: controllerUser.getConfigurations,
setConfigurations: controllerUser.setConfigurations,
setMobilePhoneNumber: controllerUser.setMobilePhoneNumber,
setNethLinkTimeStamp: controllerUser.setNethLinkTimeStamp,
getAllEndpointsEmail: controllerUser.getAllEndpointsEmail,
getNethLinkTimeStamp: controllerUser.getNethLinkTimeStamp,
hasExtensionEndpoint: controllerUser.hasExtensionEndpoint,
hasCellphoneEndpoint: controllerUser.hasCellphoneEndpoint,
getEndpointVoicemail: controllerUser.getEndpointVoicemail,
Expand Down
58 changes: 58 additions & 0 deletions root/usr/lib/node/nethcti-server/plugins/user/controller_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,62 @@ function setMobilePhoneNumber(username, pnumber, cb) {
}
}

/**
* Associate nethlink timestamp to the user.
*
* @method setNethLinkTimeStamp
* @param {string} username The username of the user to be set
* @param {string} extension The Nethlink extenion of the user to be set
* @param {string} actualDate Actual date
* @param {function} cb The callback function
*/
function setNethLinkTimeStamp(username, extension, actualDate, cb) {
try {
if (typeof username !== 'string' ||
typeof extension !== 'string' ||
typeof cb !== 'function') {

throw new Error('wrong parameters: ' + JSON.stringify(arguments));
}
if (users[username]) {
compDbconn.setUserNethlinkTimestamp(username, extension, actualDate, cb);
} else {
logger.log.warn(IDLOG, 'setting nethlink of not existent user "' + username + '"');
}
} catch (err) {
logger.log.error(IDLOG, err.stack);
return false;
}
}

/**
* Associate nethlink timestamp to the user.
*
* @method getNethLinkTimeStamp
* @param {string} username The Nethlink username of the user
*/
function getNethLinkTimeStamp(username, cb) {
try {
if (
typeof username !== 'string'){
throw new Error('wrong parameters: ' + JSON.stringify(arguments));
}

if (users[username]) {
compDbconn.getUserNethlinkTimestamp(username,
function (err, results) {
cb(err, results); }
);
} else {
logger.log.warn(IDLOG, 'reading nethlink of not existent user "' + username + '"');
}

} catch (err) {
logger.log.error(IDLOG, err.stack);
return false;
}
}

/**
* Set presence status on unavailable of the extensions to ONLINE.
*
Expand Down Expand Up @@ -3390,6 +3446,8 @@ exports.getEndpointVoicemail = getEndpointVoicemail;
exports.hasVoicemailEndpoint = hasVoicemailEndpoint;
exports.getUsernamesWithData = getUsernamesWithData;
exports.setMobilePhoneNumber = setMobilePhoneNumber;
exports.setNethLinkTimeStamp = setNethLinkTimeStamp;
exports.getNethLinkTimeStamp = getNethLinkTimeStamp;
exports.getPresenceListOnBusy = getPresenceListOnBusy;
exports.updateUserMainPresence = updateUserMainPresence;
exports.getPresenceOnUnavailable = getPresenceOnUnavailable;
Expand Down
Loading