diff --git a/server/src/routes/v2/apps.js b/server/src/routes/v2/apps.js index 7128e48a6..57ed66f4f 100644 --- a/server/src/routes/v2/apps.js +++ b/server/src/routes/v2/apps.js @@ -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) @@ -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) + }, + }, ] diff --git a/server/src/services/appVersion.js b/server/src/services/appVersion.js index 094a97eee..8976705eb 100644 --- a/server/src/services/appVersion.js +++ b/server/src/services/appVersion.js @@ -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) => { diff --git a/server/test/routes/v2/appVersions.js b/server/test/routes/v2/appVersions.js index c50057c76..abd074bd0 100644 --- a/server/test/routes/v2/appVersions.js +++ b/server/test/routes/v2/appVersions.js @@ -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 diff --git a/server/test/routes/v2/apps.js b/server/test/routes/v2/apps.js index 11075d8cd..210fcc622 100644 --- a/server/test/routes/v2/apps.js +++ b/server/test/routes/v2/apps.js @@ -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 @@ -47,6 +40,7 @@ describe('v2/apps', () => { organisationId: organisations[0].id, }, } + const dhis2App = appsMocks[0] const createFormForApp = app => { const form = new FormData() @@ -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) + }) + }) })