Skip to content

Commit

Permalink
feat(apps): availableChannels API (#559)
Browse files Browse the repository at this point in the history
* feat: add available channels api

* test: move tests to apps-test file

* style: remove unused const
  • Loading branch information
Birkbjo authored Oct 1, 2021
1 parent 854f32b commit 6a09f97
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 17 deletions.
26 changes: 23 additions & 3 deletions server/src/routes/v2/apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ module.exports = [

const { payload } = request
const appJsonPayload = JSON.parse(payload.app)
const appJsonValidationResult = CreateAppModel.def.validate(
appJsonPayload
)
const appJsonValidationResult =
CreateAppModel.def.validate(appJsonPayload)

if (appJsonValidationResult.error) {
throw Boom.badRequest(appJsonValidationResult.error)
Expand Down Expand Up @@ -159,4 +158,25 @@ module.exports = [
return h.response(app).created(`/v2/apps/${app.id}`)
},
},
{
method: 'GET',
path: '/v2/apps/{appId}/channels',
config: {
auth: false,
tags: ['api', 'v2'],
validate: {
params: Joi.object({
appId: Joi.string().required(),
}),
},
},
handler: async (request, h) => {
const { db } = h.context
const { appVersionService } = request.services(true)

const { appId } = request.params

return appVersionService.getAvailableChannels(appId, db)
},
},
]
13 changes: 13 additions & 0 deletions server/src/services/appVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ class AppVersionService extends Schmervice.Service {
model: AppVersionModel,
})
}

async getAvailableChannels(appId, knex) {
const query = getAppVersionQuery(knex)
.clear('select')
.clear('order')
.select('channel.name')
.where('app_version.app_id', appId)
.distinct()

const result = await executeQuery(query)

return result.map(c => c.name)
}
}

const createAppVersionService = (server, schmerviceOptions) => {
Expand Down
1 change: 1 addition & 0 deletions server/test/routes/v2/appVersions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { config } = require('../../../src/server/noauth-config')
const Joi = require('../../../src/utils/CustomJoi')

const dbInstance = knex(knexConfig)

describe('v2/appVersions', () => {
let server
let db
Expand Down
57 changes: 43 additions & 14 deletions server/test/routes/v2/apps.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
const fs = require('fs')
const path = require('path')
const { expect } = require('@hapi/code')
const Lab = require('@hapi/lab')
const FormData = require('form-data')
const Knex = require('knex')
const streamToPromise = require('stream-to-promise')

const {
it,
describe,
afterEach,
beforeEach,
before,
} = (exports.lab = Lab.script())

const { expect } = require('@hapi/code')
const { it, describe, afterEach, beforeEach, before } = (exports.lab =
Lab.script())
const knexConfig = require('../../../knexfile')
const dbInstance = require('knex')(knexConfig)
const organisations = require('../../../seeds/mock/organisations')
const appsMocks = require('../../../seeds/mock/apps')
const organisations = require('../../../seeds/mock/organisations')
const users = require('../../../seeds/mock/users')
const { init } = require('../../../src/server/init-server')
const { config } = require('../../../src/server/noauth-config')

const dbInstance = Knex(knexConfig)

describe('v2/apps', () => {
let server
let db
Expand All @@ -47,6 +40,7 @@ describe('v2/apps', () => {
organisationId: organisations[0].id,
},
}
const dhis2App = appsMocks[0]

const createFormForApp = app => {
const form = new FormData()
Expand Down Expand Up @@ -108,4 +102,39 @@ describe('v2/apps', () => {
expect(notCore).to.have.length(0)
})
})

describe('get channels for app', () => {
it('should only return channels that have versions published', async () => {
const canaryOnlyApp = appsMocks[5]
const request = {
method: 'GET',
url: `/api/v2/apps/${canaryOnlyApp.id}/channels`,
}

const res = await server.inject(request)

expect(res.statusCode).to.equal(200)
const result = res.result
expect(result).to.be.an.array().length(1)
expect(result[0]).to.be.equal('canary')
})

it('should return unique channels', async () => {
const request = {
method: 'GET',
url: `/api/v2/apps/${dhis2App.id}/channels`,
}

const res = await server.inject(request)

expect(res.statusCode).to.equal(200)
const result = res.result

expect(result).to.be.an.array().length(3)
expect(result).to.include('stable')

const unique = [...new Set(result)]
expect(unique.length).to.equal(result.length)
})
})
})

0 comments on commit 6a09f97

Please sign in to comment.