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

Commit

Permalink
feat: add Status enum (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanwlee authored Jul 25, 2024
1 parent 3c04496 commit d129033
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
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

0 comments on commit d129033

Please sign in to comment.