All files / api/routes baseStatusHandler.ts

100% Statements 42/42
83.33% Branches 5/6
100% Functions 1/1
100% Lines 42/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 431x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x  
import Koa from 'koa';
 
import { IApiModule } from '@api/types/baseModule';
import { ApiStatusErrorCode, apiParams } from '@constants/api';
import { getLog } from '@services/internal/logService';
import { IFormWithApprovalResponse } from '@contracts/response/formResponse';
import { errorResponse } from '@api/misc/httpResponse/errorResponse';
 
export const baseStatusHandler =
  <TD, TF, TP>(module: IApiModule<TD, TF, TP>) =>
  async (ctx: Koa.DefaultContext, next: () => Promise<Koa.BaseResponse>) => {
    const id = ctx.params[apiParams.status.id];
    const handlerName = `statusHandler-${module.segment}-${id}`;
    getLog().i(handlerName);
 
    const recordResult = await module.readRecord(id);
    if (recordResult.isSuccess == false) {
      const errMsg = `${handlerName}: ${recordResult.errorMessage}`;
      getLog().e(errMsg);
      await errorResponse({
        ctx,
        next,
        statusCode: ApiStatusErrorCode.recordNotFound.code,
        message: errMsg,
      });
      return;
    }
 
    // const dto = module.mapPersistenceToDto(recordResult.value);
    const responseObj: IFormWithApprovalResponse = {
      id: recordResult.value.id,
      name: module.getName(recordResult.value),
      iconUrl: module.getIcon?.(recordResult.value) ?? undefined,
      approvalStatus: recordResult.value.approvalStatus,
    };
 
    ctx.response.status = 200;
    ctx.set('Content-Type', 'application/json');
    ctx.body = JSON.stringify(responseObj);
 
    await next();
  };