|
| 1 | +import type {NextFunction, Request, Response} from '@gravity-ui/expresskit'; |
| 2 | +import {REQUEST_ID_PARAM_NAME} from '@gravity-ui/nodekit'; |
| 3 | + |
| 4 | +import {onFail} from '../callbacks'; |
| 5 | +import {registry} from '../registry'; |
| 6 | +import type {DatalensGatewaySchemas} from '../types/gateway'; |
| 7 | + |
| 8 | +export function getTenantSettingsMiddleware() { |
| 9 | + async function resolveTenantSettings(req: Request, res: Response, next: NextFunction) { |
| 10 | + const {ctx} = req; |
| 11 | + |
| 12 | + const requestId = req.ctx.get(REQUEST_ID_PARAM_NAME); |
| 13 | + const currentTenantId = 'common'; |
| 14 | + |
| 15 | + const {gatewayApi} = registry.getGatewayApi<DatalensGatewaySchemas>(); |
| 16 | + const {getAuthArgsUSPrivate} = registry.common.auth.getAll(); |
| 17 | + const authArgsUSPrivate = getAuthArgsUSPrivate(req, res); |
| 18 | + |
| 19 | + const tenantDetailsResponce = await gatewayApi.usPrivate._getTenantDetails({ |
| 20 | + ctx, |
| 21 | + headers: req.headers, |
| 22 | + requestId: requestId ?? '', |
| 23 | + authArgs: authArgsUSPrivate, |
| 24 | + args: {tenantId: currentTenantId}, |
| 25 | + }); |
| 26 | + const resolvedTenant = tenantDetailsResponce.responseData; |
| 27 | + res.locals.tenantDefaultColorPaletteId = resolvedTenant.settings?.defaultColorPaletteId; |
| 28 | + |
| 29 | + next(); |
| 30 | + } |
| 31 | + |
| 32 | + return function resolveTenantSettingsMiddleware( |
| 33 | + req: Request, |
| 34 | + res: Response, |
| 35 | + next: NextFunction, |
| 36 | + ) { |
| 37 | + resolveTenantSettings(req, res, next) |
| 38 | + .catch((error) => { |
| 39 | + req.ctx.logError('TENANT_RESOLVED_FAILED', error); |
| 40 | + onFail(req, res); |
| 41 | + }) |
| 42 | + .catch((error) => next(error)); |
| 43 | + }; |
| 44 | +} |
0 commit comments