Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

feat: add Status enum #53

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 84 additions & 4 deletions lib/src/protocol/models/message_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,24 +314,104 @@ class OrderData extends MessageData {
}

class OrderStatusData extends MessageData {
final String orderStatus;
final Status status;

OrderStatusData({required this.orderStatus});
OrderStatusData({required this.status});

factory OrderStatusData.fromJson(Map<String, dynamic> json) {
return OrderStatusData(
orderStatus: json['orderStatus'],
status: Status.fromJson(json['status']),
);
}

@override
Map<String, dynamic> toJson() {
return {
'orderStatus': orderStatus,
'status': status.toJson(),
};
}
}

enum Status {
payinPending,
payinInitiated,
payinSettled,
payinFailed,
payinExpired,
payoutPending,
payoutInitiated,
payoutSettled,
payoutFailed,
refundPending,
refundInitiated,
refundFailed,
refundSettled;

static Status fromJson(String json) {
switch (json) {
case 'PAYIN_PENDING':
return Status.payinPending;
case 'PAYIN_INITIATED':
return Status.payinInitiated;
case 'PAYIN_SETTLED':
return Status.payinSettled;
case 'PAYIN_FAILED':
return Status.payinFailed;
case 'PAYIN_EXPIRED':
return Status.payinExpired;
case 'PAYOUT_PENDING':
return Status.payoutPending;
case 'PAYOUT_INITIATED':
return Status.payoutInitiated;
case 'PAYOUT_SETTLED':
return Status.payoutSettled;
case 'PAYOUT_FAILED':
return Status.payoutFailed;
case 'REFUND_PENDING':
return Status.refundPending;
case 'REFUND_INITIATED':
return Status.refundInitiated;
case 'REFUND_FAILED':
return Status.refundFailed;
case 'REFUND_SETTLED':
return Status.refundSettled;
default:
throw Exception('Unknown status: $json');
}
}

String toJson() {
switch (this) {
case Status.payinPending:
return 'PAYIN_PENDING';
case Status.payinInitiated:
return 'PAYIN_INITIATED';
case Status.payinSettled:
return 'PAYIN_SETTLED';
case Status.payinFailed:
return 'PAYIN_FAILED';
case Status.payinExpired:
return 'PAYIN_EXPIRED';
case Status.payoutPending:
return 'PAYOUT_PENDING';
case Status.payoutInitiated:
return 'PAYOUT_INITIATED';
case Status.payoutSettled:
return 'PAYOUT_SETTLED';
case Status.payoutFailed:
return 'PAYOUT_FAILED';
case Status.refundPending:
return 'REFUND_PENDING';
case Status.refundInitiated:
return 'REFUND_INITIATED';
case Status.refundFailed:
return 'REFUND_FAILED';
case Status.refundSettled:
return 'REFUND_SETTLED';
}
}
}

class OrderInstructionsData extends MessageData {
final PaymentInstruction payin;
final PaymentInstruction payout;
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/test_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class TestData {
aliceDid.uri,
pfiDid.uri,
TypeId.generate(MessageKind.rfq.name),
OrderStatusData(orderStatus: 'order status'),
OrderStatusData(status: Status.payinInitiated),
);
}

Expand Down
4 changes: 2 additions & 2 deletions test/protocol/models/order_status_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ void main() async {
TestData.pfi,
TestData.alice,
TypeId.generate(MessageKind.rfq.name),
OrderStatusData(orderStatus: 'my status'),
OrderStatusData(status: Status.payinInitiated),
);

expect(orderStatus.metadata.id, startsWith(MessageKind.orderstatus.name));
expect(orderStatus.metadata.kind, equals(MessageKind.orderstatus));
expect(orderStatus.metadata.protocol, equals('1.0'));
expect(orderStatus.data.orderStatus, equals('my status'));
expect(orderStatus.data.status, equals(Status.payinInitiated));
});

test('can parse and verify order status from a json string', () async {
Expand Down
Loading