feat(tax-forms-repository): Refactor TaxFormsRepositoryImpl to simplify initialization and utilize DataConnectService for data operations
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:firebase_auth/firebase_auth.dart' as auth;
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
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,45 +8,21 @@ import '../../domain/repositories/tax_forms_repository.dart';
|
||||
import '../mappers/tax_form_mapper.dart';
|
||||
|
||||
class TaxFormsRepositoryImpl
|
||||
with dc.DataErrorHandler
|
||||
implements TaxFormsRepository {
|
||||
TaxFormsRepositoryImpl({
|
||||
required this.firebaseAuth,
|
||||
required this.dataConnect,
|
||||
});
|
||||
TaxFormsRepositoryImpl() : _service = dc.DataConnectService.instance;
|
||||
|
||||
final auth.FirebaseAuth firebaseAuth;
|
||||
final dc.ExampleConnector dataConnect;
|
||||
|
||||
/// Helper to get the logged-in staff ID.
|
||||
String _getStaffId() {
|
||||
final auth.User? user = firebaseAuth.currentUser;
|
||||
if (user == null) {
|
||||
throw const NotAuthenticatedException(
|
||||
technicalMessage: 'Firebase User is null',
|
||||
);
|
||||
}
|
||||
|
||||
final String? staffId = dc.StaffSessionStore.instance.session?.staff?.id;
|
||||
if (staffId == null || staffId.isEmpty) {
|
||||
throw const StaffProfileNotFoundException(
|
||||
technicalMessage: 'Staff ID missing in SessionStore',
|
||||
);
|
||||
}
|
||||
return staffId;
|
||||
}
|
||||
final dc.DataConnectService _service;
|
||||
|
||||
@override
|
||||
Future<List<TaxForm>> getTaxForms() async {
|
||||
return executeProtected(() async {
|
||||
final String staffId = _getStaffId();
|
||||
final QueryResult<dc.GetTaxFormsByStaffIdData, dc.GetTaxFormsByStaffIdVariables>
|
||||
result = await dataConnect
|
||||
return _service.run(() async {
|
||||
final String staffId = await _service.getStaffId();
|
||||
final response = await _service.connector
|
||||
.getTaxFormsByStaffId(staffId: staffId)
|
||||
.execute();
|
||||
|
||||
final List<TaxForm> forms =
|
||||
result.data.taxForms.map(TaxFormMapper.fromDataConnect).toList();
|
||||
response.data.taxForms.map(TaxFormMapper.fromDataConnect).toList();
|
||||
|
||||
// Check if required forms exist, create if not.
|
||||
final Set<TaxFormType> typesPresent =
|
||||
@@ -65,11 +39,9 @@ class TaxFormsRepositoryImpl
|
||||
}
|
||||
|
||||
if (createdNew) {
|
||||
final QueryResult<
|
||||
dc.GetTaxFormsByStaffIdData,
|
||||
dc.GetTaxFormsByStaffIdVariables> result2 =
|
||||
await dataConnect.getTaxFormsByStaffId(staffId: staffId).execute();
|
||||
return result2.data.taxForms
|
||||
final response2 =
|
||||
await _service.connector.getTaxFormsByStaffId(staffId: staffId).execute();
|
||||
return response2.data.taxForms
|
||||
.map(TaxFormMapper.fromDataConnect)
|
||||
.toList();
|
||||
}
|
||||
@@ -79,7 +51,7 @@ class TaxFormsRepositoryImpl
|
||||
}
|
||||
|
||||
Future<void> _createInitialForm(String staffId, TaxFormType type) async {
|
||||
await dataConnect
|
||||
await _service.connector
|
||||
.createTaxForm(
|
||||
staffId: staffId,
|
||||
formType:
|
||||
@@ -95,10 +67,10 @@ class TaxFormsRepositoryImpl
|
||||
|
||||
@override
|
||||
Future<void> updateI9Form(I9TaxForm form) async {
|
||||
return executeProtected(() async {
|
||||
return _service.run(() async {
|
||||
final Map<String, dynamic> data = form.formData;
|
||||
final dc.UpdateTaxFormVariablesBuilder builder =
|
||||
dataConnect.updateTaxForm(id: form.id);
|
||||
_service.connector.updateTaxForm(id: form.id);
|
||||
_mapCommonFields(builder, data);
|
||||
_mapI9Fields(builder, data);
|
||||
await builder.execute();
|
||||
@@ -107,10 +79,10 @@ class TaxFormsRepositoryImpl
|
||||
|
||||
@override
|
||||
Future<void> submitI9Form(I9TaxForm form) async {
|
||||
return executeProtected(() async {
|
||||
return _service.run(() async {
|
||||
final Map<String, dynamic> data = form.formData;
|
||||
final dc.UpdateTaxFormVariablesBuilder builder =
|
||||
dataConnect.updateTaxForm(id: form.id);
|
||||
_service.connector.updateTaxForm(id: form.id);
|
||||
_mapCommonFields(builder, data);
|
||||
_mapI9Fields(builder, data);
|
||||
await builder.status(dc.TaxFormStatus.SUBMITTED).execute();
|
||||
@@ -119,10 +91,10 @@ class TaxFormsRepositoryImpl
|
||||
|
||||
@override
|
||||
Future<void> updateW4Form(W4TaxForm form) async {
|
||||
return executeProtected(() async {
|
||||
return _service.run(() async {
|
||||
final Map<String, dynamic> data = form.formData;
|
||||
final dc.UpdateTaxFormVariablesBuilder builder =
|
||||
dataConnect.updateTaxForm(id: form.id);
|
||||
_service.connector.updateTaxForm(id: form.id);
|
||||
_mapCommonFields(builder, data);
|
||||
_mapW4Fields(builder, data);
|
||||
await builder.execute();
|
||||
@@ -131,10 +103,10 @@ class TaxFormsRepositoryImpl
|
||||
|
||||
@override
|
||||
Future<void> submitW4Form(W4TaxForm form) async {
|
||||
return executeProtected(() async {
|
||||
return _service.run(() async {
|
||||
final Map<String, dynamic> data = form.formData;
|
||||
final dc.UpdateTaxFormVariablesBuilder builder =
|
||||
dataConnect.updateTaxForm(id: form.id);
|
||||
_service.connector.updateTaxForm(id: form.id);
|
||||
_mapCommonFields(builder, data);
|
||||
_mapW4Fields(builder, data);
|
||||
await builder.status(dc.TaxFormStatus.SUBMITTED).execute();
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter_modular/flutter_modular.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
import 'package:krow_data_connect/krow_data_connect.dart';
|
||||
import 'package:krow_domain/krow_domain.dart';
|
||||
import 'data/repositories/tax_forms_repository_impl.dart';
|
||||
import 'domain/repositories/tax_forms_repository.dart';
|
||||
@@ -18,12 +16,7 @@ import 'presentation/pages/tax_forms_page.dart';
|
||||
class StaffTaxFormsModule extends Module {
|
||||
@override
|
||||
void binds(Injector i) {
|
||||
i.addLazySingleton<TaxFormsRepository>(
|
||||
() => TaxFormsRepositoryImpl(
|
||||
firebaseAuth: FirebaseAuth.instance,
|
||||
dataConnect: ExampleConnector.instance,
|
||||
),
|
||||
);
|
||||
i.addLazySingleton<TaxFormsRepository>(TaxFormsRepositoryImpl.new);
|
||||
|
||||
// Use Cases
|
||||
i.addLazySingleton(GetTaxFormsUseCase.new);
|
||||
|
||||
Reference in New Issue
Block a user