60 lines
1.6 KiB
Dart
60 lines
1.6 KiB
Dart
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 [];
|
||
|
|
}
|
||
|
|
}
|