All files / lib/lambda search.ts

97.72% Statements 43/44
86.84% Branches 33/38
100% Functions 1/1
97.67% Lines 42/43

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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                            1x 5x 5x 1x       1x         4x 4x   4x 4x 4x 4x   4x 4x 4x 4x 4x 4x 4x   4x   4x 4x 2x 2x 2x 2x                                         2x     2x 1x 1x                                           4x                               4x 4x 4x     4x 3x 108x 3x 3x 3x         3x         1x       1x  
import { APIGatewayEvent } from "aws-lambda";
import { response } from "libs/handler-lib";
import { getDomainAndNamespace, getOsNamespace } from "libs/utils";
import { SEATOOL_STATUS } from "shared-types";
import { BaseIndex } from "shared-types/opensearch";
import { ONEMAC_LEGACY_ORIGIN } from "shared-types/opensearch/main/transforms/legacy-transforms";
import { validateEnvVariable } from "shared-utils";
 
import { getSearchUserScope } from "../libs/api/auth/user";
import { getAppkChildren } from "../libs/api/package";
import { search } from "../libs/opensearch-lib";
import { handleOpensearchError } from "./utils";
 
// Handler function to search index
export const getSearchData = async (event: APIGatewayEvent) => {
  validateEnvVariable("osDomain");
  if (!event.pathParameters || !event.pathParameters.index) {
    console.error(
      "event.pathParameters.index path parameter required, Event: ",
      JSON.stringify(event, null, 2),
    );
    return response({
      statusCode: 400,
      body: { message: "Index path parameter required" },
    });
  }
  const requestedIndex = event.pathParameters.index as BaseIndex;
  const { domain, index } = getDomainAndNamespace(requestedIndex);
 
  try {
    let query: any = {};
    Eif (event.body) {
      query = JSON.parse(event.body);
    }
    query.query = query?.query || {};
    query.query.bool = query.query?.bool || {};
    query.query.bool.must = query.query.bool?.must || [];
    query.query.bool.must_not = query.query.bool?.must_not || [];
    query.query.bool.must_not.push({ term: { deleted: true } });
    const includeDrafts = query.includeDrafts !== false;
    delete query.includeDrafts;
 
    const { stateFilter, canViewDrafts } = await getSearchUserScope(event);
    const shouldIncludeDrafts =
      includeDrafts && requestedIndex === "main" && (Boolean(stateFilter) || canViewDrafts);
    if (stateFilter) {
      query.query.bool.must.push(stateFilter);
      Eif (shouldIncludeDrafts) {
        const draftIndex = getOsNamespace("draftmain");
        query.query.bool.must.push({
          bool: {
            should: [
              {
                bool: {
                  must_not: [{ term: { "seatoolStatus.keyword": SEATOOL_STATUS.DRAFT } }],
                },
              },
              {
                bool: {
                  must: [
                    { term: { "seatoolStatus.keyword": SEATOOL_STATUS.DRAFT } },
                    { term: { _index: draftIndex } },
                  ],
                },
              },
            ],
            minimum_should_match: 1,
          },
        });
      }
    I} else if (stateFilter === null && !canViewDrafts) {
      // Drafts are state-only and should not appear in CMS search results.
      query.query.bool.must_not.push({ term: { "seatoolStatus.keyword": SEATOOL_STATUS.DRAFT } });
    } else if (shouldIncludeDrafts) {
      const draftIndex = getOsNamespace("draftmain");
      query.query.bool.must.push({
        bool: {
          should: [
            {
              bool: {
                must_not: [{ term: { "seatoolStatus.keyword": SEATOOL_STATUS.DRAFT } }],
              },
            },
            {
              bool: {
                must: [
                  { term: { "seatoolStatus.keyword": SEATOOL_STATUS.DRAFT } },
                  { term: { _index: draftIndex } },
                ],
              },
            },
          ],
          minimum_should_match: 1,
        },
      });
    }
    // Return OneMAC records and NOSOs (denoted with SEATool origin and only NOSO)
    query.query.bool.must.push({
      bool: {
        should: [
          { terms: { "origin.keyword": ["OneMAC", ONEMAC_LEGACY_ORIGIN] } },
          {
            bool: {
              must: [
                { term: { "origin.keyword": "SEATool" } },
                { term: { "event.keyword": "NOSO" } },
              ],
            },
          },
        ],
      },
    });
 
    query.from = query.from || 0;
    query.size = query.size || 100;
    const searchIndex: Parameters<typeof search>[1] = shouldIncludeDrafts
      ? (`${index},${getOsNamespace("draftmain")}` as Parameters<typeof search>[1])
      : index;
    const results = await search(domain, searchIndex, query);
    for (let i = 0; i < results?.hits?.hits?.length; i++) {
      if (results.hits.hits[i]._source?.appkParent) {
        const children = await getAppkChildren(results.hits.hits[i]._id);
        Eif (children?.hits?.hits?.length > 0) {
          results.hits.hits[i]._source.appkChildren = children.hits.hits;
        }
      }
    }
 
    return response<unknown>({
      statusCode: 200,
      body: results,
    });
  } catch (error) {
    return response(handleOpensearchError(error));
  }
};
 
export const handler = getSearchData;