bidstage/lib/privacy-request.ts

50 lines
1.7 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { MarketInputError } from "./market-error";
export const PRIVACY_REQUEST_TYPES = [
"access",
"correction",
"deletion",
"restriction",
] as const;
export type PrivacyRequestType = (typeof PRIVACY_REQUEST_TYPES)[number];
export type PrivacyRequestState = "open" | "in_progress" | "completed" | "declined" | "cancelled";
export type PrivacyRequestInput = {
requestType: PrivacyRequestType;
details: string;
};
export function isPrivacyRequestType(value: unknown): value is PrivacyRequestType {
return typeof value === "string" && PRIVACY_REQUEST_TYPES.includes(value as PrivacyRequestType);
}
export function normalizePrivacyRequestDetails(value: unknown): string {
const details = typeof value === "string" ? value.trim().replace(/\s+/g, " ") : "";
if (details.length < 20 || details.length > 1200) {
throw new MarketInputError("Privacy request details must be 20–1200 characters");
}
return details;
}
export function parsePrivacyRequest(value: unknown): PrivacyRequestInput {
if (!value || typeof value !== "object" || Array.isArray(value)) {
throw new MarketInputError("Send a valid privacy request");
}
const body = value as Record<string, unknown>;
if (!isPrivacyRequestType(body.requestType)) {
throw new MarketInputError("Choose access, correction, deletion, or restriction");
}
return {
requestType: body.requestType,
details: normalizePrivacyRequestDetails(body.details),
};
}
export function privacyRequestLabel(type: PrivacyRequestType): string {
if (type === "access") return "Copy of account data";
if (type === "correction") return "Correct account data";
if (type === "deletion") return "Delete eligible account data";
return "Restrict account-data processing";
}