diff --git a/lib/models/system-information.js b/lib/models/system-information.js new file mode 100644 index 0000000..e76db04 --- /dev/null +++ b/lib/models/system-information.js @@ -0,0 +1,38 @@ +const Model = require('./model'); +const { promisePool } = require('../db'); +class SystemInformation extends Model { + constructor() { + super(); + } + + static async add(body) { + let [sysInfo] = await this.retrieveAll(); + if (sysInfo) { + const sql = `UPDATE ${this.table} SET ?`; + await promisePool.query(sql, body); + } else { + const sql = `INSERT INTO ${this.table} SET ?`; + await promisePool.query(sql, body); + } + [sysInfo] = await this.retrieveAll(); + return sysInfo; + } +} + +SystemInformation.table = 'system_information'; +SystemInformation.fields = [ + { + name: 'domain_name', + type: 'string', + }, + { + name: 'sip_domain_name', + type: 'string', + }, + { + name: 'monitoring_domain_name', + type: 'string', + }, +]; + +module.exports = SystemInformation; diff --git a/lib/routes/api/index.js b/lib/routes/api/index.js index 8f95bd6..e182d2e 100644 --- a/lib/routes/api/index.js +++ b/lib/routes/api/index.js @@ -16,6 +16,7 @@ const isAdminScope = (req, res, next) => { // }; api.use('/BetaInviteCodes', isAdminScope, require('./beta-invite-codes')); +api.use('/SystemInformation', isAdminScope, require('./system-information')); api.use('/ServiceProviders', require('./service-providers')); api.use('/VoipCarriers', require('./voip-carriers')); api.use('/Webhooks', require('./webhooks')); diff --git a/lib/routes/api/system-information.js b/lib/routes/api/system-information.js new file mode 100644 index 0000000..f5909ff --- /dev/null +++ b/lib/routes/api/system-information.js @@ -0,0 +1,14 @@ +const router = require('express').Router(); +const SystemInformation = require('../../models/system-information'); + +router.post('/', async(req, res) => { + const sysInfo = await SystemInformation.add(req.body); + res.status(201).json(sysInfo); +}); + +router.get('/', async(req, res) => { + const [sysInfo] = await SystemInformation.retrieveAll(); + res.status(200).json(sysInfo); +}); + +module.exports = router; diff --git a/test/index.js b/test/index.js index e4b0f4c..f6fbe19 100644 --- a/test/index.js +++ b/test/index.js @@ -19,4 +19,5 @@ require('./webapp_tests'); require('./call-test'); require('./password-settings'); require('./email_utils'); +require('./system-information'); require('./docker_stop'); diff --git a/test/system-information.js b/test/system-information.js new file mode 100644 index 0000000..852c14a --- /dev/null +++ b/test/system-information.js @@ -0,0 +1,64 @@ +const test = require('tape') ; +const jwt = require('jsonwebtoken'); +const ADMIN_TOKEN = '38700987-c7a4-4685-a5bb-af378f9734de'; +const authAdmin = {bearer: ADMIN_TOKEN}; +const request = require('request-promise-native').defaults({ + baseUrl: 'http://127.0.0.1:3000/v1' +}); + +test('system information test', async(t) => { + const app = require('../app'); + try { + let result = await request.post('/SystemInformation', { + resolveWithFullResponse: true, + auth: authAdmin, + json: true, + body: { + domain_name: 'test.com', + sip_domain_name: 'sip.test.com', + monitoring_domain_name: 'monitor.test.com' + } + }); + t.ok(result.statusCode === 201, 'successfully created system information '); + let body = result.body; + t.ok(body.domain_name === 'test.com', 'added domain_name ok'); + t.ok(body.sip_domain_name === 'sip.test.com', 'added sip_domain_name ok'); + t.ok(body.monitoring_domain_name === 'monitor.test.com', 'added monitoring_domain_name ok'); + + result = await request.get('/SystemInformation', { + auth: authAdmin, + json: true, + }); + t.ok(result.domain_name === 'test.com', 'get domain_name ok'); + t.ok(result.sip_domain_name === 'sip.test.com', 'get sip_domain_name ok'); + t.ok(result.monitoring_domain_name === 'monitor.test.com', 'get monitoring_domain_name ok'); + + result = await request.post('/SystemInformation', { + resolveWithFullResponse: true, + auth: authAdmin, + json: true, + body: { + domain_name: 'test1.com', + sip_domain_name: 'sip1.test.com', + monitoring_domain_name: 'monitor1.test.com' + } + }); + t.ok(result.statusCode === 201, 'successfully updated system information '); + body = result.body; + t.ok(body.domain_name === 'test1.com', 'updated domain_name ok'); + t.ok(body.sip_domain_name === 'sip1.test.com', 'updated sip_domain_name ok'); + t.ok(body.monitoring_domain_name === 'monitor1.test.com', 'updated monitoring_domain_name ok'); + + result = await request.get('/SystemInformation', { + auth: authAdmin, + json: true, + }); + t.ok(result.domain_name === 'test1.com', 'get domain_name ok'); + t.ok(result.sip_domain_name === 'sip1.test.com', 'get sip_domain_name ok'); + t.ok(result.monitoring_domain_name === 'monitor1.test.com', 'get monitoring_domain_name ok'); + + } catch(err) { + console.error(err); + t.end(err); + } +}); \ No newline at end of file