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:
@@ -55,6 +55,12 @@ class OrderRemoteDataProvider {
|
||||
);
|
||||
|
||||
if (response.data['success'] == false) {
|
||||
// Pesan validasi backend menyebut nama produk, jadi diteruskan apa
|
||||
// adanya ke kasir alih-alih diganti pesan generik.
|
||||
final cause = extractApiErrorCause(response.data);
|
||||
if (cause != null) {
|
||||
return DC.error(OrderFailure.dynamicErrorMessage(cause));
|
||||
}
|
||||
return DC.error(const OrderFailure.unexpectedError());
|
||||
}
|
||||
|
||||
@@ -77,6 +83,12 @@ class OrderRemoteDataProvider {
|
||||
);
|
||||
|
||||
if (response.data['success'] == false) {
|
||||
// Pesan validasi backend menyebut nama produk, jadi diteruskan apa
|
||||
// adanya ke kasir alih-alih diganti pesan generik.
|
||||
final cause = extractApiErrorCause(response.data);
|
||||
if (cause != null) {
|
||||
return DC.error(OrderFailure.dynamicErrorMessage(cause));
|
||||
}
|
||||
return DC.error(const OrderFailure.unexpectedError());
|
||||
}
|
||||
|
||||
@@ -102,6 +114,12 @@ class OrderRemoteDataProvider {
|
||||
);
|
||||
|
||||
if (response.data['success'] == false) {
|
||||
// Pesan validasi backend menyebut nama produk, jadi diteruskan apa
|
||||
// adanya ke kasir alih-alih diganti pesan generik.
|
||||
final cause = extractApiErrorCause(response.data);
|
||||
if (cause != null) {
|
||||
return DC.error(OrderFailure.dynamicErrorMessage(cause));
|
||||
}
|
||||
return DC.error(const OrderFailure.unexpectedError());
|
||||
}
|
||||
|
||||
@@ -128,6 +146,12 @@ class OrderRemoteDataProvider {
|
||||
);
|
||||
|
||||
if (response.data['success'] == false) {
|
||||
// Pesan validasi backend menyebut nama produk, jadi diteruskan apa
|
||||
// adanya ke kasir alih-alih diganti pesan generik.
|
||||
final cause = extractApiErrorCause(response.data);
|
||||
if (cause != null) {
|
||||
return DC.error(OrderFailure.dynamicErrorMessage(cause));
|
||||
}
|
||||
return DC.error(const OrderFailure.unexpectedError());
|
||||
} else {
|
||||
final order = OrderDto.fromJson(
|
||||
@@ -171,6 +195,12 @@ class OrderRemoteDataProvider {
|
||||
);
|
||||
|
||||
if (response.data['success'] == false) {
|
||||
// Pesan validasi backend menyebut nama produk, jadi diteruskan apa
|
||||
// adanya ke kasir alih-alih diganti pesan generik.
|
||||
final cause = extractApiErrorCause(response.data);
|
||||
if (cause != null) {
|
||||
return DC.error(OrderFailure.dynamicErrorMessage(cause));
|
||||
}
|
||||
return DC.error(const OrderFailure.unexpectedError());
|
||||
}
|
||||
|
||||
@@ -201,6 +231,12 @@ class OrderRemoteDataProvider {
|
||||
);
|
||||
|
||||
if (response.data['success'] == false) {
|
||||
// Pesan validasi backend menyebut nama produk, jadi diteruskan apa
|
||||
// adanya ke kasir alih-alih diganti pesan generik.
|
||||
final cause = extractApiErrorCause(response.data);
|
||||
if (cause != null) {
|
||||
return DC.error(OrderFailure.dynamicErrorMessage(cause));
|
||||
}
|
||||
return DC.error(const OrderFailure.unexpectedError());
|
||||
}
|
||||
|
||||
@@ -239,6 +275,12 @@ class OrderRemoteDataProvider {
|
||||
);
|
||||
|
||||
if (response.data['success'] == false) {
|
||||
// Pesan validasi backend menyebut nama produk, jadi diteruskan apa
|
||||
// adanya ke kasir alih-alih diganti pesan generik.
|
||||
final cause = extractApiErrorCause(response.data);
|
||||
if (cause != null) {
|
||||
return DC.error(OrderFailure.dynamicErrorMessage(cause));
|
||||
}
|
||||
return DC.error(const OrderFailure.unexpectedError());
|
||||
}
|
||||
|
||||
@@ -262,6 +304,12 @@ class OrderRemoteDataProvider {
|
||||
);
|
||||
|
||||
if (response.data['success'] == false) {
|
||||
// Pesan validasi backend menyebut nama produk, jadi diteruskan apa
|
||||
// adanya ke kasir alih-alih diganti pesan generik.
|
||||
final cause = extractApiErrorCause(response.data);
|
||||
if (cause != null) {
|
||||
return DC.error(OrderFailure.dynamicErrorMessage(cause));
|
||||
}
|
||||
return DC.error(const OrderFailure.unexpectedError());
|
||||
}
|
||||
|
||||
@@ -290,6 +338,12 @@ class OrderRemoteDataProvider {
|
||||
);
|
||||
|
||||
if (response.data['success'] == false) {
|
||||
// Pesan validasi backend menyebut nama produk, jadi diteruskan apa
|
||||
// adanya ke kasir alih-alih diganti pesan generik.
|
||||
final cause = extractApiErrorCause(response.data);
|
||||
if (cause != null) {
|
||||
return DC.error(OrderFailure.dynamicErrorMessage(cause));
|
||||
}
|
||||
return DC.error(const OrderFailure.unexpectedError());
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ class AddItemOrderRequestDto with _$AddItemOrderRequestDto {
|
||||
@JsonKey(name: 'quantity') int? quantity,
|
||||
@JsonKey(name: 'unit_price') int? unitPrice,
|
||||
@JsonKey(name: 'notes') String? notes,
|
||||
@JsonKey(name: 'weight') double? weight,
|
||||
}) = _AddItemOrderRequestDto;
|
||||
|
||||
factory AddItemOrderRequestDto.fromJson(Map<String, dynamic> json) =>
|
||||
@@ -21,6 +22,7 @@ class AddItemOrderRequestDto with _$AddItemOrderRequestDto {
|
||||
quantity: quantity ?? 0,
|
||||
unitPrice: unitPrice ?? 0,
|
||||
notes: notes ?? '',
|
||||
weight: weight,
|
||||
);
|
||||
|
||||
factory AddItemOrderRequestDto.fromDomain(
|
||||
@@ -31,6 +33,7 @@ class AddItemOrderRequestDto with _$AddItemOrderRequestDto {
|
||||
quantity: addItemOrderRequest.quantity,
|
||||
unitPrice: addItemOrderRequest.unitPrice,
|
||||
notes: addItemOrderRequest.notes,
|
||||
weight: addItemOrderRequest.weight,
|
||||
);
|
||||
|
||||
Map<String, dynamic> toRequest() {
|
||||
@@ -45,6 +48,11 @@ class AddItemOrderRequestDto with _$AddItemOrderRequestDto {
|
||||
data['product_variant_id'] = productvariantId;
|
||||
}
|
||||
|
||||
// Produk satuan ditolak backend bila membawa berat.
|
||||
if (weight != null) {
|
||||
data['weight'] = weight;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,6 +118,10 @@ class OrderItemDto with _$OrderItemDto {
|
||||
@JsonKey(name: "paid_quantity") int? paidQuantity,
|
||||
@JsonKey(name: "category_id") String? categoryId,
|
||||
@JsonKey(name: "category_name") String? categoryName,
|
||||
@JsonKey(name: "weight") double? weight,
|
||||
@JsonKey(name: "unit_id") String? unitId,
|
||||
@JsonKey(name: "unit_name") String? unitName,
|
||||
@JsonKey(name: "unit_abbreviation") String? unitAbbreviation,
|
||||
}) = _OrderItemDto;
|
||||
|
||||
factory OrderItemDto.fromJson(Map<String, dynamic> json) =>
|
||||
@@ -144,6 +148,10 @@ class OrderItemDto with _$OrderItemDto {
|
||||
paidQuantity: paidQuantity ?? 0,
|
||||
categoryId: categoryId ?? '',
|
||||
categoryName: categoryName ?? '',
|
||||
weight: weight,
|
||||
unitId: unitId ?? '',
|
||||
unitName: unitName ?? '',
|
||||
unitAbbreviation: unitAbbreviation ?? '',
|
||||
);
|
||||
|
||||
factory OrderItemDto.fromDomain(OrderItem orderItem) => OrderItemDto(
|
||||
@@ -166,5 +174,9 @@ class OrderItemDto with _$OrderItemDto {
|
||||
paidQuantity: orderItem.paidQuantity,
|
||||
categoryId: orderItem.categoryId,
|
||||
categoryName: orderItem.categoryName,
|
||||
weight: orderItem.weight,
|
||||
unitId: orderItem.unitId,
|
||||
unitName: orderItem.unitName,
|
||||
unitAbbreviation: orderItem.unitAbbreviation,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -75,6 +75,7 @@ class OrderItemRequestDto with _$OrderItemRequestDto {
|
||||
@JsonKey(name: "quantity") int? quantity,
|
||||
@JsonKey(name: "unit_price") int? unitPrice,
|
||||
@JsonKey(name: "notes") String? notes,
|
||||
@JsonKey(name: "weight") double? weight,
|
||||
}) = _OrderItemRequestDto;
|
||||
|
||||
factory OrderItemRequestDto.fromJson(Map<String, dynamic> json) =>
|
||||
@@ -87,6 +88,7 @@ class OrderItemRequestDto with _$OrderItemRequestDto {
|
||||
quantity: quantity ?? 0,
|
||||
unitPrice: unitPrice ?? 0,
|
||||
notes: notes ?? '',
|
||||
weight: weight,
|
||||
);
|
||||
|
||||
factory OrderItemRequestDto.fromDomain(OrderItemRequest request) =>
|
||||
@@ -96,6 +98,7 @@ class OrderItemRequestDto with _$OrderItemRequestDto {
|
||||
quantity: request.quantity,
|
||||
unitPrice: request.unitPrice,
|
||||
notes: request.notes,
|
||||
weight: request.weight,
|
||||
);
|
||||
|
||||
Map<String, dynamic> toRequest() {
|
||||
@@ -110,6 +113,12 @@ class OrderItemRequestDto with _$OrderItemRequestDto {
|
||||
data["product_variant_id"] = productVariantId;
|
||||
}
|
||||
|
||||
// Produk satuan ditolak backend bila membawa berat, jadi field ini hanya
|
||||
// ikut saat memang ada hasil timbangan.
|
||||
if (weight != null) {
|
||||
data["weight"] = weight;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1177,6 +1177,14 @@ mixin _$OrderItemDto {
|
||||
String? get categoryId => throw _privateConstructorUsedError;
|
||||
@JsonKey(name: "category_name")
|
||||
String? get categoryName => throw _privateConstructorUsedError;
|
||||
@JsonKey(name: "weight")
|
||||
double? get weight => 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;
|
||||
|
||||
/// Serializes this OrderItemDto to a JSON map.
|
||||
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
|
||||
@@ -1215,6 +1223,10 @@ abstract class $OrderItemDtoCopyWith<$Res> {
|
||||
@JsonKey(name: "paid_quantity") int? paidQuantity,
|
||||
@JsonKey(name: "category_id") String? categoryId,
|
||||
@JsonKey(name: "category_name") String? categoryName,
|
||||
@JsonKey(name: "weight") double? weight,
|
||||
@JsonKey(name: "unit_id") String? unitId,
|
||||
@JsonKey(name: "unit_name") String? unitName,
|
||||
@JsonKey(name: "unit_abbreviation") String? unitAbbreviation,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1252,6 +1264,10 @@ class _$OrderItemDtoCopyWithImpl<$Res, $Val extends OrderItemDto>
|
||||
Object? paidQuantity = freezed,
|
||||
Object? categoryId = freezed,
|
||||
Object? categoryName = freezed,
|
||||
Object? weight = freezed,
|
||||
Object? unitId = freezed,
|
||||
Object? unitName = freezed,
|
||||
Object? unitAbbreviation = freezed,
|
||||
}) {
|
||||
return _then(
|
||||
_value.copyWith(
|
||||
@@ -1331,6 +1347,22 @@ class _$OrderItemDtoCopyWithImpl<$Res, $Val extends OrderItemDto>
|
||||
? _value.categoryName
|
||||
: categoryName // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
weight: freezed == weight
|
||||
? _value.weight
|
||||
: weight // ignore: cast_nullable_to_non_nullable
|
||||
as double?,
|
||||
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?,
|
||||
)
|
||||
as $Val,
|
||||
);
|
||||
@@ -1366,6 +1398,10 @@ abstract class _$$OrderItemDtoImplCopyWith<$Res>
|
||||
@JsonKey(name: "paid_quantity") int? paidQuantity,
|
||||
@JsonKey(name: "category_id") String? categoryId,
|
||||
@JsonKey(name: "category_name") String? categoryName,
|
||||
@JsonKey(name: "weight") double? weight,
|
||||
@JsonKey(name: "unit_id") String? unitId,
|
||||
@JsonKey(name: "unit_name") String? unitName,
|
||||
@JsonKey(name: "unit_abbreviation") String? unitAbbreviation,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1402,6 +1438,10 @@ class __$$OrderItemDtoImplCopyWithImpl<$Res>
|
||||
Object? paidQuantity = freezed,
|
||||
Object? categoryId = freezed,
|
||||
Object? categoryName = freezed,
|
||||
Object? weight = freezed,
|
||||
Object? unitId = freezed,
|
||||
Object? unitName = freezed,
|
||||
Object? unitAbbreviation = freezed,
|
||||
}) {
|
||||
return _then(
|
||||
_$OrderItemDtoImpl(
|
||||
@@ -1481,6 +1521,22 @@ class __$$OrderItemDtoImplCopyWithImpl<$Res>
|
||||
? _value.categoryName
|
||||
: categoryName // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
weight: freezed == weight
|
||||
? _value.weight
|
||||
: weight // ignore: cast_nullable_to_non_nullable
|
||||
as double?,
|
||||
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?,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -1509,6 +1565,10 @@ class _$OrderItemDtoImpl extends _OrderItemDto {
|
||||
@JsonKey(name: "paid_quantity") this.paidQuantity,
|
||||
@JsonKey(name: "category_id") this.categoryId,
|
||||
@JsonKey(name: "category_name") this.categoryName,
|
||||
@JsonKey(name: "weight") this.weight,
|
||||
@JsonKey(name: "unit_id") this.unitId,
|
||||
@JsonKey(name: "unit_name") this.unitName,
|
||||
@JsonKey(name: "unit_abbreviation") this.unitAbbreviation,
|
||||
}) : _modifiers = modifiers,
|
||||
super._();
|
||||
|
||||
@@ -1580,10 +1640,22 @@ class _$OrderItemDtoImpl extends _OrderItemDto {
|
||||
@override
|
||||
@JsonKey(name: "category_name")
|
||||
final String? categoryName;
|
||||
@override
|
||||
@JsonKey(name: "weight")
|
||||
final double? weight;
|
||||
@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
|
||||
String toString() {
|
||||
return 'OrderItemDto(id: $id, orderId: $orderId, productId: $productId, productName: $productName, productVariantId: $productVariantId, productVariantName: $productVariantName, quantity: $quantity, unitPrice: $unitPrice, totalPrice: $totalPrice, modifiers: $modifiers, notes: $notes, status: $status, createdAt: $createdAt, updatedAt: $updatedAt, printerType: $printerType, printToChecker: $printToChecker, paidQuantity: $paidQuantity, categoryId: $categoryId, categoryName: $categoryName)';
|
||||
return 'OrderItemDto(id: $id, orderId: $orderId, productId: $productId, productName: $productName, productVariantId: $productVariantId, productVariantName: $productVariantName, quantity: $quantity, unitPrice: $unitPrice, totalPrice: $totalPrice, modifiers: $modifiers, notes: $notes, status: $status, createdAt: $createdAt, updatedAt: $updatedAt, printerType: $printerType, printToChecker: $printToChecker, paidQuantity: $paidQuantity, categoryId: $categoryId, categoryName: $categoryName, weight: $weight, unitId: $unitId, unitName: $unitName, unitAbbreviation: $unitAbbreviation)';
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -1626,7 +1698,13 @@ class _$OrderItemDtoImpl extends _OrderItemDto {
|
||||
(identical(other.categoryId, categoryId) ||
|
||||
other.categoryId == categoryId) &&
|
||||
(identical(other.categoryName, categoryName) ||
|
||||
other.categoryName == categoryName));
|
||||
other.categoryName == categoryName) &&
|
||||
(identical(other.weight, weight) || other.weight == weight) &&
|
||||
(identical(other.unitId, unitId) || other.unitId == unitId) &&
|
||||
(identical(other.unitName, unitName) ||
|
||||
other.unitName == unitName) &&
|
||||
(identical(other.unitAbbreviation, unitAbbreviation) ||
|
||||
other.unitAbbreviation == unitAbbreviation));
|
||||
}
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@@ -1652,6 +1730,10 @@ class _$OrderItemDtoImpl extends _OrderItemDto {
|
||||
paidQuantity,
|
||||
categoryId,
|
||||
categoryName,
|
||||
weight,
|
||||
unitId,
|
||||
unitName,
|
||||
unitAbbreviation,
|
||||
]);
|
||||
|
||||
/// Create a copy of OrderItemDto
|
||||
@@ -1689,6 +1771,10 @@ abstract class _OrderItemDto extends OrderItemDto {
|
||||
@JsonKey(name: "paid_quantity") final int? paidQuantity,
|
||||
@JsonKey(name: "category_id") final String? categoryId,
|
||||
@JsonKey(name: "category_name") final String? categoryName,
|
||||
@JsonKey(name: "weight") final double? weight,
|
||||
@JsonKey(name: "unit_id") final String? unitId,
|
||||
@JsonKey(name: "unit_name") final String? unitName,
|
||||
@JsonKey(name: "unit_abbreviation") final String? unitAbbreviation,
|
||||
}) = _$OrderItemDtoImpl;
|
||||
const _OrderItemDto._() : super._();
|
||||
|
||||
@@ -1752,6 +1838,18 @@ abstract class _OrderItemDto extends OrderItemDto {
|
||||
@override
|
||||
@JsonKey(name: "category_name")
|
||||
String? get categoryName;
|
||||
@override
|
||||
@JsonKey(name: "weight")
|
||||
double? get weight;
|
||||
@override
|
||||
@JsonKey(name: "unit_id")
|
||||
String? get unitId;
|
||||
@override
|
||||
@JsonKey(name: "unit_name")
|
||||
String? get unitName;
|
||||
@override
|
||||
@JsonKey(name: "unit_abbreviation")
|
||||
String? get unitAbbreviation;
|
||||
|
||||
/// Create a copy of OrderItemDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@@ -2136,6 +2234,8 @@ mixin _$OrderItemRequestDto {
|
||||
int? get unitPrice => throw _privateConstructorUsedError;
|
||||
@JsonKey(name: "notes")
|
||||
String? get notes => throw _privateConstructorUsedError;
|
||||
@JsonKey(name: "weight")
|
||||
double? get weight => throw _privateConstructorUsedError;
|
||||
|
||||
/// Serializes this OrderItemRequestDto to a JSON map.
|
||||
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
|
||||
@@ -2160,6 +2260,7 @@ abstract class $OrderItemRequestDtoCopyWith<$Res> {
|
||||
@JsonKey(name: "quantity") int? quantity,
|
||||
@JsonKey(name: "unit_price") int? unitPrice,
|
||||
@JsonKey(name: "notes") String? notes,
|
||||
@JsonKey(name: "weight") double? weight,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2183,6 +2284,7 @@ class _$OrderItemRequestDtoCopyWithImpl<$Res, $Val extends OrderItemRequestDto>
|
||||
Object? quantity = freezed,
|
||||
Object? unitPrice = freezed,
|
||||
Object? notes = freezed,
|
||||
Object? weight = freezed,
|
||||
}) {
|
||||
return _then(
|
||||
_value.copyWith(
|
||||
@@ -2206,6 +2308,10 @@ class _$OrderItemRequestDtoCopyWithImpl<$Res, $Val extends OrderItemRequestDto>
|
||||
? _value.notes
|
||||
: notes // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
weight: freezed == weight
|
||||
? _value.weight
|
||||
: weight // ignore: cast_nullable_to_non_nullable
|
||||
as double?,
|
||||
)
|
||||
as $Val,
|
||||
);
|
||||
@@ -2227,6 +2333,7 @@ abstract class _$$OrderItemRequestDtoImplCopyWith<$Res>
|
||||
@JsonKey(name: "quantity") int? quantity,
|
||||
@JsonKey(name: "unit_price") int? unitPrice,
|
||||
@JsonKey(name: "notes") String? notes,
|
||||
@JsonKey(name: "weight") double? weight,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2249,6 +2356,7 @@ class __$$OrderItemRequestDtoImplCopyWithImpl<$Res>
|
||||
Object? quantity = freezed,
|
||||
Object? unitPrice = freezed,
|
||||
Object? notes = freezed,
|
||||
Object? weight = freezed,
|
||||
}) {
|
||||
return _then(
|
||||
_$OrderItemRequestDtoImpl(
|
||||
@@ -2272,6 +2380,10 @@ class __$$OrderItemRequestDtoImplCopyWithImpl<$Res>
|
||||
? _value.notes
|
||||
: notes // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
weight: freezed == weight
|
||||
? _value.weight
|
||||
: weight // ignore: cast_nullable_to_non_nullable
|
||||
as double?,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -2286,6 +2398,7 @@ class _$OrderItemRequestDtoImpl extends _OrderItemRequestDto {
|
||||
@JsonKey(name: "quantity") this.quantity,
|
||||
@JsonKey(name: "unit_price") this.unitPrice,
|
||||
@JsonKey(name: "notes") this.notes,
|
||||
@JsonKey(name: "weight") this.weight,
|
||||
}) : super._();
|
||||
|
||||
factory _$OrderItemRequestDtoImpl.fromJson(Map<String, dynamic> json) =>
|
||||
@@ -2306,10 +2419,13 @@ class _$OrderItemRequestDtoImpl extends _OrderItemRequestDto {
|
||||
@override
|
||||
@JsonKey(name: "notes")
|
||||
final String? notes;
|
||||
@override
|
||||
@JsonKey(name: "weight")
|
||||
final double? weight;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'OrderItemRequestDto(productId: $productId, productVariantId: $productVariantId, quantity: $quantity, unitPrice: $unitPrice, notes: $notes)';
|
||||
return 'OrderItemRequestDto(productId: $productId, productVariantId: $productVariantId, quantity: $quantity, unitPrice: $unitPrice, notes: $notes, weight: $weight)';
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -2325,7 +2441,8 @@ class _$OrderItemRequestDtoImpl extends _OrderItemRequestDto {
|
||||
other.quantity == quantity) &&
|
||||
(identical(other.unitPrice, unitPrice) ||
|
||||
other.unitPrice == unitPrice) &&
|
||||
(identical(other.notes, notes) || other.notes == notes));
|
||||
(identical(other.notes, notes) || other.notes == notes) &&
|
||||
(identical(other.weight, weight) || other.weight == weight));
|
||||
}
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@@ -2337,6 +2454,7 @@ class _$OrderItemRequestDtoImpl extends _OrderItemRequestDto {
|
||||
quantity,
|
||||
unitPrice,
|
||||
notes,
|
||||
weight,
|
||||
);
|
||||
|
||||
/// Create a copy of OrderItemRequestDto
|
||||
@@ -2363,6 +2481,7 @@ abstract class _OrderItemRequestDto extends OrderItemRequestDto {
|
||||
@JsonKey(name: "quantity") final int? quantity,
|
||||
@JsonKey(name: "unit_price") final int? unitPrice,
|
||||
@JsonKey(name: "notes") final String? notes,
|
||||
@JsonKey(name: "weight") final double? weight,
|
||||
}) = _$OrderItemRequestDtoImpl;
|
||||
const _OrderItemRequestDto._() : super._();
|
||||
|
||||
@@ -2384,6 +2503,9 @@ abstract class _OrderItemRequestDto extends OrderItemRequestDto {
|
||||
@override
|
||||
@JsonKey(name: "notes")
|
||||
String? get notes;
|
||||
@override
|
||||
@JsonKey(name: "weight")
|
||||
double? get weight;
|
||||
|
||||
/// Create a copy of OrderItemRequestDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@@ -4025,6 +4147,8 @@ mixin _$AddItemOrderRequestDto {
|
||||
int? get unitPrice => throw _privateConstructorUsedError;
|
||||
@JsonKey(name: 'notes')
|
||||
String? get notes => throw _privateConstructorUsedError;
|
||||
@JsonKey(name: 'weight')
|
||||
double? get weight => throw _privateConstructorUsedError;
|
||||
|
||||
/// Serializes this AddItemOrderRequestDto to a JSON map.
|
||||
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
|
||||
@@ -4049,6 +4173,7 @@ abstract class $AddItemOrderRequestDtoCopyWith<$Res> {
|
||||
@JsonKey(name: 'quantity') int? quantity,
|
||||
@JsonKey(name: 'unit_price') int? unitPrice,
|
||||
@JsonKey(name: 'notes') String? notes,
|
||||
@JsonKey(name: 'weight') double? weight,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4075,6 +4200,7 @@ class _$AddItemOrderRequestDtoCopyWithImpl<
|
||||
Object? quantity = freezed,
|
||||
Object? unitPrice = freezed,
|
||||
Object? notes = freezed,
|
||||
Object? weight = freezed,
|
||||
}) {
|
||||
return _then(
|
||||
_value.copyWith(
|
||||
@@ -4098,6 +4224,10 @@ class _$AddItemOrderRequestDtoCopyWithImpl<
|
||||
? _value.notes
|
||||
: notes // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
weight: freezed == weight
|
||||
? _value.weight
|
||||
: weight // ignore: cast_nullable_to_non_nullable
|
||||
as double?,
|
||||
)
|
||||
as $Val,
|
||||
);
|
||||
@@ -4119,6 +4249,7 @@ abstract class _$$AddItemOrderRequestDtoImplCopyWith<$Res>
|
||||
@JsonKey(name: 'quantity') int? quantity,
|
||||
@JsonKey(name: 'unit_price') int? unitPrice,
|
||||
@JsonKey(name: 'notes') String? notes,
|
||||
@JsonKey(name: 'weight') double? weight,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4142,6 +4273,7 @@ class __$$AddItemOrderRequestDtoImplCopyWithImpl<$Res>
|
||||
Object? quantity = freezed,
|
||||
Object? unitPrice = freezed,
|
||||
Object? notes = freezed,
|
||||
Object? weight = freezed,
|
||||
}) {
|
||||
return _then(
|
||||
_$AddItemOrderRequestDtoImpl(
|
||||
@@ -4165,6 +4297,10 @@ class __$$AddItemOrderRequestDtoImplCopyWithImpl<$Res>
|
||||
? _value.notes
|
||||
: notes // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
weight: freezed == weight
|
||||
? _value.weight
|
||||
: weight // ignore: cast_nullable_to_non_nullable
|
||||
as double?,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -4179,6 +4315,7 @@ class _$AddItemOrderRequestDtoImpl extends _AddItemOrderRequestDto {
|
||||
@JsonKey(name: 'quantity') this.quantity,
|
||||
@JsonKey(name: 'unit_price') this.unitPrice,
|
||||
@JsonKey(name: 'notes') this.notes,
|
||||
@JsonKey(name: 'weight') this.weight,
|
||||
}) : super._();
|
||||
|
||||
factory _$AddItemOrderRequestDtoImpl.fromJson(Map<String, dynamic> json) =>
|
||||
@@ -4199,10 +4336,13 @@ class _$AddItemOrderRequestDtoImpl extends _AddItemOrderRequestDto {
|
||||
@override
|
||||
@JsonKey(name: 'notes')
|
||||
final String? notes;
|
||||
@override
|
||||
@JsonKey(name: 'weight')
|
||||
final double? weight;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'AddItemOrderRequestDto(productId: $productId, productvariantId: $productvariantId, quantity: $quantity, unitPrice: $unitPrice, notes: $notes)';
|
||||
return 'AddItemOrderRequestDto(productId: $productId, productvariantId: $productvariantId, quantity: $quantity, unitPrice: $unitPrice, notes: $notes, weight: $weight)';
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -4218,7 +4358,8 @@ class _$AddItemOrderRequestDtoImpl extends _AddItemOrderRequestDto {
|
||||
other.quantity == quantity) &&
|
||||
(identical(other.unitPrice, unitPrice) ||
|
||||
other.unitPrice == unitPrice) &&
|
||||
(identical(other.notes, notes) || other.notes == notes));
|
||||
(identical(other.notes, notes) || other.notes == notes) &&
|
||||
(identical(other.weight, weight) || other.weight == weight));
|
||||
}
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@@ -4230,6 +4371,7 @@ class _$AddItemOrderRequestDtoImpl extends _AddItemOrderRequestDto {
|
||||
quantity,
|
||||
unitPrice,
|
||||
notes,
|
||||
weight,
|
||||
);
|
||||
|
||||
/// Create a copy of AddItemOrderRequestDto
|
||||
@@ -4257,6 +4399,7 @@ abstract class _AddItemOrderRequestDto extends AddItemOrderRequestDto {
|
||||
@JsonKey(name: 'quantity') final int? quantity,
|
||||
@JsonKey(name: 'unit_price') final int? unitPrice,
|
||||
@JsonKey(name: 'notes') final String? notes,
|
||||
@JsonKey(name: 'weight') final double? weight,
|
||||
}) = _$AddItemOrderRequestDtoImpl;
|
||||
const _AddItemOrderRequestDto._() : super._();
|
||||
|
||||
@@ -4278,6 +4421,9 @@ abstract class _AddItemOrderRequestDto extends AddItemOrderRequestDto {
|
||||
@override
|
||||
@JsonKey(name: 'notes')
|
||||
String? get notes;
|
||||
@override
|
||||
@JsonKey(name: 'weight')
|
||||
double? get weight;
|
||||
|
||||
/// Create a copy of AddItemOrderRequestDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
|
||||
@@ -113,6 +113,10 @@ _$OrderItemDtoImpl _$$OrderItemDtoImplFromJson(Map<String, dynamic> json) =>
|
||||
paidQuantity: (json['paid_quantity'] as num?)?.toInt(),
|
||||
categoryId: json['category_id'] as String?,
|
||||
categoryName: json['category_name'] as String?,
|
||||
weight: (json['weight'] as num?)?.toDouble(),
|
||||
unitId: json['unit_id'] as String?,
|
||||
unitName: json['unit_name'] as String?,
|
||||
unitAbbreviation: json['unit_abbreviation'] as String?,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$$OrderItemDtoImplToJson(_$OrderItemDtoImpl instance) =>
|
||||
@@ -136,6 +140,10 @@ Map<String, dynamic> _$$OrderItemDtoImplToJson(_$OrderItemDtoImpl instance) =>
|
||||
'paid_quantity': instance.paidQuantity,
|
||||
'category_id': instance.categoryId,
|
||||
'category_name': instance.categoryName,
|
||||
'weight': instance.weight,
|
||||
'unit_id': instance.unitId,
|
||||
'unit_name': instance.unitName,
|
||||
'unit_abbreviation': instance.unitAbbreviation,
|
||||
};
|
||||
|
||||
_$OrderRequestDtoImpl _$$OrderRequestDtoImplFromJson(
|
||||
@@ -174,6 +182,7 @@ _$OrderItemRequestDtoImpl _$$OrderItemRequestDtoImplFromJson(
|
||||
quantity: (json['quantity'] as num?)?.toInt(),
|
||||
unitPrice: (json['unit_price'] as num?)?.toInt(),
|
||||
notes: json['notes'] as String?,
|
||||
weight: (json['weight'] as num?)?.toDouble(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$$OrderItemRequestDtoImplToJson(
|
||||
@@ -184,6 +193,7 @@ Map<String, dynamic> _$$OrderItemRequestDtoImplToJson(
|
||||
'quantity': instance.quantity,
|
||||
'unit_price': instance.unitPrice,
|
||||
'notes': instance.notes,
|
||||
'weight': instance.weight,
|
||||
};
|
||||
|
||||
_$PaymentRequestDtoImpl _$$PaymentRequestDtoImplFromJson(
|
||||
@@ -317,6 +327,7 @@ _$AddItemOrderRequestDtoImpl _$$AddItemOrderRequestDtoImplFromJson(
|
||||
quantity: (json['quantity'] as num?)?.toInt(),
|
||||
unitPrice: (json['unit_price'] as num?)?.toInt(),
|
||||
notes: json['notes'] as String?,
|
||||
weight: (json['weight'] as num?)?.toDouble(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$$AddItemOrderRequestDtoImplToJson(
|
||||
@@ -327,4 +338,5 @@ Map<String, dynamic> _$$AddItemOrderRequestDtoImplToJson(
|
||||
'quantity': instance.quantity,
|
||||
'unit_price': instance.unitPrice,
|
||||
'notes': instance.notes,
|
||||
'weight': instance.weight,
|
||||
};
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../../common/constant/local_storage_key.dart';
|
||||
import '../unit_dtos.dart';
|
||||
|
||||
/// Satuan berjumlah sedikit dan jarang berubah, jadi cukup disimpan sebagai
|
||||
/// satu blob JSON di SharedPreferences — tidak perlu tabel sqflite sendiri.
|
||||
@injectable
|
||||
class UnitLocalDataProvider {
|
||||
final SharedPreferences _sharedPreferences;
|
||||
|
||||
UnitLocalDataProvider(this._sharedPreferences);
|
||||
|
||||
Future<void> saveUnits(List<UnitDto> units) async {
|
||||
final jsonString = jsonEncode(units.map((e) => e.toJson()).toList());
|
||||
await _sharedPreferences.setString(LocalStorageKey.units, jsonString);
|
||||
}
|
||||
|
||||
List<UnitDto> getUnits() {
|
||||
final jsonString = _sharedPreferences.getString(LocalStorageKey.units);
|
||||
if (jsonString == null || jsonString.isEmpty) return const [];
|
||||
|
||||
final decoded = jsonDecode(jsonString);
|
||||
if (decoded is! List) return const [];
|
||||
|
||||
return decoded
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map(UnitDto.fromJson)
|
||||
.toList();
|
||||
}
|
||||
|
||||
Future<void> clearUnits() async {
|
||||
await _sharedPreferences.remove(LocalStorageKey.units);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:data_channel/data_channel.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
import '../../../common/api/api_client.dart';
|
||||
import '../../../common/api/api_failure.dart';
|
||||
import '../../../common/function/app_function.dart';
|
||||
import '../../../common/url/api_path.dart';
|
||||
import '../../../domain/unit/unit.dart';
|
||||
import '../unit_dtos.dart';
|
||||
|
||||
@injectable
|
||||
class UnitRemoteDataProvider {
|
||||
final ApiClient _apiClient;
|
||||
final _logName = 'UnitRemoteDataProvider';
|
||||
|
||||
UnitRemoteDataProvider(this._apiClient);
|
||||
|
||||
Future<DC<UnitFailure, List<UnitDto>>> fetchUnits() async {
|
||||
try {
|
||||
final response = await _apiClient.get(
|
||||
ApiPath.units,
|
||||
headers: getAuthorizationHeader(),
|
||||
);
|
||||
|
||||
if (response.data['data'] == null) {
|
||||
return DC.error(const UnitFailure.empty());
|
||||
}
|
||||
|
||||
return DC.data(UnitDto.listFromResponse(response.data['data']));
|
||||
} on ApiFailure catch (e, s) {
|
||||
log('fetchUnitsError', name: _logName, error: e, stackTrace: s);
|
||||
return DC.error(UnitFailure.serverError(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
part of '../unit_dtos.dart';
|
||||
|
||||
@freezed
|
||||
class UnitDto with _$UnitDto {
|
||||
const UnitDto._();
|
||||
|
||||
const factory UnitDto({
|
||||
@JsonKey(name: "id") String? id,
|
||||
@JsonKey(name: "organization_id") String? organizationId,
|
||||
@JsonKey(name: "name") String? name,
|
||||
@JsonKey(name: "abbreviation") String? abbreviation,
|
||||
@JsonKey(name: "is_active") bool? isActive,
|
||||
}) = _UnitDto;
|
||||
|
||||
factory UnitDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$UnitDtoFromJson(json);
|
||||
|
||||
ProductUnit toDomain() => ProductUnit(
|
||||
id: id ?? '',
|
||||
organizationId: organizationId ?? '',
|
||||
name: name ?? '',
|
||||
abbreviation: abbreviation ?? '',
|
||||
isActive: isActive ?? true,
|
||||
);
|
||||
|
||||
factory UnitDto.fromDomain(ProductUnit unit) => UnitDto(
|
||||
id: unit.id,
|
||||
organizationId: unit.organizationId,
|
||||
name: unit.name,
|
||||
abbreviation: unit.abbreviation,
|
||||
isActive: unit.isActive,
|
||||
);
|
||||
|
||||
/// Response `/units` belum tentu berbentuk sama di setiap versi backend —
|
||||
/// bisa `data: [...]` atau `data: { units: [...] }`. Terima keduanya supaya
|
||||
/// POS tidak kehilangan satuan hanya karena pembungkusnya berbeda.
|
||||
static List<UnitDto> listFromResponse(dynamic data) {
|
||||
if (data is List) {
|
||||
return data
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map(UnitDto.fromJson)
|
||||
.toList();
|
||||
}
|
||||
|
||||
if (data is Map<String, dynamic>) {
|
||||
for (final key in const ['units', 'items', 'data']) {
|
||||
final value = data[key];
|
||||
if (value is List) {
|
||||
return value
|
||||
.whereType<Map<String, dynamic>>()
|
||||
.map(UnitDto.fromJson)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return const [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
import '../../../domain/unit/unit.dart';
|
||||
import '../datasources/local_data_provider.dart';
|
||||
import '../datasources/remote_data_provider.dart';
|
||||
|
||||
@Injectable(as: IUnitRepository)
|
||||
class UnitRepository implements IUnitRepository {
|
||||
final UnitRemoteDataProvider _remoteDataProvider;
|
||||
final UnitLocalDataProvider _localDataProvider;
|
||||
|
||||
final _logName = 'UnitRepository';
|
||||
|
||||
UnitRepository(this._remoteDataProvider, this._localDataProvider);
|
||||
|
||||
@override
|
||||
Future<Either<UnitFailure, List<ProductUnit>>> getUnits({
|
||||
bool forceRefresh = false,
|
||||
}) async {
|
||||
try {
|
||||
final cached = _localDataProvider.getUnits();
|
||||
|
||||
if (!forceRefresh && cached.isNotEmpty) {
|
||||
log('📏 ${cached.length} satuan dari cache', name: _logName);
|
||||
return right(cached.map((e) => e.toDomain()).toList());
|
||||
}
|
||||
|
||||
final result = await _remoteDataProvider.fetchUnits();
|
||||
|
||||
if (result.hasError) {
|
||||
// Offline bukan alasan untuk kehilangan satuan yang sudah pernah ada.
|
||||
if (cached.isNotEmpty) {
|
||||
log('⚠️ Gagal ambil satuan, pakai cache', name: _logName);
|
||||
return right(cached.map((e) => e.toDomain()).toList());
|
||||
}
|
||||
return left(result.error!);
|
||||
}
|
||||
|
||||
final units = result.data!;
|
||||
await _localDataProvider.saveUnits(units);
|
||||
|
||||
log('✅ ${units.length} satuan disinkronkan', name: _logName);
|
||||
return right(units.map((e) => e.toDomain()).toList());
|
||||
} catch (e, s) {
|
||||
log('❌ Error getUnits', name: _logName, error: e, stackTrace: s);
|
||||
return left(UnitFailure.localStorageError(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<ProductUnit>> cachedUnits() async =>
|
||||
_localDataProvider.getUnits().map((e) => e.toDomain()).toList();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
import '../../domain/unit/unit.dart';
|
||||
|
||||
part 'unit_dtos.freezed.dart';
|
||||
part 'unit_dtos.g.dart';
|
||||
|
||||
part 'dtos/unit_dto.dart';
|
||||
@@ -0,0 +1,280 @@
|
||||
// coverage:ignore-file
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
|
||||
|
||||
part of 'unit_dtos.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// FreezedGenerator
|
||||
// **************************************************************************
|
||||
|
||||
T _$identity<T>(T value) => value;
|
||||
|
||||
final _privateConstructorUsedError = UnsupportedError(
|
||||
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models',
|
||||
);
|
||||
|
||||
UnitDto _$UnitDtoFromJson(Map<String, dynamic> json) {
|
||||
return _UnitDto.fromJson(json);
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
mixin _$UnitDto {
|
||||
@JsonKey(name: "id")
|
||||
String? get id => throw _privateConstructorUsedError;
|
||||
@JsonKey(name: "organization_id")
|
||||
String? get organizationId => throw _privateConstructorUsedError;
|
||||
@JsonKey(name: "name")
|
||||
String? get name => throw _privateConstructorUsedError;
|
||||
@JsonKey(name: "abbreviation")
|
||||
String? get abbreviation => throw _privateConstructorUsedError;
|
||||
@JsonKey(name: "is_active")
|
||||
bool? get isActive => throw _privateConstructorUsedError;
|
||||
|
||||
/// Serializes this UnitDto to a JSON map.
|
||||
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
|
||||
|
||||
/// Create a copy of UnitDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
$UnitDtoCopyWith<UnitDto> get copyWith => throw _privateConstructorUsedError;
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class $UnitDtoCopyWith<$Res> {
|
||||
factory $UnitDtoCopyWith(UnitDto value, $Res Function(UnitDto) then) =
|
||||
_$UnitDtoCopyWithImpl<$Res, UnitDto>;
|
||||
@useResult
|
||||
$Res call({
|
||||
@JsonKey(name: "id") String? id,
|
||||
@JsonKey(name: "organization_id") String? organizationId,
|
||||
@JsonKey(name: "name") String? name,
|
||||
@JsonKey(name: "abbreviation") String? abbreviation,
|
||||
@JsonKey(name: "is_active") bool? isActive,
|
||||
});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class _$UnitDtoCopyWithImpl<$Res, $Val extends UnitDto>
|
||||
implements $UnitDtoCopyWith<$Res> {
|
||||
_$UnitDtoCopyWithImpl(this._value, this._then);
|
||||
|
||||
// ignore: unused_field
|
||||
final $Val _value;
|
||||
// ignore: unused_field
|
||||
final $Res Function($Val) _then;
|
||||
|
||||
/// Create a copy of UnitDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? id = freezed,
|
||||
Object? organizationId = freezed,
|
||||
Object? name = freezed,
|
||||
Object? abbreviation = freezed,
|
||||
Object? isActive = freezed,
|
||||
}) {
|
||||
return _then(
|
||||
_value.copyWith(
|
||||
id: freezed == id
|
||||
? _value.id
|
||||
: id // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
organizationId: freezed == organizationId
|
||||
? _value.organizationId
|
||||
: organizationId // 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?,
|
||||
isActive: freezed == isActive
|
||||
? _value.isActive
|
||||
: isActive // ignore: cast_nullable_to_non_nullable
|
||||
as bool?,
|
||||
)
|
||||
as $Val,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class _$$UnitDtoImplCopyWith<$Res> implements $UnitDtoCopyWith<$Res> {
|
||||
factory _$$UnitDtoImplCopyWith(
|
||||
_$UnitDtoImpl value,
|
||||
$Res Function(_$UnitDtoImpl) then,
|
||||
) = __$$UnitDtoImplCopyWithImpl<$Res>;
|
||||
@override
|
||||
@useResult
|
||||
$Res call({
|
||||
@JsonKey(name: "id") String? id,
|
||||
@JsonKey(name: "organization_id") String? organizationId,
|
||||
@JsonKey(name: "name") String? name,
|
||||
@JsonKey(name: "abbreviation") String? abbreviation,
|
||||
@JsonKey(name: "is_active") bool? isActive,
|
||||
});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class __$$UnitDtoImplCopyWithImpl<$Res>
|
||||
extends _$UnitDtoCopyWithImpl<$Res, _$UnitDtoImpl>
|
||||
implements _$$UnitDtoImplCopyWith<$Res> {
|
||||
__$$UnitDtoImplCopyWithImpl(
|
||||
_$UnitDtoImpl _value,
|
||||
$Res Function(_$UnitDtoImpl) _then,
|
||||
) : super(_value, _then);
|
||||
|
||||
/// Create a copy of UnitDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? id = freezed,
|
||||
Object? organizationId = freezed,
|
||||
Object? name = freezed,
|
||||
Object? abbreviation = freezed,
|
||||
Object? isActive = freezed,
|
||||
}) {
|
||||
return _then(
|
||||
_$UnitDtoImpl(
|
||||
id: freezed == id
|
||||
? _value.id
|
||||
: id // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
organizationId: freezed == organizationId
|
||||
? _value.organizationId
|
||||
: organizationId // 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?,
|
||||
isActive: freezed == isActive
|
||||
? _value.isActive
|
||||
: isActive // ignore: cast_nullable_to_non_nullable
|
||||
as bool?,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
@JsonSerializable()
|
||||
class _$UnitDtoImpl extends _UnitDto {
|
||||
const _$UnitDtoImpl({
|
||||
@JsonKey(name: "id") this.id,
|
||||
@JsonKey(name: "organization_id") this.organizationId,
|
||||
@JsonKey(name: "name") this.name,
|
||||
@JsonKey(name: "abbreviation") this.abbreviation,
|
||||
@JsonKey(name: "is_active") this.isActive,
|
||||
}) : super._();
|
||||
|
||||
factory _$UnitDtoImpl.fromJson(Map<String, dynamic> json) =>
|
||||
_$$UnitDtoImplFromJson(json);
|
||||
|
||||
@override
|
||||
@JsonKey(name: "id")
|
||||
final String? id;
|
||||
@override
|
||||
@JsonKey(name: "organization_id")
|
||||
final String? organizationId;
|
||||
@override
|
||||
@JsonKey(name: "name")
|
||||
final String? name;
|
||||
@override
|
||||
@JsonKey(name: "abbreviation")
|
||||
final String? abbreviation;
|
||||
@override
|
||||
@JsonKey(name: "is_active")
|
||||
final bool? isActive;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'UnitDto(id: $id, organizationId: $organizationId, name: $name, abbreviation: $abbreviation, isActive: $isActive)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) ||
|
||||
(other.runtimeType == runtimeType &&
|
||||
other is _$UnitDtoImpl &&
|
||||
(identical(other.id, id) || other.id == id) &&
|
||||
(identical(other.organizationId, organizationId) ||
|
||||
other.organizationId == organizationId) &&
|
||||
(identical(other.name, name) || other.name == name) &&
|
||||
(identical(other.abbreviation, abbreviation) ||
|
||||
other.abbreviation == abbreviation) &&
|
||||
(identical(other.isActive, isActive) ||
|
||||
other.isActive == isActive));
|
||||
}
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@override
|
||||
int get hashCode => Object.hash(
|
||||
runtimeType,
|
||||
id,
|
||||
organizationId,
|
||||
name,
|
||||
abbreviation,
|
||||
isActive,
|
||||
);
|
||||
|
||||
/// Create a copy of UnitDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@override
|
||||
@pragma('vm:prefer-inline')
|
||||
_$$UnitDtoImplCopyWith<_$UnitDtoImpl> get copyWith =>
|
||||
__$$UnitDtoImplCopyWithImpl<_$UnitDtoImpl>(this, _$identity);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return _$$UnitDtoImplToJson(this);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _UnitDto extends UnitDto {
|
||||
const factory _UnitDto({
|
||||
@JsonKey(name: "id") final String? id,
|
||||
@JsonKey(name: "organization_id") final String? organizationId,
|
||||
@JsonKey(name: "name") final String? name,
|
||||
@JsonKey(name: "abbreviation") final String? abbreviation,
|
||||
@JsonKey(name: "is_active") final bool? isActive,
|
||||
}) = _$UnitDtoImpl;
|
||||
const _UnitDto._() : super._();
|
||||
|
||||
factory _UnitDto.fromJson(Map<String, dynamic> json) = _$UnitDtoImpl.fromJson;
|
||||
|
||||
@override
|
||||
@JsonKey(name: "id")
|
||||
String? get id;
|
||||
@override
|
||||
@JsonKey(name: "organization_id")
|
||||
String? get organizationId;
|
||||
@override
|
||||
@JsonKey(name: "name")
|
||||
String? get name;
|
||||
@override
|
||||
@JsonKey(name: "abbreviation")
|
||||
String? get abbreviation;
|
||||
@override
|
||||
@JsonKey(name: "is_active")
|
||||
bool? get isActive;
|
||||
|
||||
/// Create a copy of UnitDto
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@override
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
_$$UnitDtoImplCopyWith<_$UnitDtoImpl> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'unit_dtos.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
_$UnitDtoImpl _$$UnitDtoImplFromJson(Map<String, dynamic> json) =>
|
||||
_$UnitDtoImpl(
|
||||
id: json['id'] as String?,
|
||||
organizationId: json['organization_id'] as String?,
|
||||
name: json['name'] as String?,
|
||||
abbreviation: json['abbreviation'] as String?,
|
||||
isActive: json['is_active'] as bool?,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$$UnitDtoImplToJson(_$UnitDtoImpl instance) =>
|
||||
<String, dynamic>{
|
||||
'id': instance.id,
|
||||
'organization_id': instance.organizationId,
|
||||
'name': instance.name,
|
||||
'abbreviation': instance.abbreviation,
|
||||
'is_active': instance.isActive,
|
||||
};
|
||||
Reference in New Issue
Block a user