feat(api): add additional request counts (#2426)

pull/2430/head
TheCatLady 3 years ago committed by GitHub
parent d4438c82e3
commit 2535edcc7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4422,21 +4422,22 @@ paths:
schema: schema:
type: object type: object
properties: properties:
total:
type: number
movie:
type: number
tv:
type: number
pending: pending:
type: number type: number
example: 0
approved: approved:
type: number type: number
example: 10 declined:
type: number
processing: processing:
type: number type: number
example: 4
available: available:
type: number type: number
example: 6
required:
- pending
- approved
/request/{requestId}: /request/{requestId}:
get: get:
summary: Get MediaRequest summary: Get MediaRequest

@ -444,6 +444,20 @@ requestRoutes.get('/count', async (_req, res, next) => {
.createQueryBuilder('request') .createQueryBuilder('request')
.leftJoinAndSelect('request.media', 'media'); .leftJoinAndSelect('request.media', 'media');
const totalCount = await query.getCount();
const movieCount = await query
.where('request.type = :requestType', {
requestType: MediaType.MOVIE,
})
.getCount();
const tvCount = await query
.where('request.type = :requestType', {
requestType: MediaType.TV,
})
.getCount();
const pendingCount = await query const pendingCount = await query
.where('request.status = :requestStatus', { .where('request.status = :requestStatus', {
requestStatus: MediaRequestStatus.PENDING, requestStatus: MediaRequestStatus.PENDING,
@ -456,12 +470,18 @@ requestRoutes.get('/count', async (_req, res, next) => {
}) })
.getCount(); .getCount();
const declinedCount = await query
.where('request.status = :requestStatus', {
requestStatus: MediaRequestStatus.DECLINED,
})
.getCount();
const processingCount = await query const processingCount = await query
.where('request.status = :requestStatus', { .where('request.status = :requestStatus', {
requestStatus: MediaRequestStatus.APPROVED, requestStatus: MediaRequestStatus.APPROVED,
}) })
.andWhere( .andWhere(
'(request.is4k = false AND media.status != :availableStatus) OR (request.is4k = true AND media.status4k != :availableStatus)', '((request.is4k = false AND media.status != :availableStatus) OR (request.is4k = true AND media.status4k != :availableStatus))',
{ {
availableStatus: MediaStatus.AVAILABLE, availableStatus: MediaStatus.AVAILABLE,
} }
@ -473,7 +493,7 @@ requestRoutes.get('/count', async (_req, res, next) => {
requestStatus: MediaRequestStatus.APPROVED, requestStatus: MediaRequestStatus.APPROVED,
}) })
.andWhere( .andWhere(
'(request.is4k = false AND media.status = :availableStatus) OR (request.is4k = true AND media.status4k = :availableStatus)', '((request.is4k = false AND media.status = :availableStatus) OR (request.is4k = true AND media.status4k = :availableStatus))',
{ {
availableStatus: MediaStatus.AVAILABLE, availableStatus: MediaStatus.AVAILABLE,
} }
@ -481,13 +501,21 @@ requestRoutes.get('/count', async (_req, res, next) => {
.getCount(); .getCount();
return res.status(200).json({ return res.status(200).json({
total: totalCount,
movie: movieCount,
tv: tvCount,
pending: pendingCount, pending: pendingCount,
approved: approvedCount, approved: approvedCount,
declined: declinedCount,
processing: processingCount, processing: processingCount,
available: availableCount, available: availableCount,
}); });
} catch (e) { } catch (e) {
next({ status: 500, message: e.message }); logger.error('Something went wrong retrieving request counts', {
label: 'API',
errorMessage: e.message,
});
next({ status: 500, message: 'Unable to retrieve request counts.' });
} }
}); });

Loading…
Cancel
Save