All files / lib/lambda item.ts

86.95% Statements 20/23
84.21% Branches 16/19
100% Functions 1/1
86.95% Lines 20/23

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79              1x 5x 5x 1x           4x 4x   4x 3x 1x           2x 2x         1x           1x 1x       1x   1x                         1x   1x                       1x       1x  
import { handleOpensearchError } from "./utils";
import { APIGatewayEvent } from "aws-lambda";
import { validateEnvVariable } from "shared-utils";
import { getStateFilter } from "../libs/api/auth/user";
import { getAppkChildren, getPackage, getPackageChangelog } from "../libs/api/package";
import { response } from "../libs/handler-lib";
 
export const getItemData = async (event: APIGatewayEvent) => {
  validateEnvVariable("osDomain");
  if (!event.body) {
    return response({
      statusCode: 400,
      body: { message: "Event body required" },
    });
  }
 
  try {
    const body = JSON.parse(event.body);
 
    const packageResult = await getPackage(body.id);
    if (packageResult === undefined || !packageResult.found) {
      return response({
        statusCode: 404,
        body: { message: "No record found for the given id" },
      });
    }
 
    const stateFilter = await getStateFilter(event);
    if (
      stateFilter &&
      (!packageResult?._source.state ||
        !stateFilter.terms.state.includes(packageResult._source.state.toLocaleLowerCase()))
    ) {
      return response({
        statusCode: 401,
        body: { message: "Not authorized to view this resource" },
      });
    }
 
    let appkChildren: any[] = [];
    Iif (packageResult?._source?.appkParent) {
      const children = await getAppkChildren(body.id);
      appkChildren = children.hits.hits;
    }
    const filter = [];
    // This is to handle hard deletes in legacy
    Iif (
      packageResult?._source?.legacySubmissionTimestamp !== null &&
      packageResult?._source?.legacySubmissionTimestamp !== undefined
    ) {
      filter.push({
        range: {
          timestamp: {
            gte: new Date(packageResult._source.legacySubmissionTimestamp).getTime(),
          },
        },
      });
    }
 
    const changelog = await getPackageChangelog(body.id, filter);
 
    return response<unknown>({
      statusCode: 200,
      body: {
        ...packageResult,
        _source: {
          ...packageResult._source,
          ...(!!appkChildren.length && { appkChildren }),
          changelog: changelog.hits.hits,
        },
      },
    });
  } catch (error) {
    return response(handleOpensearchError(error));
  }
};
 
export const handler = getItemData;