new payment methods service
This commit is contained in:
83
api/payments/methods/internal/service/methods/util.go
Normal file
83
api/payments/methods/internal/service/methods/util.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package methods
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
"github.com/tech/sendico/pkg/model"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
methodsv1 "github.com/tech/sendico/pkg/proto/payments/methods/v1"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
func autoError[T any](ctx context.Context, logger mlogger.Logger, err error) (*T, error) {
|
||||
return gsresponse.Execute(ctx, gsresponse.Auto[T](logger, mservice.PaymentMethods, err))
|
||||
}
|
||||
|
||||
func parseObjectID(value, field string) (bson.ObjectID, error) {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
if trimmed == "" {
|
||||
return bson.NilObjectID, merrors.InvalidArgument(fmt.Sprintf("%s is required", field), field)
|
||||
}
|
||||
ref, err := bson.ObjectIDFromHex(trimmed)
|
||||
if err != nil {
|
||||
return bson.NilObjectID, merrors.InvalidArgument(fmt.Sprintf("%s must be a valid object id", field), field)
|
||||
}
|
||||
return ref, nil
|
||||
}
|
||||
|
||||
func decodePaymentMethod(data []byte) (*model.PaymentMethod, error) {
|
||||
if len(data) == 0 {
|
||||
return nil, merrors.InvalidArgument("payment_method_json is required", "payment_method_json")
|
||||
}
|
||||
res := &model.PaymentMethod{}
|
||||
if err := json.Unmarshal(data, res); err != nil {
|
||||
return nil, merrors.InvalidArgumentWrap(err, "failed to decode payment method", "payment_method_json")
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func encodePaymentMethod(pm *model.PaymentMethod) ([]byte, error) {
|
||||
if pm == nil {
|
||||
return nil, merrors.InvalidArgument("payment method is required")
|
||||
}
|
||||
payload, err := json.Marshal(pm)
|
||||
if err != nil {
|
||||
return nil, merrors.InternalWrap(err, "failed to encode payment method")
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func toModelCursor(cursor *methodsv1.ViewCursor) *model.ViewCursor {
|
||||
if cursor == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
res := &model.ViewCursor{}
|
||||
hasAny := false
|
||||
|
||||
if limit := cursor.GetLimit(); limit != nil {
|
||||
v := limit.GetValue()
|
||||
res.Limit = &v
|
||||
hasAny = true
|
||||
}
|
||||
if offset := cursor.GetOffset(); offset != nil {
|
||||
v := offset.GetValue()
|
||||
res.Offset = &v
|
||||
hasAny = true
|
||||
}
|
||||
if archived := cursor.GetIsArchived(); archived != nil {
|
||||
v := archived.GetValue()
|
||||
res.IsArchived = &v
|
||||
hasAny = true
|
||||
}
|
||||
if !hasAny {
|
||||
return nil
|
||||
}
|
||||
return res
|
||||
}
|
||||
Reference in New Issue
Block a user