From c85f4b086eb9697829ff5e65699c1677748be7bd Mon Sep 17 00:00:00 2001 From: Mark M2 Macbook Date: Wed, 20 May 2026 11:03:33 +0200 Subject: [PATCH] refactor nomi tabelle --- lib/core/data/constants.dart | 2 - lib/core/data/core_repository.dart | 21 +++--- lib/core/enums_and_consts/consts.dart | 24 +++++++ .../{enums => enums_and_consts}/enums.dart | 0 lib/core/theme/bloc/theme_bloc.dart | 2 +- .../attachments/blocs/attachments_bloc.dart | 4 ++ .../attachments/blocs/attachments_state.dart | 3 +- .../data/attachments_repository.dart | 6 +- .../attachments/models/attachment_model.dart | 7 ++ lib/features/auth/bloc/auth_cubit.dart | 2 +- .../company/data/company_repository.dart | 7 +- .../customers/data/customer_repository.dart | 30 +++++---- .../products/data/product_repository.dart | 13 ++-- .../master_data/products/ui/models_list.dart | 10 ++- .../providers/data/provider_repository.dart | 24 ++++--- .../staff/data/staff_repository.dart | 25 +++---- .../store/data/store_repository.dart | 31 +++++---- .../blocs/operation_form_cubit.dart | 17 ++++- .../data/operations_repository.dart | 63 +++++++++-------- .../data/document_sequence_repository.dart | 7 +- lib/features/settings/settings.dart | 2 +- .../settings/theme_settings_view.dart | 2 +- .../tickets/data/ticket_repository.dart | 67 ++++++------------- .../tracking/data/tracking_repository.dart | 7 +- 24 files changed, 217 insertions(+), 159 deletions(-) delete mode 100644 lib/core/data/constants.dart create mode 100644 lib/core/enums_and_consts/consts.dart rename lib/core/{enums => enums_and_consts}/enums.dart (100%) diff --git a/lib/core/data/constants.dart b/lib/core/data/constants.dart deleted file mode 100644 index 316b798..0000000 --- a/lib/core/data/constants.dart +++ /dev/null @@ -1,2 +0,0 @@ -const String resetPasswordUrl = - 'https://flux-web-invite.marco-6ba.workers.dev/'; diff --git a/lib/core/data/core_repository.dart b/lib/core/data/core_repository.dart index 2998a5c..9318a88 100644 --- a/lib/core/data/core_repository.dart +++ b/lib/core/data/core_repository.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart'; import 'package:flux/core/blocs/session/session_cubit.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/features/company/models/company_model.dart'; import 'package:flux/features/master_data/store/models/store_model.dart'; import 'package:flux/features/master_data/staff/models/staff_member_model.dart'; @@ -15,7 +16,7 @@ class CoreRepository { Future getCompanyByOwnerId(String userId) async { try { final response = await _supabase - .from('company') + .from(Tables.companies) .select() .eq('user_id', userId) // <-- Assicurati di avere questo campo nel DB! .maybeSingle(); @@ -31,7 +32,7 @@ class CoreRepository { Future getCompanyById(String companyId) async { try { final response = await _supabase - .from('company') + .from(Tables.companies) .select() .eq('id', companyId) .maybeSingle(); @@ -46,7 +47,7 @@ class CoreRepository { Future> getStoresByCompanyId(String companyId) async { try { final response = await _supabase - .from('store') + .from(Tables.stores) .select() .eq('company_id', companyId) .eq('is_active', true) // Buona pratica @@ -62,7 +63,7 @@ class CoreRepository { Future getStaffMemberByUserId(String userId) async { try { final response = await _supabase - .from('staff_member') + .from(Tables.staffMembers) .select() .eq('user_id', userId) .maybeSingle(); @@ -80,7 +81,7 @@ class CoreRepository { Future createCompany(CompanyModel company) async { try { final response = await _supabase - .from('company') + .from(Tables.companies) .insert(company.toMap()) .select() .single(); @@ -94,7 +95,7 @@ class CoreRepository { Future createStore(StoreModel store) async { try { final response = await _supabase - .from('store') + .from(Tables.stores) .insert(store.toMap()) .select() .single(); @@ -108,12 +109,12 @@ class CoreRepository { Future createStaffMember(StaffMemberModel staff) async { try { final response = await _supabase - .from('staff_member') + .from(Tables.staffMembers) .insert(staff.toMap()) .select() .single(); final StaffMemberModel staffMember = StaffMemberModel.fromMap(response); - await _supabase.from('staff_in_stores').insert({ + await _supabase.from(Tables.staffInStores).insert({ 'staff_member_id': staffMember.id, 'store_id': GetIt.I.get().state.currentStore!.id, }); @@ -126,7 +127,7 @@ class CoreRepository { // Assegna un membro a un negozio Future assignStaffToStore(String staffId, String storeId) async { - await _supabase.from('staff_in_stores').insert({ + await _supabase.from(Tables.staffInStores).insert({ 'staff_member_id': staffId, 'store_id': storeId, }); @@ -136,6 +137,6 @@ class CoreRepository { String staffId, Map data, ) async { - await _supabase.from('staff_member').update(data).eq('id', staffId); + await _supabase.from(Tables.staffMembers).update(data).eq('id', staffId); } } diff --git a/lib/core/enums_and_consts/consts.dart b/lib/core/enums_and_consts/consts.dart new file mode 100644 index 0000000..4f61c4f --- /dev/null +++ b/lib/core/enums_and_consts/consts.dart @@ -0,0 +1,24 @@ +class Tables { + static const String attachments = 'attachments'; + static const String brands = 'brands'; + static const String campaigns = 'campaigns'; + static const String companies = 'companies'; + static const String customers = 'customers'; + static const String documentSequences = 'document_sequences'; + static const String models = 'models'; + static const String notes = 'notes'; + static const String noteCollaborators = 'note_collaborators'; + static const String operations = 'operations'; + static const String providerLocations = 'provider_locations'; + static const String providers = 'providers'; + static const String providersInStores = 'providers_in_stores'; + static const String shippingDocuments = 'shipping_documents'; + static const String staffInStores = 'staff_in_stores'; + static const String staffMembers = 'staff_members'; + static const String stores = 'stores'; + static const String tickets = 'tickets'; + static const String trackings = 'trackings'; +} + +const String resetPasswordUrl = + 'https://flux-web-invite.marco-6ba.workers.dev/'; diff --git a/lib/core/enums/enums.dart b/lib/core/enums_and_consts/enums.dart similarity index 100% rename from lib/core/enums/enums.dart rename to lib/core/enums_and_consts/enums.dart diff --git a/lib/core/theme/bloc/theme_bloc.dart b/lib/core/theme/bloc/theme_bloc.dart index 6aac76a..c935d64 100644 --- a/lib/core/theme/bloc/theme_bloc.dart +++ b/lib/core/theme/bloc/theme_bloc.dart @@ -1,6 +1,6 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:equatable/equatable.dart'; -import 'package:flux/core/enums/enums.dart'; +import 'package:flux/core/enums_and_consts/enums.dart'; import 'package:get_it/get_it.dart'; import 'package:shared_preferences/shared_preferences.dart'; diff --git a/lib/features/attachments/blocs/attachments_bloc.dart b/lib/features/attachments/blocs/attachments_bloc.dart index d081739..d49e099 100644 --- a/lib/features/attachments/blocs/attachments_bloc.dart +++ b/lib/features/attachments/blocs/attachments_bloc.dart @@ -306,6 +306,8 @@ class AttachmentsBloc extends Bloc { return file.copyWith(operationId: event.targetId); case AttachmentParentType.shippingDocument: return file.copyWith(shippingDocumentId: event.targetId); + case AttachmentParentType.note: + return file.copyWith(noteId: event.targetId); } } return file; @@ -405,6 +407,8 @@ class AttachmentsBloc extends Bloc { return Bucket.documents; case AttachmentParentType.shippingDocument: return Bucket.companyDocuments; + case AttachmentParentType.note: + return Bucket.documents; } } } diff --git a/lib/features/attachments/blocs/attachments_state.dart b/lib/features/attachments/blocs/attachments_state.dart index b62115c..16e9474 100644 --- a/lib/features/attachments/blocs/attachments_state.dart +++ b/lib/features/attachments/blocs/attachments_state.dart @@ -6,7 +6,8 @@ enum AttachmentParentType { operation('operation_id'), ticket('ticket_id'), customer('customer_id'), - shippingDocument('shipping_document_id'); + shippingDocument('shipping_document_id'), + note('note_id'); final String dbColumn; const AttachmentParentType(this.dbColumn); diff --git a/lib/features/attachments/data/attachments_repository.dart b/lib/features/attachments/data/attachments_repository.dart index 7391463..8c205cd 100644 --- a/lib/features/attachments/data/attachments_repository.dart +++ b/lib/features/attachments/data/attachments_repository.dart @@ -1,5 +1,6 @@ import 'dart:typed_data'; import 'package:file_picker/file_picker.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/features/attachments/models/attachment_model.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import 'package:flux/features/attachments/blocs/attachments_bloc.dart'; @@ -14,8 +15,7 @@ enum Bucket { class AttachmentsRepository { final _supabase = Supabase.instance.client; - static const String _tableName = - 'attachment'; // Cambia col vero nome della tua tabella se diverso! + static const String _tableName = Tables.attachments; /// Scarica i byte di un file direttamente da Supabase Storage Future downloadAttachmentBytes({ @@ -43,6 +43,8 @@ class AttachmentsRepository { return 'customer_id'; case AttachmentParentType.shippingDocument: return 'shipping_document_id'; + case AttachmentParentType.note: + return 'note_id'; } } diff --git a/lib/features/attachments/models/attachment_model.dart b/lib/features/attachments/models/attachment_model.dart index 35de452..b46f3c7 100644 --- a/lib/features/attachments/models/attachment_model.dart +++ b/lib/features/attachments/models/attachment_model.dart @@ -9,6 +9,7 @@ class AttachmentModel extends Equatable { final String? operationId; final String? ticketId; final String? shippingDocumentId; + final String? noteId; final String name; final String extension; final String? storagePath; @@ -23,6 +24,7 @@ class AttachmentModel extends Equatable { this.operationId, this.ticketId, this.shippingDocumentId, + this.noteId, required this.name, required this.extension, this.storagePath, @@ -39,6 +41,7 @@ class AttachmentModel extends Equatable { operationId, ticketId, shippingDocumentId, + noteId, name, extension, storagePath, @@ -67,6 +70,7 @@ class AttachmentModel extends Equatable { String? operationId, String? ticketId, String? shippingDocumentId, + String? noteId, String? name, String? extension, String? storagePath, @@ -80,6 +84,7 @@ class AttachmentModel extends Equatable { operationId: operationId ?? this.operationId, ticketId: ticketId ?? this.ticketId, shippingDocumentId: shippingDocumentId ?? this.shippingDocumentId, + noteId: noteId ?? this.noteId, name: name ?? this.name, extension: extension ?? this.extension, storagePath: storagePath ?? this.storagePath, @@ -98,6 +103,7 @@ class AttachmentModel extends Equatable { operationId: map['operation_id'] as String?, ticketId: map['ticket_id'] as String?, shippingDocumentId: map['shipping_document_id'] as String?, + noteId: map['note_id'] as String?, name: map['name'] as String, extension: map['extension'] as String, storagePath: map['storage_path'] as String?, @@ -118,6 +124,7 @@ class AttachmentModel extends Equatable { 'operation_id': operationId, 'ticket_id': ticketId, 'shipping_document_id': shippingDocumentId, + 'note_id': noteId, 'file_size': fileSize, 'company_id': companyId, }; diff --git a/lib/features/auth/bloc/auth_cubit.dart b/lib/features/auth/bloc/auth_cubit.dart index b4faf4a..03f06fe 100644 --- a/lib/features/auth/bloc/auth_cubit.dart +++ b/lib/features/auth/bloc/auth_cubit.dart @@ -1,7 +1,7 @@ import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flux/core/blocs/session/session_cubit.dart'; -import 'package:flux/core/data/constants.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/core/utils/app_message.dart'; import 'package:get_it/get_it.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; diff --git a/lib/features/company/data/company_repository.dart b/lib/features/company/data/company_repository.dart index 11e4891..c5fa6f3 100644 --- a/lib/features/company/data/company_repository.dart +++ b/lib/features/company/data/company_repository.dart @@ -1,5 +1,6 @@ import 'dart:typed_data'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import '../models/company_model.dart'; @@ -10,7 +11,7 @@ class CompanyRepository { try { // .select().single() trasforma la risposta nell'oggetto appena inserito final response = await _supabase - .from('company') + .from(Tables.companies) .insert(company.toMap()) .select() .single(); @@ -26,7 +27,7 @@ class CompanyRepository { Future updateCompany(CompanyModel company) async { try { final response = await _supabase - .from('company') + .from(Tables.companies) .update(company.toMap()) .eq('id', company.id!) .select() @@ -83,7 +84,7 @@ class CompanyRepository { try { final userId = _supabase.auth.currentUser?.id; final response = await _supabase - .from('company') + .from(Tables.companies) .select() .eq('user_id', userId as Object) .maybeSingle(); diff --git a/lib/features/customers/data/customer_repository.dart b/lib/features/customers/data/customer_repository.dart index 80b85a3..7da2f60 100644 --- a/lib/features/customers/data/customer_repository.dart +++ b/lib/features/customers/data/customer_repository.dart @@ -1,5 +1,6 @@ import 'package:file_picker/file_picker.dart'; import 'package:flux/core/blocs/session/session_cubit.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/core/utils/extensions.dart'; import 'package:flux/features/attachments/models/attachment_model.dart'; import 'package:get_it/get_it.dart'; @@ -14,7 +15,7 @@ class CustomerRepository { Future insertCustomer(CustomerModel customer) async { try { final response = await _supabase - .from('customer') + .from(Tables.customers) .upsert(customer.toJson()) .select() .single(); @@ -27,7 +28,7 @@ class CustomerRepository { Future updateCustomer(CustomerModel customer) async { try { final response = await _supabase - .from('customer') + .from(Tables.customers) .update(customer.toJson()) .eq('id', customer.id!) .select() @@ -42,10 +43,10 @@ class CustomerRepository { Future> getCustomers(String companyId) async { try { final response = await _supabase - .from('customer') + .from(Tables.customers) .select(''' *, - attachment(*) + ${Tables.attachments}(*) ''') .eq('company_id', companyId) .eq('is_active', true) @@ -60,10 +61,10 @@ class CustomerRepository { Future getCustomerById(String customerId) async { try { final response = await _supabase - .from('customer') + .from(Tables.customers) .select(''' *, - attachment(*) + ${Tables.attachments}(*) ''') .eq('id', customerId) .single(); @@ -81,7 +82,7 @@ class CustomerRepository { ) async { try { final response = await _supabase - .from('customer') + .from(Tables.customers) .select() .eq('company_id', companyId) .or('name.ilike.%$query%,phone_number.ilike.%$query%') @@ -96,7 +97,7 @@ class CustomerRepository { /// Ascolta in tempo reale i file caricati per un cliente Stream> getCustomerFilesStream(String customerId) { return _supabase - .from('attachment') + .from(Tables.attachments) .stream(primaryKey: ['id']) .eq('customer_id', customerId) .order('created_at', ascending: false) @@ -110,7 +111,7 @@ class CustomerRepository { Future> getCustomerFiles(String customerId) async { try { final response = await _supabase - .from('attachment') + .from(Tables.attachments) .select() .eq('customer_id', customerId); @@ -161,7 +162,7 @@ class CustomerRepository { } final response = await _supabase - .from('attachment') + .from(Tables.attachments) .insert(fileToSave.toMap()) .select() .single(); @@ -173,7 +174,7 @@ class CustomerRepository { } Future saveFileReference(AttachmentModel file) async { - await _supabase.from('attachment').upsert(file.toMap()); + await _supabase.from(Tables.attachments).upsert(file.toMap()); } Future deleteDocuments(List files) async { @@ -192,13 +193,16 @@ class CustomerRepository { } try { if (idsToDelete.isNotEmpty) { - await _supabase.from('attachment').delete().inFilter('id', idsToDelete); + await _supabase + .from(Tables.attachments) + .delete() + .inFilter('id', idsToDelete); // 3. Cancellazione MASSIVA dallo Storage await _supabase.storage.from('documents').remove(storagePathsToDelete); } if (idsToEdit.isNotEmpty) { await _supabase - .from('attachment') + .from(Tables.attachments) .update({'customer_id': null}) .inFilter('id', idsToEdit); } diff --git a/lib/features/master_data/products/data/product_repository.dart b/lib/features/master_data/products/data/product_repository.dart index 1fdbe68..cac0ff4 100644 --- a/lib/features/master_data/products/data/product_repository.dart +++ b/lib/features/master_data/products/data/product_repository.dart @@ -1,4 +1,5 @@ import 'package:flux/core/blocs/session/session_cubit.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:get_it/get_it.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import '../models/brand_model.dart'; @@ -14,7 +15,7 @@ class ProductRepository { Future> getBrands() async { try { final response = await _supabase - .from('brand') + .from(Tables.brands) .select() .eq('company_id', _companyId) .eq('is_active', true) @@ -30,7 +31,7 @@ class ProductRepository { Future upsertBrand(BrandModel brand) async { try { final response = await _supabase - .from('brand') + .from(Tables.brands) .upsert(brand.toJson()) .select() .single(); @@ -47,7 +48,7 @@ class ProductRepository { Future> getModelsByBrand(String brandId) async { try { final response = await _supabase - .from('model') + .from(Tables.models) .select() .eq('brand_id', brandId) .eq('is_active', true) @@ -62,7 +63,7 @@ class ProductRepository { Future> getModels() async { try { final response = await _supabase - .from('model') + .from(Tables.models) .select() .eq('is_active', true) .order('name'); @@ -77,7 +78,7 @@ class ProductRepository { Future upsertModel(ModelModel model) async { try { final response = await _supabase - .from('model') + .from(Tables.models) .upsert(model.toJson()) .select() .single(); @@ -102,7 +103,7 @@ class ProductRepository { Future> searchModels(String query) async { try { final response = await _supabase - .from('model') + .from(Tables.models) .select() .ilike('name_with_brand', '%$query%') // Cerca ovunque nel nome .eq('is_active', true) diff --git a/lib/features/master_data/products/ui/models_list.dart b/lib/features/master_data/products/ui/models_list.dart index e9dd5c6..57821fd 100644 --- a/lib/features/master_data/products/ui/models_list.dart +++ b/lib/features/master_data/products/ui/models_list.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/core/theme/theme.dart'; import 'package:flux/features/master_data/products/blocs/product_cubit.dart'; import 'package:flux/features/master_data/products/ui/product_dialogs.dart'; @@ -63,9 +64,12 @@ class ModelsList extends StatelessWidget { : Icons.visibility_off_outlined, color: model.isActive ? context.accent : Colors.grey, ), - onPressed: () => context - .read() - .toggleStatus('model', model.id!, model.isActive), + onPressed: () => + context.read().toggleStatus( + Tables.models, + model.id!, + model.isActive, + ), ), ], ), diff --git a/lib/features/master_data/providers/data/provider_repository.dart b/lib/features/master_data/providers/data/provider_repository.dart index a11d5a3..ac986ff 100644 --- a/lib/features/master_data/providers/data/provider_repository.dart +++ b/lib/features/master_data/providers/data/provider_repository.dart @@ -1,4 +1,5 @@ import 'package:flux/core/blocs/session/session_cubit.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:get_it/get_it.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; import '../models/provider_model.dart'; @@ -11,12 +12,12 @@ class ProviderRepository { // 1. Carica i provider abilitati per uno specifico Store Future> getProvidersByStore(String storeId) async { final response = await _supabase - .from('providers_in_stores') + .from(Tables.providersInStores) .select(''' provider_id, - provider:provider ( + provider:${Tables.providers} ( *, - provider_locations (*) + ${Tables.providerLocations} (*) ) ''') .eq('store_id', storeId) @@ -31,8 +32,8 @@ class ProviderRepository { // 2. Carica TUTTI i provider della Company (per la gestione anagrafica) Future> getAllCompanyProviders() async { final response = await _supabase - .from('provider') - .select('*, provider_locations (*)') + .from(Tables.providers) + .select('*, ${Tables.providerLocations} (*)') .order('name'); return (response as List) @@ -48,7 +49,7 @@ class ProviderRepository { // A. Salva/Aggiorna il Provider principale final providerWithCompany = provider.copyWith(companyId: _companyId); final savedRow = await _supabase - .from('provider') + .from(Tables.providers) .upsert(providerWithCompany.toMap()) .select() .single(); @@ -58,7 +59,7 @@ class ProviderRepository { // B. Sincronizza gli Store (Cancelliamo i vecchi e mettiamo i nuovi per semplicità) // In un'app ad alto traffico faremmo un confronto, qui l'upsert totale è più veloce da scrivere. await _supabase - .from('providers_in_stores') + .from(Tables.providersInStores) .delete() .eq('provider_id', savedProvider.id!); @@ -67,7 +68,7 @@ class ProviderRepository { .map((sId) => {'provider_id': savedProvider.id, 'store_id': sId}) .toList(); - await _supabase.from('providers_in_stores').insert(storeLinks); + await _supabase.from(Tables.providersInStores).insert(storeLinks); } return savedProvider; @@ -75,10 +76,13 @@ class ProviderRepository { // 4. Gestione Sedi (Locations) Future saveLocation(ProviderLocationModel location) async { - await _supabase.from('provider_locations').upsert(location.toMap()); + await _supabase.from(Tables.providerLocations).upsert(location.toMap()); } Future deleteLocation(String locationId) async { - await _supabase.from('provider_locations').delete().eq('id', locationId); + await _supabase + .from(Tables.providerLocations) + .delete() + .eq('id', locationId); } } diff --git a/lib/features/master_data/staff/data/staff_repository.dart b/lib/features/master_data/staff/data/staff_repository.dart index c6a3553..9c89c9d 100644 --- a/lib/features/master_data/staff/data/staff_repository.dart +++ b/lib/features/master_data/staff/data/staff_repository.dart @@ -1,3 +1,4 @@ +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/features/master_data/staff/models/staff_member_model.dart'; import 'package:flux/features/master_data/store/models/store_model.dart'; import 'package:get_it/get_it.dart'; @@ -11,7 +12,7 @@ class StaffRepository { // Prende tutto lo staff della Company (per l'Hub Anagrafiche) Future> getStaffMembers(String companyId) async { final response = await _supabase - .from('staff_member') + .from(Tables.staffMembers) .select() .eq('company_id', companyId) .order('name', ascending: true); @@ -21,7 +22,7 @@ class StaffRepository { Future saveStaffMember(StaffMemberModel member) async { final response = await _supabase - .from('staff_member') + .from(Tables.staffMembers) .upsert(member.toMap()) .select() .single(); @@ -64,7 +65,7 @@ class StaffRepository { try { await _supabase.auth.resetPasswordForEmail( email, - redirectTo: 'https://flux-web-invite.marco-6ba.workers.dev/', + redirectTo: resetPasswordUrl, ); } catch (e) { throw Exception("Errore nell'invio del link: $e"); @@ -77,14 +78,14 @@ class StaffRepository { // Qui facciamo una JOIN per avere i dati del membro partendo dalla tabella di giunzione Future> getStaffMembersInStore(String storeId) async { final response = await _supabase - .from('staff_in_stores') + .from(Tables.staffInStores) .select( - 'staff_member (*)', + '${Tables.staffMembers} (*)', ) // Prende tutti i campi della tabella staff_member collegata .eq('store_id', storeId); return (response as List) - .map((item) => StaffMemberModel.fromMap(item['staff_member'])) + .map((item) => StaffMemberModel.fromMap(item[Tables.staffMembers])) .toList(); } @@ -92,20 +93,20 @@ class StaffRepository { // Qui facciamo una JOIN per avere i dati del membro partendo dalla tabella di giunzione Future> getStaffMemberStore(String staffId) async { final response = await _supabase - .from('staff_in_stores') + .from(Tables.staffInStores) .select( - 'store (*)', + '${Tables.stores} (*)', ) // Prende tutti i campi della tabella store collegata .eq('staff_member_id', staffId); return (response as List) - .map((item) => StoreModel.fromMap(item['store'])) + .map((item) => StoreModel.fromMap(item[Tables.stores])) .toList(); } // Assegna un membro a un negozio Future assignStaffToStore(String staffId, String storeId) async { - await _supabase.from('staff_in_stores').insert({ + await _supabase.from(Tables.staffInStores).insert({ 'staff_member_id': staffId, 'store_id': storeId, }); @@ -114,7 +115,7 @@ class StaffRepository { // Rimuove l'assegnazione Future removeStaffFromStore(String staffId, String storeId) async { await _supabase - .from('staff_in_stores') + .from(Tables.staffInStores) .delete() .eq('staff_member_id', staffId) .eq('store_id', storeId); @@ -125,7 +126,7 @@ class StaffRepository { // Utility per pulire le assegnazioni esistenti prima di riscriverle Future clearStoreAssignments(String staffId) async { await _supabase - .from('staff_in_stores') + .from(Tables.staffInStores) .delete() .eq('staff_member_id', staffId); } diff --git a/lib/features/master_data/store/data/store_repository.dart b/lib/features/master_data/store/data/store_repository.dart index 0d64d91..8cbdd60 100644 --- a/lib/features/master_data/store/data/store_repository.dart +++ b/lib/features/master_data/store/data/store_repository.dart @@ -1,3 +1,5 @@ +import 'package:flutter/widgets.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/features/master_data/providers/models/provider_model.dart'; import 'package:flux/features/master_data/staff/models/staff_member_model.dart'; import 'package:get_it/get_it.dart'; @@ -10,7 +12,7 @@ class StoreRepository { /// Crea un nuovo negozio associato alla compagnia dell'utente Future createStore(StoreModel store) async { try { - await _supabase.from('store').insert(store.toMap()); + await _supabase.from(Tables.stores).insert(store.toMap()); } on PostgrestException catch (e) { // Intercettiamo errori specifici del database throw e.message; @@ -22,7 +24,7 @@ class StoreRepository { Future saveStore(StoreModel store) async { try { final response = await _supabase - .from('store') + .from(Tables.stores) .upsert(store.toMap()) .select() .single(); @@ -41,7 +43,7 @@ class StoreRepository { try { // 1. Eliminiamo tutte le associazioni correnti per questo negozio await _supabase - .from('providers_in_stores') + .from(Tables.providersInStores) .delete() .eq('store_id', storeId); @@ -51,7 +53,7 @@ class StoreRepository { .map((p) => {'store_id': storeId, 'provider_id': p.id}) .toList(); - await _supabase.from('providers_in_stores').insert(inserts); + await _supabase.from(Tables.providersInStores).insert(inserts); } } catch (e) { throw 'Errore durante la sincronizzazione provider: $e'; @@ -64,7 +66,10 @@ class StoreRepository { ) async { try { // 1. Eliminiamo tutte le associazioni correnti per questo negozio - await _supabase.from('staff_in_stores').delete().eq('store_id', storeId); + await _supabase + .from(Tables.staffInStores) + .delete() + .eq('store_id', storeId); // 2. Se ci sono nuovi dipendenti da associare, li inseriamo if (staffMembers.isNotEmpty) { @@ -72,7 +77,7 @@ class StoreRepository { .map((s) => {'store_id': storeId, 'staff_id': s.id}) .toList(); - await _supabase.from('staff_in_stores').insert(inserts); + await _supabase.from(Tables.staffInStores).insert(inserts); } } catch (e) { throw 'Errore durante la sincronizzazione staff: $e'; @@ -83,16 +88,16 @@ class StoreRepository { Future> fetchAllCompanyStores(String companyId) async { try { final response = await _supabase - .from('store') + .from(Tables.stores) .select(''' *, - associated_providers:providers_in_stores ( - provider ( + associated_providers:${Tables.providersInStores} ( + ${Tables.providers} ( * ) ) - associated_staff:staff_in_stores ( - staff_member ( + associated_staff:${Tables.staffInStores} ( + ${Tables.staffMembers} ( * ) ) @@ -111,7 +116,7 @@ class StoreRepository { required String providerId, }) async { try { - await _supabase.from('providers_in_stores').insert({ + await _supabase.from(Tables.providersInStores).insert({ 'store_id': storeId, 'provider_id': providerId, }); @@ -126,7 +131,7 @@ class StoreRepository { }) async { try { await _supabase - .from('providers_in_stores') + .from(Tables.providersInStores) .delete() .eq('store_id', storeId) .eq('provider_id', providerId); diff --git a/lib/features/operations/blocs/operation_form_cubit.dart b/lib/features/operations/blocs/operation_form_cubit.dart index e10f742..0ef7386 100644 --- a/lib/features/operations/blocs/operation_form_cubit.dart +++ b/lib/features/operations/blocs/operation_form_cubit.dart @@ -104,6 +104,7 @@ class OperationFormCubit extends Cubit { batchUuid: current.batchUuid, // MANTIENE IL COLLEGAMENTO customerId: current.customerId, // MANTIENE IL CLIENTE customer: current.customer, + reference: current.reference, status: OperationStatus.draft, createdAt: DateTime.now(), ), @@ -122,7 +123,21 @@ class OperationFormCubit extends Cubit { ); try { - final operationToSave = state.operation.copyWith(status: targetStatus); + OperationModel operationToSave = state.operation.copyWith( + status: targetStatus, + ); + if (operationToSave.reference.isEmpty) { + if (operationToSave.customer != null && + operationToSave.customer!.phoneNumber.isNotEmpty) { + operationToSave = operationToSave.copyWith( + reference: '${operationToSave.customer?.phoneNumber} - auto', + ); + } else { + operationToSave = operationToSave.copyWith( + reference: 'Nessun riferimento', + ); + } + } final savedOperation = await _repository.saveFullOperation( operation: operationToSave, ); diff --git a/lib/features/operations/data/operations_repository.dart b/lib/features/operations/data/operations_repository.dart index 1abc4f5..780fcce 100644 --- a/lib/features/operations/data/operations_repository.dart +++ b/lib/features/operations/data/operations_repository.dart @@ -1,6 +1,7 @@ import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; import 'package:flux/core/blocs/session/session_cubit.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/core/utils/extensions.dart'; import 'package:flux/features/attachments/models/attachment_model.dart'; import 'package:get_it/get_it.dart'; @@ -15,15 +16,15 @@ class OperationsRepository { Future fetchOperationById(String id) async { try { final response = await _supabase - .from('operation') + .from(Tables.operations) .select(''' *, - customer(*), - store(name), - staff_member(name), - provider(name), - model(name_with_brand), - attachment(*) + ${Tables.customers}(*), + ${Tables.stores}(name), + ${Tables.staffMembers}(name), + ${Tables.providers}(name), + ${Tables.models}(name_with_brand), + ${Tables.attachments}(*) ''') .eq('id', id) .single(); @@ -44,15 +45,15 @@ class OperationsRepository { }) async { try { var query = _supabase - .from('operation') + .from(Tables.operations) .select(''' *, - customer(*), - store(name), - provider(name), - model(name_with_brand), - staff_member(name), - attachment(*) + ${Tables.customers}(*), + ${Tables.stores}(name), + ${Tables.providers}(name), + ${Tables.models}(name_with_brand), + ${Tables.staffMembers}(name), + ${Tables.attachments}(*) ''') .eq('company_id', companyId); @@ -87,7 +88,7 @@ class OperationsRepository { required int limit, }) { return _supabase - .from('operation') + .from(Tables.operations) .stream(primaryKey: ['id']) .eq('store_id', storeId) .order('created_at', ascending: false) @@ -105,10 +106,10 @@ class OperationsRepository { try { // 1. Salvataggio classico dell'operazione corrente final response = await _supabase - .from('operation') + .from(Tables.operations) .upsert(operation.toMap()) .select( - '*, provider(*), model(*), store(*), staff_member(*), customer(*), attachment(*)', + '*, ${Tables.providers}(*), ${Tables.models}(*), ${Tables.stores}(*), ${Tables.staffMembers}(*), ${Tables.customers}(*), ${Tables.attachments}(*)', ) .single(); @@ -117,7 +118,7 @@ class OperationsRepository { // 2. ALLINEAMENTO BATCH SEMPRE ATTIVO! if (operation.batchUuid.isNotEmpty) { await _supabase - .from('operation') + .from(Tables.operations) .update({'note': operation.note}) // Spalmiamo la nota attuale .eq( 'batch_uuid', @@ -134,7 +135,7 @@ class OperationsRepository { // --- ELIMINAZIONE --- Future deleteOperation(String id) async { try { - await _supabase.from('operation').delete().eq('id', id); + await _supabase.from(Tables.operations).delete().eq('id', id); } catch (e) { throw Exception('$e'); } @@ -146,7 +147,7 @@ class OperationsRepository { // Cerchiamo i tipi più frequenti associati ai servizi di questa company // Nota: dobbiamo passare attraverso la tabella 'operation' per filtrare per company_id final response = await _supabase - .from('operation') + .from(Tables.operations) .select('description') .eq('company_id', companyId) .eq('type', 'Entertainment') @@ -176,7 +177,7 @@ class OperationsRepository { /// Ascolta in tempo reale i file caricati per una pratica Stream> getOperationFilesStream(String operationId) { return _supabase - .from('attachment') + .from(Tables.attachments) .stream(primaryKey: ['id']) .eq('operation_id', operationId) .order('created_at', ascending: false) @@ -226,7 +227,7 @@ class OperationsRepository { } final response = await _supabase - .from('attachment') + .from(Tables.attachments) .insert(fileToSave.toMap()) .select() .single(); @@ -242,14 +243,17 @@ class OperationsRepository { required String customerId, }) async { await _supabase - .from('attachment') + .from(Tables.attachments) .update({'customer_id': customerId}) .eq('id', file.id!); } Future renameAttachment(String id, String newName) async { try { - await _supabase.from('attachment').update({'name': newName}).eq('id', id); + await _supabase + .from(Tables.attachments) + .update({'name': newName}) + .eq('id', id); } catch (e) { throw '$e'; } @@ -258,11 +262,11 @@ class OperationsRepository { Future deleteSpecificOperationFile(AttachmentModel file) async { try { if (file.customerId == null) { - await _supabase.from('attachment').delete().eq('id', file.id!); + await _supabase.from(Tables.attachments).delete().eq('id', file.id!); await _supabase.storage.from('documents').remove([file.storagePath!]); } else { await _supabase - .from('attachment') + .from(Tables.attachments) .update({'operation_id': null}) .eq('id', file.id!); } @@ -287,12 +291,15 @@ class OperationsRepository { } try { if (idsToDelete.isNotEmpty) { - await _supabase.from('attachment').delete().inFilter('id', idsToDelete); + await _supabase + .from(Tables.attachments) + .delete() + .inFilter('id', idsToDelete); await _supabase.storage.from('documents').remove(storagePathsToDelete); } if (idsToEdit.isNotEmpty) { await _supabase - .from('attachment') + .from(Tables.attachments) .update({'operation_id': null}) .inFilter('id', idsToEdit); } diff --git a/lib/features/settings/document_sequence/data/document_sequence_repository.dart b/lib/features/settings/document_sequence/data/document_sequence_repository.dart index 4d8d72c..9d84122 100644 --- a/lib/features/settings/document_sequence/data/document_sequence_repository.dart +++ b/lib/features/settings/document_sequence/data/document_sequence_repository.dart @@ -1,4 +1,5 @@ import 'package:flux/core/blocs/session/session_cubit.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/features/settings/document_sequence/models/document_sequence_model.dart'; import 'package:get_it/get_it.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; @@ -10,7 +11,7 @@ class DocumentSequenceRepository { Future> getDocumentSequences() async { try { final response = await _supabase - .from('document_sequences') + .from(Tables.documentSequences) .select() .eq('company_id', _companyId); @@ -39,7 +40,7 @@ class DocumentSequenceRepository { required DocumentSequence sequence, }) async { try { - await _supabase.from('document_sequences').upsert({ + await _supabase.from(Tables.documentSequences).upsert({ 'company_id': _companyId, 'doc_type': sequence.docType, 'next_value': sequence.nextValue, @@ -51,7 +52,7 @@ class DocumentSequenceRepository { } Future updateSequence({required DocumentSequence sequence}) async { - await _supabase.from('document_sequences').upsert({ + await _supabase.from(Tables.documentSequences).upsert({ 'company_id': _companyId, 'doc_type': sequence.docType, 'next_value': sequence.nextValue, diff --git a/lib/features/settings/settings.dart b/lib/features/settings/settings.dart index b20c77a..86ee8ff 100644 --- a/lib/features/settings/settings.dart +++ b/lib/features/settings/settings.dart @@ -1,4 +1,4 @@ -import 'package:flux/core/enums/enums.dart'; +import 'package:flux/core/enums_and_consts/enums.dart'; import 'package:get_it/get_it.dart'; import 'package:shared_preferences/shared_preferences.dart'; diff --git a/lib/features/settings/theme_settings_view.dart b/lib/features/settings/theme_settings_view.dart index 572d441..ecb03c3 100644 --- a/lib/features/settings/theme_settings_view.dart +++ b/lib/features/settings/theme_settings_view.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:flux/core/enums/enums.dart'; +import 'package:flux/core/enums_and_consts/enums.dart'; import 'package:flux/core/theme/theme.dart'; import 'package:flux/core/theme/bloc/theme_bloc.dart'; diff --git a/lib/features/tickets/data/ticket_repository.dart b/lib/features/tickets/data/ticket_repository.dart index 0ebb4e2..2a938bf 100644 --- a/lib/features/tickets/data/ticket_repository.dart +++ b/lib/features/tickets/data/ticket_repository.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flux/core/blocs/session/session_cubit.dart'; +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/features/settings/document_sequence/data/document_sequence_repository.dart'; import 'package:flux/features/settings/document_sequence/models/document_sequence_model.dart'; import 'package:flux/features/tickets/models/ticket_model.dart'; @@ -13,7 +14,7 @@ class TicketRepository { TicketRepository(); - static const String _tableName = 'ticket'; + static const String _tableName = Tables.tickets; // --- RECUPERO PAGINATO CON FILTRI E JOIN DEI TICKET DI UNO STORE --- Future> fetchStoreTickets({ @@ -29,14 +30,14 @@ class TicketRepository { var query = _supabase .from(_tableName) .select(''' - *, - customer (*), - shipping_documents (*, attachment (*)), - created_by:staff_member!ticket_staff_id_fkey (*), - assigned_to:staff_member!ticket_assigned_to_id_fkey (*), - target_model:model!ticket_model_id_1_fkey (*), - source_model:model!ticket_model_id_2_fkey (*) - ''') + *, + ${Tables.customers} (*), + ${Tables.shippingDocuments} (*, ${Tables.attachments} (*)), + created_by:${Tables.staffMembers}!ticket_staff_id_fkey (*), + assigned_to:${Tables.staffMembers}!ticket_assigned_to_id_fkey (*), + target_model:${Tables.models}!ticket_model_id_1_fkey (*), + source_model:${Tables.models}!ticket_model_id_2_fkey (*) + ''') .eq('store_id', GetIt.I.get().state.currentStore!.id!); // Filtro Range Date @@ -88,12 +89,12 @@ class TicketRepository { .from(_tableName) .select(''' *, - customer (*), - shipping_documents (*, attachment (*)), - created_by:staff_member!ticket_staff_id_fkey (*), - assigned_to:staff_member!ticket_assigned_to_id_fkey (*), - target_model:model!ticket_model_id_1_fkey (*), - source_model:model!ticket_model_id_2_fkey (*) + ${Tables.customers} (*), + ${Tables.shippingDocuments} (*, ${Tables.attachments} (*)), + created_by:${Tables.staffMembers}!ticket_staff_id_fkey (*), + assigned_to:${Tables.staffMembers}!ticket_assigned_to_id_fkey (*), + target_model:${Tables.models}!ticket_model_id_1_fkey (*), + source_model:${Tables.models}!ticket_model_id_2_fkey (*) ''') .eq('company_id', GetIt.I.get().state.company!.id!); @@ -199,12 +200,12 @@ class TicketRepository { .from(_tableName) .select(''' *, - customer (*), - target_model:model!ticket_model_id_1_fkey (*), - source_model:model!ticket_model_id_2_fkey (*), - created_by:staff_member!ticket_staff_id_fkey (*), - assigned_to:staff_member!ticket_assigned_to_id_fkey (*), - shipping_documents (*, attachment (*)) + ${Tables.customers} (*), + target_model:${Tables.models}!ticket_model_id_1_fkey (*), + source_model:${Tables.models}!ticket_model_id_2_fkey (*), + created_by:${Tables.staffMembers}!ticket_staff_id_fkey (*), + assigned_to:${Tables.staffMembers}!ticket_assigned_to_id_fkey (*), + ${Tables.shippingDocuments} (*, ${Tables.attachments} (*)) ''') .eq('id', ticketId) .single(); @@ -215,30 +216,6 @@ class TicketRepository { } } - // Future generateTicketReference(String companyId) async { - // final response = await Supabase.instance.client.rpc( - // 'get_next_document_number', - // params: {'p_company_id': companyId, 'p_doc_type': 'ticket'}, - // ); - - // // Estraiamo i dati dal JSON - // final int nextValue = response['next_value']; - // final String prefix = response['prefix'] ?? ''; - - // final year = DateTime.now().year; // 2026 - - // // Formattazione con zeri iniziali (es. 000125) - // final paddedNumber = nextValue.toString().padLeft(6, '0'); - - // // Costruiamo la stringa. Se c'è un prefisso mette "TCK-2026-000125", - // // altrimenti solo "2026-000125" - // if (prefix.isNotEmpty) { - // return '$prefix-$year-$paddedNumber'; - // } else { - // return '$year-$paddedNumber'; - // } - // } - /// Salva il ticket Future insertTicket(TicketModel ticket) async { if (ticket.id != null) { diff --git a/lib/features/tracking/data/tracking_repository.dart b/lib/features/tracking/data/tracking_repository.dart index 424fb39..03ddc6b 100644 --- a/lib/features/tracking/data/tracking_repository.dart +++ b/lib/features/tracking/data/tracking_repository.dart @@ -1,3 +1,4 @@ +import 'package:flux/core/enums_and_consts/consts.dart'; import 'package:flux/features/tracking/models/tracking_model.dart'; import 'package:get_it/get_it.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; @@ -14,8 +15,8 @@ class TrackingRepository { }) async { // Facciamo la query con la JOIN per recuperare il nome dello staff al volo final response = await _supabase - .from('tracking') - .select('*, staff_member(name)') + .from(Tables.trackings) + .select('*, ${Tables.staffMembers}(name)') .eq('parent_id', parentId) .eq('parent_type', parentType.name) .order('created_at', ascending: false); @@ -25,7 +26,7 @@ class TrackingRepository { /// Inserisce un nuovo evento di tracking Future logEvent(TrackingModel tracking) async { - await _supabase.from('tracking').insert(tracking.toMap()); + await _supabase.from(Tables.trackings).insert(tracking.toMap()); } /// Metodo helper rapido per loggare un cambio di stato o una nota