Skip to content

Commit

Permalink
feat: change emailjs to aws ses for sending emails
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecastrosales committed Apr 16, 2024
1 parent 5ce8268 commit 1e0a869
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 90 deletions.
1 change: 0 additions & 1 deletion lib/app/core/injections/injections.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ void configureDependencies() {
if (!getIt.isRegistered<ContactRepository>()) {
getIt.registerSingleton<ContactRepository>(
ContactRepositoryImpl(
firebaseRemoteConfig: getIt(),
httpClient: getIt(),
),
);
Expand Down
11 changes: 6 additions & 5 deletions lib/app/features/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -42,18 +44,17 @@ class _HomePageState extends State<HomePage> {
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(),
];
}
Expand Down
6 changes: 4 additions & 2 deletions lib/app/features/home/widgets/contact/contact_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class ContactWidget extends StatelessWidget {
}) : _contactController = contactController ??
ContactController(
contactRepository: ContactRepositoryImpl(
firebaseRemoteConfig: getIt(),
httpClient: getIt(),
),
);
Expand All @@ -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,
Expand All @@ -68,6 +67,7 @@ class ContactWidget extends StatelessWidget {
subject: subjectController.text,
),
);

for (var controller in [
nameController,
emailController,
Expand All @@ -76,6 +76,8 @@ class ContactWidget extends StatelessWidget {
]) {
controller.clear();
}

formKey.currentState?.reset();
} else if (context.mounted) {
appShowSnackBar(
context,
Expand Down
8 changes: 1 addition & 7 deletions lib/data/constants/constants_api.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 4 additions & 0 deletions lib/data/models/contact.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

class Contact {
Contact({
required this.name,
Expand All @@ -16,4 +18,6 @@ class Contact {
'template_body': contact.message,
};
}

static String toJsonString(Contact contact) => jsonEncode(toJson(contact));
}
41 changes: 6 additions & 35 deletions lib/data/repositories/contact/contact_repository_impl.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/data/services/firebase/firebase_service_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 5 additions & 2 deletions lib/data/services/recaptcha/recaptcha_service.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -15,7 +15,10 @@ class RecaptchaService {
static Future<bool> isNotABot() async {
final verificationResponse = await _getVerificationResponse();

if (Platform.isAndroid || Platform.isIOS) {
if ([
TargetPlatform.iOS,
TargetPlatform.android,
].contains(defaultTargetPlatform)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
17 changes: 0 additions & 17 deletions test/data/repositories/contact/contact_repository_impl_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 1e0a869

Please sign in to comment.