feat: dukung penjualan produk per timbangan

Produk kini punya cara jual `unit` (cacah) atau `weight` (timbangan),
mengikuti kontrak backend di docs/integration-weight-based-products.md.

- tipe SellBy, domain/infrastructure unit baru, dan endpoint /api/v1/units
- dialog input berat saat produk timbangan ditambahkan ke keranjang
- tiap penimbangan jadi baris keranjang sendiri; total baris dihitung
  berat x harga satuan, sedangkan produk cacah tetap digabung
- request ke backend mengirim quantity 1 dan membawa berat
- migrasi DB v3: kolom sell_by, unit_id, unit_name, unit_abbreviation
  di tabel products supaya satuan tetap tampil saat offline
- tampilan keranjang, struk, split bill, void, dan refund memakai berat
- test/weight_based_products_test.dart menutup perilaku di atas (9 test)

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
efrilm
2026-09-06 23:03:31 +07:00
co-authored by Claude Opus 5
parent 9777f63426
commit 1505a83dd3
72 changed files with 5388 additions and 584 deletions
@@ -24,6 +24,25 @@ class ListProductDto with _$ListProductDto {
);
}
/// Satuan yang menempel pada produk.
///
/// Backend hanya dijamin mengirim `unit_id`; sebagian response ikut menyertakan
/// objek `unit` lengkap. Kalau ada, dipakai langsung supaya POS tidak perlu
/// menunggu master satuan termuat.
@freezed
class ProductUnitDto with _$ProductUnitDto {
const ProductUnitDto._();
const factory ProductUnitDto({
@JsonKey(name: "id") String? id,
@JsonKey(name: "name") String? name,
@JsonKey(name: "abbreviation") String? abbreviation,
}) = _ProductUnitDto;
factory ProductUnitDto.fromJson(Map<String, dynamic> json) =>
_$ProductUnitDtoFromJson(json);
}
@freezed
class ProductDto with _$ProductDto {
const ProductDto._();
@@ -46,11 +65,28 @@ class ProductDto with _$ProductDto {
@JsonKey(name: "created_at") String? createdAt,
@JsonKey(name: "updated_at") String? updatedAt,
@JsonKey(name: "variants") List<ProductVariantDto>? variants,
@JsonKey(name: "sell_by") String? sellBy,
@JsonKey(name: "unit_id") String? unitId,
@JsonKey(name: "unit_name") String? unitName,
@JsonKey(name: "unit_abbreviation") String? unitAbbreviation,
@JsonKey(name: "unit") ProductUnitDto? unit,
}) = _ProductDto;
factory ProductDto.fromJson(Map<String, dynamic> json) =>
_$ProductDtoFromJson(json);
String get _resolvedUnitId =>
(unitId?.isNotEmpty == true ? unitId : unit?.id) ?? '';
String get _resolvedUnitName =>
(unitName?.isNotEmpty == true ? unitName : unit?.name) ?? '';
String get _resolvedUnitAbbreviation =>
(unitAbbreviation?.isNotEmpty == true
? unitAbbreviation
: unit?.abbreviation) ??
'';
/// Mapping ke domain
Product toDomain() => Product(
id: id ?? '',
@@ -70,6 +106,10 @@ class ProductDto with _$ProductDto {
createdAt: createdAt ?? '',
updatedAt: updatedAt ?? '',
variants: variants?.map((v) => v.toDomain()).toList() ?? [],
sellBy: SellBy.normalize(sellBy),
unitId: _resolvedUnitId,
unitName: _resolvedUnitName,
unitAbbreviation: _resolvedUnitAbbreviation,
);
Map<String, dynamic> toMap() => {
@@ -89,6 +129,10 @@ class ProductDto with _$ProductDto {
'is_active': isActive == true ? 1 : 0,
'created_at': createdAt,
'updated_at': updatedAt,
'sell_by': SellBy.normalize(sellBy),
'unit_id': _resolvedUnitId,
'unit_name': _resolvedUnitName,
'unit_abbreviation': _resolvedUnitAbbreviation,
};
factory ProductDto.fromMap(
@@ -117,6 +161,10 @@ class ProductDto with _$ProductDto {
createdAt: map['created_at'] as String?,
updatedAt: map['updated_at'] as String?,
variants: variants,
sellBy: map['sell_by'] as String?,
unitId: map['unit_id'] as String?,
unitName: map['unit_name'] as String?,
unitAbbreviation: map['unit_abbreviation'] as String?,
);
factory ProductDto.fromDomain(Product product) => ProductDto(
@@ -139,6 +187,10 @@ class ProductDto with _$ProductDto {
variants: product.variants
.map((v) => ProductVariantDto.fromDomain(v))
.toList(),
sellBy: product.sellBy,
unitId: product.unitId,
unitName: product.unitName,
unitAbbreviation: product.unitAbbreviation,
);
}
@@ -2,6 +2,7 @@ import 'dart:convert';
import 'package:freezed_annotation/freezed_annotation.dart';
import '../../common/types/sell_by.dart';
import '../../domain/product/product.dart';
part 'product_dtos.freezed.dart';
@@ -293,6 +293,223 @@ abstract class _ListProductDto extends ListProductDto {
throw _privateConstructorUsedError;
}
ProductUnitDto _$ProductUnitDtoFromJson(Map<String, dynamic> json) {
return _ProductUnitDto.fromJson(json);
}
/// @nodoc
mixin _$ProductUnitDto {
@JsonKey(name: "id")
String? get id => throw _privateConstructorUsedError;
@JsonKey(name: "name")
String? get name => throw _privateConstructorUsedError;
@JsonKey(name: "abbreviation")
String? get abbreviation => throw _privateConstructorUsedError;
/// Serializes this ProductUnitDto to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
/// Create a copy of ProductUnitDto
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$ProductUnitDtoCopyWith<ProductUnitDto> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $ProductUnitDtoCopyWith<$Res> {
factory $ProductUnitDtoCopyWith(
ProductUnitDto value,
$Res Function(ProductUnitDto) then,
) = _$ProductUnitDtoCopyWithImpl<$Res, ProductUnitDto>;
@useResult
$Res call({
@JsonKey(name: "id") String? id,
@JsonKey(name: "name") String? name,
@JsonKey(name: "abbreviation") String? abbreviation,
});
}
/// @nodoc
class _$ProductUnitDtoCopyWithImpl<$Res, $Val extends ProductUnitDto>
implements $ProductUnitDtoCopyWith<$Res> {
_$ProductUnitDtoCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of ProductUnitDto
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? id = freezed,
Object? name = freezed,
Object? abbreviation = freezed,
}) {
return _then(
_value.copyWith(
id: freezed == id
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as String?,
name: freezed == name
? _value.name
: name // ignore: cast_nullable_to_non_nullable
as String?,
abbreviation: freezed == abbreviation
? _value.abbreviation
: abbreviation // ignore: cast_nullable_to_non_nullable
as String?,
)
as $Val,
);
}
}
/// @nodoc
abstract class _$$ProductUnitDtoImplCopyWith<$Res>
implements $ProductUnitDtoCopyWith<$Res> {
factory _$$ProductUnitDtoImplCopyWith(
_$ProductUnitDtoImpl value,
$Res Function(_$ProductUnitDtoImpl) then,
) = __$$ProductUnitDtoImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({
@JsonKey(name: "id") String? id,
@JsonKey(name: "name") String? name,
@JsonKey(name: "abbreviation") String? abbreviation,
});
}
/// @nodoc
class __$$ProductUnitDtoImplCopyWithImpl<$Res>
extends _$ProductUnitDtoCopyWithImpl<$Res, _$ProductUnitDtoImpl>
implements _$$ProductUnitDtoImplCopyWith<$Res> {
__$$ProductUnitDtoImplCopyWithImpl(
_$ProductUnitDtoImpl _value,
$Res Function(_$ProductUnitDtoImpl) _then,
) : super(_value, _then);
/// Create a copy of ProductUnitDto
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? id = freezed,
Object? name = freezed,
Object? abbreviation = freezed,
}) {
return _then(
_$ProductUnitDtoImpl(
id: freezed == id
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as String?,
name: freezed == name
? _value.name
: name // ignore: cast_nullable_to_non_nullable
as String?,
abbreviation: freezed == abbreviation
? _value.abbreviation
: abbreviation // ignore: cast_nullable_to_non_nullable
as String?,
),
);
}
}
/// @nodoc
@JsonSerializable()
class _$ProductUnitDtoImpl extends _ProductUnitDto {
const _$ProductUnitDtoImpl({
@JsonKey(name: "id") this.id,
@JsonKey(name: "name") this.name,
@JsonKey(name: "abbreviation") this.abbreviation,
}) : super._();
factory _$ProductUnitDtoImpl.fromJson(Map<String, dynamic> json) =>
_$$ProductUnitDtoImplFromJson(json);
@override
@JsonKey(name: "id")
final String? id;
@override
@JsonKey(name: "name")
final String? name;
@override
@JsonKey(name: "abbreviation")
final String? abbreviation;
@override
String toString() {
return 'ProductUnitDto(id: $id, name: $name, abbreviation: $abbreviation)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$ProductUnitDtoImpl &&
(identical(other.id, id) || other.id == id) &&
(identical(other.name, name) || other.name == name) &&
(identical(other.abbreviation, abbreviation) ||
other.abbreviation == abbreviation));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType, id, name, abbreviation);
/// Create a copy of ProductUnitDto
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$ProductUnitDtoImplCopyWith<_$ProductUnitDtoImpl> get copyWith =>
__$$ProductUnitDtoImplCopyWithImpl<_$ProductUnitDtoImpl>(
this,
_$identity,
);
@override
Map<String, dynamic> toJson() {
return _$$ProductUnitDtoImplToJson(this);
}
}
abstract class _ProductUnitDto extends ProductUnitDto {
const factory _ProductUnitDto({
@JsonKey(name: "id") final String? id,
@JsonKey(name: "name") final String? name,
@JsonKey(name: "abbreviation") final String? abbreviation,
}) = _$ProductUnitDtoImpl;
const _ProductUnitDto._() : super._();
factory _ProductUnitDto.fromJson(Map<String, dynamic> json) =
_$ProductUnitDtoImpl.fromJson;
@override
@JsonKey(name: "id")
String? get id;
@override
@JsonKey(name: "name")
String? get name;
@override
@JsonKey(name: "abbreviation")
String? get abbreviation;
/// Create a copy of ProductUnitDto
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$ProductUnitDtoImplCopyWith<_$ProductUnitDtoImpl> get copyWith =>
throw _privateConstructorUsedError;
}
ProductDto _$ProductDtoFromJson(Map<String, dynamic> json) {
return _ProductDto.fromJson(json);
}
@@ -333,6 +550,16 @@ mixin _$ProductDto {
String? get updatedAt => throw _privateConstructorUsedError;
@JsonKey(name: "variants")
List<ProductVariantDto>? get variants => throw _privateConstructorUsedError;
@JsonKey(name: "sell_by")
String? get sellBy => throw _privateConstructorUsedError;
@JsonKey(name: "unit_id")
String? get unitId => throw _privateConstructorUsedError;
@JsonKey(name: "unit_name")
String? get unitName => throw _privateConstructorUsedError;
@JsonKey(name: "unit_abbreviation")
String? get unitAbbreviation => throw _privateConstructorUsedError;
@JsonKey(name: "unit")
ProductUnitDto? get unit => throw _privateConstructorUsedError;
/// Serializes this ProductDto to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@@ -369,7 +596,14 @@ abstract class $ProductDtoCopyWith<$Res> {
@JsonKey(name: "created_at") String? createdAt,
@JsonKey(name: "updated_at") String? updatedAt,
@JsonKey(name: "variants") List<ProductVariantDto>? variants,
@JsonKey(name: "sell_by") String? sellBy,
@JsonKey(name: "unit_id") String? unitId,
@JsonKey(name: "unit_name") String? unitName,
@JsonKey(name: "unit_abbreviation") String? unitAbbreviation,
@JsonKey(name: "unit") ProductUnitDto? unit,
});
$ProductUnitDtoCopyWith<$Res>? get unit;
}
/// @nodoc
@@ -404,6 +638,11 @@ class _$ProductDtoCopyWithImpl<$Res, $Val extends ProductDto>
Object? createdAt = freezed,
Object? updatedAt = freezed,
Object? variants = freezed,
Object? sellBy = freezed,
Object? unitId = freezed,
Object? unitName = freezed,
Object? unitAbbreviation = freezed,
Object? unit = freezed,
}) {
return _then(
_value.copyWith(
@@ -475,10 +714,44 @@ class _$ProductDtoCopyWithImpl<$Res, $Val extends ProductDto>
? _value.variants
: variants // ignore: cast_nullable_to_non_nullable
as List<ProductVariantDto>?,
sellBy: freezed == sellBy
? _value.sellBy
: sellBy // ignore: cast_nullable_to_non_nullable
as String?,
unitId: freezed == unitId
? _value.unitId
: unitId // ignore: cast_nullable_to_non_nullable
as String?,
unitName: freezed == unitName
? _value.unitName
: unitName // ignore: cast_nullable_to_non_nullable
as String?,
unitAbbreviation: freezed == unitAbbreviation
? _value.unitAbbreviation
: unitAbbreviation // ignore: cast_nullable_to_non_nullable
as String?,
unit: freezed == unit
? _value.unit
: unit // ignore: cast_nullable_to_non_nullable
as ProductUnitDto?,
)
as $Val,
);
}
/// Create a copy of ProductDto
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$ProductUnitDtoCopyWith<$Res>? get unit {
if (_value.unit == null) {
return null;
}
return $ProductUnitDtoCopyWith<$Res>(_value.unit!, (value) {
return _then(_value.copyWith(unit: value) as $Val);
});
}
}
/// @nodoc
@@ -508,7 +781,15 @@ abstract class _$$ProductDtoImplCopyWith<$Res>
@JsonKey(name: "created_at") String? createdAt,
@JsonKey(name: "updated_at") String? updatedAt,
@JsonKey(name: "variants") List<ProductVariantDto>? variants,
@JsonKey(name: "sell_by") String? sellBy,
@JsonKey(name: "unit_id") String? unitId,
@JsonKey(name: "unit_name") String? unitName,
@JsonKey(name: "unit_abbreviation") String? unitAbbreviation,
@JsonKey(name: "unit") ProductUnitDto? unit,
});
@override
$ProductUnitDtoCopyWith<$Res>? get unit;
}
/// @nodoc
@@ -542,6 +823,11 @@ class __$$ProductDtoImplCopyWithImpl<$Res>
Object? createdAt = freezed,
Object? updatedAt = freezed,
Object? variants = freezed,
Object? sellBy = freezed,
Object? unitId = freezed,
Object? unitName = freezed,
Object? unitAbbreviation = freezed,
Object? unit = freezed,
}) {
return _then(
_$ProductDtoImpl(
@@ -613,6 +899,26 @@ class __$$ProductDtoImplCopyWithImpl<$Res>
? _value._variants
: variants // ignore: cast_nullable_to_non_nullable
as List<ProductVariantDto>?,
sellBy: freezed == sellBy
? _value.sellBy
: sellBy // ignore: cast_nullable_to_non_nullable
as String?,
unitId: freezed == unitId
? _value.unitId
: unitId // ignore: cast_nullable_to_non_nullable
as String?,
unitName: freezed == unitName
? _value.unitName
: unitName // ignore: cast_nullable_to_non_nullable
as String?,
unitAbbreviation: freezed == unitAbbreviation
? _value.unitAbbreviation
: unitAbbreviation // ignore: cast_nullable_to_non_nullable
as String?,
unit: freezed == unit
? _value.unit
: unit // ignore: cast_nullable_to_non_nullable
as ProductUnitDto?,
),
);
}
@@ -639,6 +945,11 @@ class _$ProductDtoImpl extends _ProductDto {
@JsonKey(name: "created_at") this.createdAt,
@JsonKey(name: "updated_at") this.updatedAt,
@JsonKey(name: "variants") final List<ProductVariantDto>? variants,
@JsonKey(name: "sell_by") this.sellBy,
@JsonKey(name: "unit_id") this.unitId,
@JsonKey(name: "unit_name") this.unitName,
@JsonKey(name: "unit_abbreviation") this.unitAbbreviation,
@JsonKey(name: "unit") this.unit,
}) : _metadata = metadata,
_variants = variants,
super._();
@@ -713,9 +1024,25 @@ class _$ProductDtoImpl extends _ProductDto {
return EqualUnmodifiableListView(value);
}
@override
@JsonKey(name: "sell_by")
final String? sellBy;
@override
@JsonKey(name: "unit_id")
final String? unitId;
@override
@JsonKey(name: "unit_name")
final String? unitName;
@override
@JsonKey(name: "unit_abbreviation")
final String? unitAbbreviation;
@override
@JsonKey(name: "unit")
final ProductUnitDto? unit;
@override
String toString() {
return 'ProductDto(id: $id, organizationId: $organizationId, categoryId: $categoryId, sku: $sku, name: $name, description: $description, price: $price, cost: $cost, businessType: $businessType, imageUrl: $imageUrl, printerType: $printerType, printToChecker: $printToChecker, metadata: $metadata, isActive: $isActive, createdAt: $createdAt, updatedAt: $updatedAt, variants: $variants)';
return 'ProductDto(id: $id, organizationId: $organizationId, categoryId: $categoryId, sku: $sku, name: $name, description: $description, price: $price, cost: $cost, businessType: $businessType, imageUrl: $imageUrl, printerType: $printerType, printToChecker: $printToChecker, metadata: $metadata, isActive: $isActive, createdAt: $createdAt, updatedAt: $updatedAt, variants: $variants, sellBy: $sellBy, unitId: $unitId, unitName: $unitName, unitAbbreviation: $unitAbbreviation, unit: $unit)';
}
@override
@@ -749,12 +1076,19 @@ class _$ProductDtoImpl extends _ProductDto {
other.createdAt == createdAt) &&
(identical(other.updatedAt, updatedAt) ||
other.updatedAt == updatedAt) &&
const DeepCollectionEquality().equals(other._variants, _variants));
const DeepCollectionEquality().equals(other._variants, _variants) &&
(identical(other.sellBy, sellBy) || other.sellBy == sellBy) &&
(identical(other.unitId, unitId) || other.unitId == unitId) &&
(identical(other.unitName, unitName) ||
other.unitName == unitName) &&
(identical(other.unitAbbreviation, unitAbbreviation) ||
other.unitAbbreviation == unitAbbreviation) &&
(identical(other.unit, unit) || other.unit == unit));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(
int get hashCode => Object.hashAll([
runtimeType,
id,
organizationId,
@@ -773,7 +1107,12 @@ class _$ProductDtoImpl extends _ProductDto {
createdAt,
updatedAt,
const DeepCollectionEquality().hash(_variants),
);
sellBy,
unitId,
unitName,
unitAbbreviation,
unit,
]);
/// Create a copy of ProductDto
/// with the given fields replaced by the non-null parameter values.
@@ -808,6 +1147,11 @@ abstract class _ProductDto extends ProductDto {
@JsonKey(name: "created_at") final String? createdAt,
@JsonKey(name: "updated_at") final String? updatedAt,
@JsonKey(name: "variants") final List<ProductVariantDto>? variants,
@JsonKey(name: "sell_by") final String? sellBy,
@JsonKey(name: "unit_id") final String? unitId,
@JsonKey(name: "unit_name") final String? unitName,
@JsonKey(name: "unit_abbreviation") final String? unitAbbreviation,
@JsonKey(name: "unit") final ProductUnitDto? unit,
}) = _$ProductDtoImpl;
const _ProductDto._() : super._();
@@ -865,6 +1209,21 @@ abstract class _ProductDto extends ProductDto {
@override
@JsonKey(name: "variants")
List<ProductVariantDto>? get variants;
@override
@JsonKey(name: "sell_by")
String? get sellBy;
@override
@JsonKey(name: "unit_id")
String? get unitId;
@override
@JsonKey(name: "unit_name")
String? get unitName;
@override
@JsonKey(name: "unit_abbreviation")
String? get unitAbbreviation;
@override
@JsonKey(name: "unit")
ProductUnitDto? get unit;
/// Create a copy of ProductDto
/// with the given fields replaced by the non-null parameter values.
@@ -27,6 +27,21 @@ Map<String, dynamic> _$$ListProductDtoImplToJson(
'total_pages': instance.totalPages,
};
_$ProductUnitDtoImpl _$$ProductUnitDtoImplFromJson(Map<String, dynamic> json) =>
_$ProductUnitDtoImpl(
id: json['id'] as String?,
name: json['name'] as String?,
abbreviation: json['abbreviation'] as String?,
);
Map<String, dynamic> _$$ProductUnitDtoImplToJson(
_$ProductUnitDtoImpl instance,
) => <String, dynamic>{
'id': instance.id,
'name': instance.name,
'abbreviation': instance.abbreviation,
};
_$ProductDtoImpl _$$ProductDtoImplFromJson(Map<String, dynamic> json) =>
_$ProductDtoImpl(
id: json['id'] as String?,
@@ -48,6 +63,13 @@ _$ProductDtoImpl _$$ProductDtoImplFromJson(Map<String, dynamic> json) =>
variants: (json['variants'] as List<dynamic>?)
?.map((e) => ProductVariantDto.fromJson(e as Map<String, dynamic>))
.toList(),
sellBy: json['sell_by'] as String?,
unitId: json['unit_id'] as String?,
unitName: json['unit_name'] as String?,
unitAbbreviation: json['unit_abbreviation'] as String?,
unit: json['unit'] == null
? null
: ProductUnitDto.fromJson(json['unit'] as Map<String, dynamic>),
);
Map<String, dynamic> _$$ProductDtoImplToJson(_$ProductDtoImpl instance) =>
@@ -69,6 +91,11 @@ Map<String, dynamic> _$$ProductDtoImplToJson(_$ProductDtoImpl instance) =>
'created_at': instance.createdAt,
'updated_at': instance.updatedAt,
'variants': instance.variants,
'sell_by': instance.sellBy,
'unit_id': instance.unitId,
'unit_name': instance.unitName,
'unit_abbreviation': instance.unitAbbreviation,
'unit': instance.unit,
};
_$ProductVariantDtoImpl _$$ProductVariantDtoImplFromJson(
@@ -3,6 +3,7 @@ import 'dart:developer';
import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';
import '../../../common/service/unit_registry.dart';
import '../../../domain/product/product.dart';
import '../datasources/local_data_provider.dart';
import '../datasources/remote_data_provider.dart';
@@ -12,10 +13,38 @@ import '../product_dtos.dart';
class ProductRepository implements IProductRepository {
final ProductRemoteDataProvider _remoteDataProvider;
final ProductLocalDataProvider _localDataProvider;
final UnitRegistry _unitRegistry;
final _logName = 'ProductRepository';
ProductRepository(this._remoteDataProvider, this._localDataProvider);
ProductRepository(
this._remoteDataProvider,
this._localDataProvider,
this._unitRegistry,
);
/// Response produk hanya dijamin membawa `unit_id`. Lengkapi nama dan
/// singkatannya dari master satuan supaya keranjang bisa menulis "4,2 ons".
Product _withUnit(Product product) {
if (!product.isSoldByWeight || product.unitId.isEmpty) return product;
if (product.unitAbbreviation.isNotEmpty && product.unitName.isNotEmpty) {
return product;
}
final unit = _unitRegistry.byId(product.unitId);
if (unit == null) return product;
return product.copyWith(
unitName: product.unitName.isNotEmpty ? product.unitName : unit.name,
unitAbbreviation: product.unitAbbreviation.isNotEmpty
? product.unitAbbreviation
: unit.abbreviation,
);
}
List<Product> _withUnits(List<Product> products) =>
products.map(_withUnit).toList();
@override
Future<Either<ProductFailure, Product>> getProductById(String id) async {
@@ -33,7 +62,7 @@ class ProductRepository implements IProductRepository {
);
}
final productDomain = product.data!.toDomain();
final productDomain = _withUnit(product.data!.toDomain());
log('✅ Product loaded: ${productDomain.name}', name: _logName);
return Right(productDomain);
@@ -85,7 +114,9 @@ class ProductRepository implements IProductRepository {
// 🧱 Bangun entity domain ListProduct
final result = ListProduct(
products: cachedProducts.data!.map((p) => p.toDomain()).toList(),
products: _withUnits(
cachedProducts.data!.map((p) => p.toDomain()).toList(),
),
totalCount: totalCount,
page: page,
limit: limit,
@@ -132,7 +163,9 @@ class ProductRepository implements IProductRepository {
return left(cachedProducts.error!);
}
final products = cachedProducts.data!.map((p) => p.toDomain()).toList();
final products = _withUnits(
cachedProducts.data!.map((p) => p.toDomain()).toList(),
);
final totalCount = await _localDataProvider.getTotalCount(
categoryId: categoryId,
@@ -180,7 +213,9 @@ class ProductRepository implements IProductRepository {
}
// ✅ Mapping ke domain entity (kalau hasilnya masih berupa DTO)
final products = results.data!.map((p) => p.toDomain()).toList();
final products = _withUnits(
results.data!.map((p) => p.toDomain()).toList(),
);
log(
'✅ Local search completed: ${products.length} results',
@@ -369,7 +404,7 @@ class ProductRepository implements IProductRepository {
final products = result.data!.toDomain();
return right(products);
return right(products.copyWith(products: _withUnits(products.products)));
} catch (e, s) {
log('getProducts', name: _logName, error: e, stackTrace: s);
return left(const ProductFailure.unexpectedError());