part of '../wallet_dtos.dart'; /// `terms` belum dibaca: bentuknya masih disepakati dengan tim backoffice, /// sementara itu yang ditampilkan `description` saja (ยง9.1). @freezed abstract class VoucherDto with _$VoucherDto { const factory VoucherDto({ @JsonKey(name: 'id') String? id, @JsonKey(name: 'name') String? name, @JsonKey(name: 'description') String? description, @JsonKey(name: 'image_url') String? imageUrl, @JsonKey(name: 'voucher_type') String? voucherType, @JsonKey(name: 'face_value') int? faceValue, @JsonKey(name: 'point_cost') int? pointCost, @JsonKey(name: 'max_per_customer') int? maxPerCustomer, @JsonKey(name: 'valid_until') String? validUntil, @JsonKey(name: 'available') int? available, }) = _VoucherDto; factory VoucherDto.fromJson(Map json) => _$VoucherDtoFromJson(json); const VoucherDto._(); Voucher toDomain() => Voucher( id: id ?? '', name: name ?? '', description: (description?.trim().isEmpty ?? true) ? null : description, imageUrl: (imageUrl?.isEmpty ?? true) ? null : imageUrl, type: VoucherType.fromApi(voucherType), faceValue: faceValue ?? 0, pointCost: pointCost ?? 0, maxPerCustomer: maxPerCustomer, validUntil: _parseDateTime(validUntil), available: available, ); } /// Item "Voucher saya" dan response tukar voucher; [pointBalance] dan /// [replayed] hanya ada di response tukar. @freezed abstract class VoucherRedemptionDto with _$VoucherRedemptionDto { const factory VoucherRedemptionDto({ @JsonKey(name: 'id') String? id, @JsonKey(name: 'voucher_id') String? voucherId, @JsonKey(name: 'voucher_name') String? voucherName, @JsonKey(name: 'voucher_image_url') String? voucherImageUrl, @JsonKey(name: 'voucher_type') String? voucherType, @JsonKey(name: 'status') String? status, @JsonKey(name: 'face_value') int? faceValue, @JsonKey(name: 'point_cost') int? pointCost, @JsonKey(name: 'code') String? code, @JsonKey(name: 'code_expires_at') String? codeExpiresAt, @JsonKey(name: 'completed_at') String? completedAt, @JsonKey(name: 'created_at') String? createdAt, @JsonKey(name: 'point_balance') int? pointBalance, @JsonKey(name: 'replayed') bool? replayed, }) = _VoucherRedemptionDto; factory VoucherRedemptionDto.fromJson(Map json) => _$VoucherRedemptionDtoFromJson(json); const VoucherRedemptionDto._(); VoucherRedemption toDomain() => VoucherRedemption( id: id ?? '', voucherId: voucherId ?? '', voucherName: voucherName ?? '', voucherImageUrl: (voucherImageUrl?.isEmpty ?? true) ? null : voucherImageUrl, voucherType: VoucherType.fromApi(voucherType), status: VoucherRedemptionStatus.fromApi(status), faceValue: faceValue ?? 0, pointCost: pointCost ?? 0, code: (code?.isEmpty ?? true) ? null : code, codeExpiresAt: _parseDateTime(codeExpiresAt), completedAt: _parseDateTime(completedAt), createdAt: _parseDateTime(createdAt), ); VoucherRedeemResult toRedeemResult() => VoucherRedeemResult( redemption: toDomain(), pointBalance: pointBalance ?? 0, replayed: replayed ?? false, ); } class VoucherRedemptionPageDto { final List items; final WalletPaginationDto pagination; const VoucherRedemptionPageDto({ required this.items, required this.pagination, }); VoucherRedemptionPage toDomain() => VoucherRedemptionPage( items: items.map((e) => e.toDomain()).toList(), page: pagination.page ?? 1, totalPages: pagination.totalPages ?? 1, ); }