Skip to content

Commit

Permalink
fix: limit versions returned from /apps endpoints (#570)
Browse files Browse the repository at this point in the history
* fix: sort api/v1/ app Versions by createdAt

* fix: only return 5 versions in api/v1/apps
  • Loading branch information
Birkbjo authored Oct 13, 2021
1 parent 0913143 commit 74550df
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions server/src/data/getAppById.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const getAppById = async (appId, languageCode, knex) => {
app_id: appId,
language_code: languageCode,
})
.orderBy('version_created_at', 'desc')
} catch (err) {
throw new Error(`Could not get app with id: ${appId}. ${err.message}`)
}
Expand Down
8 changes: 7 additions & 1 deletion server/src/data/getApps.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ const getApps = (
})
}
})
.orderBy('name')
.orderBy([
'name',
{
column: 'version_created_at',
order: 'desc',
},
])
}

module.exports = getApps
1 change: 1 addition & 0 deletions server/src/data/getAppsByIdAndStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const getAppsByIdAndStatus = (id, status, languageCode, knex) => {
status,
language_code: languageCode,
})
.orderBy('version_created_at', 'desc')
}

module.exports = getAppsByIdAndStatus
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ const convertAll = (apps, request) => {
debug(`Using serverUrl: ${serverUrl}`)

const formattedApps = {}

// use api/v2 for paginated response, only return 5 latest versions
const maxVersions = 5
apps.forEach(app => {
let currentApp = formattedApps[app.app_id]

Expand All @@ -103,6 +104,7 @@ const convertAll = (apps, request) => {

//Prevent duplicate versions
if (
currentApp.versions.length < maxVersions &&
!currentApp.versions.find(version => version.id === app.version_id)
) {
currentApp.versions.push(convertAppToV1AppVersion(app, serverUrl))
Expand Down
1 change: 1 addition & 0 deletions server/src/routes/v2/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ module.exports = [
apps,
request.query.dhis_version
)

const pager = request.plugins.pagination
const result = convertAppsToApiV1Format(filteredApps, request)
return h.paginate(pager, {
Expand Down

0 comments on commit 74550df

Please sign in to comment.