mirror of
https://github.com/glenndehaan/unifi-voucher-site.git
synced 2026-04-05 08:53:53 -04:00
Refactored print.js, bulk-print.ejs, details.ejs, email.ejs, print.ejs, voucher.ejs and size.js for object compatibility with the UniFi Integration API. Updated the unifi.js module to implement the UniFi Integration API. Added the UNIFI_TOKEN to variables.js. Added fetch.js util. Check for undefined state in notes.js. Added undici to the dependencies. server.js refactored for compatibility with the UniFi integration API. Fixed incorrect quote filter within server.js. Temporary fixed guest mapping to voucher_code instead of ids
This commit is contained in:
158
utils/fetch.js
Normal file
158
utils/fetch.js
Normal file
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* Import vendor modules
|
||||
*/
|
||||
const querystring = require('node:querystring');
|
||||
const {Agent} = require('undici');
|
||||
|
||||
/**
|
||||
* Import own modules
|
||||
*/
|
||||
const variables = require('../modules/variables');
|
||||
const log = require('../modules/log');
|
||||
|
||||
/**
|
||||
* UniFi Settings
|
||||
*/
|
||||
const controller = {
|
||||
ip: variables.unifiIp,
|
||||
port: variables.unifiPort,
|
||||
username: variables.unifiUsername,
|
||||
password: variables.unifiPassword,
|
||||
token: variables.unifiToken,
|
||||
siteID: variables.unifiSiteId,
|
||||
siteUUID: null
|
||||
};
|
||||
|
||||
/**
|
||||
* Request a Controller Site UUID
|
||||
*
|
||||
* @returns {Promise<unknown>}
|
||||
*/
|
||||
const getSiteUUID = () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch(`https://${controller.ip}:${controller.port}/proxy/network/integration/v1/sites?filter=internalReference.eq('${controller.siteID}')`, {
|
||||
headers: {
|
||||
'User-Agent': 'unifi-voucher-site',
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-KEY': controller.token
|
||||
},
|
||||
dispatcher: new Agent({
|
||||
connect: {
|
||||
rejectUnauthorized: false
|
||||
}
|
||||
})
|
||||
})
|
||||
.then((response) => {
|
||||
return response.json();
|
||||
})
|
||||
.then((response) => {
|
||||
if(response.error) {
|
||||
log.error(`[UniFi] Error while requesting site uuid. Error: ${response.error.message}`);
|
||||
log.debug(response.error);
|
||||
reject(response.error.message);
|
||||
return;
|
||||
}
|
||||
|
||||
if(response.statusCode) {
|
||||
log.error(`[UniFi] Error while requesting site uuid. Error: ${response.message}`);
|
||||
log.debug(response);
|
||||
reject(response.message);
|
||||
return;
|
||||
}
|
||||
|
||||
if(response.data.length < 1) {
|
||||
log.error(`[UniFi] Unknown site id: ${controller.siteID}.`);
|
||||
log.debug(response);
|
||||
reject(`Unknown site id: ${controller.siteID}`);
|
||||
return;
|
||||
}
|
||||
|
||||
log.debug(`[UniFi] Site UUID: ${response.data[0].id}`);
|
||||
resolve(response.data[0].id);
|
||||
})
|
||||
.catch((err) => {
|
||||
log.error('[UniFi] Error while processing request.');
|
||||
log.debug(err);
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch util to get data from a UniFi Controller
|
||||
*
|
||||
* @param endpoint
|
||||
* @param method
|
||||
* @param params
|
||||
* @param data
|
||||
* @returns {Promise<unknown>}
|
||||
*/
|
||||
module.exports = (endpoint, method = 'GET', params = {}, data = null) => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// Auto-resolve siteUUID if not set
|
||||
if(controller.siteUUID === null) {
|
||||
log.debug('[UniFi] Requesting Site UUID...');
|
||||
|
||||
const siteUUID = await getSiteUUID().catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
|
||||
if(siteUUID) {
|
||||
controller.siteUUID = siteUUID;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Define base request
|
||||
const request = {
|
||||
method,
|
||||
headers: {
|
||||
'User-Agent': 'unifi-voucher-site',
|
||||
'Content-Type': 'application/json',
|
||||
'X-API-KEY': controller.token
|
||||
},
|
||||
dispatcher: new Agent({
|
||||
connect: {
|
||||
rejectUnauthorized: false
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
// Add data to body when object is given
|
||||
if(data !== null) {
|
||||
request.body = JSON.stringify(data);
|
||||
}
|
||||
|
||||
fetch(`https://${controller.ip}:${controller.port}/proxy/network/integration/v1/sites/${controller.siteUUID}${endpoint}?${querystring.stringify(params)}`, request)
|
||||
.then((response) => {
|
||||
return response.json();
|
||||
})
|
||||
.then((response) => {
|
||||
if(response.error) {
|
||||
log.error(`[UniFi] Error while processing request. Error: ${response.error.message}`);
|
||||
log.debug(response.error);
|
||||
reject(response.error.message);
|
||||
return;
|
||||
}
|
||||
|
||||
if(response.statusCode) {
|
||||
log.error(`[UniFi] Error while processing request. Error: ${response.message}`);
|
||||
log.debug(response);
|
||||
reject(response.message);
|
||||
return;
|
||||
}
|
||||
|
||||
if(response.data) {
|
||||
resolve(response.data);
|
||||
} else {
|
||||
resolve(response);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
log.error(`[UniFi] Error while processing request. Error: ${err}`);
|
||||
log.debug(err);
|
||||
reject(err.message);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
* @returns {*}
|
||||
*/
|
||||
module.exports = (string) => {
|
||||
if(string === null) {
|
||||
if(string === null || typeof string === 'undefined') {
|
||||
return {
|
||||
note: null,
|
||||
source: null,
|
||||
|
||||
@@ -9,15 +9,15 @@ const variables = require('../modules/variables');
|
||||
module.exports = (voucher) => {
|
||||
let base = variables.unifiSsid !== '' ? variables.unifiSsidPassword !== '' ? 415 : 375 : 260;
|
||||
|
||||
if(voucher.qos_usage_quota) {
|
||||
if(voucher.dataUsageLimitMBytes) {
|
||||
base += 10;
|
||||
}
|
||||
|
||||
if(voucher.qos_rate_max_down) {
|
||||
if(voucher.rxRateLimitKbps) {
|
||||
base += 10;
|
||||
}
|
||||
|
||||
if(voucher.qos_rate_max_up) {
|
||||
if(voucher.txRateLimitKbps) {
|
||||
base += 10;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user