All files / lib/attachment-archive repair-audit.ts

96.66% Statements 58/60
78.33% Branches 47/60
100% Functions 14/14
96.36% Lines 53/55

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 140 141 142 143 144 145 146 147 148 149 150 151      1x 1x                                           1x 1x 1x 1x   1x 443x   443x 8x 6x 6x   2x   8x     435x 32x 32x 32x     403x 3x       3x 3x 3x   3x 3x 3x     400x     1x 1x 1x     1x       1x 1x 1x       1x 3x 27x     3x                             1x 3x                     5x 4x 1x     3x   3x       3x 1x     2x 3x   2x 1x     1x                       2x    
import { AttachmentArchiveIntegrityDiscrepancy } from "./integrity-types";
import { AttachmentArchiveFailureCode, AttachmentArchiveStatus } from "./types";
 
const CLEAN_STATUS = "CLEAN";
const TERMINAL_FAILURE_CODES = new Set<AttachmentArchiveFailureCode>([
  "ALL_ATTACHMENTS_UNAVAILABLE",
  "ATTACHMENT_NOT_CLEAN",
]);
 
export type AttachmentArchiveRepairClassification =
  | "repairable_scan_failure"
  | "terminal_exception_candidate"
  | "rebuild_only"
  | "residual_worker_failure";
 
export interface AttachmentArchiveRepairCurrentEntry {
  status: AttachmentArchiveStatus;
  failureCode?: AttachmentArchiveFailureCode;
}
 
export interface AttachmentArchiveRepairSourceInspection {
  exists: boolean;
  virusScanStatus?: string;
}
 
function parseCsvRows(content: string) {
  const rows: string[][] = [];
  let row: string[] = [];
  let field = "";
  let inQuotes = false;
 
  for (let index = 0; index < content.length; index += 1) {
    const character = content[index];
 
    if (character === '"') {
      if (inQuotes && content[index + 1] === '"') {
        field += '"';
        index += 1;
      } else {
        inQuotes = !inQuotes;
      }
      continue;
    }
 
    if (!inQuotes && character === ",") {
      row.push(field);
      field = "";
      continue;
    }
 
    if (!inQuotes && (character === "\n" || character === "\r")) {
      Iif (character === "\r" && content[index + 1] === "\n") {
        index += 1;
      }
 
      row.push(field);
      Eif (row.some((value) => value.length > 0)) {
        rows.push(row);
      }
      row = [];
      field = "";
      continue;
    }
 
    field += character;
  }
 
  row.push(field);
  Eif (row.some((value) => value.length > 0)) {
    rows.push(row);
  }
 
  return rows;
}
 
export function parseIntegrityReportCsv(content: string): AttachmentArchiveIntegrityDiscrepancy[] {
  const rows = parseCsvRows(content);
  const [header, ...dataRows] = rows;
  Iif (!header || header.length === 0) {
    return [];
  }
 
  return dataRows.map((row) => {
    const record = Object.fromEntries(
      header.map((column, index) => [column, row[index] === undefined ? "" : row[index]]),
    ) as Partial<AttachmentArchiveIntegrityDiscrepancy>;
 
    return {
      authority: String(record.authority || ""),
      packageId: String(record.packageId || ""),
      sectionId: String(record.sectionId || ""),
      cmsStatus: String(record.cmsStatus || ""),
      submissionDate: String(record.submissionDate || ""),
      issueScope: record.issueScope === "Download All" ? "Download All" : "Section",
      discrepancyType: String(record.discrepancyType || ""),
      expectedValue: String(record.expectedValue || ""),
      actualValue: String(record.actualValue || ""),
    };
  });
}
 
export function getIntegrityReportPackageIds(rows: AttachmentArchiveIntegrityDiscrepancy[]) {
  return Array.from(
    new Set(rows.map((row) => row.packageId).filter((packageId) => packageId.length > 0)),
  ).sort();
}
 
export function classifyPackageRepairCandidate({
  currentEntries,
  sourceInspections,
}: {
  currentEntries: AttachmentArchiveRepairCurrentEntry[];
  sourceInspections: AttachmentArchiveRepairSourceInspection[];
}): AttachmentArchiveRepairClassification {
  const failedEntries = currentEntries.filter((entry) => entry.status === "FAILED");
  if (failedEntries.length === 0) {
    return "rebuild_only";
  }
 
  const hasRepairableScanFailure = sourceInspections.some(
    (inspection) =>
      inspection.exists &&
      inspection.virusScanStatus !== undefined &&
      inspection.virusScanStatus !== CLEAN_STATUS,
  );
  if (hasRepairableScanFailure) {
    return "repairable_scan_failure";
  }
 
  const allFailedEntriesAreTerminal = failedEntries.every(
    (entry) => entry.failureCode && TERMINAL_FAILURE_CODES.has(entry.failureCode),
  );
  if (allFailedEntriesAreTerminal) {
    return "terminal_exception_candidate";
  }
 
  return "residual_worker_failure";
}
 
export function resolveQueuedPackageIds({
  inputPackageIds,
  packageIdsToRebuild,
  rebuildInputPackages,
}: {
  inputPackageIds: string[];
  packageIdsToRebuild: string[];
  rebuildInputPackages: boolean;
}) {
  return Array.from(new Set(rebuildInputPackages ? inputPackageIds : packageIdsToRebuild)).sort();
}