diff --git a/client/promotion.go b/client/promotion.go new file mode 100644 index 0000000..e6c73cb --- /dev/null +++ b/client/promotion.go @@ -0,0 +1,37 @@ +package client + +import ( + "encoding/json" + "errors" + "git.selly.red/Selly-Modules/natsio" + "git.selly.red/Selly-Modules/natsio/model" + "git.selly.red/Selly-Modules/natsio/subject" +) + +type Promotion struct{} + +func GetPromotion() Promotion { + return Promotion{} +} + +func (s Promotion) GenerateVoucherPromotion(p model.GenerateVoucherPromotionRequest) (*model.ResponseGenerateVoucherPromotion, error) { + msg, err := natsio.GetServer().Request(subject.Promotion.GenerateVoucherPromotion, toBytes(p)) + if err != nil { + return nil, err + } + + var r struct { + Data *model.ResponseGenerateVoucherPromotion `json:"data"` + Error string `json:"error"` + } + + if err = json.Unmarshal(msg.Data, &r); err != nil { + return nil, err + } + + if r.Error != "" { + return nil, errors.New(r.Error) + } + + return r.Data, nil +} diff --git a/model/promotion_response.go b/model/promotion_response.go new file mode 100644 index 0000000..dffadd1 --- /dev/null +++ b/model/promotion_response.go @@ -0,0 +1,20 @@ +package model + +import "go.mongodb.org/mongo-driver/bson/primitive" + +type GenerateVoucherPromotionRequest struct { + Cash float64 `json:"cash"` + TargetID primitive.ObjectID `json:"targetId"` + TargetType string `json:"targetType"` + Options GenerateVoucherPromotionOptions `json:"options"` + Seller primitive.ObjectID `json:"seller"` +} + +type GenerateVoucherPromotionOptions struct { + Title string `json:"title"` +} + +type ResponseGenerateVoucherPromotion struct { + Success bool `json:"success"` + Error string `json:"error"` +}