Skip to content

Commit

Permalink
411709: Add in progress filters endpoint
Browse files Browse the repository at this point in the history
- Add querry params to in-progress endpoint
  • Loading branch information
feedmypixel committed Jan 9, 2025
1 parent 8444e79 commit af958fc
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 14 deletions.
25 changes: 25 additions & 0 deletions src/api/status/controllers/in-progress-filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Joi from 'joi'

import { statuses } from '~/src/constants/statuses.js'
import { getStatusFilters } from '~/src/api/status/helpers/get-status-filters.js'

const inProgressFiltersController = {
options: {
validate: {
query: Joi.object({
kind: Joi.string().allow('')
})
}
},
handler: async (request, h) => {
const filters = await getStatusFilters(
request.db,
[statuses.inProgress, statuses.failure],
request.query
)

return h.response({ message: 'success', filters })
}
}

export { inProgressFiltersController }
21 changes: 16 additions & 5 deletions src/api/status/controllers/in-progress.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import Joi from 'joi'

import { statuses } from '~/src/constants/statuses.js'
import { getStatus } from '~/src/api/status/helpers/get-status.js'

const inProgressController = {
options: {},
options: {
validate: {
query: Joi.object({
service: Joi.string().allow(''),
teamId: Joi.string().allow(''),
kind: Joi.string().allow('')
})
}
},
handler: async (request, h) => {
const repositoriesStatus = await getStatus(request.db, [
statuses.inProgress,
statuses.failure
])
const repositoriesStatus = await getStatus(
request.db,
[statuses.inProgress, statuses.failure],
request.query
)

return h.response({ message: 'success', inProgress: repositoriesStatus })
}
Expand Down
4 changes: 0 additions & 4 deletions src/api/status/controllers/index.js

This file was deleted.

47 changes: 47 additions & 0 deletions src/api/status/helpers/get-status-filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import isNil from 'lodash/isNil.js'

/**
* @param {import('mongodb').Db} db
* @param {string[]} statuses
* @param {Record<string, string>} [queryParams]
* @returns {Promise<*>}
*/
async function getStatusFilters(db, statuses, queryParams = {}) {
const kind = queryParams.kind

const stages = []

if (!isNil(kind)) {
stages.push({
$match: { kind }
})
}

stages.push(
{
$match: { status: { $in: statuses } }
},
{
$group: {
_id: null,
services: { $addToSet: '$repositoryName' },
teams: { $addToSet: '$team' }
}
},
{
$project: {
_id: 0,
services: 1,
teams: {
teamId: 1,
name: 1
}
}
}
)

const filters = await db.collection('status').aggregate(stages).toArray()
return filters.at(0)
}

export { getStatusFilters }
28 changes: 27 additions & 1 deletion src/api/status/helpers/get-status.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import isNil from 'lodash/isNil.js'

/**
* Get create service status
* @param {import('mongodb').Db} db
* @param {string[]} statuses
* @param {Record<string, string>} [queryParams]
* @returns {Promise<*>}
*/
async function getStatus(db, statuses) {
async function getStatus(db, statuses, queryParams = {}) {
const service = queryParams.service
const teamId = queryParams.teamId
const kind = queryParams.kind
const stages = []

if (statuses?.length) {
Expand All @@ -13,6 +19,26 @@ async function getStatus(db, statuses) {
})
}

if (!isNil(kind)) {
stages.push({
$match: { kind }
})
}

if (!isNil(teamId)) {
stages.push({
$match: { 'team.teamId': teamId }
})
}

if (!isNil(service)) {
stages.push({
$match: {
$or: [{ repositoryName: { $regex: service, $options: 'i' } }]
}
})
}

stages.push({ $project: { _id: 0 } })

return await db.collection('status').aggregate(stages).toArray()
Expand Down
12 changes: 8 additions & 4 deletions src/api/status/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
inProgressController,
repositoryStatusController
} from '~/src/api/status/controllers/index.js'
import { inProgressController } from '~/src/api/status/controllers/in-progress.js'
import { repositoryStatusController } from '~/src/api/status/controllers/repository-status.js'
import { inProgressFiltersController } from '~/src/api/status/controllers/in-progress-filters.js'

const status = {
plugin: {
Expand All @@ -13,6 +12,11 @@ const status = {
path: '/status/in-progress',
...inProgressController
},
{
method: 'GET',
path: '/status/in-progress/filters',
...inProgressFiltersController
},
{
method: 'GET',
path: '/status/{repositoryName}',
Expand Down

0 comments on commit af958fc

Please sign in to comment.