Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Badge GET api #163

Merged
merged 8 commits into from Mar 6, 2021
25 changes: 25 additions & 0 deletions controllers/badgeController.js
@@ -0,0 +1,25 @@
const badgeQuery = require('../models/badges')

/**
* Get badges data
*
* @param req {Object} - Express request object
* @param res {Object} - Express response object
*/

const getBadges = async (req, res) => {
try {
const allBadges = await badgeQuery.fetchBadges(req.query)
return res.json({
message: 'Badges returned successfully!',
badges: allBadges
})
} catch (error) {
logger.error(`Error while fetching all badges: ${error}`)
return res.boom.serverUnavailable('Something went wrong please contact admin')
}
}

module.exports = {
getBadges
}
20 changes: 20 additions & 0 deletions docs/swaggerDefinition.js
Expand Up @@ -425,6 +425,26 @@ const swaggerOptions = {
}
}
},
badges: {
type: 'object',
properties: {
title: {
type: 'string'
},
description: {
type: 'string'
},
imgUrl: {
type: 'string'
},
users: {
type: 'array',
items: {
type: 'string'
}
}
}
},
errors: {
unAuthorized: {
type: 'object',
Expand Down
32 changes: 32 additions & 0 deletions models/badges.js
@@ -0,0 +1,32 @@
const firestore = require('../utils/firestore')
const badgeModel = firestore.collection('badges')

/**
* Fetches the data about our badges
* @param query { Object }: Filter for badges data
* @return {Promise<badgeModel|Array>}
*/
const fetchBadges = async ({
size = 100,
page = 0
}) => {
try {
const snapshot = await badgeModel
.limit(parseInt(size))
.offset((parseInt(size)) * (parseInt(page)))
.get()

const allBadges = []
snapshot.forEach((doc) => {
allBadges.push(doc.data())
})
return allBadges
} catch (err) {
logger.error('Error retrieving badges', err)
return err
}
}

module.exports = {
fetchBadges
}
2 changes: 1 addition & 1 deletion public/apiSchema.json

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions routes/badges.js
@@ -0,0 +1,37 @@
const express = require('express')
const router = express.Router()
const badgeController = require('../controllers/badgeController.js')

/**
* @swagger
* /badges:
* get:
* summary: Get all the badges in system.
*
* tags:
* - Badges
* responses:
* 200:
* description: Badge details
* content:
* application/json:
* schema:
* type: object
* properties:
* message:
* type: string
* example: Badges returned successfully!
* badges:
* type: array
* items:
* $ref: '#/components/schemas/badges'
* 503:
* description: serverUnavailable
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/errors/serverUnavailable'
*/
router.get('/', badgeController.getBadges)

module.exports = router
1 change: 1 addition & 0 deletions routes/index.js
Expand Up @@ -9,5 +9,6 @@ app.use('/tasks', require('./tasks'))
app.use('/challenges', require('./challenges.js'))
app.use('/pullrequests', require('./pullrequests.js'))
app.use('/contributions', require('./contributions'))
app.use('/badges', require('./badges.js'))

module.exports = app
26 changes: 26 additions & 0 deletions test/integration/badgeController.test.js
@@ -0,0 +1,26 @@
const chai = require('chai')
const { expect } = chai
const chaiHttp = require('chai-http')

const app = require('../../server')

chai.use(chaiHttp)

describe('Badges', function () {
describe('GET /badges', function () {
it('Should get all the list of badges', function (done) {
chai
.request(app)
.get('/badges')
.end((err, res) => {
if (err) { return done() }
expect(res).to.have.status(200)
expect(res.body).to.be.a('object')
expect(res.body.message).to.equal('Badges returned successfully!')
expect(res.body.badges).to.be.a('array')

return done()
})
})
})
})
Comment on lines +9 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are we adding the badges in the database to GET them? 🤔
How will this test case pass on CI? 🤔

Copy link
Contributor Author

@whyDontI whyDontI Mar 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ankush is going to add badges manually in database until we get the authorization in place.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ankush is going to add badges manually in database until we get the authorization in place.

I guess the CI also uses firebase emulators that's why we install it on every commit.
I meant the CI doesn't use the production db, right?
Feel free to correct me 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems like CI uses emulator, check test.yml file in .git/workflow
CI fetching and adding data in prod DB won't be a good idea

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I'm saying 😅
The prod DB will have the badges details
but the firebase emulator wont and hence the test wouldn't pass 😅

Let me know if I'm wrong 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's the convention, even if the GET Endpoint returns an empty array, the response code will still be 200, and the request is considered fulfilled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool