129 lines
3.2 KiB
Go
129 lines
3.2 KiB
Go
package mutations
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"git.lehouerou.net/laurent/sorare/graphql"
|
|
"git.lehouerou.net/laurent/sorare/types"
|
|
)
|
|
|
|
type prepareAcceptOfferInput struct {
|
|
ClientMutationId string `json:"clientMutationId"`
|
|
OfferId string `json:"offerId"`
|
|
SettlementInfo SettlementInfo `json:"settlementInfo"`
|
|
}
|
|
|
|
type PrepareAcceptOfferParams struct {
|
|
Input prepareAcceptOfferInput `graphql:"input"`
|
|
}
|
|
|
|
func (m *Mutations) newPrepareAcceptOfferMutation() *graphql.Mutation[AuthorizationPayload, PrepareAcceptOfferParams] {
|
|
return graphql.NewMutation[AuthorizationPayload, PrepareAcceptOfferParams](
|
|
m.c,
|
|
"prepareAcceptOffer",
|
|
)
|
|
}
|
|
|
|
type acceptOfferInput struct {
|
|
Approvals []AuthorizationApprovalInput `json:"approvals"`
|
|
ClientMutationId string `json:"clientMutationId"`
|
|
OfferId string `json:"offerId"`
|
|
SettlementInfo SettlementInfo `json:"settlementInfo"`
|
|
}
|
|
|
|
type AcceptOfferParams struct {
|
|
Input acceptOfferInput `graphql:"input"`
|
|
}
|
|
|
|
func (m *Mutations) newAcceptOfferMutation() *graphql.Mutation[BaseMutationPayload, AcceptOfferParams] {
|
|
return graphql.NewMutation[BaseMutationPayload, AcceptOfferParams](
|
|
m.c,
|
|
"acceptOffer",
|
|
)
|
|
}
|
|
|
|
type AcceptOfferOptions struct {
|
|
PaymentMethod types.PaymentMethod
|
|
}
|
|
|
|
func WithAcceptOfferPaymentMethod(paymentMethod types.PaymentMethod) func(*AcceptOfferOptions) {
|
|
return func(o *AcceptOfferOptions) {
|
|
o.PaymentMethod = paymentMethod
|
|
}
|
|
}
|
|
|
|
func (m *Mutations) AcceptOffer(
|
|
ctx context.Context,
|
|
offerId string,
|
|
currency types.SupportedCurrency,
|
|
privateKey string,
|
|
options ...func(*AcceptOfferOptions),
|
|
) error {
|
|
opts := AcceptOfferOptions{
|
|
PaymentMethod: types.PaymentMethodWallet,
|
|
}
|
|
for _, opt := range options {
|
|
opt(&opts)
|
|
}
|
|
|
|
exchangeRate, err := m.ExchangeRate.Get(ctx, graphql.EmptyParams{})
|
|
if err != nil {
|
|
return errors.Wrap(err, "getting exchange rate")
|
|
}
|
|
si := SettlementInfo{
|
|
Currency: currency,
|
|
PaymentMethod: opts.PaymentMethod,
|
|
ExchangeRateId: exchangeRate.Id.String(),
|
|
}
|
|
|
|
prepareAcceptOfferRes, err := m.newPrepareAcceptOfferMutation().Execute(
|
|
ctx,
|
|
PrepareAcceptOfferParams{
|
|
Input: prepareAcceptOfferInput{
|
|
ClientMutationId: GetRandomId(),
|
|
OfferId: offerId,
|
|
SettlementInfo: si,
|
|
},
|
|
},
|
|
)
|
|
if err != nil {
|
|
return errors.Wrap(err, "preparing accept offer")
|
|
}
|
|
if len(prepareAcceptOfferRes.Errors) > 0 {
|
|
return errors.Wrap(
|
|
errors.New(prepareAcceptOfferRes.Errors[0].Message),
|
|
"preparing accept offer",
|
|
)
|
|
}
|
|
|
|
approvals, err := signAuthorizationRequests(
|
|
privateKey,
|
|
prepareAcceptOfferRes.Authorizations,
|
|
)
|
|
if err != nil {
|
|
return errors.Wrap(err, "signing authorization requests")
|
|
}
|
|
|
|
acceptOfferMutationRes, err := m.newAcceptOfferMutation().Execute(
|
|
ctx,
|
|
AcceptOfferParams{
|
|
Input: acceptOfferInput{
|
|
Approvals: approvals,
|
|
ClientMutationId: GetRandomId(),
|
|
OfferId: offerId,
|
|
SettlementInfo: si,
|
|
},
|
|
},
|
|
)
|
|
if err != nil {
|
|
return errors.Wrap(err, "accepting offer")
|
|
}
|
|
if len(acceptOfferMutationRes.Errors) > 0 {
|
|
return errors.Wrap(errors.New(acceptOfferMutationRes.Errors[0].Message), "accepting offer")
|
|
}
|
|
return nil
|
|
|
|
}
|