Add aggregate results by entityRef

Signed-off-by: Roohn <tol.ronald@gmail.com>
This commit is contained in:
Roohn
2023-03-13 16:10:52 +01:00
parent a0804a0371
commit 7eba760e6f
9 changed files with 100 additions and 7 deletions
@@ -258,6 +258,22 @@ describe('createRouter', () => {
});
});
describe('GET /ratings/:entityRef/aggregate', () => {
it('should get aggregated ratings for an entity correctly', async () => {
const response = await request(app)
.get('/ratings/component%3Adefault%2Fservice/aggregate')
.send();
expect(mockDbHandler.getRatings).toHaveBeenCalledWith(
'component:default/service',
);
expect(response.status).toEqual(200);
expect(response.body).toEqual({
DISLIKE: 1,
LIKE: 2,
});
});
});
describe('POST /responses/:entityRef', () => {
it('should record a response correctly', async () => {
const body = { response: 'blah', comments: 'feedback', consent: true };
@@ -25,7 +25,10 @@ import {
getBearerTokenFromAuthorizationHeader,
IdentityApi,
} from '@backstage/plugin-auth-node';
import { EntityRatingsData } from '@backstage/plugin-entity-feedback-common';
import {
EntityRatingsData,
Ratings,
} from '@backstage/plugin-entity-feedback-common';
import express from 'express';
import Router from 'express-promise-router';
import { Logger } from 'winston';
@@ -167,6 +170,19 @@ export async function createRouter(
res.json(ratings.filter(r => accessibleEntityRefs.includes(r.userRef)));
});
router.get('/ratings/:entityRef/aggregate', async (req, res) => {
const entityRatings = (
await dbHandler.getRatings(req.params.entityRef)
).reduce((ratings: Ratings, { rating }) => {
ratings[rating] = ratings[rating] ?? 0;
ratings[rating]++;
return ratings;
}, {});
res.json(entityRatings);
});
router.post('/responses/:entityRef', async (req, res) => {
const user = await identity.getIdentity({ request: req });
const { response, comments, consent } = req.body;