All files / lib/packages/shared-types attachments.ts

64% Statements 16/25
40% Branches 8/20
66.66% Functions 4/6
66.66% Lines 12/18

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        215x                   215x                                               215x             4557x 4557x 4557x 4557x 4342x     215x     215x 7750x 7750x    
import { z } from "zod";
 
import { s3ParseUrl } from "../shared-utils/s3-url-parser";
 
export const attachmentSchema = z.object({
  filename: z.string(),
  title: z.string(),
  bucket: z.string(),
  key: z.string(),
  uploadDate: z.number(),
});
export type Attachment = z.infer<typeof attachmentSchema>;
 
// Attachment schema for legacy records
export const legacyAttachmentSchema = z.object({
  s3Key: z.string(),
  filename: z.string(),
  title: z.string(),
  contentType: z.string(),
  url: z.string().url(),
});
export type LegacyAttachment = z.infer<typeof legacyAttachmentSchema>;
 
export function handleLegacyAttachment(attachment: LegacyAttachment): Attachment | null {
  const parsedUrl = s3ParseUrl(attachment.url || "");
  if (!parsedUrl) return null;
  const bucket = parsedUrl.bucket;
  const key = parsedUrl.key;
  const uploadDate = parseInt(attachment.s3Key?.split("/")[0] || "0");
  return {
    title: attachment.title === "CMS Form 179" ? "CMS-179 Form" : attachment.title,
    filename: attachment.filename,
    uploadDate,
    bucket,
    key,
  } as Attachment;
}
 
export const attachmentArraySchema = ({
  max,
  message = "Required",
}: {
  max?: number;
  message?: string;
} = {}) => {
  const min = 1;
  const baseSchema = z.array(attachmentSchema);
  const noMax = max === 0 || max === undefined;
  if (noMax) {
    return baseSchema.min(min, { message });
  }
 
  return baseSchema.min(min, { message }).max(max, { message });
};
 
export const attachmentArraySchemaOptional = () => {
  const baseSchema = z.array(attachmentSchema);
  return baseSchema.optional();
};