mmmh
This commit is contained in:
@@ -8,6 +8,7 @@ import 'package:flux/features/settings/data/settings_repository.dart';
|
||||
import 'package:flux/features/tasks/data/task_repository.dart';
|
||||
import 'package:flux/features/tasks/models/task_model.dart';
|
||||
import 'package:flux/features/tasks/models/task_reminder_config.dart';
|
||||
import 'package:flux/features/tasks/models/task_status.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
part 'task_form_state.dart';
|
||||
|
||||
@@ -30,7 +31,7 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
}
|
||||
|
||||
String get _companyId => _sessionCubit.state.company!.id!;
|
||||
String get _currentUserId => _sessionCubit.state.currentStaffMember!.id!;
|
||||
StaffMemberModel get _currentUser => _sessionCubit.state.currentStaffMember!;
|
||||
String? get _currentStoreId => _sessionCubit.state.currentStore?.id;
|
||||
|
||||
// --- ARMED INITIALIZATION (Nuovo, Esistente o Deep Link) ---
|
||||
@@ -129,7 +130,7 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
try {
|
||||
final defaults = await _settingsRepository.getMyReminderDefaults(
|
||||
companyId: _companyId,
|
||||
staffId: _currentUserId,
|
||||
staffId: _currentUser.id!,
|
||||
);
|
||||
final initialReminders = defaults
|
||||
.map(
|
||||
@@ -155,7 +156,7 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
try {
|
||||
final existingConfigs = await _repository.fetchPersonalReminders(
|
||||
taskId: taskId,
|
||||
staffId: _currentUserId,
|
||||
staffId: _currentUser.id!,
|
||||
);
|
||||
emit(state.copyWith(reminders: existingConfigs));
|
||||
} catch (e) {
|
||||
@@ -218,7 +219,7 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
final taskToSave = TaskModel(
|
||||
id: state.id,
|
||||
companyId: _companyId,
|
||||
createdById: _currentUserId,
|
||||
createdBy: _currentUser,
|
||||
title: state.title.trim(),
|
||||
description: state.description.trim(),
|
||||
dueDate: state.dueDate,
|
||||
@@ -233,14 +234,14 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
await _repository.createTask(
|
||||
task: taskToSave,
|
||||
assignedStaffIds: state.selectedStaffIds,
|
||||
currentUserId: _currentUserId,
|
||||
currentUserId: _currentUser.id!,
|
||||
currentUserCustomReminders: state.reminders,
|
||||
);
|
||||
} else {
|
||||
await _repository.updateTask(
|
||||
task: taskToSave,
|
||||
assignedStaffIds: state.selectedStaffIds,
|
||||
currentUserId: _currentUserId,
|
||||
currentUserId: _currentUser.id!,
|
||||
currentUserCustomReminders: state.reminders,
|
||||
);
|
||||
}
|
||||
@@ -254,4 +255,47 @@ class TaskFormCubit extends Cubit<TaskFormState> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteTask() async {
|
||||
if (state.id == null) return;
|
||||
emit(state.copyWith(status: TaskFormStatus.submitting));
|
||||
try {
|
||||
await _repository.deleteTask(state.id!);
|
||||
emit(state.copyWith(status: TaskFormStatus.success));
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: TaskFormStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateTaskStatus(TaskStatus newStatus) async {
|
||||
try {
|
||||
// Chiamiamo il repo passando il task aggiornato
|
||||
await _repository.updateTaskStatus(
|
||||
taskId: state.id!,
|
||||
newStatus: newStatus,
|
||||
updatedById: _currentUser.id!,
|
||||
);
|
||||
|
||||
if (!isClosed) {
|
||||
// Se l'update va a buon fine, aggiorniamo lo stato locale del cubit
|
||||
emit(
|
||||
state.copyWith(status: TaskFormStatus.success, taskStatus: newStatus),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (!isClosed) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: TaskFormStatus.failure,
|
||||
errorMessage: e.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ class TaskFormState extends Equatable {
|
||||
final bool isGlobal;
|
||||
final List<String> selectedStaffIds;
|
||||
final List<TaskReminderConfig> reminders;
|
||||
final Map<String, List<StaffMemberModel>>
|
||||
groupedAvailableStaff; // <-- RIPRISTINATO
|
||||
final Map<String, List<StaffMemberModel>> groupedAvailableStaff;
|
||||
final String? errorMessage;
|
||||
final TaskStatus taskStatus;
|
||||
|
||||
const TaskFormState({
|
||||
this.id,
|
||||
@@ -26,6 +26,7 @@ class TaskFormState extends Equatable {
|
||||
this.reminders = const [],
|
||||
this.groupedAvailableStaff = const {},
|
||||
this.errorMessage,
|
||||
this.taskStatus = TaskStatus.open,
|
||||
});
|
||||
|
||||
bool get isFormValid => title.trim().isNotEmpty;
|
||||
@@ -41,6 +42,7 @@ class TaskFormState extends Equatable {
|
||||
List<TaskReminderConfig>? reminders,
|
||||
Map<String, List<StaffMemberModel>>? groupedAvailableStaff,
|
||||
String? errorMessage,
|
||||
TaskStatus? taskStatus,
|
||||
}) {
|
||||
return TaskFormState(
|
||||
id: id ?? this.id,
|
||||
@@ -54,6 +56,7 @@ class TaskFormState extends Equatable {
|
||||
groupedAvailableStaff:
|
||||
groupedAvailableStaff ?? this.groupedAvailableStaff,
|
||||
errorMessage: errorMessage,
|
||||
taskStatus: taskStatus ?? this.taskStatus,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -69,5 +72,6 @@ class TaskFormState extends Equatable {
|
||||
reminders,
|
||||
groupedAvailableStaff,
|
||||
errorMessage,
|
||||
taskStatus,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ class TasksRepository {
|
||||
}) async {
|
||||
try {
|
||||
final response = await _supabase
|
||||
.from('task_reminders')
|
||||
.from(Tables.taskReminders)
|
||||
.select()
|
||||
.eq('task_id', taskId)
|
||||
.eq('staff_id', staffId)
|
||||
@@ -53,13 +53,17 @@ class TasksRepository {
|
||||
int? limit,
|
||||
}) async {
|
||||
try {
|
||||
// 1. FASE FILTRI: Usa il join esplicito stile "Notes"
|
||||
// 1. FASE FILTRI: Disambiguazione completa su Tasks e Assignments
|
||||
var filterBuilder = _supabase
|
||||
.from(Tables.tasks)
|
||||
.select('''
|
||||
*,
|
||||
creator:${Tables.staffMembers}!created_by_id(*),
|
||||
updater:${Tables.staffMembers}!updated_by_id(*),
|
||||
task_assignments:${Tables.taskAssignments} (
|
||||
${Tables.staffMembers} (*)
|
||||
*,
|
||||
assignee:${Tables.staffMembers}!staff_id(*),
|
||||
assigner:${Tables.staffMembers}!assigned_by_id(*)
|
||||
)
|
||||
''')
|
||||
.eq('company_id', companyId);
|
||||
@@ -71,7 +75,6 @@ class TasksRepository {
|
||||
}
|
||||
|
||||
if (staffId != null) {
|
||||
// Grazie al trigger, hai l'array pronto per il filtro senza impazzire!
|
||||
filterBuilder = filterBuilder.contains('assigned_to_ids', [staffId]);
|
||||
}
|
||||
|
||||
@@ -105,8 +108,12 @@ class TasksRepository {
|
||||
.from(Tables.tasks)
|
||||
.select('''
|
||||
*,
|
||||
creator:${Tables.staffMembers}!created_by_id(*),
|
||||
updater:${Tables.staffMembers}!updated_by_id(*),
|
||||
task_assignments:${Tables.taskAssignments} (
|
||||
${Tables.staffMembers} (*)
|
||||
*,
|
||||
assignee:${Tables.staffMembers}!staff_id(*),
|
||||
assigner:${Tables.staffMembers}!assigned_by_id(*)
|
||||
)
|
||||
''')
|
||||
.eq('id', taskId)
|
||||
@@ -122,14 +129,11 @@ class TasksRepository {
|
||||
// =========================================================================
|
||||
// REALTIME STREAM (La sentinella per la bacheca)
|
||||
// =========================================================================
|
||||
Stream<List<TaskModel>> watchCompanyTasks(String companyId) {
|
||||
Stream<List<Map<String, dynamic>>> watchCompanyTasks(String companyId) {
|
||||
return _supabase
|
||||
.from('tasks')
|
||||
.stream(primaryKey: ['id'])
|
||||
.eq('company_id', companyId)
|
||||
.map((listOfMaps) {
|
||||
return listOfMaps.map((map) => TaskModel.fromMap(map)).toList();
|
||||
});
|
||||
.eq('company_id', companyId);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
@@ -160,6 +164,7 @@ class TasksRepository {
|
||||
'task_id': taskId,
|
||||
'staff_id': staffId,
|
||||
'company_id': task.companyId,
|
||||
'assigned_by_id': currentUserId,
|
||||
},
|
||||
)
|
||||
.toList();
|
||||
@@ -276,7 +281,7 @@ class TasksRepository {
|
||||
|
||||
// 1. Aggiornamento dati Task Base
|
||||
await _supabase
|
||||
.from('tasks')
|
||||
.from(Tables.tasks)
|
||||
.update({
|
||||
'title': task.title,
|
||||
'description': task.description,
|
||||
@@ -286,15 +291,51 @@ class TasksRepository {
|
||||
})
|
||||
.eq('id', taskId);
|
||||
|
||||
// 2. Aggiornamento Assegnazioni: eliminiamo le vecchie, inseriamo le nuove
|
||||
await _supabase.from('task_assignments').delete().eq('task_id', taskId);
|
||||
if (assignedStaffIds.isNotEmpty) {
|
||||
final assignmentsToInsert = assignedStaffIds
|
||||
// 🥷 2. GESTIONE CHIRURGICA DELLE ASSEGNAZIONI (Addio spam!)
|
||||
|
||||
// A) Recuperiamo chi è GIÀ assegnato a questo task
|
||||
final existingAssignmentsResponse = await _supabase
|
||||
.from('task_assignments')
|
||||
.select('staff_id')
|
||||
.eq('task_id', taskId);
|
||||
|
||||
final List<String> existingStaffIds =
|
||||
(existingAssignmentsResponse as List)
|
||||
.map((row) => row['staff_id'] as String)
|
||||
.toList();
|
||||
|
||||
// B) Calcoliamo i Delta con i Set di Dart (Pura magia matematica)
|
||||
final newStaffIdsSet = assignedStaffIds.toSet();
|
||||
final existingStaffIdsSet = existingStaffIds.toSet();
|
||||
|
||||
// Quelli da inserire (presenti nei nuovi, ma non nei vecchi)
|
||||
final toInsertIds = newStaffIdsSet
|
||||
.difference(existingStaffIdsSet)
|
||||
.toList();
|
||||
|
||||
// Quelli da eliminare (presenti nei vecchi, ma non nei nuovi)
|
||||
final toDeleteIds = existingStaffIdsSet
|
||||
.difference(newStaffIdsSet)
|
||||
.toList();
|
||||
|
||||
// C) Eseguiamo solo lo stretto necessario
|
||||
if (toDeleteIds.isNotEmpty) {
|
||||
await _supabase
|
||||
.from('task_assignments')
|
||||
.delete()
|
||||
.eq('task_id', taskId)
|
||||
.inFilter('staff_id', toDeleteIds);
|
||||
}
|
||||
|
||||
if (toInsertIds.isNotEmpty) {
|
||||
final assignmentsToInsert = toInsertIds
|
||||
.map(
|
||||
(staffId) => {
|
||||
'task_id': taskId,
|
||||
'staff_id': staffId,
|
||||
'company_id': task.companyId,
|
||||
'assigned_by_id':
|
||||
currentUserId, // Il nostro salvavita anti-fantasma
|
||||
},
|
||||
)
|
||||
.toList();
|
||||
@@ -352,6 +393,35 @@ class TasksRepository {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateTaskStatus({
|
||||
required String taskId,
|
||||
required TaskStatus newStatus,
|
||||
required String? updatedById,
|
||||
}) async {
|
||||
try {
|
||||
await _supabase
|
||||
.from(Tables.tasks)
|
||||
.update({
|
||||
'status': newStatus.toValue,
|
||||
'updated_by_id': updatedById,
|
||||
'updated_at': DateTime.now().toIso8601String(),
|
||||
})
|
||||
.eq('id', taskId);
|
||||
} catch (e) {
|
||||
throw Exception(
|
||||
'Errore durante l\'aggiornamento dello stato del task: $e',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteTask(String taskId) async {
|
||||
try {
|
||||
await _supabase.from(Tables.tasks).delete().eq('id', taskId);
|
||||
} catch (e) {
|
||||
throw Exception('Errore durante la cancellazione del task: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// --- HELPER PRIVATO PER LA MAPPA DEL REMINDER ---
|
||||
Map<String, dynamic> _buildReminderRow(
|
||||
TaskModel task,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flux/core/utils/extensions.dart';
|
||||
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
||||
import 'package:flux/features/tasks/models/task_status.dart';
|
||||
|
||||
@@ -9,11 +10,12 @@ class TaskModel extends Equatable {
|
||||
final String? description;
|
||||
final List<String> assignedToIds;
|
||||
final List<StaffMemberModel> assignedToStaff; // I dati completi dal JOIN
|
||||
final String? createdById;
|
||||
final StaffMemberModel? createdBy;
|
||||
final DateTime? dueDate;
|
||||
final TaskStatus status;
|
||||
final DateTime? createdAt;
|
||||
final String? storeId;
|
||||
final StaffMemberModel? updatedBy;
|
||||
|
||||
const TaskModel({
|
||||
this.id,
|
||||
@@ -22,24 +24,25 @@ class TaskModel extends Equatable {
|
||||
this.description,
|
||||
this.assignedToIds = const [],
|
||||
this.assignedToStaff = const [],
|
||||
this.createdById,
|
||||
this.createdBy,
|
||||
this.dueDate,
|
||||
this.status = TaskStatus.open,
|
||||
this.createdAt,
|
||||
this.storeId,
|
||||
this.updatedBy,
|
||||
});
|
||||
|
||||
bool get isGlobal => storeId == null;
|
||||
|
||||
// --- FACTORY: MODELLO VUOTO (Per le creazioni) ---
|
||||
factory TaskModel.empty({String? companyId, String? createdById}) {
|
||||
factory TaskModel.empty({String? companyId, StaffMemberModel? createdBy}) {
|
||||
return TaskModel(
|
||||
companyId: companyId,
|
||||
title: '',
|
||||
description: '',
|
||||
assignedToIds: const [],
|
||||
assignedToStaff: const [],
|
||||
createdById: createdById,
|
||||
createdBy: createdBy,
|
||||
status: TaskStatus.open,
|
||||
createdAt: DateTime.now(),
|
||||
);
|
||||
@@ -54,11 +57,12 @@ class TaskModel extends Equatable {
|
||||
description,
|
||||
assignedToIds,
|
||||
assignedToStaff,
|
||||
createdById,
|
||||
createdBy,
|
||||
dueDate,
|
||||
status,
|
||||
createdAt,
|
||||
storeId,
|
||||
updatedBy,
|
||||
];
|
||||
|
||||
// --- COPY WITH ---
|
||||
@@ -69,13 +73,15 @@ class TaskModel extends Equatable {
|
||||
String? description,
|
||||
List<String>? assignedToIds,
|
||||
List<StaffMemberModel>? assignedToStaff,
|
||||
String? createdById,
|
||||
StaffMemberModel? createdBy,
|
||||
DateTime? dueDate,
|
||||
bool clearDueDate = false, // Flag ninja per resettare la scadenza
|
||||
TaskStatus? status,
|
||||
DateTime? createdAt,
|
||||
String? storeId,
|
||||
bool clearStoreId = false,
|
||||
StaffMemberModel? updatedBy,
|
||||
String? updatedByDisplayName,
|
||||
}) {
|
||||
return TaskModel(
|
||||
id: id ?? this.id,
|
||||
@@ -84,11 +90,12 @@ class TaskModel extends Equatable {
|
||||
description: description ?? this.description,
|
||||
assignedToIds: assignedToIds ?? this.assignedToIds,
|
||||
assignedToStaff: assignedToStaff ?? this.assignedToStaff,
|
||||
createdById: createdById ?? this.createdById,
|
||||
createdBy: createdBy ?? this.createdBy,
|
||||
dueDate: clearDueDate ? null : (dueDate ?? this.dueDate),
|
||||
status: status ?? this.status,
|
||||
createdAt: createdAt ?? this.createdAt,
|
||||
storeId: clearStoreId ? null : (storeId ?? this.storeId),
|
||||
updatedBy: updatedBy ?? this.updatedBy,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -124,7 +131,9 @@ class TaskModel extends Equatable {
|
||||
description: map['description'] as String?,
|
||||
assignedToIds: parsedAssignedToIds,
|
||||
assignedToStaff: staffList,
|
||||
createdById: map['created_by_id'] as String?,
|
||||
createdBy: map['created_by_id'] != null
|
||||
? StaffMemberModel.fromMap(map['creator'])
|
||||
: null,
|
||||
dueDate: map['due_date'] != null
|
||||
? DateTime.parse(map['due_date'] as String).toLocal()
|
||||
: null,
|
||||
@@ -133,6 +142,9 @@ class TaskModel extends Equatable {
|
||||
? DateTime.parse(map['created_at'] as String).toLocal()
|
||||
: null,
|
||||
storeId: map['store_id'] as String?,
|
||||
updatedBy: map['updated_by_id'] != null
|
||||
? StaffMemberModel.fromMap(map['updater'])
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -145,10 +157,11 @@ class TaskModel extends Equatable {
|
||||
if (description != null) 'description': description,
|
||||
// Passiamo l'array vuoto se non ci sono assegnazioni
|
||||
'assigned_to_ids': assignedToIds.isEmpty ? null : assignedToIds,
|
||||
if (createdById != null) 'created_by_id': createdById,
|
||||
if (createdBy != null) 'created_by_id': createdBy!.id,
|
||||
'due_date': dueDate?.toUtc().toIso8601String(),
|
||||
'status': status.toValue,
|
||||
'store_id': storeId,
|
||||
if (updatedBy != null) 'updated_by_id': updatedBy!.id,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/features/tasks/blocs/task_form_cubit.dart';
|
||||
import 'package:flux/features/tasks/models/task_status.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class TaskFormScreen extends StatefulWidget {
|
||||
@@ -182,6 +183,43 @@ class _TaskFormScreenState extends State<TaskFormScreen> {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
if (state.id != null &&
|
||||
state.taskStatus != TaskStatus.completed)
|
||||
Container(
|
||||
margin: const EdgeInsets.only(bottom: 24.0),
|
||||
width: double.infinity,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
// Chiama direttamente l'update immediato nel DB!
|
||||
context
|
||||
.read<TaskFormCubit>()
|
||||
.updateTaskStatus(TaskStatus.completed);
|
||||
},
|
||||
icon: const Icon(
|
||||
Icons.check_circle_outline,
|
||||
size: 28,
|
||||
),
|
||||
label: const Text(
|
||||
'Segna come Completato',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.green.shade600,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
vertical: 16,
|
||||
),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
elevation: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
const Divider(height: 30),
|
||||
_buildFormFields(context, state, cubit),
|
||||
const SizedBox(height: 30),
|
||||
ElevatedButton.icon(
|
||||
|
||||
Reference in New Issue
Block a user