feat/367 added support for name query parameter for retrieving application (#368)

* feat/367 added support for name query parameter for getting application by name

* feat/367 - Updated test case and modified retrieveAll method to make name independent of service_provider_id and account_sid

* updated query to use WHERE 1=1 to avoid whereFlag variable

* feat/367: removed empty line added accidently
This commit is contained in:
rammohan-y
2024-12-18 18:48:48 +05:30
committed by GitHub
parent e080118b6a
commit 8851b3fac0
3 changed files with 31 additions and 6 deletions
+10 -5
View File
@@ -37,19 +37,24 @@ class Application extends Model {
}
/**
* list all applications - for all service providers, for one service provider, or for one account
* list all applications - for all service providers, for one service provider, or for one account,
* or by an optional name
*/
static retrieveAll(service_provider_sid, account_sid) {
let sql = retrieveSql;
static retrieveAll(service_provider_sid, account_sid, name) {
let sql = retrieveSql + ' WHERE 1 = 1';
const args = [];
if (account_sid) {
sql = `${sql} WHERE app.account_sid = ?`;
sql = `${sql} AND app.account_sid = ?`;
args.push(account_sid);
}
else if (service_provider_sid) {
sql = `${sql} WHERE account_sid in (SELECT account_sid from accounts WHERE service_provider_sid = ?)`;
sql = `${sql} AND account_sid in (SELECT account_sid from accounts WHERE service_provider_sid = ?)`;
args.push(service_provider_sid);
}
if (name) {
sql = `${sql} AND app.name = ?`;
args.push(name);
}
return new Promise((resolve, reject) => {
getMysqlConnection((err, conn) => {
if (err) return reject(err);
+2 -1
View File
@@ -156,7 +156,8 @@ router.get('/', async(req, res) => {
try {
const service_provider_sid = req.user.hasServiceProviderAuth ? req.user.service_provider_sid : null;
const account_sid = req.user.hasAccountAuth ? req.user.account_sid : null;
const results = await Application.retrieveAll(service_provider_sid, account_sid);
const name = req.query.name;
const results = await Application.retrieveAll(service_provider_sid, account_sid, name);
res.status(200).json(results);
} catch (err) {
sysError(logger, res, err);
+19
View File
@@ -121,6 +121,25 @@ test('application tests', async(t) => {
let app_json = JSON.parse(result.app_json);
t.ok(app_json[0].verb === 'play', 'successfully retrieved app_json from application')
/* query one application by name*/
result = await request.get(`/Applications`, {
qs : {
name: 'daveh'
},
auth: authAdmin,
json: true,
});
t.ok(result.length === 1 && result[0].name === 'daveh', 'successfully queried application by name');
/* query application with invalid name*/
result = await request.get(`/Applications`, {
qs : {
name: 'daveh-invalid'
},
auth: authAdmin,
json: true,
});
t.ok(result.length === 0, 'successfully queried application by invalid name, no results found');
/* update applications */
result = await request.put(`/Applications/${sid}`, {