resource-watch/adapter-gfw

View on GitHub
src/middleware/dataset.middleware.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { Context, Next } from 'koa';

import logger from 'logger';
import DatasetService from 'services/dataset.service';

class DatasetMiddleware {
    static async getDatasetById(ctx: Context, next: Next): Promise<void> {
        const datasetId: string = ctx.params.dataset;
        logger.debug('[DatasetRouter - getDatasetById] - Dataset id', datasetId);

        if (!datasetId) {
            ctx.throw(400, 'Invalid request');
        }

        const dataset: Record<string, any> = await DatasetService.getDatasetById(datasetId, ctx.request.headers['x-api-key'] as string);

        if (!dataset) {
            ctx.throw(404, 'Dataset not found');
        }

        if (dataset.attributes.connectorType !== 'rest') {
            ctx.throw(422, 'This operation is only supported for datasets with connectorType \'rest\'');
        }

        if (dataset.attributes.provider !== 'gfw') {
            ctx.throw(422, 'This operation is only supported for datasets with provider \'gfw\'');
        }

        if (!ctx.request.hasOwnProperty('body')) {
            ctx.request.body = {};
        }
        ctx.request.body.dataset = {
            id: dataset.id,
            ...dataset.attributes,
        };

        await next();
    }
}

export default DatasetMiddleware;