File size: 3,147 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
import { isEnabled } from '@automattic/calypso-config';
import page, { type Callback } from '@automattic/calypso-router';
import { getQueryArgs, addQueryArgs } from '@wordpress/url';
import isA8CForAgencies from 'calypso/lib/a8c-for-agencies/is-a8c-for-agencies';
import {
	getActiveAgency,
	getUserBillingType,
	hasAgency,
	hasFetchedAgency,
} from 'calypso/state/a8c-for-agencies/agency/selectors';
import {
	A4A_CLIENT_LANDING_LINK,
	A4A_CLIENT_SUBSCRIPTIONS_LINK,
	A4A_LANDING_LINK,
	A4A_OVERVIEW_LINK,
} from './components/sidebar-menu/lib/constants';
import { isPathAllowed, isPathAllowedForTier } from './lib/permission';
import TierPermissionError from './sections/agency-tier/tier-permission-error';
import type { Agency } from 'calypso/state/a8c-for-agencies/types';

export const redirectToLandingContext: Callback = () => {
	if ( isA8CForAgencies() ) {
		const args = getQueryArgs( window.location.href );
		page.redirect( addQueryArgs( A4A_LANDING_LINK, args ) );
		return;
	}
	window.location.href = 'https://automattic.com/for/agencies';
	return;
};

// This function is used to check if the user has access to the current path
const handleMultiUserSupport = ( agency: Agency, pathname: string, next: () => void ) => {
	if ( isPathAllowed( pathname, agency ) ) {
		next();
		return;
	}
	window.location.href = A4A_OVERVIEW_LINK;
	return;
};

export const requireAccessContext: Callback = ( context, next ) => {
	const state = context.store.getState();
	const agency = getActiveAgency( state );
	const { search, hash } = window.location;
	const pathname = context.pathname;

	if ( agency ) {
		// If multi-user support is enabled, we need to check if the user has access to the current path
		handleMultiUserSupport( agency, pathname, next );
		return;
	}

	const args = getQueryArgs( window.location.href );
	page.redirect( addQueryArgs( A4A_LANDING_LINK, { ...args, return: pathname + search + hash } ) );
};

export const requireClientAccessContext: Callback = ( context, next ) => {
	const state = context.store.getState();
	const hasFetchedAgencies = hasFetchedAgency( state );
	const isAgency = hasAgency( state );

	if ( hasFetchedAgencies && ! isAgency ) {
		next();
		return;
	}

	const { search, hash } = window.location;
	const pathname = context.pathname;
	const args = getQueryArgs( window.location.href );
	page.redirect(
		addQueryArgs( A4A_CLIENT_LANDING_LINK, { ...args, return: pathname + search + hash } )
	);
};

export const requireLegacyClientBillingContext: Callback = ( context, next ) => {
	const state = context.store.getState();
	const userBillingType = getUserBillingType( state );

	if ( userBillingType !== 'legacy' ) {
		page.redirect( A4A_CLIENT_SUBSCRIPTIONS_LINK );
		return;
	}

	next();
};

export const requireTierAccessContext: Callback = ( context, next ) => {
	const state = context.store.getState();
	const agency = getActiveAgency( state );
	const pathname = context.pathname;

	if ( isEnabled( 'a8c-for-agencies-agency-tier' ) && ! isPathAllowedForTier( pathname, agency ) ) {
		context.primary = <TierPermissionError section={ context.section.name } />;
		next();
		return;
	}
	next();
};