Files
cozsweet-frontend-nextjs/src/stores/private-room/helper/unlock.ts
T

42 lines
1.4 KiB
TypeScript

import type { PrivateAlbumUnlockResponse } from "@/data/schemas/private-room";
import type { PrivateRoomUnlockPaywallRequest } from "../private-room-state";
export function toPrivateRoomErrorMessage(error: unknown): string {
if (error instanceof Error && error.message) return error.message;
return "Private room is temporarily unavailable. Please try again.";
}
export function toUnlockErrorMessage(
response: PrivateAlbumUnlockResponse,
): string | null {
switch (response.reason) {
case "cost_changed":
return "Unlock price changed. Please confirm again.";
case "unlock_in_progress":
return "This album is already unlocking. Please try again shortly.";
case "deduct_failed":
return "Credit deduction failed. Please try again.";
case "persist_failed_refunded":
return "Unlock failed and your credits were refunded. Please try again.";
case "not_found":
return "This private album is no longer available.";
default:
return response.unlocked && !response.locked
? null
: "Unlock failed. Please try again.";
}
}
export function toPaywallRequest(
response: PrivateAlbumUnlockResponse,
): PrivateRoomUnlockPaywallRequest {
return {
albumId: response.albumId,
reason: response.reason,
requiredCredits: response.requiredCredits || response.unlockCost,
currentCredits: response.creditBalance,
shortfallCredits: response.shortfallCredits,
};
}