All files / lib/libs/api/package itemExists.ts

86.66% Statements 13/15
100% Branches 5/5
100% Functions 1/1
84.61% Lines 11/13

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                          40x 40x 40x 40x   40x 40x   40x 29x   4x 4x   4x            
import * as os from "libs/opensearch-lib";
import { getDomain, getOsNamespace } from "libs/utils";
import { BaseIndex } from "shared-types/opensearch";
 
import { isActiveDraftPackage, isActiveMainNonDraftPackage } from "./packageStatus";
 
export async function itemExists({
  id,
  includeDrafts = false,
}: {
  id: string;
  includeDrafts?: boolean;
}): Promise<boolean> {
  try {
    const domain = getDomain();
    const mainIndex: `${string}${BaseIndex}` = getOsNamespace("main");
    const draftIndex: `${string}${BaseIndex}` = getOsNamespace("draftmain");
 
    const mainPackageResult = await os.getItem(domain, mainIndex, id);
    const hasMainNonDraftPackage = isActiveMainNonDraftPackage(mainPackageResult);
 
    if (hasMainNonDraftPackage) return true;
    if (!includeDrafts) return false;
 
    const draftPackageResult = await os.getItem(domain, draftIndex, id);
    const hasDraftPackage = isActiveDraftPackage(draftPackageResult);
 
    return hasDraftPackage;
  } catch (error) {
    console.error(error);
    return false;
  }
}