Merge pull request 'fixed payment methods serialization deserialization' (#24) from address-book-#16 into main
Some checks failed
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/mntx_gateway Pipeline failed
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline failed
ci/woodpecker/push/notification Pipeline failed
Some checks failed
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/mntx_gateway Pipeline failed
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline failed
ci/woodpecker/push/notification Pipeline failed
Reviewed-on: #24
This commit was merged in pull request #24.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
@@ -71,3 +72,73 @@ type PaymentMethod struct {
|
|||||||
Data bson.Raw `bson:"data" json:"data"`
|
Data bson.Raw `bson:"data" json:"data"`
|
||||||
IsMain bool `bson:"isMain" json:"isMain"`
|
IsMain bool `bson:"isMain" json:"isMain"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type paymentMethodJSON struct {
|
||||||
|
PermissionBound `json:",inline"`
|
||||||
|
Describable `json:",inline"`
|
||||||
|
|
||||||
|
RecipientRef primitive.ObjectID `json:"recipientRef"`
|
||||||
|
Type PaymentType `json:"type"`
|
||||||
|
Data json.RawMessage `json:"data"`
|
||||||
|
IsMain bool `json:"isMain"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m PaymentMethod) MarshalJSON() ([]byte, error) {
|
||||||
|
var marshaledData json.RawMessage
|
||||||
|
|
||||||
|
if len(m.Data) > 0 {
|
||||||
|
var data bson.M
|
||||||
|
if err := bson.Unmarshal(m.Data, &data); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if data != nil {
|
||||||
|
b, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
marshaledData = b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := paymentMethodJSON{
|
||||||
|
PermissionBound: m.PermissionBound,
|
||||||
|
Describable: m.Describable,
|
||||||
|
RecipientRef: m.RecipientRef,
|
||||||
|
Type: m.Type,
|
||||||
|
Data: marshaledData,
|
||||||
|
IsMain: m.IsMain,
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.Marshal(payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *PaymentMethod) UnmarshalJSON(data []byte) error {
|
||||||
|
var payload paymentMethodJSON
|
||||||
|
if err := json.Unmarshal(data, &payload); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.PermissionBound = payload.PermissionBound
|
||||||
|
m.Describable = payload.Describable
|
||||||
|
m.RecipientRef = payload.RecipientRef
|
||||||
|
m.Type = payload.Type
|
||||||
|
m.IsMain = payload.IsMain
|
||||||
|
|
||||||
|
if len(payload.Data) == 0 || bytes.Equal(payload.Data, []byte("null")) {
|
||||||
|
m.Data = nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawData map[string]any
|
||||||
|
if err := json.Unmarshal(payload.Data, &rawData); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
raw, err := bson.Marshal(rawData)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Data = raw
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -71,7 +71,6 @@ class PaymentMethodsProvider extends GenericProvider<PaymentMethod> {
|
|||||||
).toDTO().toJson(),
|
).toDTO().toJson(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
Future<void> makeMain(PaymentMethod method) {
|
Future<void> makeMain(PaymentMethod method) {
|
||||||
// TODO: create separate backend method to manage main payment method
|
// TODO: create separate backend method to manage main payment method
|
||||||
final updates = <Future<void>>[];
|
final updates = <Future<void>>[];
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
|
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import 'package:pshared/models/payment/methods/data.dart';
|
import 'package:pshared/models/payment/methods/data.dart';
|
||||||
@@ -14,11 +16,11 @@ import 'package:pshared/provider/recipient/provider.dart';
|
|||||||
import 'package:pweb/pages/address_book/form/view.dart';
|
import 'package:pweb/pages/address_book/form/view.dart';
|
||||||
// import 'package:pweb/services/amplitude.dart';
|
// import 'package:pweb/services/amplitude.dart';
|
||||||
import 'package:pweb/utils/error/snackbar.dart';
|
import 'package:pweb/utils/error/snackbar.dart';
|
||||||
|
|
||||||
import 'package:pweb/generated/i18n/app_localizations.dart';
|
|
||||||
import 'package:pweb/utils/payment/label.dart';
|
import 'package:pweb/utils/payment/label.dart';
|
||||||
import 'package:pweb/utils/snackbar.dart';
|
import 'package:pweb/utils/snackbar.dart';
|
||||||
|
|
||||||
|
import 'package:pweb/generated/i18n/app_localizations.dart';
|
||||||
|
|
||||||
|
|
||||||
class AdressBookRecipientForm extends StatefulWidget {
|
class AdressBookRecipientForm extends StatefulWidget {
|
||||||
final Recipient? recipient;
|
final Recipient? recipient;
|
||||||
@@ -71,18 +73,31 @@ class _AdressBookRecipientFormState extends State<AdressBookRecipientForm> {
|
|||||||
Future<Recipient?> _doSave() async {
|
Future<Recipient?> _doSave() async {
|
||||||
final recipients = context.read<RecipientsProvider>();
|
final recipients = context.read<RecipientsProvider>();
|
||||||
final methods = context.read<PaymentMethodsProvider>();
|
final methods = context.read<PaymentMethodsProvider>();
|
||||||
final recipient = await recipients.create(
|
final recipient = widget.recipient == null
|
||||||
name: _nameCtrl.text,
|
? await recipients.create(
|
||||||
email: _emailCtrl.text,
|
name: _nameCtrl.text,
|
||||||
);
|
email: _emailCtrl.text,
|
||||||
if (methods.currentObject == null) return recipient;
|
)
|
||||||
final data = methods.currentObject!;
|
: widget.recipient!;
|
||||||
await methods.create(
|
for (final type in _methods.keys) {
|
||||||
reacipientRef: recipient.id,
|
final data = _methods[type]!;
|
||||||
name: getPaymentTypeLabel(context, data.type),
|
final exising = methods.methods.firstWhereOrNull((m) => m.type == type);
|
||||||
data: data.data,
|
if (exising != null) {
|
||||||
);
|
await methods.updateMethod(exising.copyWith(data: data));
|
||||||
return recipient;
|
} else {
|
||||||
|
await methods.create(
|
||||||
|
reacipientRef: recipient.id,
|
||||||
|
name: getPaymentTypeLabel(context, type),
|
||||||
|
data: data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
await methods.create(
|
||||||
|
reacipientRef: recipient.id,
|
||||||
|
name: getPaymentTypeLabel(context, data.type),
|
||||||
|
data: data,
|
||||||
|
);
|
||||||
|
return recipient;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Change when registration is ready
|
//TODO: Change when registration is ready
|
||||||
|
|||||||
Reference in New Issue
Block a user