All files / lib/attachment-archive failure-state.ts

75% Statements 18/24
56% Branches 14/25
100% Functions 7/7
75% Lines 18/24

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                  8x   8x   8x               3x                                   4x 1x         3x 3x 3x                         3x   3x                     1x                             17x                         6x 6x                                             5x 3x     2x    
import {
  ATTACHMENT_ARCHIVE_FAILURE_CODES,
  AttachmentArchiveBlockedAttachment,
  AttachmentArchiveCurrent,
  AttachmentArchiveFailureCode,
  AttachmentArchiveScope,
  AttachmentArchiveSourceAttachment,
} from "./types";
 
const DEFAULT_FAILURE_MESSAGE = "Unable to prepare the attachment archive.";
const PARTIAL_ARCHIVE_WARNING_MESSAGE =
  "Some attachments in this download are no longer available and were not included.";
 
export const ATTACHMENT_ARCHIVE_TERMINAL_FAILURE_CODES = new Set<AttachmentArchiveFailureCode>(
  ATTACHMENT_ARCHIVE_FAILURE_CODES,
);
 
export function buildAttachmentArchiveBlockedAttachment(
  attachment: AttachmentArchiveSourceAttachment,
  virusScanStatus?: string,
): AttachmentArchiveBlockedAttachment {
  return {
    bucket: attachment.bucket,
    key: attachment.key,
    filename: attachment.filename,
    title: attachment.title,
    ...(virusScanStatus ? { virusScanStatus } : {}),
  };
}
 
export function buildAttachmentArchiveFailureMessage({
  failureCode,
  blockedAttachment,
  scope,
}: {
  failureCode: AttachmentArchiveFailureCode;
  blockedAttachment?: AttachmentArchiveBlockedAttachment;
  scope?: AttachmentArchiveScope;
}) {
  if (failureCode === "ALL_ATTACHMENTS_UNAVAILABLE") {
    return scope === "section"
      ? "The attachments in this section are no longer available, so this download could not be created."
      : "The attachments in this package are no longer available, so this download could not be created.";
  }
 
  Eif (failureCode === "ATTACHMENT_NOT_CLEAN") {
    const filename = blockedAttachment?.filename || "One of the attachments";
    return `Unable to prepare the attachment archive because ${filename} is not available for download. File scanning did not complete successfully.`;
  }
 
  return DEFAULT_FAILURE_MESSAGE;
}
 
export function buildAttachmentNotCleanArchiveFailure({
  attachment,
  virusScanStatus,
}: {
  attachment: AttachmentArchiveSourceAttachment;
  virusScanStatus?: string;
}) {
  const blockedAttachment = buildAttachmentArchiveBlockedAttachment(attachment, virusScanStatus);
 
  return {
    failureCode: "ATTACHMENT_NOT_CLEAN" as const,
    failureMessage: buildAttachmentArchiveFailureMessage({
      failureCode: "ATTACHMENT_NOT_CLEAN",
      blockedAttachment,
    }),
    blockedAttachment,
  };
}
 
export function buildAllAttachmentsUnavailableArchiveFailure(scope: AttachmentArchiveScope) {
  return {
    failureCode: "ALL_ATTACHMENTS_UNAVAILABLE" as const,
    failureMessage: buildAttachmentArchiveFailureMessage({
      failureCode: "ALL_ATTACHMENTS_UNAVAILABLE",
      scope,
    }),
  };
}
 
export function isTerminalAttachmentArchiveFailure(
  current?: Pick<AttachmentArchiveCurrent, "status" | "failureCode">,
): current is Pick<AttachmentArchiveCurrent, "status" | "failureCode"> & {
  status: "FAILED";
  failureCode: AttachmentArchiveFailureCode;
} {
  return !!(
    current?.status === "FAILED" &&
    current.failureCode &&
    ATTACHMENT_ARCHIVE_TERMINAL_FAILURE_CODES.has(current.failureCode)
  );
}
 
export function getAttachmentArchiveFailureMessage(
  current?: Pick<
    AttachmentArchiveCurrent,
    "failureCode" | "failureMessage" | "errorMessage" | "blockedAttachment" | "scope"
  >,
) {
  Eif (current?.failureMessage) {
    return current.failureMessage;
  }
 
  if (current?.failureCode === "ALL_ATTACHMENTS_UNAVAILABLE") {
    return buildAttachmentArchiveFailureMessage({
      failureCode: "ALL_ATTACHMENTS_UNAVAILABLE",
      scope: current.scope,
    });
  }
 
  if (current?.blockedAttachment) {
    return buildAttachmentArchiveFailureMessage({
      failureCode: "ATTACHMENT_NOT_CLEAN",
      blockedAttachment: current.blockedAttachment,
    });
  }
 
  return current?.errorMessage || DEFAULT_FAILURE_MESSAGE;
}
 
export function getAttachmentArchiveWarningMessage(
  current?: Pick<AttachmentArchiveCurrent, "status" | "skippedAttachmentCount">,
) {
  if (current?.status !== "READY" || !current.skippedAttachmentCount) {
    return undefined;
  }
 
  return PARTIAL_ARCHIVE_WARNING_MESSAGE;
}