shoc-frontend-new/src/domain/work-orders/utils/work-order-media-limits.ts
Alexandre Brandizzi 509b3c3212 fix(work-orders): persist completedDate via board and media sh-116
leave EditWorkorder for completedDate; await mutations before draft saved
toast; upload-then-categorize with MIME allowlist, progress, cancel, retry.
2026-07-31 12:24:31 -03:00

44 lines
1.5 KiB
TypeScript

/** Backend RequestSizeLimit for media and completion-doc uploads. */
export const MAX_WORK_ORDER_MEDIA_BYTES = 30 * 1024 * 1024;
const ALLOWED_MIME_TYPES = new Set(["image/jpeg", "image/png", "video/mp4", "video/quicktime"]);
const ALLOWED_EXTENSIONS = new Set([".jpg", ".jpeg", ".png", ".mp4", ".mov"]);
export function isWorkOrderFileTooLarge(
file: File,
maxBytes = MAX_WORK_ORDER_MEDIA_BYTES,
): boolean {
return file.size > maxBytes;
}
export function isWorkOrderMediaTypeAllowed(file: File): boolean {
const mimeOk = Boolean(file.type) && ALLOWED_MIME_TYPES.has(file.type.toLowerCase());
const name = file.name.toLowerCase();
const dot = name.lastIndexOf(".");
const ext = dot >= 0 ? name.slice(dot) : "";
const extOk = ALLOWED_EXTENSIONS.has(ext);
return mimeOk || extOk;
}
export function workOrderMediaAcceptAttribute(): string {
return "image/jpeg,image/png,video/mp4,video/quicktime,.jpg,.jpeg,.png,.mp4,.mov";
}
export function formatMediaSizeLimit(maxBytes = MAX_WORK_ORDER_MEDIA_BYTES): string {
return `${Math.round(maxBytes / (1024 * 1024))} MB`;
}
/** Backend IsReadOnly: Canceled or Completed. */
export function isWorkOrderDocumentsReadOnly(status: string | null | undefined): boolean {
return status === "Canceled" || status === "Completed";
}
export function canDeleteWorkOrderMedia(media: {
id: string | number;
isLegacy?: boolean;
}): boolean {
if (media.isLegacy) return false;
const numericId = typeof media.id === "number" ? media.id : Number(media.id);
return Number.isFinite(numericId) && numericId > 0;
}