File size: 2,853 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
import config from '@automattic/calypso-config';
import { mapRecordKeysRecursively, camelToSnakeCase } from '@automattic/js-utils';
import { logToLogstash } from 'calypso/lib/logstash';
import { addQueryArgs } from 'calypso/lib/url';
import type {
	DIFMDependencies,
	WebsiteContent,
	WebsiteContentRequestDTO,
} from 'calypso/state/signup/steps/website-content/types';
import type { SiteId } from 'calypso/types';

const logValidationFailure = (
	message: string,
	context: string,
	dependencies: Partial< DIFMDependencies >
) => {
	logToLogstash( {
		feature: 'calypso_client',
		message,
		severity: config( 'env_id' ) === 'production' ? 'error' : 'debug',
		properties: {
			type: 'calypso_difm_extras_validation_failure',
			dependencies: JSON.stringify( dependencies ),
			context,
		},
	} );
};

export function buildDIFMCartExtrasObject(
	dependencies: Partial< DIFMDependencies >,
	siteId: SiteId,
	context: string
) {
	const {
		newOrExistingSiteChoice,
		siteTitle,
		siteDescription,
		tagline,
		searchTerms,
		selectedDesign,
		selectedSiteCategory,
		isLetUsChooseSelected,
		twitterUrl,
		facebookUrl,
		linkedinUrl,
		instagramUrl,
		displayEmail,
		displayPhone,
		displayAddress,
		selectedPageTitles,
		isStoreFlow,
	} = dependencies;

	if ( ! siteTitle ) {
		logValidationFailure( 'siteTitle does not exist', context, dependencies );
	}

	if ( ! selectedPageTitles?.length ) {
		logValidationFailure( 'selectedPageTitles does not exist', context, dependencies );
	}

	return {
		new_or_existing_site_choice: newOrExistingSiteChoice,
		site_title: siteTitle || 'NO_SITE_TITLE',
		site_description: siteDescription || tagline,
		search_terms: searchTerms,
		selected_design: selectedDesign?.theme,
		site_category: selectedSiteCategory,
		let_us_choose_selected: isLetUsChooseSelected,
		twitter_url: twitterUrl,
		facebook_url: facebookUrl,
		linkedin_url: linkedinUrl,
		instagram_url: instagramUrl,
		display_email: displayEmail,
		display_phone: displayPhone,
		display_address: displayAddress,
		selected_page_titles: selectedPageTitles,
		is_store_flow: isStoreFlow,
		afterPurchaseUrl: addQueryArgs( { siteId }, '/start/site-content-collection' ),
	};
}

export function buildDIFMWebsiteContentRequestDTO(
	websiteContent: WebsiteContent
): WebsiteContentRequestDTO {
	const {
		pages,
		siteInformationSection: { siteLogoUrl: site_logo_url, searchTerms: search_terms },
		feedbackSection: { genericFeedback: generic_feedback },
	} = websiteContent;
	const pagesDTO = pages
		.map( ( page ) => {
			return {
				...page,
				media: page.media.filter( ( mediaItem ) => !! mediaItem.url ),
			};
		} )
		.map( ( page ) => mapRecordKeysRecursively( page, camelToSnakeCase ) );
	return {
		pages: pagesDTO,
		site_logo_url: site_logo_url ?? '',
		generic_feedback: generic_feedback ?? '',
		search_terms: search_terms ?? '',
	};
}