uff
This commit is contained in:
@@ -1,19 +1,28 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/blocs/session/session_cubit.dart';
|
||||
import 'package:flux/features/customers/models/customer_model.dart';
|
||||
import 'package:flux/features/master_data/staff/models/staff_member_model.dart';
|
||||
import 'package:flux/features/tickets/models/ticket_model.dart';
|
||||
import 'package:flux/features/tickets/data/ticket_repository.dart';
|
||||
import 'package:flux/features/tracking/data/tracking_repository.dart';
|
||||
import 'package:flux/features/tracking/models/tracking_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'ticket_form_state.dart';
|
||||
|
||||
class TicketFormCubit extends Cubit<TicketFormState> {
|
||||
final TicketRepository _repository = GetIt.I.get<TicketRepository>();
|
||||
final SessionCubit _sessionCubit = GetIt.I.get<SessionCubit>();
|
||||
final StaffMemberModel? _createdBy;
|
||||
|
||||
TicketFormCubit()
|
||||
TicketFormCubit({StaffMemberModel? createdBy})
|
||||
: super(
|
||||
// Inizializziamo con un ticket vuoto di default
|
||||
TicketFormState(ticket: TicketModel.empty()),
|
||||
TicketFormState(
|
||||
ticket: TicketModel.empty().copyWith(
|
||||
createdById: createdBy?.id,
|
||||
createdByName: createdBy?.name,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
/// 1. INIZIALIZZAZIONE (Se stiamo modificando un ticket esistente)
|
||||
@@ -48,15 +57,14 @@ class TicketFormCubit extends Cubit<TicketFormState> {
|
||||
} else {
|
||||
// SCENARIO 3 (Nuovo Ticket):
|
||||
// È un nuovo ticket! Inseriamo i default base (Azienda, Negozio, Creatore)
|
||||
final currentUser = _sessionCubit.state.currentStaffMember;
|
||||
final currentStore = _sessionCubit.state.currentStore;
|
||||
final companyId = _sessionCubit.state.company?.id ?? '';
|
||||
|
||||
final newTicket = TicketModel.empty().copyWith(
|
||||
companyId: companyId,
|
||||
storeId: currentStore?.id,
|
||||
createdById: currentUser?.id,
|
||||
createdByName: currentUser?.name,
|
||||
createdById: createdBy.id,
|
||||
createdByName: _createdBy.name,
|
||||
// Impostiamo lo stato iniziale
|
||||
ticketStatus: TicketStatus.open,
|
||||
ticketType: TicketType.repair, // Default
|
||||
@@ -219,4 +227,51 @@ class TicketFormCubit extends Cubit<TicketFormState> {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> takeInCharge({
|
||||
required String staffId,
|
||||
required String staffName,
|
||||
}) async {
|
||||
final currentTicket = state.ticket;
|
||||
|
||||
// Sicurezza: non possiamo prendere in carico un ticket fantasma
|
||||
if (currentTicket.id == null || currentTicket.id!.isEmpty) return;
|
||||
|
||||
// 1. Prepariamo il ticket aggiornato
|
||||
final updatedTicket = currentTicket.copyWith(
|
||||
ticketStatus: TicketStatus
|
||||
.inProgress, // Assumendo che tu abbia un enum per gli stati
|
||||
assignedToId: staffId,
|
||||
assignedToName: staffName,
|
||||
);
|
||||
|
||||
try {
|
||||
// 2. Aggiorniamo il ticket sul Database (usa il tuo metodo esistente del repo)
|
||||
await _repository.updateTicket(updatedTicket);
|
||||
|
||||
// 3. Spara il log automatico nella Timeline!
|
||||
await GetIt.I.get<TrackingRepository>().logQuickEvent(
|
||||
companyId: currentTicket.companyId,
|
||||
message: "Ticket preso in carico. Inizio lavorazione.",
|
||||
type: TrackingType.statusChange,
|
||||
parentId: currentTicket.id!,
|
||||
parentType: TrackingParentType.ticket,
|
||||
staffId: staffId,
|
||||
// Lo mettiamo pubblico (isInternal: false) così il cliente a casa vede che
|
||||
// il suo dispositivo è ufficialmente sotto i ferri!
|
||||
isInternal: false,
|
||||
);
|
||||
|
||||
// 4. Aggiorniamo lo stato locale del Cubit per far scattare la UI
|
||||
emit(state.copyWith(ticket: updatedTicket));
|
||||
} catch (e) {
|
||||
// Gestisci eventuali errori (es. mostrando una snackbar)
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: TicketFormStatus.failure,
|
||||
errorMessage: 'Errore durante la presa in carico: $e',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user