Refactor code structure for improved readability and maintainability

This commit is contained in:
Achintha Isuru
2026-01-28 12:56:35 -05:00
parent f2382fba3d
commit 710421a832
10 changed files with 17786 additions and 17732 deletions

View File

@@ -33,11 +33,11 @@ class TaxFormsRepositoryImpl implements TaxFormsRepository {
@override
Future<List<TaxForm>> getTaxForms() async {
final String staffId = _getStaffId();
final QueryResult<dc.GetTaxFormsBystaffIdData, dc.GetTaxFormsBystaffIdVariables>
final QueryResult<dc.GetTaxFormsByStaffIdData, dc.GetTaxFormsByStaffIdVariables>
result =
await dataConnect.getTaxFormsBystaffId(staffId: staffId).execute();
await dataConnect.getTaxFormsByStaffId(staffId: staffId).execute();
final List<TaxForm> forms = result.data.taxForms.map((dc.GetTaxFormsBystaffIdTaxForms e) => _mapToEntity(e)).toList();
final List<TaxForm> forms = result.data.taxForms.map((dc.GetTaxFormsByStaffIdTaxForms e) => _mapToEntity(e)).toList();
// Check if required forms exist, create if not.
final Set<TaxFormType> typesPresent = forms.map((TaxForm f) => f.type).toSet();
@@ -53,21 +53,142 @@ class TaxFormsRepositoryImpl implements TaxFormsRepository {
}
if (createdNew) {
final QueryResult<dc.GetTaxFormsBystaffIdData, dc.GetTaxFormsBystaffIdVariables>
final QueryResult<dc.GetTaxFormsByStaffIdData, dc.GetTaxFormsByStaffIdVariables>
result2 =
await dataConnect.getTaxFormsBystaffId(staffId: staffId).execute();
return result2.data.taxForms.map((dc.GetTaxFormsBystaffIdTaxForms e) => _mapToEntity(e)).toList();
await dataConnect.getTaxFormsByStaffId(staffId: staffId).execute();
return result2.data.taxForms.map((dc.GetTaxFormsByStaffIdTaxForms e) => _mapToEntity(e)).toList();
}
return forms;
}
Future<void> _createInitialForm(String staffId, TaxFormType type) async {
await dataConnect
.createTaxForm(
staffId: staffId,
formType:
dc.TaxFormType.values.byName(TaxFormAdapter.typeToString(type)),
firstName: '',
lastName: '',
socialSN: 0,
address: '',
status: dc.TaxFormStatus.NOT_STARTED,
)
.execute();
}
@override
Future<void> submitForm(TaxFormType type, Map<String, dynamic> data) async {
final String staffId = _getStaffId();
final QueryResult<dc.GetTaxFormsByStaffIdData, dc.GetTaxFormsByStaffIdVariables>
result =
await dataConnect.getTaxFormsByStaffId(staffId: staffId).execute();
final String targetTypeString = TaxFormAdapter.typeToString(type);
final dc.GetTaxFormsByStaffIdTaxForms form =
result.data.taxForms.firstWhere(
(dc.GetTaxFormsByStaffIdTaxForms e) =>
e.formType.stringValue == targetTypeString,
orElse: () => throw Exception('Form not found for submission'),
);
final builder = dataConnect.updateTaxForm(id: form.id);
// Map input fields to DataConnect variables
if (data.containsKey('firstName')) {
builder.firstName(data['firstName'] as String);
}
if (data.containsKey('lastName')) {
builder.lastName(data['lastName'] as String);
}
if (data.containsKey('middleInitial')) {
builder.mInitial(data['middleInitial'] as String);
}
if (data.containsKey('otherLastNames')) {
builder.oLastName(data['otherLastNames'] as String);
}
if (data.containsKey('ssn') && data['ssn'] != null) {
builder.socialSN(int.tryParse(data['ssn'].toString()) ?? 0);
}
if (data.containsKey('email')) {
builder.email(data['email'] as String);
}
if (data.containsKey('phone')) {
builder.phone(data['phone'] as String);
}
if (data.containsKey('address')) {
builder.address(data['address'] as String);
}
if (data.containsKey('aptNumber')) {
builder.apt(data['aptNumber'] as String);
}
if (data.containsKey('city')) {
builder.city(data['city'] as String);
}
if (data.containsKey('state')) {
builder.state(data['state'] as String);
}
if (data.containsKey('zipCode')) {
builder.zipCode(data['zipCode'] as String);
}
// Citizenship / Marital / Bool fields would go here.
// For now, mapping the core identity fields visible in the form logic.
// Assuming UI keys match these:
if (data.containsKey('citizenshipStatus')) {
// Need mapping for enum
}
await builder.status(dc.TaxFormStatus.SUBMITTED).execute();
}
@override
Future<void> updateFormStatus(TaxFormType type, TaxFormStatus status) async {
final String staffId = _getStaffId();
final QueryResult<dc.GetTaxFormsByStaffIdData, dc.GetTaxFormsByStaffIdVariables>
result =
await dataConnect.getTaxFormsByStaffId(staffId: staffId).execute();
final String targetTypeString = TaxFormAdapter.typeToString(type);
final dc.GetTaxFormsByStaffIdTaxForms form =
result.data.taxForms.firstWhere(
(dc.GetTaxFormsByStaffIdTaxForms e) =>
e.formType.stringValue == targetTypeString,
orElse: () => throw Exception('Form not found for update'),
);
await dataConnect
.updateTaxForm(
id: form.id,
)
.status(dc.TaxFormStatus.values
.byName(TaxFormAdapter.statusToString(status)))
.execute();
}
TaxForm _mapToEntity(dc.GetTaxFormsByStaffIdTaxForms form) {
// Construct the legacy map for the entity
final Map<String, dynamic> formData = {
'firstName': form.firstName,
'lastName': form.lastName,
'middleInitial': form.mInitial,
'otherLastNames': form.oLastName,
'ssn': form.socialSN.toString(),
'email': form.email,
'phone': form.phone,
'address': form.address,
'aptNumber': form.apt,
'city': form.city,
'state': form.state,
'zipCode': form.zipCode,
// Add other fields as they become available in the UI
};
String title = '';
String subtitle = '';
String description = '';
if (type == TaxFormType.i9) {
if (form.formType == dc.TaxFormType.I9) {
title = 'Form I-9';
subtitle = 'Employment Eligibility Verification';
description = 'Required for all new hires to verify identity.';
@@ -77,72 +198,15 @@ class TaxFormsRepositoryImpl implements TaxFormsRepository {
description = 'Determines federal income tax withholding.';
}
await dataConnect
.createTaxForm(
staffId: staffId,
formType: dc.TaxFormType.values.byName(TaxFormAdapter.typeToString(type)),
title: title,
)
.subtitle(subtitle)
.description(description)
.status(dc.TaxFormStatus.NOT_STARTED)
.execute();
}
@override
Future<void> submitForm(TaxFormType type, Map<String, dynamic> data) async {
final String staffId = _getStaffId();
final QueryResult<dc.GetTaxFormsBystaffIdData, dc.GetTaxFormsBystaffIdVariables>
result =
await dataConnect.getTaxFormsBystaffId(staffId: staffId).execute();
final String targetTypeString = TaxFormAdapter.typeToString(type);
final dc.GetTaxFormsBystaffIdTaxForms form = result.data.taxForms.firstWhere(
(dc.GetTaxFormsBystaffIdTaxForms e) => e.formType.stringValue == targetTypeString,
orElse: () => throw Exception('Form not found for submission'),
);
// AnyValue expects a scalar, list, or map.
await dataConnect
.updateTaxForm(
id: form.id,
)
.formData(AnyValue.fromJson(data))
.status(dc.TaxFormStatus.SUBMITTED)
.execute();
}
@override
Future<void> updateFormStatus(TaxFormType type, TaxFormStatus status) async {
final String staffId = _getStaffId();
final QueryResult<dc.GetTaxFormsBystaffIdData, dc.GetTaxFormsBystaffIdVariables>
result =
await dataConnect.getTaxFormsBystaffId(staffId: staffId).execute();
final String targetTypeString = TaxFormAdapter.typeToString(type);
final dc.GetTaxFormsBystaffIdTaxForms form = result.data.taxForms.firstWhere(
(dc.GetTaxFormsBystaffIdTaxForms e) => e.formType.stringValue == targetTypeString,
orElse: () => throw Exception('Form not found for update'),
);
await dataConnect
.updateTaxForm(
id: form.id,
)
.status(dc.TaxFormStatus.values.byName(TaxFormAdapter.statusToString(status)))
.execute();
}
TaxForm _mapToEntity(dc.GetTaxFormsBystaffIdTaxForms form) {
return TaxFormAdapter.fromPrimitives(
id: form.id,
type: form.formType.stringValue,
title: form.title,
subtitle: form.subtitle,
description: form.description,
title: title,
subtitle: subtitle,
description: description,
status: form.status.stringValue,
staffId: form.staffId,
formData: form.formData, // Adapter expects dynamic
formData: formData,
updatedAt: form.updatedAt?.toDateTime(),
);
}