Files
worker/HIVE_MODELS_COMPLETED.md
2025-10-24 11:31:48 +07:00

14 KiB

Hive CE Data Models - Completion Summary

All Models Created Successfully!

A total of 25 Hive CE data models have been created for the Worker mobile app, covering all features from the database schema.

📊 Created Models by Feature

1. Authentication (2 models)

  • user_model.dart - Type ID: 0
    • Maps to users table
    • Includes: user info, loyalty tier, points, company info, referral
  • user_session_model.dart - Type ID: 1
    • Maps to user_sessions table
    • Includes: session management, device info, tokens

2. Products (2 models)

  • product_model.dart - Type ID: 2
    • Maps to products table
    • Includes: product details, images, specifications, pricing
  • stock_level_model.dart - Type ID: 3
    • Maps to stock_levels table
    • Includes: inventory tracking, warehouse info

3. Cart (2 models)

  • cart_model.dart - Type ID: 4
    • Maps to carts table
    • Includes: cart totals, sync status
  • cart_item_model.dart - Type ID: 5
    • Maps to cart_items table
    • Includes: product items, quantities, prices

4. Orders (4 models)

  • order_model.dart - Type ID: 6
    • Maps to orders table
    • Includes: order details, status, addresses, delivery
  • order_item_model.dart - Type ID: 7
    • Maps to order_items table
    • Includes: line items, discounts
  • invoice_model.dart - Type ID: 8
    • Maps to invoices table
    • Includes: invoice details, payment status, amounts
  • payment_line_model.dart - Type ID: 9
    • Maps to payment_lines table
    • Includes: payment records, methods, receipts

5. Loyalty (4 models)

  • loyalty_point_entry_model.dart - Type ID: 10
    • Maps to loyalty_point_entries table
    • Includes: points transactions, balance, complaints
  • gift_catalog_model.dart - Type ID: 11
    • Maps to gift_catalog table
    • Includes: available rewards, points cost
  • redeemed_gift_model.dart - Type ID: 12
    • Maps to redeemed_gifts table
    • Includes: user gifts, vouchers, QR codes
  • points_record_model.dart - Type ID: 13
    • Maps to points_records table
    • Includes: invoice submissions for points

6. Projects (2 models)

  • project_submission_model.dart - Type ID: 14
    • Maps to project_submissions table
    • Includes: project photos, review status
  • design_request_model.dart - Type ID: 15
    • Maps to design_requests table
    • Includes: design requirements, assignments

7. Quotes (2 models)

  • quote_model.dart - Type ID: 16
    • Maps to quotes table
    • Includes: quotation details, validity, conversion
  • quote_item_model.dart - Type ID: 17
    • Maps to quote_items table
    • Includes: quoted products, negotiated prices

8. Chat (2 models)

  • chat_room_model.dart - Type ID: 18
    • Maps to chat_rooms table
    • Includes: room info, participants, related entities
  • message_model.dart - Type ID: 19
    • Maps to chat_messages table
    • Includes: message content, attachments, read status

9. Notifications (1 model)

  • notification_model.dart - Type ID: 20
    • Maps to notifications table
    • Includes: notification type, data, read status

10. Showrooms (2 models)

  • showroom_model.dart - Type ID: 21
    • Maps to showrooms table
    • Includes: showroom details, images, 360 view
  • showroom_product_model.dart - Type ID: 22
    • Maps to showroom_products table
    • Includes: products used in showrooms

11. Account (2 models)

  • payment_reminder_model.dart - Type ID: 23
    • Maps to payment_reminders table
    • Includes: reminder scheduling, status
  • audit_log_model.dart - Type ID: 24
    • Maps to audit_logs table
    • Includes: user actions, changes tracking

🎯 Enum Types Created (21 enums)

All enum types are defined in /Users/ssg/project/worker/lib/core/database/models/enums.dart:

  • UserRole (Type ID: 30)
  • UserStatus (Type ID: 31)
  • LoyaltyTier (Type ID: 32)
  • OrderStatus (Type ID: 33)
  • InvoiceType (Type ID: 34)
  • InvoiceStatus (Type ID: 35)
  • PaymentMethod (Type ID: 36)
  • PaymentStatus (Type ID: 37)
  • EntryType (Type ID: 38)
  • EntrySource (Type ID: 39)
  • ComplaintStatus (Type ID: 40)
  • GiftCategory (Type ID: 41)
  • GiftStatus (Type ID: 42)
  • PointsStatus (Type ID: 43)
  • ProjectType (Type ID: 44)
  • SubmissionStatus (Type ID: 45)
  • DesignStatus (Type ID: 46)
  • QuoteStatus (Type ID: 47)
  • RoomType (Type ID: 48)
  • ContentType (Type ID: 49)
  • ReminderType (Type ID: 50)

📦 Model Features

Each model includes:

  • @HiveType annotation with unique Type ID
  • @HiveField annotations for all fields
  • fromJson() factory constructor for API deserialization
  • toJson() method for API serialization
  • Helper methods for JSONB fields (get as Map/List)
  • Computed properties and validation
  • copyWith() method for immutability
  • toString() override
  • Equality operators (== and hashCode)
  • Comprehensive documentation

🚀 Next Steps

1. Generate Type Adapters

Run the Hive code generator to create .g.dart files:

cd /Users/ssg/project/worker
dart run build_runner build --delete-conflicting-outputs

This will generate adapter files for all models and enums.

2. Register Adapters

Create or update Hive initialization file (e.g., lib/core/database/hive_service.dart):

import 'package:hive_ce/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';

// Import all generated adapters
import 'package:worker/core/database/models/enums.dart';
import 'package:worker/features/auth/data/models/user_model.dart';
import 'package:worker/features/auth/data/models/user_session_model.dart';
// ... import all other models

class HiveService {
  static Future<void> init() async {
    await Hive.initFlutter();

    // Register all enum adapters
    Hive.registerAdapter(UserRoleAdapter());
    Hive.registerAdapter(UserStatusAdapter());
    Hive.registerAdapter(LoyaltyTierAdapter());
    Hive.registerAdapter(OrderStatusAdapter());
    Hive.registerAdapter(InvoiceTypeAdapter());
    Hive.registerAdapter(InvoiceStatusAdapter());
    Hive.registerAdapter(PaymentMethodAdapter());
    Hive.registerAdapter(PaymentStatusAdapter());
    Hive.registerAdapter(EntryTypeAdapter());
    Hive.registerAdapter(EntrySourceAdapter());
    Hive.registerAdapter(ComplaintStatusAdapter());
    Hive.registerAdapter(GiftCategoryAdapter());
    Hive.registerAdapter(GiftStatusAdapter());
    Hive.registerAdapter(PointsStatusAdapter());
    Hive.registerAdapter(ProjectTypeAdapter());
    Hive.registerAdapter(SubmissionStatusAdapter());
    Hive.registerAdapter(DesignStatusAdapter());
    Hive.registerAdapter(QuoteStatusAdapter());
    Hive.registerAdapter(RoomTypeAdapter());
    Hive.registerAdapter(ContentTypeAdapter());
    Hive.registerAdapter(ReminderTypeAdapter());

    // Register all model adapters
    Hive.registerAdapter(UserModelAdapter());
    Hive.registerAdapter(UserSessionModelAdapter());
    Hive.registerAdapter(ProductModelAdapter());
    Hive.registerAdapter(StockLevelModelAdapter());
    Hive.registerAdapter(CartModelAdapter());
    Hive.registerAdapter(CartItemModelAdapter());
    Hive.registerAdapter(OrderModelAdapter());
    Hive.registerAdapter(OrderItemModelAdapter());
    Hive.registerAdapter(InvoiceModelAdapter());
    Hive.registerAdapter(PaymentLineModelAdapter());
    Hive.registerAdapter(LoyaltyPointEntryModelAdapter());
    Hive.registerAdapter(GiftCatalogModelAdapter());
    Hive.registerAdapter(RedeemedGiftModelAdapter());
    Hive.registerAdapter(PointsRecordModelAdapter());
    Hive.registerAdapter(ProjectSubmissionModelAdapter());
    Hive.registerAdapter(DesignRequestModelAdapter());
    Hive.registerAdapter(QuoteModelAdapter());
    Hive.registerAdapter(QuoteItemModelAdapter());
    Hive.registerAdapter(ChatRoomModelAdapter());
    Hive.registerAdapter(MessageModelAdapter());
    Hive.registerAdapter(NotificationModelAdapter());
    Hive.registerAdapter(ShowroomModelAdapter());
    Hive.registerAdapter(ShowroomProductModelAdapter());
    Hive.registerAdapter(PaymentReminderModelAdapter());
    Hive.registerAdapter(AuditLogModelAdapter());

    // Open boxes
    await Hive.openBox(HiveBoxNames.userBox);
    await Hive.openBox(HiveBoxNames.productBox);
    await Hive.openBox(HiveBoxNames.cartBox);
    await Hive.openBox(HiveBoxNames.orderBox);
    await Hive.openBox(HiveBoxNames.loyaltyBox);
    await Hive.openBox(HiveBoxNames.projectBox);
    await Hive.openBox(HiveBoxNames.notificationBox);
    // ... open all other boxes
  }
}

3. Create Datasources

Implement local datasources using these models:

// Example: lib/features/auth/data/datasources/auth_local_datasource.dart
class AuthLocalDataSource {
  final Box userBox;

  Future<void> cacheUser(UserModel user) async {
    await userBox.put(HiveKeys.currentUser, user);
  }

  UserModel? getCachedUser() {
    return userBox.get(HiveKeys.currentUser) as UserModel?;
  }
}

4. Update Repository Implementations

Use models in repository implementations for caching:

class ProductRepositoryImpl implements ProductRepository {
  final ProductRemoteDataSource remoteDataSource;
  final ProductLocalDataSource localDataSource;

  @override
  Future<List<Product>> getProducts() async {
    try {
      // Try to fetch from API
      final products = await remoteDataSource.getProducts();

      // Cache locally
      await localDataSource.cacheProducts(products);

      return products;
    } catch (e) {
      // Return cached data on error
      return localDataSource.getCachedProducts();
    }
  }
}

📁 File Structure

lib/features/
├── auth/data/models/
│   ├── user_model.dart ✅
│   ├── user_model.g.dart (generated)
│   ├── user_session_model.dart ✅
│   └── user_session_model.g.dart (generated)
├── products/data/models/
│   ├── product_model.dart ✅
│   ├── product_model.g.dart (generated)
│   ├── stock_level_model.dart ✅
│   └── stock_level_model.g.dart (generated)
├── cart/data/models/
│   ├── cart_model.dart ✅
│   ├── cart_model.g.dart (generated)
│   ├── cart_item_model.dart ✅
│   └── cart_item_model.g.dart (generated)
├── orders/data/models/
│   ├── order_model.dart ✅
│   ├── order_model.g.dart (generated)
│   ├── order_item_model.dart ✅
│   ├── order_item_model.g.dart (generated)
│   ├── invoice_model.dart ✅
│   ├── invoice_model.g.dart (generated)
│   ├── payment_line_model.dart ✅
│   └── payment_line_model.g.dart (generated)
├── loyalty/data/models/
│   ├── loyalty_point_entry_model.dart ✅
│   ├── loyalty_point_entry_model.g.dart (generated)
│   ├── gift_catalog_model.dart ✅
│   ├── gift_catalog_model.g.dart (generated)
│   ├── redeemed_gift_model.dart ✅
│   ├── redeemed_gift_model.g.dart (generated)
│   ├── points_record_model.dart ✅
│   └── points_record_model.g.dart (generated)
├── projects/data/models/
│   ├── project_submission_model.dart ✅
│   ├── project_submission_model.g.dart (generated)
│   ├── design_request_model.dart ✅
│   └── design_request_model.g.dart (generated)
├── quotes/data/models/
│   ├── quote_model.dart ✅
│   ├── quote_model.g.dart (generated)
│   ├── quote_item_model.dart ✅
│   └── quote_item_model.g.dart (generated)
├── chat/data/models/
│   ├── chat_room_model.dart ✅
│   ├── chat_room_model.g.dart (generated)
│   ├── message_model.dart ✅
│   └── message_model.g.dart (generated)
├── notifications/data/models/
│   ├── notification_model.dart ✅
│   └── notification_model.g.dart (generated)
├── showrooms/data/models/
│   ├── showroom_model.dart ✅
│   ├── showroom_model.g.dart (generated)
│   ├── showroom_product_model.dart ✅
│   └── showroom_product_model.g.dart (generated)
└── account/data/models/
    ├── payment_reminder_model.dart ✅
    ├── payment_reminder_model.g.dart (generated)
    ├── audit_log_model.dart ✅
    └── audit_log_model.g.dart (generated)

⚠️ Important Notes

Type ID Management

  • All Type IDs are unique across the app (0-24 for models, 30-50 for enums)
  • Never change a Type ID once assigned - it will break existing cached data
  • Type IDs are centrally managed in /Users/ssg/project/worker/lib/core/constants/storage_constants.dart

JSONB Field Handling

  • All JSONB fields from database are stored as JSON-encoded strings in Hive
  • Helper methods provide easy access to parsed data (e.g., companyInfoMap, participantsList)
  • Always use try-catch when decoding JSON fields

DateTime Support

  • Hive CE natively supports DateTime
  • No need for custom serialization
  • Use DateTime.parse() for JSON and toIso8601String() for API

Best Practices

  • Always extend HiveObject for automatic key management
  • Use sequential field numbering (0, 1, 2, ...)
  • Include comprehensive documentation
  • Implement helper methods for computed properties
  • Handle null values appropriately

🎉 Summary

  • 25 data models created
  • 21 enum types defined
  • All database tables mapped
  • Complete JSON serialization/deserialization
  • Comprehensive helper methods
  • Full documentation
  • Type-safe implementations
  • Clean architecture compliant

The Worker app now has a complete, production-ready Hive CE local database implementation for offline-first functionality and API response caching!

📚 Reference Documents

  • HIVE_MODELS_REFERENCE.md - Detailed reference and templates
  • /Users/ssg/project/worker/database.md - Original database schema
  • /Users/ssg/project/worker/lib/core/constants/storage_constants.dart - Type IDs and constants
  • /Users/ssg/project/worker/lib/core/database/models/enums.dart - All enum definitions

Generated: 2025-10-24 Status: Complete and ready for code generation