diff --git a/backend/src/v1/middlewares/validations/index.ts b/backend/src/v1/middlewares/validations/index.ts index f61ea188..072e3c1b 100644 --- a/backend/src/v1/middlewares/validations/index.ts +++ b/backend/src/v1/middlewares/validations/index.ts @@ -7,7 +7,7 @@ export type UseValidateOptions = { schema: ZodSchema; }; -export const useValidate = ({ mode, schema }: UseValidateOptions) => { +export const useValidate = ({ mode, schema, }: UseValidateOptions) => { return async (req: Request, res: Response, next: NextFunction) => { try { const data = req[mode]; @@ -15,8 +15,10 @@ export const useValidate = ({ mode, schema }: UseValidateOptions) => { req[mode] = results; next(); } catch (error) { - logger.error(error); - return next(error); + const { path, method } = req; + const errorMessage = `${method} - ${path} - Data validation failed`; + logger.error(errorMessage, error); + return next({ errorMessage, error }); } }; }; diff --git a/backend/src/v1/routes/analytic-routes.spec.ts b/backend/src/v1/routes/analytic-routes.spec.ts index ebfbe5c1..33e79509 100644 --- a/backend/src/v1/routes/analytic-routes.spec.ts +++ b/backend/src/v1/routes/analytic-routes.spec.ts @@ -8,7 +8,10 @@ jest.mock('../services/analytic-service', () => { const actual = jest.requireActual('../services/analytic-service'); return { ...actual, - getEmbedInfo: (...args) => mockGetEmbedInfo(...args), + analyticsService: { + ...actual.analyticsService, + getEmbedInfo: (...args) => mockGetEmbedInfo(...args), + }, }; }); diff --git a/backend/src/v1/routes/analytic-routes.ts b/backend/src/v1/routes/analytic-routes.ts index 65bab988..365edf6d 100644 --- a/backend/src/v1/routes/analytic-routes.ts +++ b/backend/src/v1/routes/analytic-routes.ts @@ -1,6 +1,6 @@ import { Router, Request, Response } from 'express'; import { - getEmbedInfo, + analyticsService, PowerBiResourceName, } from '../services/analytic-service'; import { utils } from '../services/utils-service'; @@ -23,7 +23,7 @@ router.get( req: Request, res: Response, ) => { - const info = await getEmbedInfo(req.query.resources); + const info = await analyticsService.getEmbedInfo(req.query.resources); return res.status(200).json(info); }, diff --git a/backend/src/v1/services/analytic-service.spec.ts b/backend/src/v1/services/analytic-service.spec.ts index 4c24cdf1..0830a9f0 100644 --- a/backend/src/v1/services/analytic-service.spec.ts +++ b/backend/src/v1/services/analytic-service.spec.ts @@ -1,5 +1,5 @@ import { - getEmbedInfo, + analyticsService, PowerBiEmbedInfo, PowerBiResourceName, } from './analytic-service'; @@ -47,7 +47,7 @@ describe('getEmbedInfo', () => { ], embedToken: { token: output.accessToken, expiration: output.expiry }, }); - const json = await getEmbedInfo([ + const json = await analyticsService.getEmbedInfo([ PowerBiResourceName.SubmissionAnalytics, PowerBiResourceName.DataAnalytics, ]); diff --git a/backend/src/v1/services/analytic-service.ts b/backend/src/v1/services/analytic-service.ts index 5bb23efb..8a892ff0 100644 --- a/backend/src/v1/services/analytic-service.ts +++ b/backend/src/v1/services/analytic-service.ts @@ -32,36 +32,38 @@ const resourceIds: Record = { }, }; -/** - * Generate embed token and embed urls for PowerBi resources - * @return Details like Embed URL, Access token and Expiry - */ -export async function getEmbedInfo( - resourceNames: PowerBiResourceName[], -): Promise { - const powerBi = new PowerBiService( - config.get('powerbi:powerBiUrl'), - config.get('entra:clientId'), - config.get('entra:clientSecret'), - config.get('entra:tenantId'), - ); +export const analyticsService = { + /** + * Generate embed token and embed urls for PowerBi resources + * @return Details like Embed URL, Access token and Expiry + */ + async getEmbedInfo( + resourceNames: PowerBiResourceName[], + ): Promise { + const powerBi = new PowerBiService( + config.get('powerbi:powerBiUrl'), + config.get('entra:clientId'), + config.get('entra:clientSecret'), + config.get('entra:tenantId'), + ); - const embedParams = await powerBi.getEmbedParamsForReports( - resourceNames.map((name) => resourceIds[name]), - ); + const embedParams = await powerBi.getEmbedParamsForReports( + resourceNames.map((name) => resourceIds[name]), + ); - const resources = []; - for (let i = 0; i < resourceNames.length; ++i) { - resources.push({ - name: resourceNames[i], - id: embedParams.resources[i].id, - embedUrl: embedParams.resources[i].embedUrl, - }); - } + const resources = []; + for (let i = 0; i < resourceNames.length; ++i) { + resources.push({ + name: resourceNames[i], + id: embedParams.resources[i].id, + embedUrl: embedParams.resources[i].embedUrl, + }); + } - return { - resources: resources, - accessToken: embedParams.embedToken.token, - expiry: embedParams.embedToken.expiration, - }; -} + return { + resources: resources, + accessToken: embedParams.embedToken.token, + expiry: embedParams.embedToken.expiration, + }; + }, +};