File size: 8,519 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { DomainData } from '@automattic/data-stores';
import { camelCase, mapKeys } from 'lodash';
import { getDomainType } from './get-domain-type';
import { getGdprConsentStatus } from './get-gdpr-consent-status';
import { getTransferStatus } from './get-transfer-status';
import {
	DnssecRecords,
	DomainType,
	GDPRConsentStatus,
	GoogleEmailSubscription,
	TitanEmailSubscription,
	TransferStatus,
} from './types';

function assembleGoogleAppsSubscription( googleAppsSubscription: {
	status: string;
	is_eligible_for_introductory_offer: boolean;
} ) {
	if ( ! googleAppsSubscription ) {
		return;
	}

	return mapKeys( googleAppsSubscription, ( _, key ) => camelCase( key ) );
}

function assembleCurrentUserCannotAddEmailReason( reason: {
	errors: { [ key: string ]: string[] };
} ) {
	if ( ! reason || ! reason.errors ) {
		return null;
	}

	const errorDetails = Object.entries( reason.errors ).map( ( entry ) => {
		const [ errorCode, errorMessages ] = entry;
		return {
			code: errorCode,
			message: errorMessages[ 0 ],
		};
	} );
	if ( ! errorDetails.length ) {
		return null;
	}
	return errorDetails[ 0 ];
}

function assembleDnssecRecords( dnssecRecords?: DnssecRecords ) {
	if ( ! dnssecRecords ) {
		return {};
	}

	return {
		dnskey: dnssecRecords.dnskey,
		dsData: dnssecRecords.dsData,
	};
}

export const createSiteDomainObject = ( domain: DomainData ) => {
	let transferEndDate = null;
	if ( domain.transfer_start_date ) {
		transferEndDate = new Date( domain.transfer_start_date );
		transferEndDate.setDate( transferEndDate.getDate() + 7 ); // Add 7 days.
		transferEndDate = transferEndDate.toISOString();
	}

	return {
		aRecordsRequiredForMapping: domain.a_records_required_for_mapping,
		autoRenewalDate: String( domain.auto_renewal_date ),
		adminEmail: domain.admin_email,
		aftermarketAuction: Boolean( domain.aftermarket_auction ),
		aftermarketAuctionEnd: String( domain.aftermarket_auction_end ?? '' ),
		aftermarketAuctionStart: String( domain.aftermarket_auction_start ?? '' ),
		authCodeRequired: Boolean( domain.auth_code_required ),
		autoRenewing: Boolean( domain.auto_renewing ),
		beginTransferUntilDate: String( domain.begin_transfer_until_date ),
		blogId: Number( domain.blog_id ),
		bundledPlanSubscriptionId: domain.bundled_plan_subscription_id,
		canSetAsPrimary: Boolean( domain.can_set_as_primary ),
		canManageDnsRecords: Boolean( domain.can_manage_dns_records ),
		canManageNameServers: Boolean( domain.can_manage_name_servers ),
		canUpdateContactInfo: Boolean( domain.can_update_contact_info ),
		cannotManageDnsRecordsReason: domain.cannot_manage_dns_records_reason
			? String( domain.cannot_manage_dns_records_reason )
			: null,
		cannotManageNameServersReason: domain.cannot_manage_name_servers_reason
			? String( domain.cannot_manage_name_servers_reason )
			: null,
		cannotUpdateContactInfoReason: domain.cannot_update_contact_info_reason
			? String( domain.cannot_update_contact_info_reason )
			: null,
		canTransferToAnyUser: Boolean( domain.can_transfer_to_any_user ),
		canTransferToOtherSite: Boolean( domain.can_transfer_to_other_site ),
		connectionMode: String( domain.connection_mode ),
		contactInfoDisclosureAvailable: Boolean( domain.contact_info_disclosure_available ),
		contactInfoDisclosed: Boolean( domain.contact_info_disclosed ),
		currentUserCanAddEmail: Boolean( domain.current_user_can_add_email ),
		currentUserCanCreateSiteFromDomainOnly: Boolean(
			domain.current_user_can_create_site_from_domain_only
		),
		currentUserCanManage: Boolean( domain.current_user_can_manage ),
		currentUserCannotAddEmailReason: assembleCurrentUserCannotAddEmailReason(
			domain.current_user_cannot_add_email_reason
		),
		currentUserIsOwner: Boolean( domain.current_user_is_owner ),
		dnssecRecords: assembleDnssecRecords( domain.dnssec_records ),
		domain: String( domain.domain ),
		domainLockingAvailable: Boolean( domain.domain_locking_available ),
		domainRegistrationAgreementUrl: domain.domain_registration_agreement_url ?? null,
		emailForwardsCount: Number( domain.email_forwards_count ),
		expired: Boolean( domain.expired ),
		expiry: ! domain.expiry ? null : String( domain.expiry ),
		expirySoon: Boolean( domain.expiry_soon ),
		gdprConsentStatus: getGdprConsentStatus( domain ) as GDPRConsentStatus,
		googleAppsSubscription: assembleGoogleAppsSubscription(
			domain.google_apps_subscription
		) as GoogleEmailSubscription,
		titanMailSubscription: assembleGoogleAppsSubscription(
			domain.titan_mail_subscription
		) as TitanEmailSubscription,
		hasPendingContactUpdate: Boolean( domain.has_pending_contact_update ),
		hasRegistration: Boolean( domain.has_registration ),
		hasWpcomNameservers: domain.has_wpcom_nameservers,
		hasZone: Boolean( domain.has_zone ),
		isDnssecEnabled: Boolean( domain.is_dnssec_enabled ),
		isDnssecSupported: Boolean( domain.is_dnssec_supported ),
		isGravatarDomain: Boolean( domain.is_gravatar_domain ),
		isGravatarRestrictedDomain: Boolean( domain.is_gravatar_restricted_domain ),
		isHundredYearDomain: Boolean( domain.is_hundred_year_domain ),
		isLocked: Boolean( domain.is_locked ),
		isRenewable: Boolean( domain.is_renewable ),
		isRedeemable: Boolean( domain.is_redeemable ),
		isEligibleForInboundTransfer: Boolean( domain.is_eligible_for_inbound_transfer ),
		isAutoRenewing: Boolean( domain.auto_renewing ),
		isIcannVerificationSuspended:
			typeof domain.is_icann_verification_suspended === 'boolean'
				? Boolean( domain.is_icann_verification_suspended )
				: null,
		isMappedToAtomicSite: Boolean( domain.is_mapped_to_atomic_site ),
		isMoveToNewSitePending: Boolean( domain.is_move_to_new_site_pending ),
		isPendingIcannVerification: Boolean( domain.is_pending_icann_verification ),
		isRootDomainRegisteredWithAutomattic: Boolean(
			domain.is_root_domain_registered_with_automattic
		),
		isPendingRenewal: Boolean( domain.pending_renewal ),
		isPremium: Boolean( domain.is_premium ),
		isPrimary: Boolean( domain.primary_domain ),
		isPendingWhoisUpdate: Boolean( domain.pending_whois_update ),
		isSubdomain: Boolean( domain.is_subdomain ),
		isWPCOMDomain: Boolean( domain.wpcom_domain ),
		isWpcomStagingDomain: Boolean( domain.is_wpcom_staging_domain ),
		lastTransferError: String( domain.last_transfer_error ?? '' ),
		manualTransferRequired: Boolean( domain.manual_transfer_required ),
		mustRemovePrivacyBeforeContactUpdate: Boolean(
			domain.must_remove_privacy_before_contact_update
		),
		name: String( domain.domain ),
		nominetDomainSuspended: Boolean( domain.nominet_domain_suspended ),
		nominetPendingContactVerificationRequest: Boolean(
			domain.nominet_pending_contact_verification_request
		),
		owner: domain.owner,
		partnerDomain: Boolean( domain.partner_domain ),
		pendingRegistration: Boolean( domain.pending_registration ),
		pendingRegistrationAtRegistry: Boolean( domain.pending_registration_at_registry ),
		pendingRegistrationAtRegistryUrl: String( domain.pending_registration_at_registry_url ?? '' ),
		pendingTransfer: domain.pending_transfer,
		pointsToWpcom: Boolean( domain.points_to_wpcom ),
		privateDomain: domain.private_domain,
		privacyAvailable: Boolean( domain.privacy_available ),
		registrar: String( domain.registrar ),
		registrationDate: String( domain.registration_date ),
		renewableUntil: String( domain.renewable_until ),
		redeemableUntil: String( domain.redeemable_until ),
		registeredViaTrustee: Boolean( domain.registered_via_trustee ),
		registeredViaTrusteeUrl: String( domain.registered_via_trustee_url ?? '' ),
		registryExpiryDate: String( domain.registry_expiry_date ?? '' ),
		sslStatus: domain.ssl_status,
		subdomainPart: String( domain.subdomain_part ),
		subscriptionId: domain.subscription_id,
		supportsDomainConnect: Boolean( domain.supports_domain_connect ),
		supportsGdprConsentManagement: Boolean( domain.supports_gdpr_consent_management ),
		supportsTransferApproval: Boolean( domain.supports_transfer_approval ),
		tldMaintenanceEndTime: domain.tld_maintenance_end_time,
		transferAwayEligibleAt: ! domain.transfer_away_eligible_at
			? null
			: String( domain.transfer_away_eligible_at ),
		transferLockOnWhoisUpdateOptional: Boolean( domain.transfer_lock_on_whois_update_optional ),
		type: getDomainType( domain ) as DomainType,
		transferStatus: getTransferStatus( domain ) as TransferStatus,
		transferStartDate: ! domain.transfer_start_date ? null : String( domain.transfer_start_date ),
		transferEndDate,
		whoisUpdateUnmodifiableFields: domain.whois_update_unmodifiable_fields,
	};
};