diff --git a/models/bill/bill.go b/models/bill/bill.go index 5df1751..aa31062 100644 --- a/models/bill/bill.go +++ b/models/bill/bill.go @@ -1,14 +1,15 @@ package bill import ( + "encoding/json" "sync" "time" "cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/models/common/enum" - "cloud.o-forge.io/core/oc-lib/models/common/pricing" "cloud.o-forge.io/core/oc-lib/models/order" "cloud.o-forge.io/core/oc-lib/models/peer" + "cloud.o-forge.io/core/oc-lib/models/resources" "cloud.o-forge.io/core/oc-lib/models/resources/purchase_resource" "cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/tools" @@ -135,20 +136,25 @@ type PeerOrder struct { } func (d *PeerOrder) Pay(request *tools.APIRequest, response chan *PeerOrder, wg *sync.WaitGroup) { + d.Status = enum.PENDING go func() { + // DO SOMETHING TO PAY ON BLOCKCHAIN OR WHATEVER ON RETURN UPDATE STATUS d.Status = enum.PAID // TO REMOVE LATER IT'S A MOCK if d.Status == enum.PAID { for _, b := range d.Items { - if !b.Item.IsPurchasable() { + var priced *resources.PricedResource + bb, _ := json.Marshal(b.Item) + json.Unmarshal(bb, priced) + if !priced.IsPurchasable() { continue } accessor := purchase_resource.NewAccessor(request) accessor.StoreOne(&purchase_resource.PurchaseResource{ - ResourceID: b.Item.GetID(), - ResourceType: b.Item.GetType(), - EndDate: b.Item.GetLocationEnd(), + ResourceID: priced.GetID(), + ResourceType: priced.GetType(), + EndDate: priced.GetLocationEnd(), }) } } @@ -174,14 +180,17 @@ func (d *PeerOrder) SumUpBill(request *tools.APIRequest) error { type PeerItemOrder struct { Quantity int `json:"quantity,omitempty" bson:"quantity,omitempty"` Purchase *purchase_resource.PurchaseResource `json:"purchase,omitempty" bson:"purchase,omitempty"` - Item pricing.PricedItemITF `json:"item,omitempty" bson:"item,omitempty"` + Item map[string]interface{} `json:"item,omitempty" bson:"item,omitempty"` } func (d *PeerItemOrder) GetPrice(request *tools.APIRequest) (float64, error) { + var priced *resources.PricedResource + b, _ := json.Marshal(d.Item) + json.Unmarshal(b, priced) accessor := purchase_resource.NewAccessor(request) search, code, _ := accessor.Search(&dbs.Filters{ And: map[string][]dbs.Filter{ - "resource_id": {{Operator: dbs.EQUAL.String(), Value: d.Item.GetID()}}, + "resource_id": {{Operator: dbs.EQUAL.String(), Value: priced.GetID()}}, }, }, "", d.Purchase.IsDraft) if code == 200 && len(search) > 0 { @@ -191,7 +200,7 @@ func (d *PeerItemOrder) GetPrice(request *tools.APIRequest) (float64, error) { } } } - p, err := d.Item.GetPrice() + p, err := priced.GetPrice() if err != nil { return 0, err } diff --git a/models/resources/purchase_resource/purchase_resource.go b/models/resources/purchase_resource/purchase_resource.go index ca528ae..0767981 100644 --- a/models/resources/purchase_resource/purchase_resource.go +++ b/models/resources/purchase_resource/purchase_resource.go @@ -3,7 +3,6 @@ package purchase_resource import ( "time" - "cloud.o-forge.io/core/oc-lib/models/common/pricing" "cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/tools" ) @@ -11,11 +10,11 @@ import ( type PurchaseResource struct { utils.AbstractObject DestPeerID string - PricedItem pricing.PricedItemITF `json:"priced_item,omitempty" bson:"priced_item,omitempty" validate:"required"` - ExecutionsID string `json:"executions_id,omitempty" bson:"executions_id,omitempty" validate:"required"` // ExecutionsID is the ID of the executions - EndDate *time.Time `json:"end_buying_date,omitempty" bson:"end_buying_date,omitempty"` - ResourceID string `json:"resource_id" bson:"resource_id" validate:"required"` - ResourceType tools.DataType `json:"resource_type" bson:"resource_type" validate:"required"` + PricedItem map[string]interface{} `json:"priced_item,omitempty" bson:"priced_item,omitempty" validate:"required"` + ExecutionsID string `json:"executions_id,omitempty" bson:"executions_id,omitempty" validate:"required"` // ExecutionsID is the ID of the executions + EndDate *time.Time `json:"end_buying_date,omitempty" bson:"end_buying_date,omitempty"` + ResourceID string `json:"resource_id" bson:"resource_id" validate:"required"` + ResourceType tools.DataType `json:"resource_type" bson:"resource_type" validate:"required"` } func (d *PurchaseResource) GetAccessor(request *tools.APIRequest) utils.Accessor {