-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Added option to show user's data usage on status page #735
When subscriptions module is used, show user's current plan. If user's current plan is free, then show option to upgrade the plan. Closes #735
- Loading branch information
Showing
8 changed files
with
244 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import axios from "axios"; | ||
import merge from "deepmerge"; | ||
import config from "../config.json"; | ||
import defaultConfig from "../utils/default-config"; | ||
import {logResponseError} from "../utils/logger"; | ||
import reverse from "../utils/openwisp-urls"; | ||
import getSlug from "../utils/get-slug"; | ||
|
||
const getUserRadiusUsage = (req, res) => { | ||
const reqOrg = req.params.organization; | ||
const validSlug = config.some((org) => { | ||
if (org.slug === reqOrg) { | ||
// merge default config and custom config | ||
const conf = merge(defaultConfig, org); | ||
const {host} = conf; | ||
let radiusUsagePathName; | ||
if (conf.settings.subscriptions) { | ||
radiusUsagePathName = "user_plan_radius_usage"; | ||
} else { | ||
radiusUsagePathName = "user_radius_usage"; | ||
} | ||
const userRadiusUsageUrl = reverse(radiusUsagePathName, getSlug(conf)); | ||
const timeout = conf.timeout * 1000; | ||
// make AJAX request | ||
axios({ | ||
method: "get", | ||
headers: { | ||
"content-type": "application/x-www-form-urlencoded", | ||
Authorization: req.headers.authorization, | ||
"accept-language": req.headers["accept-language"], | ||
}, | ||
url: `${host}${userRadiusUsageUrl}/`, | ||
timeout, | ||
params: req.query, | ||
}) | ||
.then((response) => { | ||
if ("link" in response.headers) { | ||
res.setHeader("link", response.headers.link); | ||
} | ||
res | ||
.status(response.status) | ||
.type("application/json") | ||
.send(response.data); | ||
}) | ||
.catch((error) => { | ||
logResponseError(error); | ||
// forward error | ||
try { | ||
res | ||
.status(error.response.status) | ||
.type("application/json") | ||
.send(error.response.data); | ||
} catch (err) { | ||
res.status(500).type("application/json").send({ | ||
response_code: "INTERNAL_SERVER_ERROR", | ||
}); | ||
} | ||
}); | ||
} | ||
return org.slug === reqOrg; | ||
}); | ||
// return 404 for invalid organization slug or org not listed in config | ||
if (!validSlug) { | ||
res.status(404).type("application/json").send({ | ||
response_code: "INTERNAL_SERVER_ERROR", | ||
}); | ||
} | ||
}; | ||
|
||
export default getUserRadiusUsage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters