handleShopifySubmit method

Future<void> handleShopifySubmit(
  1. String email
)

Implementation

Future<void> handleShopifySubmit(
  String email,
) async {
  if (!formKey.currentState!.validate()) return;
  emit(state.copyWith(isLoading: true));
  shopifyEmailController.text = email;
  try {
    // Check if the submitted email matches the previous submitted email
    if (state.lastSubmittedEmail != null &&
        state.lastSubmittedEmail == email.trim()) {
      emit(state.copyWith(emailOtpSent: true, isLoading: false));
      return;
    }
    // Validate disposable email
    final isValidEmail = await ShopifyService.validateDisposableEmail(email);

    if (!isValidEmail) {
      emit(state.copyWith(isLoading: false));
      const errorMessage = 'Entered email is not valid';
      emit(state.copyWith(
          error: SingleUseData(errorMessage),
          isLoading: false,
          emailOtpSent: false));
      onErrorData?.call(
          FlowResult(flowType: FlowType.emailOtpSend, error: errorMessage));
      return;
    }

    // Check if user is new by calling customerShopifySession
    Map<String, dynamic>? responseToCheckIfUserIsNew;
    if (email.isNotEmpty && state.multipleEmails.isEmpty) {
      try {
        responseToCheckIfUserIsNew =
            await ShopifyService.customerShopifySession(
          email: email,
          shopifyCustomerId: state.shopifyCustomerId ??
              "", // Use saved shopifyCustomerId if available
          isMarketingEventSubscribed: state.notifications,
        );
      } catch (err) {
        // If customerShopifySession fails, continue with normal flow
      }
    }

    if (responseToCheckIfUserIsNew?['data']?['isNewUser'] == true) {
      if (onAnalytics != null) {
        onAnalytics!(
          cdnConfigInstance.getAnalyticsEvent(AnalyticsEventKeys.appLoginSuccess)!,
          {
            'email': email,
            'phone': phoneController.text.toString(),
            'customer_id': responseToCheckIfUserIsNew?['data']
                        ?['shopifyCustomerId']
                    ?.toString() ??
                "",
          },
        );
      }
      if (responseToCheckIfUserIsNew!.containsKey('data')) {
        if (!responseToCheckIfUserIsNew['data'].containsKey('phone')) {
          responseToCheckIfUserIsNew['data']['phone'] =
              phoneController.text.toString();
        }
      }
      onSuccessData?.call(FlowResult(
        flowType: FlowType.emailOtpSend,
        data: responseToCheckIfUserIsNew?['data'],
      ));
      emit(state.copyWith(isSuccess: true, isLoading: false));
      return;
    }

    await ShopifyService.shopifySendEmailVerificationCode(email);

    emit(state.copyWith(
        emailOtpSent: true,
        isNewUser: false,
        isLoading: false,
        lastSubmittedEmail: email.trim()));
  } catch (err) {
    emit(state.copyWith(
        error: SingleUseData((err is Failure) ? err.message : err.toString()),
        isLoading: false,
        emailOtpSent: false));
    onErrorData
        ?.call(FlowResult(flowType: FlowType.emailOtpSend, error: err));
  }
}