feat(availability): Refactor AvailabilityRepositoryImpl to use DataConnectService and simplify dependency injection
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart' as firebase;
|
||||
import 'package:firebase_data_connect/firebase_data_connect.dart';
|
||||
import 'package:krow_data_connect/krow_data_connect.dart' as dc;
|
||||
import 'package:krow_domain/krow_domain.dart';
|
||||
@@ -10,44 +9,19 @@ import '../../domain/repositories/availability_repository.dart';
|
||||
/// not specific date availability. Therefore, updating availability for a specific
|
||||
/// date will update the availability for that Day of Week globally (Recurring).
|
||||
class AvailabilityRepositoryImpl
|
||||
with dc.DataErrorHandler
|
||||
implements AvailabilityRepository {
|
||||
final dc.ExampleConnector _dataConnect;
|
||||
final firebase.FirebaseAuth _firebaseAuth;
|
||||
String? _cachedStaffId;
|
||||
final dc.DataConnectService _service;
|
||||
|
||||
AvailabilityRepositoryImpl({
|
||||
required dc.ExampleConnector dataConnect,
|
||||
required firebase.FirebaseAuth firebaseAuth,
|
||||
}) : _dataConnect = dataConnect,
|
||||
_firebaseAuth = firebaseAuth;
|
||||
|
||||
Future<String> _getStaffId() async {
|
||||
if (_cachedStaffId != null) return _cachedStaffId!;
|
||||
|
||||
final firebase.User? user = _firebaseAuth.currentUser;
|
||||
if (user == null) {
|
||||
throw NotAuthenticatedException(
|
||||
technicalMessage: 'User not authenticated');
|
||||
}
|
||||
|
||||
final QueryResult<dc.GetStaffByUserIdData, dc.GetStaffByUserIdVariables> result =
|
||||
await _dataConnect.getStaffByUserId(userId: user.uid).execute();
|
||||
if (result.data.staffs.isEmpty) {
|
||||
throw const ServerException(technicalMessage: 'Staff profile not found');
|
||||
}
|
||||
_cachedStaffId = result.data.staffs.first.id;
|
||||
return _cachedStaffId!;
|
||||
}
|
||||
AvailabilityRepositoryImpl() : _service = dc.DataConnectService.instance;
|
||||
|
||||
@override
|
||||
Future<List<DayAvailability>> getAvailability(DateTime start, DateTime end) async {
|
||||
return executeProtected(() async {
|
||||
final String staffId = await _getStaffId();
|
||||
return _service.run(() async {
|
||||
final String staffId = await _service.getStaffId();
|
||||
|
||||
// 1. Fetch Weekly recurring availability
|
||||
final QueryResult<dc.ListStaffAvailabilitiesByStaffIdData, dc.ListStaffAvailabilitiesByStaffIdVariables> result =
|
||||
await _dataConnect.listStaffAvailabilitiesByStaffId(staffId: staffId).limit(100).execute();
|
||||
await _service.connector.listStaffAvailabilitiesByStaffId(staffId: staffId).limit(100).execute();
|
||||
|
||||
final List<dc.ListStaffAvailabilitiesByStaffIdStaffAvailabilities> items = result.data.staffAvailabilities;
|
||||
|
||||
@@ -124,8 +98,8 @@ class AvailabilityRepositoryImpl
|
||||
|
||||
@override
|
||||
Future<DayAvailability> updateDayAvailability(DayAvailability availability) async {
|
||||
return executeProtected(() async {
|
||||
final String staffId = await _getStaffId();
|
||||
return _service.run(() async {
|
||||
final String staffId = await _service.getStaffId();
|
||||
final dc.DayOfWeek dow = _toBackendDay(availability.date.weekday);
|
||||
|
||||
// Update each slot in the backend.
|
||||
@@ -143,8 +117,8 @@ class AvailabilityRepositoryImpl
|
||||
|
||||
@override
|
||||
Future<List<DayAvailability>> applyQuickSet(DateTime start, DateTime end, String type) async {
|
||||
return executeProtected(() async {
|
||||
final String staffId = await _getStaffId();
|
||||
return _service.run(() async {
|
||||
final String staffId = await _service.getStaffId();
|
||||
|
||||
// QuickSet updates the Recurring schedule for all days involved.
|
||||
// However, if the user selects a range that covers e.g. Mon-Fri, we update Mon-Fri.
|
||||
@@ -204,7 +178,7 @@ class AvailabilityRepositoryImpl
|
||||
|
||||
Future<void> _upsertSlot(String staffId, dc.DayOfWeek day, dc.AvailabilitySlot slot, dc.AvailabilityStatus status) async {
|
||||
// Check if exists
|
||||
final result = await _dataConnect.getStaffAvailabilityByKey(
|
||||
final result = await _service.connector.getStaffAvailabilityByKey(
|
||||
staffId: staffId,
|
||||
day: day,
|
||||
slot: slot,
|
||||
@@ -212,14 +186,14 @@ class AvailabilityRepositoryImpl
|
||||
|
||||
if (result.data.staffAvailability != null) {
|
||||
// Update
|
||||
await _dataConnect.updateStaffAvailability(
|
||||
await _service.connector.updateStaffAvailability(
|
||||
staffId: staffId,
|
||||
day: day,
|
||||
slot: slot,
|
||||
).status(status).execute();
|
||||
} else {
|
||||
// Create
|
||||
await _dataConnect.createStaffAvailability(
|
||||
await _service.connector.createStaffAvailability(
|
||||
staffId: staffId,
|
||||
day: day,
|
||||
slot: slot,
|
||||
|
||||
@@ -18,12 +18,7 @@ class StaffAvailabilityModule extends Module {
|
||||
@override
|
||||
void binds(Injector i) {
|
||||
// Repository
|
||||
i.add<AvailabilityRepository>(
|
||||
() => AvailabilityRepositoryImpl(
|
||||
dataConnect: ExampleConnector.instance,
|
||||
firebaseAuth: FirebaseAuth.instance,
|
||||
),
|
||||
);
|
||||
i.add<AvailabilityRepository>(AvailabilityRepositoryImpl.new);
|
||||
|
||||
// UseCases
|
||||
i.add(GetWeeklyAvailabilityUseCase.new);
|
||||
|
||||
Reference in New Issue
Block a user