From 1e0a869ab65c8e366bfbe9f82625d73272dadda4 Mon Sep 17 00:00:00 2001 From: Felipe Sales Date: Mon, 15 Apr 2024 21:15:18 -0300 Subject: [PATCH] feat: change `emailjs` to `aws ses` for sending emails --- lib/app/core/injections/injections.dart | 1 - lib/app/features/home/home_page.dart | 11 ++--- .../home/widgets/contact/contact_widget.dart | 6 ++- lib/data/constants/constants_api.dart | 8 +--- lib/data/models/contact.dart | 4 ++ .../contact/contact_repository_impl.dart | 41 +++---------------- .../firebase/firebase_service_impl.dart | 2 +- .../services/recaptcha/recaptcha_service.dart | 7 +++- .../controller/contact_controller_test.dart | 20 --------- .../contact/contact_repository_impl_test.dart | 17 -------- 10 files changed, 27 insertions(+), 90 deletions(-) diff --git a/lib/app/core/injections/injections.dart b/lib/app/core/injections/injections.dart index a48d7dd0..c69b9028 100644 --- a/lib/app/core/injections/injections.dart +++ b/lib/app/core/injections/injections.dart @@ -26,7 +26,6 @@ void configureDependencies() { if (!getIt.isRegistered()) { getIt.registerSingleton( ContactRepositoryImpl( - firebaseRemoteConfig: getIt(), httpClient: getIt(), ), ); diff --git a/lib/app/features/home/home_page.dart b/lib/app/features/home/home_page.dart index 1fe2b973..3d9ba53b 100644 --- a/lib/app/features/home/home_page.dart +++ b/lib/app/features/home/home_page.dart @@ -25,6 +25,8 @@ class HomePage extends StatefulWidget { }) : _firebaseRemoteConfig = firebaseRemoteConfig ?? getIt(), _httpClient = httpClient ?? getIt(); + /// The [FirebaseRemoteConfig] instance is here to be used for future updates and configurations. + // ignore: unused_field final FirebaseRemoteConfig _firebaseRemoteConfig; final http.Client _httpClient; @@ -42,18 +44,17 @@ class _HomePageState extends State { void initState() { super.initState(); items = [ + Presentation(itemScrollController), + const Projects(), + const Experience(), + const Social(), ContactWidget( contactController: ContactController( contactRepository: ContactRepositoryImpl( - firebaseRemoteConfig: widget._firebaseRemoteConfig, httpClient: widget._httpClient, ), ), ), - Presentation(itemScrollController), - const Projects(), - const Experience(), - const Social(), const CustomFooter(), ]; } diff --git a/lib/app/features/home/widgets/contact/contact_widget.dart b/lib/app/features/home/widgets/contact/contact_widget.dart index f7154a75..3e403924 100644 --- a/lib/app/features/home/widgets/contact/contact_widget.dart +++ b/lib/app/features/home/widgets/contact/contact_widget.dart @@ -22,7 +22,6 @@ class ContactWidget extends StatelessWidget { }) : _contactController = contactController ?? ContactController( contactRepository: ContactRepositoryImpl( - firebaseRemoteConfig: getIt(), httpClient: getIt(), ), ); @@ -49,7 +48,7 @@ class ContactWidget extends StatelessWidget { final isNotABot = await RecaptchaService.isNotABot(); if (isNotABot) { - formKey.currentState!.save(); + formKey.currentState?.save(); if (context.mounted) { appShowSnackBar( context, @@ -68,6 +67,7 @@ class ContactWidget extends StatelessWidget { subject: subjectController.text, ), ); + for (var controller in [ nameController, emailController, @@ -76,6 +76,8 @@ class ContactWidget extends StatelessWidget { ]) { controller.clear(); } + + formKey.currentState?.reset(); } else if (context.mounted) { appShowSnackBar( context, diff --git a/lib/data/constants/constants_api.dart b/lib/data/constants/constants_api.dart index 69d28373..805edd4e 100644 --- a/lib/data/constants/constants_api.dart +++ b/lib/data/constants/constants_api.dart @@ -1,14 +1,8 @@ import 'package:site/infra/env/env.dart'; class ConstantsAPI { - static const baseUrl = 'https://api.emailjs.com/api/v1.0/email/send'; - static const headers = { - 'origin': 'http://localhost', - 'Content-Type': 'application/json', - }; - /// Send email endpoint. - static final apiSendMail = Env.apiSendMail; + static final apiSendMail = Uri.parse(Env.apiSendMail); /// Recaptcha information. static final recaptchaPublicKey = Env.recaptchaPublicKey; diff --git a/lib/data/models/contact.dart b/lib/data/models/contact.dart index dae42d48..38f00ade 100644 --- a/lib/data/models/contact.dart +++ b/lib/data/models/contact.dart @@ -1,3 +1,5 @@ +import 'dart:convert'; + class Contact { Contact({ required this.name, @@ -16,4 +18,6 @@ class Contact { 'template_body': contact.message, }; } + + static String toJsonString(Contact contact) => jsonEncode(toJson(contact)); } diff --git a/lib/data/repositories/contact/contact_repository_impl.dart b/lib/data/repositories/contact/contact_repository_impl.dart index 6ec97dac..e6bdbbdc 100644 --- a/lib/data/repositories/contact/contact_repository_impl.dart +++ b/lib/data/repositories/contact/contact_repository_impl.dart @@ -1,7 +1,5 @@ -import 'dart:convert'; import 'dart:developer'; -import 'package:firebase_remote_config/firebase_remote_config.dart'; import 'package:http/http.dart' as http; import 'package:site/data/constants/constants_api.dart'; @@ -10,49 +8,22 @@ import 'package:site/data/repositories/contact/contact.dart'; class ContactRepositoryImpl implements ContactRepository { ContactRepositoryImpl({ - required FirebaseRemoteConfig firebaseRemoteConfig, required http.Client httpClient, - }) : _firebaseRemoteConfig = firebaseRemoteConfig, - _httpClient = httpClient; + }) : _httpClient = httpClient; - final FirebaseRemoteConfig _firebaseRemoteConfig; final http.Client _httpClient; @override Future sendMail({ required Contact contact, }) async { - // final serviceId = _firebaseRemoteConfig.getString('service_id'); - // final templateId = _firebaseRemoteConfig.getString('template_id'); - // final userId = _firebaseRemoteConfig.getString('user_id'); - // final toEmail = _firebaseRemoteConfig.getString('to_email'); - // final baseUrl = Uri.parse(ConstantsAPI.baseUrl); - - // final response = await _httpClient.post( - // baseUrl, - // headers: ConstantsAPI.headers, - // body: json.encode( - // { - // 'service_id': serviceId, - // 'template_id': templateId, - // 'user_id': userId, - // 'template_params': { - // 'user_name': contact.name, - // 'user_email': contact.email, - // 'user_subject': contact.email, - // 'user_message': contact.message, - // 'to_email': toEmail, - // }, - // }, - // ), - // ); - try { - final apiEndpoint = ConstantsAPI.apiSendMail; - final response = await _httpClient.post( - Uri.parse(apiEndpoint), - body: Contact.toJson(contact), + ConstantsAPI.apiSendMail, + body: Contact.toJsonString(contact), + headers: { + 'Content-Type': 'application/json', + }, ); return response; diff --git a/lib/data/services/firebase/firebase_service_impl.dart b/lib/data/services/firebase/firebase_service_impl.dart index dea81d34..1f317962 100644 --- a/lib/data/services/firebase/firebase_service_impl.dart +++ b/lib/data/services/firebase/firebase_service_impl.dart @@ -23,7 +23,7 @@ class FirebaseServiceImpl implements FirebaseService { try { await remoteConfig.setDefaults(remoteConfigKeys); await remoteConfig.setConfigSettings(remoteConfigSettings); - await remoteConfig.fetchAndActivate(); + // await remoteConfig.fetchAndActivate(); } catch (e, s) { developer.log( 'setUpRemoteConfig', diff --git a/lib/data/services/recaptcha/recaptcha_service.dart b/lib/data/services/recaptcha/recaptcha_service.dart index f5fb333c..273cad34 100644 --- a/lib/data/services/recaptcha/recaptcha_service.dart +++ b/lib/data/services/recaptcha/recaptcha_service.dart @@ -1,6 +1,6 @@ import 'dart:developer'; -import 'dart:io'; +import 'package:flutter/foundation.dart'; import 'package:g_recaptcha_v3/g_recaptcha_v3.dart'; import 'package:http/http.dart' as http; import 'package:site/data/constants/constants_api.dart'; @@ -15,7 +15,10 @@ class RecaptchaService { static Future isNotABot() async { final verificationResponse = await _getVerificationResponse(); - if (Platform.isAndroid || Platform.isIOS) { + if ([ + TargetPlatform.iOS, + TargetPlatform.android, + ].contains(defaultTargetPlatform)) { return true; } diff --git a/test/app/features/home/widgets/contact/controller/contact_controller_test.dart b/test/app/features/home/widgets/contact/controller/contact_controller_test.dart index 9d50e997..16017a00 100644 --- a/test/app/features/home/widgets/contact/controller/contact_controller_test.dart +++ b/test/app/features/home/widgets/contact/controller/contact_controller_test.dart @@ -12,39 +12,19 @@ import '../../../../../../utils/utils.dart'; void main() { setupFirebaseAuthMocks(); - - late MockFirebaseRemoteConfig mockFirebaseRemoteConfig; late MockHttpClient mockHttpClient; late ContactRepositoryImpl contactRepository; late ContactController contactController; setUp(() { - mockFirebaseRemoteConfig = MockFirebaseRemoteConfig(); mockHttpClient = MockHttpClient(); contactRepository = ContactRepositoryImpl( - firebaseRemoteConfig: mockFirebaseRemoteConfig, httpClient: mockHttpClient, ); contactController = ContactController( contactRepository: contactRepository, ); - when( - () => mockFirebaseRemoteConfig.getString('service_id'), - ).thenReturn('service_id'); - - when( - () => mockFirebaseRemoteConfig.getString('template_id'), - ).thenReturn('template_id'); - - when( - () => mockFirebaseRemoteConfig.getString('user_id'), - ).thenReturn('user_id'); - - when( - () => mockFirebaseRemoteConfig.getString('to_email'), - ).thenReturn('to_email'); - when( () => mockHttpClient.post( any(), diff --git a/test/data/repositories/contact/contact_repository_impl_test.dart b/test/data/repositories/contact/contact_repository_impl_test.dart index 775b7458..ca48d919 100644 --- a/test/data/repositories/contact/contact_repository_impl_test.dart +++ b/test/data/repositories/contact/contact_repository_impl_test.dart @@ -16,26 +16,9 @@ void main() { mockHttpClient = MockHttpClient(); contactRepository = ContactRepositoryImpl( - firebaseRemoteConfig: mockFirebaseRemoteConfig, httpClient: mockHttpClient, ); - when( - () => mockFirebaseRemoteConfig.getString('service_id'), - ).thenReturn('service_id'); - - when( - () => mockFirebaseRemoteConfig.getString('template_id'), - ).thenReturn('template_id'); - - when( - () => mockFirebaseRemoteConfig.getString('user_id'), - ).thenReturn('user_id'); - - when( - () => mockFirebaseRemoteConfig.getString('to_email'), - ).thenReturn('to_email'); - when( () => mockHttpClient.post( any(),