129 lines
3.6 KiB
Go
129 lines
3.6 KiB
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"git.sendico.io/tech/sxchnge/bff/pkg/db/storable"
|
|
"git.sendico.io/tech/sxchnge/bff/pkg/merrors"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
type RecipientStatus int
|
|
|
|
const (
|
|
RecipientStatusReady RecipientStatus = iota
|
|
RecipientStatusRegistered
|
|
RecipientStatusNotRegistered
|
|
)
|
|
|
|
var recipientStatusToString = map[RecipientStatus]string{
|
|
RecipientStatusReady: "ready",
|
|
RecipientStatusRegistered: "registered",
|
|
RecipientStatusNotRegistered: "notRegistered",
|
|
}
|
|
|
|
var recipientStatusFromString = map[string]RecipientStatus{
|
|
"ready": RecipientStatusReady,
|
|
"registered": RecipientStatusRegistered,
|
|
"notRegistered": RecipientStatusNotRegistered,
|
|
}
|
|
|
|
func (s RecipientStatus) String() string {
|
|
if v, ok := recipientStatusToString[s]; ok {
|
|
return v
|
|
}
|
|
return "ready" // дефолт, можно поменять
|
|
}
|
|
|
|
// JSON: храним как строку ("ready" / "registered" / "notRegistered")
|
|
func (s RecipientStatus) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(s.String())
|
|
}
|
|
|
|
func (s *RecipientStatus) UnmarshalJSON(data []byte) error {
|
|
var val string
|
|
if err := json.Unmarshal(data, &val); err != nil {
|
|
return err
|
|
}
|
|
v, ok := recipientStatusFromString[val]
|
|
if !ok {
|
|
return merrors.InvalidArgument(fmt.Sprintf("unknown RecipientStatus: %q", val))
|
|
}
|
|
*s = v
|
|
return nil
|
|
}
|
|
|
|
// RecipientType { internal, external }
|
|
|
|
type RecipientType int
|
|
|
|
const (
|
|
RecipientTypeInternal RecipientType = iota
|
|
RecipientTypeExternal
|
|
)
|
|
|
|
var recipientTypeToString = map[RecipientType]string{
|
|
RecipientTypeInternal: "internal",
|
|
RecipientTypeExternal: "external",
|
|
}
|
|
|
|
var recipientTypeFromString = map[string]RecipientType{
|
|
"internal": RecipientTypeInternal,
|
|
"external": RecipientTypeExternal,
|
|
}
|
|
|
|
func (t RecipientType) String() string {
|
|
if v, ok := recipientTypeToString[t]; ok {
|
|
return v
|
|
}
|
|
return "internal"
|
|
}
|
|
|
|
func (t RecipientType) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(t.String())
|
|
}
|
|
|
|
func (t *RecipientType) UnmarshalJSON(data []byte) error {
|
|
var val string
|
|
if err := json.Unmarshal(data, &val); err != nil {
|
|
return err
|
|
}
|
|
v, ok := recipientTypeFromString[val]
|
|
if !ok {
|
|
return merrors.InvalidArgument(fmt.Sprintf("unknown RecipientType: %q", val))
|
|
}
|
|
*t = v
|
|
return nil
|
|
}
|
|
|
|
// Owner is the ownership token stored under the "owner" BSON/JSON key.
|
|
// storable.Ref must stay embedded: a named field would keep the bson ",inline"
|
|
// but encoding/json has no inline option and would nest the ref under "Ref",
|
|
// while embedding promotes it, so both wire paths are owner.ref and owner.kind.
|
|
type Owner struct {
|
|
storable.Ref `bson:",inline" json:",inline"`
|
|
Kind OwnerKind `bson:"kind" json:"kind"`
|
|
}
|
|
|
|
type Recipient struct {
|
|
PermissionBound `bson:",inline" json:",inline"`
|
|
Describable `bson:",inline" json:",inline"`
|
|
Email string `bson:"email" json:"email"`
|
|
AvatarURL *string `bson:"avatarUrl,omitempty" json:"avatarUrl,omitempty"`
|
|
|
|
Status RecipientStatus `bson:"status" json:"status"`
|
|
Type RecipientType `bson:"type" json:"type"`
|
|
Owner Owner `bson:"owner" json:"owner"`
|
|
|
|
// INN is the recipient's taxpayer identifier. It is the canonical source of
|
|
// the INN for payouts to this recipient (flows into customer.tax_identifier),
|
|
// replacing the per-payment-method inn field for recipient-owned methods.
|
|
INN string `bson:"inn,omitempty" json:"inn,omitempty"`
|
|
}
|
|
|
|
func (r *Recipient) GetOwnerKind() OwnerKind { return r.Owner.Kind }
|
|
func (r *Recipient) GetOwnerRef() bson.ObjectID { return r.Owner.Ref.Ref }
|
|
func (r *Recipient) SetOwnerKind(k OwnerKind) { r.Owner.Kind = k }
|
|
func (r *Recipient) SetOwnerRef(ref bson.ObjectID) { r.Owner.Ref.Ref = ref }
|