Skip to content

Commit

Permalink
Invite Notification (#31)
Browse files Browse the repository at this point in the history
* Add implementation for invite notification

* Update test accouring invite notification change

* rename function

* add crashlitics logs for notification error

* Disable firebase crashlytics for debug mode

* Add log on notification sent

* Fix pipeline
  • Loading branch information
cp-pratik-k authored Jul 13, 2023
1 parent c92bdf5 commit 9de4c1e
Show file tree
Hide file tree
Showing 12 changed files with 780 additions and 343 deletions.
2 changes: 2 additions & 0 deletions lib/data/di/service_locator.config.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/data/provider/user_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class UserStateNotifier with ChangeNotifier {

Space? get currentSpace => _userPreference.getSpace();

String? get currentSpaceName => _userPreference.getSpace()?.name;

String? get currentSpaceId => _userPreference.getSpace()?.id;

Employee? get _employee => _userPreference.getEmployee();
Expand Down
62 changes: 53 additions & 9 deletions lib/data/services/mail_notification_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'dart:convert';
import 'dart:developer';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:injectable/injectable.dart';
import 'package:http/http.dart' as http;
Expand All @@ -18,13 +20,13 @@ class NotificationService {
httpClient.close();
}

Future<bool> notifyHRForNewLeave(
Future<void> notifyHRForNewLeave(
{required String name,
required String reason,
required DateTime startDate,
required DateTime endDate,
required String receiver}) async {
if (kDebugMode) return true;
if (kDebugMode) return;
try {
http.Response response =
await httpClient.post(Uri.https(baseURL, 'api/leave/new'),
Expand All @@ -35,13 +37,16 @@ class NotificationService {
'reason': reason,
'receiver': receiver,
}));
return response.statusCode == 200;
} on Exception {
return false;
if (response.statusCode == 200) {
log('New Leave notification mail send successfully',
name: 'Notification');
}
} on Exception catch (e) {
FirebaseCrashlytics.instance.log(e.toString());
}
}

Future<bool> leaveResponse(
Future<void> leaveResponse(
{required String name,
required DateTime startDate,
required DateTime endDate,
Expand All @@ -56,9 +61,12 @@ class NotificationService {
"status": status.value,
"receiver": receiver,
}));
return response.statusCode == 200;
} on Exception {
return false;
if (response.statusCode == 200) {
log('Leave request update notification mail send successfully',
name: 'Notification');
}
} on Exception catch (e) {
FirebaseCrashlytics.instance.log(e.toString());
}
}

Expand All @@ -71,4 +79,40 @@ class NotificationService {
}
return '${DateFormat('dd MMM yyyy').format(startDate)} to ${DateFormat('dd MMM yyyy').format(endDate)}';
}

Future<void> sendInviteNotification(
{required String companyName, required String receiver}) async {
try {
http.Response response =
await httpClient.post(Uri.https(baseURL, '/api/invitation'),
body: json.encode({
"receiver": receiver,
"companyname": companyName,
"spacelink": "https://unity.canopas.com/home",
}));
if (response.statusCode == 200) {
log('Invite notification mail send successfully', name: 'Notification');
}
} on Exception catch (e) {
FirebaseCrashlytics.instance.log(e.toString());
}
}

Future<void> sendSpaceInviteAcceptNotification(
{required String sender, required String receiver}) async {
try {
http.Response response =
await httpClient.post(Uri.https(baseURL, '/api/acceptence'),
body: json.encode({
"receiver": receiver,
"sender": sender,
}));
if (response.statusCode == 200) {
log('Accept invitation notification mail send successfully',
name: 'Notification');
}
} on Exception catch (e) {
FirebaseCrashlytics.instance.log(e.toString());
}
}
}
5 changes: 5 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:ui';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart' show kDebugMode;
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localization.dart';
Expand Down Expand Up @@ -33,6 +34,10 @@ Future<void> main() async {
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
return true;
};

if (kDebugMode) {
await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(false);
}
usePathUrlStrategy();
runApp(MyApp());
}
Expand Down
10 changes: 8 additions & 2 deletions lib/ui/admin/home/invite_member/bloc/invite_member_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:injectable/injectable.dart';
import 'package:projectunity/data/core/exception/error_const.dart';
import 'package:projectunity/data/services/employee_service.dart';
import 'package:projectunity/data/services/mail_notification_service.dart';
import '../../../../../data/core/utils/bloc_status.dart';
import '../../../../../data/provider/user_state.dart';
import '../../../../../data/services/invitation_services.dart';
Expand All @@ -12,10 +13,11 @@ import 'invite_member_state.dart';
class InviteMemberBloc extends Bloc<InvitationEvent, InviteMemberState> {
final InvitationService _invitationService;
final EmployeeService _employeeService;
final NotificationService _notificationService;
final UserStateNotifier _userManager;

InviteMemberBloc(
this._invitationService, this._userManager, this._employeeService)
InviteMemberBloc(this._invitationService, this._userManager,
this._employeeService, this._notificationService)
: super(const InviteMemberState()) {
on<AddEmailEvent>(_enterEmailEvent);
on<InviteMemberEvent>(_inviteMember);
Expand Down Expand Up @@ -44,6 +46,10 @@ class InviteMemberBloc extends Bloc<InvitationEvent, InviteMemberState> {
senderId: _userManager.userUID!,
spaceId: _userManager.currentSpaceId!,
receiverEmail: state.email);
await _notificationService.sendInviteNotification(
companyName: _userManager.currentSpaceName!,
receiver: state.email,
);
emit(state.copyWith(status: Status.success));
}
} on Exception {
Expand Down
16 changes: 13 additions & 3 deletions lib/ui/space/join_space/bloc/join_space_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:projectunity/data/model/employee/employee.dart';
import 'package:projectunity/data/provider/user_state.dart';
import 'package:projectunity/data/services/account_service.dart';
import 'package:projectunity/data/services/auth_service.dart';
import 'package:projectunity/data/services/mail_notification_service.dart';
import 'package:projectunity/ui/space/join_space/bloc/join_space_event.dart';
import 'package:projectunity/ui/space/join_space/bloc/join_space_state.dart';
import '../../../../data/core/utils/bloc_status.dart';
Expand All @@ -21,12 +22,19 @@ class JoinSpaceBloc extends Bloc<JoinSpaceEvents, JoinSpaceState> {
final EmployeeService _employeeService;
final SpaceService _spaceService;
final InvitationService _invitationService;
final NotificationService _notificationService;
final AccountService accountService;
final AuthService _authService;
late final List<Invitation> invitations;

JoinSpaceBloc(this._invitationService, this._spaceService, this._userManager,
this.accountService, this._employeeService, this._authService)
JoinSpaceBloc(
this._invitationService,
this._spaceService,
this._userManager,
this.accountService,
this._employeeService,
this._authService,
this._notificationService)
: super(const JoinSpaceState()) {
on<JoinSpaceInitialFetchEvent>(_init);
on<SelectSpaceEvent>(_joinSpace);
Expand Down Expand Up @@ -108,7 +116,9 @@ class JoinSpaceBloc extends Bloc<JoinSpaceEvents, JoinSpaceState> {
space: event.space, spaceUser: employee);
final invitation = getSelectedInvitation(event.space.id);
await _invitationService.deleteInvitation(id: invitation.id);

await _notificationService.sendSpaceInviteAcceptNotification(
sender: _userManager.userEmail!,
receiver: event.space.notificationEmail!);
emit(state.copyWith(selectSpaceStatus: Status.success));
} on Exception {
emit(state.copyWith(
Expand Down
Loading

0 comments on commit 9de4c1e

Please sign in to comment.