From e4039d09c0380d80f03c7a00b51a150f88c02cca Mon Sep 17 00:00:00 2001 From: Danshil Kokil Mungur Date: Tue, 26 Apr 2022 03:06:36 +0400 Subject: [PATCH] feat(api): add issue counts endpoint (#2713) --- overseerr-api.yml | 30 ++++++++++++++++++++ server/routes/issue.ts | 64 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/overseerr-api.yml b/overseerr-api.yml index 77282ea1..be9ee0c0 100644 --- a/overseerr-api.yml +++ b/overseerr-api.yml @@ -5562,6 +5562,36 @@ paths: application/json: schema: $ref: '#/components/schemas/Issue' + + /issue/count: + get: + summary: Gets issue counts + description: | + Returns the number of open and closed issues, as well as the number of issues of each type. + tags: + - issue + responses: + '200': + description: Issue counts returned + content: + application/json: + schema: + type: object + properties: + total: + type: number + video: + type: number + audio: + type: number + subtitles: + type: number + others: + type: number + open: + type: number + closed: + type: number /issue/{issueId}: get: summary: Get issue diff --git a/server/routes/issue.ts b/server/routes/issue.ts index c7db5232..07cf3277 100644 --- a/server/routes/issue.ts +++ b/server/routes/issue.ts @@ -1,6 +1,6 @@ import { Router } from 'express'; import { getRepository } from 'typeorm'; -import { IssueStatus } from '../constants/issue'; +import { IssueStatus, IssueType } from '../constants/issue'; import Issue from '../entity/Issue'; import IssueComment from '../entity/IssueComment'; import Media from '../entity/Media'; @@ -146,6 +146,68 @@ issueRoutes.post< } ); +issueRoutes.get('/count', async (req, res, next) => { + const issueRepository = getRepository(Issue); + + try { + const query = issueRepository.createQueryBuilder('issue'); + + const totalCount = await query.getCount(); + + const videoCount = await query + .where('issue.issueType = :issueType', { + issueType: IssueType.VIDEO, + }) + .getCount(); + + const audioCount = await query + .where('issue.issueType = :issueType', { + issueType: IssueType.AUDIO, + }) + .getCount(); + + const subtitlesCount = await query + .where('issue.issueType = :issueType', { + issueType: IssueType.SUBTITLES, + }) + .getCount(); + + const othersCount = await query + .where('issue.issueType = :issueType', { + issueType: IssueType.OTHER, + }) + .getCount(); + + const openCount = await query + .where('issue.status = :issueStatus', { + issueStatus: IssueStatus.OPEN, + }) + .getCount(); + + const closedCount = await query + .where('issue.status = :issueStatus', { + issueStatus: IssueStatus.RESOLVED, + }) + .getCount(); + + return res.status(200).json({ + total: totalCount, + video: videoCount, + audio: audioCount, + subtitles: subtitlesCount, + others: othersCount, + open: openCount, + closed: closedCount, + }); + } catch (e) { + logger.debug('Something went wrong retrieving issue counts.', { + label: 'API', + errorMessage: e.message, + }); + next({ status: 500, message: 'Unable to retrieve issue counts.' }); + } +}); + issueRoutes.get<{ issueId: string }>( '/:issueId', isAuthenticated(