File size: 4,142 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { keepPreviousData, useQuery } from '@tanstack/react-query';
import sha256 from 'hash.js/lib/hash/sha/256';
import wpcomRequest from 'wpcom-proxy-request';
import { DomainValidationOptions } from 'calypso/landing/stepper/declarative-flow/internals/steps-repository/domain-transfer-domains/use-validation-message';
import { domainAvailability } from 'calypso/lib/domains/constants';

const VERSION = 2;

/**
 * Irreversibly hash the auth code to avoid storing it as query key.
 * @param code The domain auth code.
 * @returns the hash.
 */
function hashAuthCode( code: string ) {
	return sha256().update( code ).digest( 'hex' );
}

type DomainCodeResponse = {
	success: boolean;
};

type DomainLockResponse = {
	transfer_restriction_status?: string;
	transfer_eligible_date?: any;
	term_maximum_in_years?: number;
	admin_email?: string;
	creation_date?: string;
	registrar?: string;
	registrar_iana_id?: string;
	privacy?: boolean;
	unlocked: boolean | null | undefined;
	in_redemption?: boolean;
	status: string;
	transferrability?: string;
	raw_price?: number;
	sale_cost?: number;
	currency_code?: string;
	tld?: string;
	is_price_limit_exceeded?: boolean;
	cannot_transfer_due_to_unsupported_premium_tld?: boolean;
};

type DomainCodePair = {
	domain: string;
	auth: string;
	options?: DomainValidationOptions;
};

export function useIsDomainCodeValid( pair: DomainCodePair, queryOptions = {} ) {
	return useQuery( {
		queryKey: [ 'domain-code-valid', VERSION, pair.domain, hashAuthCode( pair.auth ) ],
		queryFn: async () => {
			try {
				const options = ! pair.options
					? {}
					: Object.entries( pair.options ).reduce(
							( acc, [ key, value ] ) => {
								acc[ key ] = String( value );
								return acc;
							},
							{} as Record< string, string >
					  );
				const availability = await wpcomRequest< DomainLockResponse >( {
					apiVersion: '1.3',
					path: `/domains/${ encodeURIComponent( pair.domain ) }/is-available`,
					query: new URLSearchParams( options ).toString(),
				} );

				// A `transferrability` property was added in D115244-code to check whether a mapped domain can be transferred
				const isUnlocked =
					[
						domainAvailability.TRANSFERRABLE,
						domainAvailability.TRANSFERRABLE_PREMIUM,
						domainAvailability.MAPPED_SAME_SITE_TRANSFERRABLE,
					].includes( availability.status ) ||
					( [ domainAvailability.MAPPED, domainAvailability.MAPPED_OTHER_SITE_SAME_USER ].includes(
						availability.status
					) &&
						[ domainAvailability.TRANSFERRABLE, domainAvailability.TRANSFERRABLE_PREMIUM ].includes(
							availability?.transferrability ?? ''
						) );

				if ( ! isUnlocked || availability?.is_price_limit_exceeded === true ) {
					return {
						domain: pair.domain,
						tld: availability.tld,
						status: availability.status,
						unlocked: false,
						transferrability: availability.transferrability,
						is_price_limit_exceeded: availability?.is_price_limit_exceeded,
						cannot_transfer_due_to_unsupported_premium_tld:
							availability?.cannot_transfer_due_to_unsupported_premium_tld,
					};
				}

				const response = await wpcomRequest< DomainCodeResponse >( {
					apiVersion: '1.1',
					path: `/domains/${ encodeURIComponent( pair.domain ) }/inbound-transfer-check-auth-code`,
					query: `auth_code=${ encodeURIComponent( pair.auth ) }`,
				} ).catch( () => ( { success: false } ) );

				return {
					domain: pair.domain,
					registered: true,
					unlocked: true,
					auth_code_valid: response.success,
					status: availability.status,
					transferrability: availability.transferrability,
					raw_price: availability.raw_price,
					sale_cost: availability.sale_cost,
					currency_code: availability.currency_code,
					cannot_transfer_due_to_unsupported_premium_tld:
						availability?.cannot_transfer_due_to_unsupported_premium_tld,
				};
			} catch ( error ) {
				return {
					error: 'Failed to check domain lock status.',
				};
			}
		},
		staleTime: 5 * 60 * 1000,
		gcTime: 5 * 60 * 1000,
		placeholderData: keepPreviousData,
		refetchOnWindowFocus: false,
		refetchOnMount: false,
		...queryOptions,
	} );
}