167 lines
4.5 KiB
Dart
167 lines
4.5 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flux/core/services/whatsapp/whatsapp_response_model.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
class WhatsAppService {
|
|
static const String _functionName = 'send-whatsapp';
|
|
|
|
final SupabaseClient _supabase;
|
|
|
|
WhatsAppService(this._supabase);
|
|
|
|
/// Invia un messaggio WhatsApp tramite la Edge Function `send-whatsapp`.
|
|
Future<WhatsAppResponse> sendMessage({
|
|
required String phone,
|
|
required String message,
|
|
bool linkPreview = true,
|
|
}) async {
|
|
final cleanedPhone = phone.trim();
|
|
final cleanedMessage = message.trim();
|
|
|
|
if (cleanedPhone.isEmpty) {
|
|
return const WhatsAppResponse(
|
|
success: false,
|
|
exists: false,
|
|
error: 'Numero di telefono mancante.',
|
|
);
|
|
}
|
|
|
|
if (cleanedMessage.isEmpty) {
|
|
return const WhatsAppResponse(
|
|
success: false,
|
|
exists: false,
|
|
error: 'Il messaggio non può essere vuoto.',
|
|
);
|
|
}
|
|
|
|
return _invoke({
|
|
'phone': cleanedPhone,
|
|
'message': cleanedMessage,
|
|
'linkPreview': linkPreview,
|
|
});
|
|
}
|
|
|
|
/// Invia un documento (es. PDF ricevuta) su WhatsApp con eventuale didascalia.
|
|
Future<WhatsAppResponse> sendDocument({
|
|
required String phone,
|
|
required Uint8List bytes,
|
|
required String filename,
|
|
String? caption,
|
|
String mimetype = 'application/pdf',
|
|
}) async {
|
|
final cleanedPhone = phone.trim();
|
|
if (cleanedPhone.isEmpty) {
|
|
return const WhatsAppResponse(
|
|
success: false,
|
|
exists: false,
|
|
error: 'Numero di telefono mancante.',
|
|
);
|
|
}
|
|
|
|
final cleanedCaption = caption?.trim() ?? '';
|
|
return _invoke({
|
|
'phone': cleanedPhone,
|
|
if (cleanedCaption.isNotEmpty) 'message': cleanedCaption,
|
|
if (cleanedCaption.isNotEmpty) 'caption': cleanedCaption,
|
|
'file': {
|
|
'mimetype': mimetype,
|
|
'filename': filename,
|
|
'data': base64Encode(bytes),
|
|
},
|
|
});
|
|
}
|
|
|
|
/// Verifica se il numero è registrato su WhatsApp senza inviare alcun messaggio.
|
|
Future<WhatsAppResponse> checkNumber(String phone) async {
|
|
final cleanedPhone = phone.trim();
|
|
|
|
if (cleanedPhone.isEmpty) {
|
|
return const WhatsAppResponse(
|
|
success: false,
|
|
exists: false,
|
|
error: 'Numero di telefono mancante.',
|
|
);
|
|
}
|
|
|
|
return _invoke({
|
|
'phone': cleanedPhone,
|
|
'checkOnly': true,
|
|
});
|
|
}
|
|
|
|
/// Scorciatoia booleana per verificare se un numero ha WhatsApp attivo.
|
|
Future<bool> hasWhatsApp(String phone) async {
|
|
final result = await checkNumber(phone);
|
|
return result.exists;
|
|
}
|
|
|
|
Future<WhatsAppResponse> _invoke(Map<String, dynamic> body) async {
|
|
try {
|
|
final response = await _supabase.functions.invoke(
|
|
_functionName,
|
|
body: body,
|
|
);
|
|
|
|
final data = _decodeData(response.data);
|
|
if (data != null) {
|
|
return WhatsAppResponse.fromMap(data);
|
|
}
|
|
|
|
return WhatsAppResponse(
|
|
success: response.status >= 200 && response.status < 300,
|
|
exists: false,
|
|
error: response.status >= 200 && response.status < 300
|
|
? null
|
|
: 'Risposta non valida dal server (HTTP ${response.status}).',
|
|
);
|
|
} on FunctionException catch (e) {
|
|
debugPrint('WhatsAppService FunctionException (${e.status}): ${e.details}');
|
|
final details = _decodeData(e.details);
|
|
if (details != null) {
|
|
final parsed = WhatsAppResponse.fromMap(details);
|
|
return parsed.copyWith(
|
|
success: false,
|
|
error: parsed.error ?? e.reasonPhrase ?? 'Errore invio WhatsApp.',
|
|
);
|
|
}
|
|
return WhatsAppResponse(
|
|
success: false,
|
|
exists: false,
|
|
error: e.reasonPhrase ?? 'Errore di comunicazione con il server (${e.status}).',
|
|
);
|
|
} catch (e) {
|
|
debugPrint('WhatsAppService error: $e');
|
|
return WhatsAppResponse(
|
|
success: false,
|
|
exists: false,
|
|
error: 'Impossibile contattare il servizio WhatsApp: $e',
|
|
);
|
|
}
|
|
}
|
|
|
|
Map<String, dynamic>? _decodeData(dynamic raw) {
|
|
if (raw is Map<String, dynamic>) {
|
|
return raw;
|
|
}
|
|
if (raw is Map) {
|
|
return Map<String, dynamic>.from(raw);
|
|
}
|
|
if (raw is String && raw.trim().isNotEmpty) {
|
|
try {
|
|
final decoded = jsonDecode(raw);
|
|
if (decoded is Map<String, dynamic>) {
|
|
return decoded;
|
|
}
|
|
if (decoded is Map) {
|
|
return Map<String, dynamic>.from(decoded);
|
|
}
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|