Documentation Index
Fetch the complete documentation index at: https://docs-mstore.faisalaffan.com/llms.txt
Use this file to discover all available pages before exploring further.
Shift Management
Manajemen shift kasir dan reconciliation
๐ฏ Overview
Shift adalah salah satu fitur utama dalam MStore Mobile yang menyediakan fungsionalitas untuk manajemen shift kasir dan reconciliation.
๐ Features
- โ
Open/Close shift
- โ
Shift handover
- โ
Cash counting
- โ
Shift reports
- โ
Discrepancy tracking
๐๏ธ Architecture
BLoC Implementation
BLoC: ShiftBloc
// Events
abstract class ShiftEvent extends Equatable {}
class LoadShift extends ShiftEvent {}
class CreateShift extends ShiftEvent {}
class UpdateShift extends ShiftEvent {}
class DeleteShift extends ShiftEvent {}
// States
abstract class ShiftState extends Equatable {}
class ShiftInitial extends ShiftState {}
class ShiftLoading extends ShiftState {}
class ShiftLoaded extends ShiftState {}
class ShiftError extends ShiftState {}
// BLoC
class ShiftBloc extends Bloc<ShiftEvent, ShiftState> {
final ShiftRepository _repository;
ShiftBloc({required ShiftRepository repository})
: _repository = repository,
super(ShiftInitial()) {
on<LoadShift>(_onLoad);
on<CreateShift>(_onCreate);
on<UpdateShift>(_onUpdate);
on<DeleteShift>(_onDelete);
}
Future<void> _onLoad(
LoadShift event,
Emitter<ShiftState> emit,
) async {
emit(ShiftLoading());
final result = await _repository.getShifts();
result.fold(
(failure) => emit(ShiftError(failure.message)),
(data) => emit(ShiftLoaded(data)),
);
}
}
Repository Pattern
abstract class ShiftRepository {
Future<Either<Failure, List<Shift>>> getShifts();
Future<Either<Failure, Shift>> getShiftById(String id);
Future<Either<Failure, Shift>> createShift(Shift data);
Future<Either<Failure, Shift>> updateShift(String id, Shift data);
Future<Either<Failure, void>> deleteShift(String id);
}
class ShiftRepositoryImpl implements ShiftRepository {
final ShiftApi _api;
final ShiftLocalRepository _localRepo;
@override
Future<Either<Failure, List<Shift>>> getShifts() async {
try {
// Try local first (offline-first)
final local = await _localRepo.getShifts();
// Sync with API in background
final result = await _api.getShifts();
result.fold(
(failure) => null,
(data) => _localRepo.saveShifts(data),
);
return Right(local.isNotEmpty ? local : result.getOrElse(() => []));
} catch (e) {
return Left(UnexpectedFailure(e.toString()));
}
}
}
๐ก API Integration
Endpoints
Request/Response Examples
Get List
GET /api/v1/shifts/*
Authorization: Bearer {access_token}
Response:
{
"success": true,
"data": [
{
"id": "123",
"name": "Example",
"created_at": "2024-10-14T10:00:00Z"
}
],
"pagination": {
"page": 1,
"limit": 20,
"total": 100
}
}
๐พ Local Database (Isar)
@collection
class ShiftLocal {
Id id = Isar.autoIncrement;
@Index()
String? shiftId;
String? name;
DateTime? createdAt;
DateTime? updatedAt;
DateTime? syncedAt;
bool? isSynced;
bool? isDeleted;
}
Queries
// Get all
final items = await isar.shiftLocals.where().findAll();
// Get by ID
final item = await isar.shiftLocals
.filter()
.shiftIdEqualTo(id)
.findFirst();
// Search
final results = await isar.shiftLocals
.filter()
.nameContains(query, caseSensitive: false)
.findAll();
// Get unsynced
final unsynced = await isar.shiftLocals
.filter()
.isSyncedEqualTo(false)
.findAll();
๐ Offline-First Strategy
Write Operations
- Save to local Isar immediately
- Show success to user
- Add to sync queue
- Background sync when online
- Update with server response
Read Operations
- Read from local Isar (fast)
- Show to user immediately
- Background fetch from API
- Update local cache if changed
- Notify UI if data updated
Conflict Resolution
- Strategy: Last-write-wins
- Timestamp: Server timestamp as source of truth
- Logging: All conflicts logged for audit
๐จ UI Components
Main Screen
class ShiftPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => getIt<ShiftBloc>()..add(LoadShift()),
child: Scaffold(
appBar: AppBar(title: Text('Shift Management')),
body: BlocBuilder<ShiftBloc, ShiftState>(
builder: (context, state) {
if (state is ShiftLoading) {
return Center(child: CircularProgressIndicator());
}
if (state is ShiftError) {
return ErrorWidget(message: state.message);
}
if (state is ShiftLoaded) {
return ShiftListView(items: state.items);
}
return SizedBox.shrink();
},
),
floatingActionButton: FloatingActionButton(
onPressed: () => _navigateToCreate(context),
child: Icon(Icons.add),
),
),
);
}
}
๐งช Testing
Unit Tests
void main() {
group('ShiftBloc', () {
late ShiftBloc bloc;
late MockShiftRepository mockRepository;
setUp(() {
mockRepository = MockShiftRepository();
bloc = ShiftBloc(repository: mockRepository);
});
tearDown(() {
bloc.close();
});
test('initial state is ShiftInitial', () {
expect(bloc.state, equals(ShiftInitial()));
});
blocTest<ShiftBloc, ShiftState>(
'emits [Loading, Loaded] when Load succeeds',
build: () {
when(() => mockRepository.getShifts()).thenAnswer(
(_) async => Right([Shift(id: '1', name: 'Test')]),
);
return bloc;
},
act: (bloc) => bloc.add(LoadShift()),
expect: () => [
ShiftLoading(),
isA<ShiftLoaded>(),
],
);
});
}
- Lazy Loading: Load data on demand
- Pagination: Implement pagination for large datasets
- Caching: Cache frequently accessed data
- Indexing: Use Isar indexes for fast queries
- Background Sync: Sync in background to avoid blocking UI
๐ Security
- Authorization: Check user permissions before operations
- Data Encryption: Sensitive data encrypted in Isar
- Input Validation: Validate all user inputs
- Audit Trail: Log all operations for audit
- Use Cupertino widgets
- Follow iOS HIG
- Handle safe area insets
Android
- Use Material widgets
- Follow Material Design
- Handle back button
Last Updated: October 14, 2024
Status: โ
Production Ready