@@ -43,7 +43,6 @@ class EventFormCubit extends Cubit<EventFormState> {
4343 try {
4444 emit (EventFormTicketTypesLoading ());
4545 final ticketTypes = await _eventRepository.getTicketTypesForEvent (eventId);
46-
4746 emit (EventFormTicketTypesLoaded (ticketTypes));
4847 } on ApiException catch (e) {
4948 emit (EventFormError ('Failed to load ticket types: ${e .message }' ));
@@ -65,44 +64,23 @@ class EventFormCubit extends Cubit<EventFormState> {
6564 }
6665 }
6766
67+ /// Update event details only - ticket types are managed separately
6868 Future <void > updateEventWithTicketTypes (
6969 int eventId,
7070 Map <String , dynamic > eventData,
71- List <TicketType > additionalTicketTypes
71+ List <TicketType > newTicketTypes // Only new ticket types to be created
7272 ) async {
7373 try {
7474 emit (const EventFormSubmitting (locations: []));
7575
7676 // 1. Update the event details first
7777 await _eventRepository.updateEvent (eventId, eventData);
7878
79- // 2. Get existing ticket types to compare
80- final existingTicketTypes = await _eventRepository.getTicketTypesForEvent (eventId);
81- final existingAdditionalTypes = existingTicketTypes
82- .where ((t) => (t.description ?? '' ) != "Standard Ticket" )
83- .toList ();
84-
85- // 3. Delete removed ticket types
86- for (final existing in existingAdditionalTypes) {
87- final stillExists = additionalTicketTypes.any ((t) =>
88- t.typeId != null && t.typeId == existing.typeId);
89-
90- if (! stillExists && existing.typeId != null ) {
91- await _eventRepository.deleteTicketType (existing.typeId! );
92- print ('Deleted ticket type: ${existing .description }' );
93- }
94- }
95-
96- // 4. Create or update ticket types
97- for (final ticketType in additionalTicketTypes) {
79+ // 2. Create only NEW ticket types (no deletion/updating of existing ones)
80+ for (final ticketType in newTicketTypes) {
9881 if (ticketType.typeId == null ) {
99- // Create new ticket type
10082 await _createSingleTicketType (eventId, ticketType);
10183 print ('Created new ticket type: ${ticketType .description }' );
102- } else {
103- // Update existing ticket type
104- await _updateSingleTicketType (ticketType);
105- print ('Updated ticket type: ${ticketType .description }' );
10684 }
10785 }
10886
@@ -114,31 +92,41 @@ class EventFormCubit extends Cubit<EventFormState> {
11492 }
11593 }
11694
117- Future <void > _createSingleTicketType (int eventId, TicketType ticketType) async {
118- if (ticketType.availableFrom == null ) {
119- throw Exception ('Available from date is required for ticket type: ${ticketType .description }' );
120- }
121-
122- await _eventRepository.createTicketType ({
123- 'event_id' : eventId,
124- 'description' : ticketType.description ?? '' ,
125- 'max_count' : ticketType.maxCount,
126- 'price' : ticketType.price,
127- 'currency' : ticketType.currency,
128- 'available_from' : ticketType.availableFrom! .toIso8601String (),
129- });
95+ bool canDeleteTicketType (TicketType ticketType) {
96+ if (ticketType.availableFrom == null ) return false ;
97+ return ticketType.availableFrom! .isAfter (DateTime .now ());
13098 }
13199
132- Future <void > _updateSingleTicketType (TicketType ticketType) async {
133- if (ticketType.typeId == null ) {
134- throw Exception ('Cannot update ticket type without ID' );
100+ Future <void > deleteTicketType (int typeId, TicketType ticketType) async {
101+ try {
102+ // Check if deletion is allowed
103+ if (! canDeleteTicketType (ticketType)) {
104+ emit (EventFormError (
105+ 'Cannot delete ticket type "${ticketType .description ?? '' }" - sales have already started or no availability date set.'
106+ ));
107+ return ;
108+ }
109+
110+ await _eventRepository.deleteTicketType (typeId);
111+ emit (EventFormTicketTypeDeleted ());
112+
113+ if (ticketType.eventId != null ) {
114+ await loadExistingTicketTypes (ticketType.eventId);
115+ }
116+ } on ApiException catch (e) {
117+ emit (EventFormError (e.message));
118+ } catch (e) {
119+ emit (EventFormError ('Failed to delete ticket type: $e ' ));
135120 }
121+ }
136122
123+ Future <void > _createSingleTicketType (int eventId, TicketType ticketType) async {
137124 if (ticketType.availableFrom == null ) {
138125 throw Exception ('Available from date is required for ticket type: ${ticketType .description }' );
139126 }
140127
141- await _eventRepository.updateTicketType (ticketType.typeId! , {
128+ await _eventRepository.createTicketType ({
129+ 'event_id' : eventId,
142130 'description' : ticketType.description ?? '' ,
143131 'max_count' : ticketType.maxCount,
144132 'price' : ticketType.price,
@@ -151,32 +139,12 @@ class EventFormCubit extends Cubit<EventFormState> {
151139 try {
152140 await _createSingleTicketType (eventId, ticketType);
153141 emit (EventFormTicketTypeCreated ());
154- } on ApiException catch (e) {
155- emit (EventFormError (e.message));
156- } catch (e) {
157- emit (EventFormError ('Failed to create ticket type: $e ' ));
158- }
159- }
160142
161- Future <void > updateTicketType (TicketType ticketType) async {
162- try {
163- await _updateSingleTicketType (ticketType);
164- emit (EventFormTicketTypeUpdated ());
143+ await loadExistingTicketTypes (eventId);
165144 } on ApiException catch (e) {
166145 emit (EventFormError (e.message));
167146 } catch (e) {
168- emit (EventFormError ('Failed to update ticket type: $e ' ));
169- }
170- }
171-
172- Future <void > deleteTicketType (int typeId) async {
173- try {
174- await _eventRepository.deleteTicketType (typeId);
175- emit (EventFormTicketTypeDeleted ());
176- } on ApiException catch (e) {
177- emit (EventFormError (e.message));
178- } catch (e) {
179- emit (EventFormError ('Failed to delete ticket type: $e ' ));
147+ emit (EventFormError ('Failed to create ticket type: $e ' ));
180148 }
181149 }
182150}
0 commit comments