File size: 2,054 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
import { isMappedDomainWithWpcomNameservers, isRegisteredDomain } from 'calypso/lib/domains';
import { isRecentlyRegistered } from 'calypso/lib/domains/utils';
import { canDomainAddGSuite } from './can-domain-add-gsuite';
import { hasGSuiteWithAnotherProvider } from './has-gsuite-with-another-provider';
import { hasGSuiteWithUs } from './has-gsuite-with-us';

/**
 * @typedef { import('calypso/lib/domains/types').ResponseDomain } ResponseDomain domain object
 */

/**
 * Filters a list of domains by the domains that eligible for G Suite.
 * @param {ResponseDomain[]} domains - list of domain objects
 * @returns {ResponseDomain[]} - the list of domains that are eligible for G Suite
 */
export function getGSuiteSupportedDomains( domains ) {
	return domains.filter( function ( domain ) {
		if ( hasGSuiteWithAnotherProvider( domain ) ) {
			return false;
		}

		// If the domain is registered through us, there is a provisioning period when
		// `hasWpcomNameservers` will be false. We still want to let users buy Google Workspace
		// during that period, even if we normally wouldn't let them under these conditions.
		// Therefore, we check those conditions and return true if the registration happened less
		// than 15 minutes ago. 15 minutes is an arbitrary number.
		if (
			isRegisteredDomain( domain ) &&
			! domain.hasWpcomNameservers &&
			isRecentlyRegistered( domain.registrationDate, 15 )
		) {
			return true;
		}

		const isHostedOnWpcom =
			isRegisteredDomain( domain ) && ( domain.hasWpcomNameservers || hasGSuiteWithUs( domain ) );

		if ( ! isHostedOnWpcom && ! isMappedDomainWithWpcomNameservers( domain ) ) {
			return false;
		}

		return canDomainAddGSuite( domain.name );
	} );
}

/**
 * Given a list of domains does one of them support G Suite
 * @param {ResponseDomain?[]} domains - list of domain objects
 * @returns {boolean} - Does list of domains contain a G Suited supported domain
 */
export function hasGSuiteSupportedDomain( domains ) {
	return getGSuiteSupportedDomains( domains.filter( Boolean ) ).length > 0;
}