var uuid = require('uuid'); // required for generating timebased guids
class organizations {
constructor(logger) {
// always initialize all instance properties
this._logger = logger;
}
/**
* returns rows of all organizations in core database
* @param {*} req
* @param {*} res
* @param {*} callback
*/
getListOfOrganizations(callback) {
let query = "SELECT * FROM `organizations`";
global.coreDb.all(query, [], (err, rows) => {
if (err) {
return callback(null);
} else {
return callback(rows);
}
});
}
/**
* Returns database row for organization with given orgId
* @param {integer} orgId
* @param {rows} callback
*/
getOrganizationWithId(orgId, callback) {
let query = "SELECT * FROM `organizations` WHERE id = ?";
global.coreDb.all(query, [
orgId
], (err, rows) => {
if (err) {
return null;
} else {
return callback(rows);
}
});
}
/**
* Adds a new organization to core database (sqlite)
* @param {object} formBody req.form holding the form fields
* @param {function} callback success, true or false
*/
addOrganization(formBody, callback) {
let name = formBody.name;
let street = formBody.street;
let zip = formBody.zip;
let city = formBody.city;
let country = formBody.country;
let state = formBody.state;
let latitude = formBody.latitude;
let longitude = formBody.longitude;
// calculate uuid for organization
var orgUUID = uuid.v1();
let query = 'INSERT INTO `organizations` (name, street, zip, city, country, state, latitude, longitude, uuid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)';
global.coreDb.run(query,
[
name,
street,
zip,
city,
country,
state,
latitude,
longitude,
orgUUID
],
function (err) {
if (err) {
callback(false);
} else {
callback(true);
}
});
}
/**
* Updates an organizaton object in core database
* @param {object} reqBody form data (usually req.body)
* @param {*} orgId id of organization to update
* @param {*} callback success (true or false)
*/
editOrganization(reqBody, orgId, callback) {
let name = reqBody.name;
let street = reqBody.street;
let zip = reqBody.zip;
let city = reqBody.city;
let state = reqBody.state;
let country = reqBody.country;
let latitude = reqBody.latitude;
let longitude = reqBody.longitude;
let query = "UPDATE `organizations` SET `name` = ?," +
"`street` = ?," +
"`zip` = ?," +
"`city` = ?," +
"`state` = ?," +
"`country` = ?," +
"`latitude` = ?," +
"`longitude` = ? " +
" WHERE `organizations`.`id` = ?";
global.coreDb.run(query,
[
name,
street,
zip,
city,
state,
country,
latitude,
longitude,
orgId
],
function (err) {
if (err) {
callback(false);
} else {
callback(true);
}
});
}
/**
* Removes an organization from database
* @param {integer} orgId
* @param {function} callback
*
* TBD: check if there are other entities referenzing to this organization prio removing
*/
removeOrganizationWithId(orgId, callback) {
let query = 'DELETE FROM `organizations` WHERE id = ?';
console.log(query + ' | orgId => ' + orgId);
global.coreDb.run(query,
[
orgId
],
function (err) {
if (err) {
callback(false);
} else {
callback(true);
}
});
}
/**
* Updates the name of the avatar image for an organization in core database
* @param {string} image
* @param {integer} orgId
* @param {function} callback
*/
setOrganizationAvatar(image, orgId, callback) {
// send the user's details to the database
let query = 'UPDATE `organizations` SET image = ? WHERE id = ?';
global.coreDb.run(query, [
image,
orgId
], (err) => {
if (err) {
req.app.logger.error('ROUTE-ORGANIZATIONS | error updating avatar-path in database for organization ' + orgId);
return callback(false);
}
return callback(true);
});
}
}
// export the class
module.exports = organizations;