File size: 11,076 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import { useMutation, useQueryClient } from '@tanstack/react-query';
import wpcom from 'calypso/lib/wp';
import { DEFAULT_PER_PAGE } from '../constants';
import {
	getSubscriberDetailsCacheKey,
	getSubscriberDetailsType,
	getSubscribersCacheKey,
} from '../helpers';
import { useRecordSubscriberRemoved } from '../tracks';
import type { SubscriberEndpointResponse, Subscriber, SubscriberQueryParams } from '../types';

type ApiResponseError = {
	error: string;
	message: string;
};

/**
 * Gets the email subscription ID from a subscriber object.
 * @param {Subscriber} subscriber - The subscriber object
 * @returns {number} The email subscription ID, or 0 if not found
 * @deprecated The `subscription_id` property is deprecated and from the old API endpoint response. Use `email_subscription_id` instead.
 */
const getEmailSubscriptionId = ( subscriber: Subscriber ): number => {
	// `subscription_id` is from the old API endpoint response.
	return subscriber.email_subscription_id || subscriber.subscription_id || 0;
};

/**
 * Gets the WordPress.com subscription ID from a subscriber object.
 * @param {Subscriber} subscriber - The subscriber object
 * @returns {number} The WordPress.com subscription ID, or 0 if not found
 * @deprecated The `subscription_id` property is deprecated and from the old API endpoint response. Use `wpcom_subscription_id` instead.
 */
const getWpcomSubscriptionId = ( subscriber: Subscriber ): number => {
	// `subscription_id` is from the old API endpoint response.
	return subscriber.wpcom_subscription_id || subscriber.subscription_id || 0;
};

/**
 * Hook to remove subscribers from a site.
 * Handles removal of email subscribers, WordPress.com followers, and paid subscription members.
 * @param {number | null} siteId - The ID of the site
 * @param {SubscriberQueryParams} SubscriberQueryParams - Query parameters for subscriber list
 * @param {boolean} invalidateDetailsCache - Whether to invalidate the subscriber details cache (default: false)
 */
const useSubscriberRemoveMutation = (
	siteId: number | null,
	SubscriberQueryParams: SubscriberQueryParams,
	invalidateDetailsCache = false
) => {
	const {
		page,
		perPage = DEFAULT_PER_PAGE,
		filters = [],
		search,
		sortTerm,
	} = SubscriberQueryParams;
	const queryClient = useQueryClient();
	const recordSubscriberRemoved = useRecordSubscriberRemoved();

	// Get the cache key for the current page
	const currentPageCacheKey = getSubscribersCacheKey( {
		siteId,
		...SubscriberQueryParams,
	} );

	return useMutation( {
		mutationFn: async ( subscribers: Subscriber[] ) => {
			if ( ! siteId || ! subscribers || ! subscribers.length ) {
				throw new Error(
					// reminder: translate this string when we add it to the UI
					'Something went wrong while unsubscribing.'
				);
			}

			if ( subscribers.length > 100 ) {
				throw new Error(
					// reminder: translate this string when we add it to the UI
					'The maximum number of subscribers you can remove at once is 100.'
				);
			}

			const subscriberPromises = subscribers.map( async ( subscriber ) => {
				if ( subscriber.plans?.length ) {
					// unsubscribe this user from all plans
					const promises = subscriber.plans.map( ( plan ) =>
						wpcom.req.post(
							`/sites/${ siteId }/memberships/subscriptions/${ plan.paid_subscription_id }/cancel`,
							{
								user_id: subscriber.user_id,
							}
						)
					);

					await Promise.all( promises );
				}

				let emailRemoved = false;
				let wpcomRemoved = false;

				const numericUserId = Number( subscriber.user_id );
				const emailSubscriptionId = getEmailSubscriptionId( subscriber );

				// Remove the subscriber from the followers if they have a numeric user_id
				if ( ! isNaN( numericUserId ) ) {
					try {
						const response = await wpcom.req.post(
							`/sites/${ siteId }/followers/${ numericUserId }/delete`
						);
						wpcomRemoved = response?.deleted === true;
					} catch ( e ) {
						// Only throw if they don't have an email subscription ID to try next
						if ( ( e as ApiResponseError )?.error === 'not_found' && ! emailSubscriptionId ) {
							throw new Error( ( e as ApiResponseError )?.message );
						}
					}
				}

				// Try to remove as email follower if they have an email subscription ID
				if ( emailSubscriptionId ) {
					try {
						const response = await wpcom.req.post(
							`/sites/${ siteId }/email-followers/${ emailSubscriptionId }/delete`
						);
						// Verify the response indicates successful deletion
						emailRemoved = response?.deleted === true;
					} catch ( e ) {
						// Consider "not_following" as a successful removal since they're already not following
						if ( ( e as ApiResponseError )?.error === 'not_following' ) {
							emailRemoved = true;
						} else if ( ! wpcomRemoved ) {
							// Only throw if we haven't successfully removed them through any other method
							throw new Error( ( e as ApiResponseError )?.message );
						}
					}
				}

				// Consider removal successful if:
				// 1. Email subscription was either:
				//    - Successfully removed (if it existed)
				//    - Did not exist (no removal needed)
				// 2. WPCOM following was either:
				//    - Successfully removed (if it existed)
				//    - Did not exist (no removal needed)
				const isFullyRemoved =
					( emailSubscriptionId ? emailRemoved : true ) &&
					( ! isNaN( numericUserId ) ? wpcomRemoved : true );

				return isFullyRemoved;
			} );
			const promiseResults = await Promise.allSettled( subscriberPromises );
			if (
				promiseResults.every( ( promiseResults ) => {
					return promiseResults.status === 'fulfilled' && promiseResults.value === true;
				} )
			) {
				return true;
			}
			const errorPromise = promiseResults.find( ( promiseResult ) => {
				return promiseResult.status === 'rejected';
			} );
			if ( errorPromise ) {
				throw new Error( errorPromise.reason );
			}
			return false;
		},
		onMutate: async ( subscribers ) => {
			// Cancel any outgoing refetches
			await queryClient.cancelQueries( { queryKey: [ 'subscribers', siteId ] } );
			await queryClient.cancelQueries( { queryKey: [ 'subscribers', 'counts', siteId ] } );

			// Get the current page data
			const previousData =
				queryClient.getQueryData< SubscriberEndpointResponse >( currentPageCacheKey );

			// Get the current count data
			const previousCountData = queryClient.getQueryData< { total_subscribers: number } >( [
				'subscribers',
				'counts',
				siteId,
			] );

			if ( previousData ) {
				// Update the current page data
				const updatedData = {
					...previousData,
					subscribers: previousData.subscribers.filter( ( s ) => {
						return ! subscribers.some( ( subscriber ) => {
							// Match on either wpcom or email subscription ID
							const sEmailId = getEmailSubscriptionId( s );
							const subscriberEmailId = getEmailSubscriptionId( subscriber );
							const sWpcomId = getWpcomSubscriptionId( s );
							const subscriberWpcomId = getWpcomSubscriptionId( subscriber );
							return (
								( sEmailId && sEmailId === subscriberEmailId ) ||
								( sWpcomId && sWpcomId === subscriberWpcomId )
							);
						} );
					} ),
					total: previousData.total - subscribers.length,
					pages: Math.ceil( ( previousData.total - subscribers.length ) / previousData.per_page ),
				};

				// Update the current page cache
				queryClient.setQueryData( currentPageCacheKey, updatedData );

				// If this was the last item on the page and we're not on the first page,
				// we'll need to fetch the previous page's data
				if ( page > 1 && updatedData.subscribers.length === 0 ) {
					const previousPageCacheKey = getSubscribersCacheKey( {
						siteId,
						page: page - 1,
						perPage,
						search,
						sortTerm,
						filters,
					} );
					await queryClient.invalidateQueries( { queryKey: previousPageCacheKey } );
				}
			}

			// Update the count cache if it exists
			if ( previousCountData ) {
				const updatedCountData = {
					...previousCountData,
					total_subscribers: previousCountData.total_subscribers - subscribers.length,
				};
				queryClient.setQueryData( [ 'subscribers', 'counts', siteId ], updatedCountData );
			}

			// Handle subscriber details cache if needed
			let previousDetailsData;
			if ( invalidateDetailsCache ) {
				for ( const subscriber of subscribers ) {
					const detailsCacheKey = getSubscriberDetailsCacheKey(
						siteId,
						getEmailSubscriptionId( subscriber ),
						subscriber.user_id,
						getSubscriberDetailsType( subscriber.user_id )
					);
					await queryClient.cancelQueries( { queryKey: detailsCacheKey } );
					previousDetailsData = queryClient.getQueryData< Subscriber >( detailsCacheKey );
				}
			}

			return {
				previousData,
				previousCountData,
				previousDetailsData,
			};
		},
		onError: ( error, variables, context ) => {
			// If the mutation fails, revert the optimistic update
			if ( context?.previousData ) {
				queryClient.setQueryData( currentPageCacheKey, context.previousData );
			}

			// Revert the count data if it exists
			if ( context?.previousCountData ) {
				queryClient.setQueryData( [ 'subscribers', 'counts', siteId ], context.previousCountData );
			}

			if ( context?.previousDetailsData ) {
				const detailsCacheKey = getSubscriberDetailsCacheKey(
					siteId,
					getEmailSubscriptionId( context.previousDetailsData ),
					context.previousDetailsData.user_id,
					getSubscriberDetailsType( context.previousDetailsData.user_id )
				);
				queryClient.setQueryData( detailsCacheKey, context.previousDetailsData );
			}

			// Force invalidate all subscriber queries to ensure UI is in sync
			queryClient.invalidateQueries( { queryKey: [ 'subscribers', siteId ] } );
			queryClient.invalidateQueries( { queryKey: [ 'subscribers', 'counts', siteId ] } );
		},
		onSuccess: ( data, subscribers ) => {
			// Force invalidate all subscriber queries to ensure UI is in sync
			queryClient.invalidateQueries( { queryKey: [ 'subscribers', siteId ] } );
			queryClient.invalidateQueries( { queryKey: [ 'subscribers', 'counts', siteId ] } );

			for ( const subscriber of subscribers ) {
				recordSubscriberRemoved( {
					site_id: siteId,
					subscription_id: getEmailSubscriptionId( subscriber ),
					user_id: subscriber.user_id,
				} );
			}
		},
		onSettled: ( data, error, subscribers ) => {
			// Always invalidate and refetch everything to ensure consistency
			queryClient.invalidateQueries( { queryKey: [ 'subscribers', siteId ] } );
			queryClient.invalidateQueries( { queryKey: [ 'subscribers', 'counts', siteId ] } );

			// Always handle subscriber details cache if requested
			if ( invalidateDetailsCache ) {
				for ( const subscriber of subscribers ) {
					const detailsCacheKey = getSubscriberDetailsCacheKey(
						siteId,
						getEmailSubscriptionId( subscriber ),
						subscriber.user_id,
						getSubscriberDetailsType( subscriber.user_id )
					);
					queryClient.invalidateQueries( { queryKey: detailsCacheKey } );
				}
			}
		},
	} );
};

export default useSubscriberRemoveMutation;