Customer Point

This commit is contained in:
efrilm
2025-09-18 13:01:31 +07:00
parent a5d66c63b7
commit 73918430b2
22 changed files with 1970 additions and 98 deletions
@@ -0,0 +1,8 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import '../../domain/customer/customer.dart';
part 'customer_dtos.freezed.dart';
part 'customer_dtos.g.dart';
part 'dtos/customer_point_dto.dart';
@@ -0,0 +1,443 @@
// 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 'customer_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',
);
CustomerPointDto _$CustomerPointDtoFromJson(Map<String, dynamic> json) {
return _CustomerPointDto.fromJson(json);
}
/// @nodoc
mixin _$CustomerPointDto {
@JsonKey(name: 'status')
String? get status => throw _privateConstructorUsedError;
@JsonKey(name: 'message')
String? get message => throw _privateConstructorUsedError;
@JsonKey(name: 'data')
CustomerPointDataDto? get data => throw _privateConstructorUsedError;
/// Serializes this CustomerPointDto to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
/// Create a copy of CustomerPointDto
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$CustomerPointDtoCopyWith<CustomerPointDto> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $CustomerPointDtoCopyWith<$Res> {
factory $CustomerPointDtoCopyWith(
CustomerPointDto value,
$Res Function(CustomerPointDto) then,
) = _$CustomerPointDtoCopyWithImpl<$Res, CustomerPointDto>;
@useResult
$Res call({
@JsonKey(name: 'status') String? status,
@JsonKey(name: 'message') String? message,
@JsonKey(name: 'data') CustomerPointDataDto? data,
});
$CustomerPointDataDtoCopyWith<$Res>? get data;
}
/// @nodoc
class _$CustomerPointDtoCopyWithImpl<$Res, $Val extends CustomerPointDto>
implements $CustomerPointDtoCopyWith<$Res> {
_$CustomerPointDtoCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of CustomerPointDto
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? status = freezed,
Object? message = freezed,
Object? data = freezed,
}) {
return _then(
_value.copyWith(
status: freezed == status
? _value.status
: status // ignore: cast_nullable_to_non_nullable
as String?,
message: freezed == message
? _value.message
: message // ignore: cast_nullable_to_non_nullable
as String?,
data: freezed == data
? _value.data
: data // ignore: cast_nullable_to_non_nullable
as CustomerPointDataDto?,
)
as $Val,
);
}
/// Create a copy of CustomerPointDto
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$CustomerPointDataDtoCopyWith<$Res>? get data {
if (_value.data == null) {
return null;
}
return $CustomerPointDataDtoCopyWith<$Res>(_value.data!, (value) {
return _then(_value.copyWith(data: value) as $Val);
});
}
}
/// @nodoc
abstract class _$$CustomerPointDtoImplCopyWith<$Res>
implements $CustomerPointDtoCopyWith<$Res> {
factory _$$CustomerPointDtoImplCopyWith(
_$CustomerPointDtoImpl value,
$Res Function(_$CustomerPointDtoImpl) then,
) = __$$CustomerPointDtoImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({
@JsonKey(name: 'status') String? status,
@JsonKey(name: 'message') String? message,
@JsonKey(name: 'data') CustomerPointDataDto? data,
});
@override
$CustomerPointDataDtoCopyWith<$Res>? get data;
}
/// @nodoc
class __$$CustomerPointDtoImplCopyWithImpl<$Res>
extends _$CustomerPointDtoCopyWithImpl<$Res, _$CustomerPointDtoImpl>
implements _$$CustomerPointDtoImplCopyWith<$Res> {
__$$CustomerPointDtoImplCopyWithImpl(
_$CustomerPointDtoImpl _value,
$Res Function(_$CustomerPointDtoImpl) _then,
) : super(_value, _then);
/// Create a copy of CustomerPointDto
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? status = freezed,
Object? message = freezed,
Object? data = freezed,
}) {
return _then(
_$CustomerPointDtoImpl(
status: freezed == status
? _value.status
: status // ignore: cast_nullable_to_non_nullable
as String?,
message: freezed == message
? _value.message
: message // ignore: cast_nullable_to_non_nullable
as String?,
data: freezed == data
? _value.data
: data // ignore: cast_nullable_to_non_nullable
as CustomerPointDataDto?,
),
);
}
}
/// @nodoc
@JsonSerializable()
class _$CustomerPointDtoImpl extends _CustomerPointDto {
const _$CustomerPointDtoImpl({
@JsonKey(name: 'status') this.status,
@JsonKey(name: 'message') this.message,
@JsonKey(name: 'data') this.data,
}) : super._();
factory _$CustomerPointDtoImpl.fromJson(Map<String, dynamic> json) =>
_$$CustomerPointDtoImplFromJson(json);
@override
@JsonKey(name: 'status')
final String? status;
@override
@JsonKey(name: 'message')
final String? message;
@override
@JsonKey(name: 'data')
final CustomerPointDataDto? data;
@override
String toString() {
return 'CustomerPointDto(status: $status, message: $message, data: $data)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$CustomerPointDtoImpl &&
(identical(other.status, status) || other.status == status) &&
(identical(other.message, message) || other.message == message) &&
(identical(other.data, data) || other.data == data));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType, status, message, data);
/// Create a copy of CustomerPointDto
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$CustomerPointDtoImplCopyWith<_$CustomerPointDtoImpl> get copyWith =>
__$$CustomerPointDtoImplCopyWithImpl<_$CustomerPointDtoImpl>(
this,
_$identity,
);
@override
Map<String, dynamic> toJson() {
return _$$CustomerPointDtoImplToJson(this);
}
}
abstract class _CustomerPointDto extends CustomerPointDto {
const factory _CustomerPointDto({
@JsonKey(name: 'status') final String? status,
@JsonKey(name: 'message') final String? message,
@JsonKey(name: 'data') final CustomerPointDataDto? data,
}) = _$CustomerPointDtoImpl;
const _CustomerPointDto._() : super._();
factory _CustomerPointDto.fromJson(Map<String, dynamic> json) =
_$CustomerPointDtoImpl.fromJson;
@override
@JsonKey(name: 'status')
String? get status;
@override
@JsonKey(name: 'message')
String? get message;
@override
@JsonKey(name: 'data')
CustomerPointDataDto? get data;
/// Create a copy of CustomerPointDto
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$CustomerPointDtoImplCopyWith<_$CustomerPointDtoImpl> get copyWith =>
throw _privateConstructorUsedError;
}
CustomerPointDataDto _$CustomerPointDataDtoFromJson(Map<String, dynamic> json) {
return _CustomerPointDataDto.fromJson(json);
}
/// @nodoc
mixin _$CustomerPointDataDto {
@JsonKey(name: 'total_points')
int? get totalPoints => throw _privateConstructorUsedError;
@JsonKey(name: 'last_updated')
String? get lastUpdated => throw _privateConstructorUsedError;
/// Serializes this CustomerPointDataDto to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
/// Create a copy of CustomerPointDataDto
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$CustomerPointDataDtoCopyWith<CustomerPointDataDto> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $CustomerPointDataDtoCopyWith<$Res> {
factory $CustomerPointDataDtoCopyWith(
CustomerPointDataDto value,
$Res Function(CustomerPointDataDto) then,
) = _$CustomerPointDataDtoCopyWithImpl<$Res, CustomerPointDataDto>;
@useResult
$Res call({
@JsonKey(name: 'total_points') int? totalPoints,
@JsonKey(name: 'last_updated') String? lastUpdated,
});
}
/// @nodoc
class _$CustomerPointDataDtoCopyWithImpl<
$Res,
$Val extends CustomerPointDataDto
>
implements $CustomerPointDataDtoCopyWith<$Res> {
_$CustomerPointDataDtoCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of CustomerPointDataDto
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({Object? totalPoints = freezed, Object? lastUpdated = freezed}) {
return _then(
_value.copyWith(
totalPoints: freezed == totalPoints
? _value.totalPoints
: totalPoints // ignore: cast_nullable_to_non_nullable
as int?,
lastUpdated: freezed == lastUpdated
? _value.lastUpdated
: lastUpdated // ignore: cast_nullable_to_non_nullable
as String?,
)
as $Val,
);
}
}
/// @nodoc
abstract class _$$CustomerPointDataDtoImplCopyWith<$Res>
implements $CustomerPointDataDtoCopyWith<$Res> {
factory _$$CustomerPointDataDtoImplCopyWith(
_$CustomerPointDataDtoImpl value,
$Res Function(_$CustomerPointDataDtoImpl) then,
) = __$$CustomerPointDataDtoImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({
@JsonKey(name: 'total_points') int? totalPoints,
@JsonKey(name: 'last_updated') String? lastUpdated,
});
}
/// @nodoc
class __$$CustomerPointDataDtoImplCopyWithImpl<$Res>
extends _$CustomerPointDataDtoCopyWithImpl<$Res, _$CustomerPointDataDtoImpl>
implements _$$CustomerPointDataDtoImplCopyWith<$Res> {
__$$CustomerPointDataDtoImplCopyWithImpl(
_$CustomerPointDataDtoImpl _value,
$Res Function(_$CustomerPointDataDtoImpl) _then,
) : super(_value, _then);
/// Create a copy of CustomerPointDataDto
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({Object? totalPoints = freezed, Object? lastUpdated = freezed}) {
return _then(
_$CustomerPointDataDtoImpl(
totalPoints: freezed == totalPoints
? _value.totalPoints
: totalPoints // ignore: cast_nullable_to_non_nullable
as int?,
lastUpdated: freezed == lastUpdated
? _value.lastUpdated
: lastUpdated // ignore: cast_nullable_to_non_nullable
as String?,
),
);
}
}
/// @nodoc
@JsonSerializable()
class _$CustomerPointDataDtoImpl implements _CustomerPointDataDto {
const _$CustomerPointDataDtoImpl({
@JsonKey(name: 'total_points') this.totalPoints,
@JsonKey(name: 'last_updated') this.lastUpdated,
});
factory _$CustomerPointDataDtoImpl.fromJson(Map<String, dynamic> json) =>
_$$CustomerPointDataDtoImplFromJson(json);
@override
@JsonKey(name: 'total_points')
final int? totalPoints;
@override
@JsonKey(name: 'last_updated')
final String? lastUpdated;
@override
String toString() {
return 'CustomerPointDataDto(totalPoints: $totalPoints, lastUpdated: $lastUpdated)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$CustomerPointDataDtoImpl &&
(identical(other.totalPoints, totalPoints) ||
other.totalPoints == totalPoints) &&
(identical(other.lastUpdated, lastUpdated) ||
other.lastUpdated == lastUpdated));
}
@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType, totalPoints, lastUpdated);
/// Create a copy of CustomerPointDataDto
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$CustomerPointDataDtoImplCopyWith<_$CustomerPointDataDtoImpl>
get copyWith =>
__$$CustomerPointDataDtoImplCopyWithImpl<_$CustomerPointDataDtoImpl>(
this,
_$identity,
);
@override
Map<String, dynamic> toJson() {
return _$$CustomerPointDataDtoImplToJson(this);
}
}
abstract class _CustomerPointDataDto implements CustomerPointDataDto {
const factory _CustomerPointDataDto({
@JsonKey(name: 'total_points') final int? totalPoints,
@JsonKey(name: 'last_updated') final String? lastUpdated,
}) = _$CustomerPointDataDtoImpl;
factory _CustomerPointDataDto.fromJson(Map<String, dynamic> json) =
_$CustomerPointDataDtoImpl.fromJson;
@override
@JsonKey(name: 'total_points')
int? get totalPoints;
@override
@JsonKey(name: 'last_updated')
String? get lastUpdated;
/// Create a copy of CustomerPointDataDto
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$CustomerPointDataDtoImplCopyWith<_$CustomerPointDataDtoImpl>
get copyWith => throw _privateConstructorUsedError;
}
@@ -0,0 +1,39 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'customer_dtos.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_$CustomerPointDtoImpl _$$CustomerPointDtoImplFromJson(
Map<String, dynamic> json,
) => _$CustomerPointDtoImpl(
status: json['status'] as String?,
message: json['message'] as String?,
data: json['data'] == null
? null
: CustomerPointDataDto.fromJson(json['data'] as Map<String, dynamic>),
);
Map<String, dynamic> _$$CustomerPointDtoImplToJson(
_$CustomerPointDtoImpl instance,
) => <String, dynamic>{
'status': instance.status,
'message': instance.message,
'data': instance.data,
};
_$CustomerPointDataDtoImpl _$$CustomerPointDataDtoImplFromJson(
Map<String, dynamic> json,
) => _$CustomerPointDataDtoImpl(
totalPoints: (json['total_points'] as num?)?.toInt(),
lastUpdated: json['last_updated'] as String?,
);
Map<String, dynamic> _$$CustomerPointDataDtoImplToJson(
_$CustomerPointDataDtoImpl instance,
) => <String, dynamic>{
'total_points': instance.totalPoints,
'last_updated': instance.lastUpdated,
};
@@ -0,0 +1,50 @@
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/customer/customer.dart';
import '../customer_dtos.dart';
@injectable
class CustomerRemoteDataProvider {
final ApiClient _apiClient;
final String _logName = "CustomerRemoteDataProvider";
CustomerRemoteDataProvider(this._apiClient);
Future<DC<CustomerFailure, CustomerPointDto>> fetchCustomerPoint() async {
try {
final response = await _apiClient.get(
ApiPath.customerPoint,
headers: getAuthorizationHeader(),
);
if (response.data['code'] == 401) {
return DC.error(
CustomerFailure.serverError(
ApiFailure.unauthorized('Session Expired'),
),
);
}
if (response.data['status'] == false) {
return DC.error(
CustomerFailure.dynamicErrorMessage(
'Terjadi kesalahan coba lagi nanti',
),
);
}
final dto = CustomerPointDto.fromJson(response.data['data']);
return DC.data(dto);
} on ApiFailure catch (e, s) {
log('fetchCustomerPoint', name: _logName, error: e, stackTrace: s);
return DC.error(CustomerFailure.serverError(e));
}
}
}
@@ -0,0 +1,34 @@
part of '../customer_dtos.dart';
@freezed
class CustomerPointDto with _$CustomerPointDto {
const factory CustomerPointDto({
@JsonKey(name: 'status') String? status,
@JsonKey(name: 'message') String? message,
@JsonKey(name: 'data') CustomerPointDataDto? data,
}) = _CustomerPointDto;
factory CustomerPointDto.fromJson(Map<String, dynamic> json) =>
_$CustomerPointDtoFromJson(json);
const CustomerPointDto._();
/// mapping ke domain
CustomerPoint toDomain() => CustomerPoint(
status: status ?? '',
message: message ?? '',
totalPoints: data?.totalPoints ?? 0,
lastUpdated: data?.lastUpdated ?? '',
);
}
@freezed
class CustomerPointDataDto with _$CustomerPointDataDto {
const factory CustomerPointDataDto({
@JsonKey(name: 'total_points') int? totalPoints,
@JsonKey(name: 'last_updated') String? lastUpdated,
}) = _CustomerPointDataDto;
factory CustomerPointDataDto.fromJson(Map<String, dynamic> json) =>
_$CustomerPointDataDtoFromJson(json);
}
@@ -0,0 +1,33 @@
import 'dart:developer';
import 'package:dartz/dartz.dart';
import 'package:injectable/injectable.dart';
import '../../../domain/customer/customer.dart';
import '../datasources/remote_data_provider.dart';
@Injectable(as: ICustomerRepository)
class CustomerRepository implements ICustomerRepository {
final CustomerRemoteDataProvider _remoteDataProvider;
final String _logName = 'CustomerRepository';
CustomerRepository(this._remoteDataProvider);
@override
Future<Either<CustomerFailure, CustomerPoint>> getPoints() async {
try {
final result = await _remoteDataProvider.fetchCustomerPoint();
if (result.hasError) {
return left(result.error!);
}
final data = result.data!.toDomain();
return right(data);
} catch (e, s) {
log('getPoints', name: _logName, error: e, stackTrace: s);
return left(const CustomerFailure.unexpectedError());
}
}
}