All files / react-app/src/api itemExists.ts

93.33% Statements 14/15
76.47% Branches 13/17
100% Functions 1/1
93.33% Lines 14/15

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                    106x 2124x 2124x 2124x 864x       1260x     1260x 1260x   1260x       1258x                 1258x   2x     2x 2x      
import { API } from "aws-amplify";
import { SEATOOL_STATUS } from "shared-types";
 
import { sendGAEvent } from "@/utils/ReactGA/SendGAEvent";
 
type ItemExistsOptions = {
  includeDrafts?: boolean;
  allowDraftId?: string;
};
 
export const itemExists = async (id: string, options: ItemExistsOptions = {}): Promise<boolean> => {
  try {
    const normalizedId = id?.trim().toUpperCase();
    if (!normalizedId) {
      return false;
    }
 
    const draftIdFromUrl =
      typeof window !== "undefined"
        ? (new URLSearchParams(window.location.search).get("draftId") ?? undefined)
        : undefined;
    const allowDraftId = options.allowDraftId ?? draftIdFromUrl;
    const includeDrafts = options.includeDrafts ?? false;
 
    const response = await API.post("os", "/itemExists", {
      body: { id: normalizedId, includeDrafts },
    });
 
    Iif (
      includeDrafts &&
      allowDraftId &&
      allowDraftId.toUpperCase() === normalizedId &&
      response?.status === SEATOOL_STATUS.DRAFT
    ) {
      return false;
    }
 
    return response.exists;
  } catch (error) {
    sendGAEvent("api_error", {
      error: `failure /itemExists ${id}`,
    });
    console.error("Error checking if item exists:", error);
    return false;
  }
};