File size: 2,192 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { OAuthErrorCode } from "@midday/app-store/oauth-errors";

/**
 * Re-export the shared OAuth error code type for dashboard use
 */
export type AppOAuthErrorCode = OAuthErrorCode;

/**
 * User-friendly error titles
 */
const ERROR_TITLES: Record<AppOAuthErrorCode, string> = {
  access_denied: "Connection Cancelled",
  missing_license: "License Required",
  missing_permissions: "Admin Access Required",
  invalid_state: "Session Expired",
  token_exchange_failed: "Connection Failed",
  unknown_error: "Something Went Wrong",
};

/**
 * User-friendly error descriptions
 */
const ERROR_DESCRIPTIONS: Record<AppOAuthErrorCode, string> = {
  access_denied: "You cancelled the connection or denied access.",
  missing_license:
    "Your account doesn't have the required license for this integration. Please upgrade your plan or contact support.",
  missing_permissions:
    "You need administrator permissions to connect this integration. Please contact your account admin or try with an admin account.",
  invalid_state:
    "The connection session expired. Please close this window and try again.",
  token_exchange_failed:
    "We couldn't complete the connection. Please close this window and try again.",
  unknown_error:
    "An unexpected error occurred. Please close this window and try again.",
};

/**
 * Get user-friendly error title
 */
export function getErrorTitle(
  errorCode: AppOAuthErrorCode | null | undefined,
): string {
  return (
    ERROR_TITLES[errorCode ?? "unknown_error"] ?? ERROR_TITLES.unknown_error
  );
}

/**
 * Get user-friendly error description
 */
export function getErrorDescription(
  errorCode: AppOAuthErrorCode | null | undefined,
): string {
  return (
    ERROR_DESCRIPTIONS[errorCode ?? "unknown_error"] ??
    ERROR_DESCRIPTIONS.unknown_error
  );
}

/**
 * Format provider name for display
 */
export function formatProviderName(provider: string): string {
  const providerNames: Record<string, string> = {
    fortnox: "Fortnox",
    xero: "Xero",
    quickbooks: "QuickBooks",
    gmail: "Gmail",
    outlook: "Outlook",
    slack: "Slack",
    stripe: "Stripe",
  };

  return providerNames[provider.toLowerCase()] ?? provider;
}