Order Flow Payment Draft
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
package resources
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/config"
|
||||
"cloud.o-forge.io/core/oc-lib/models/common/pricing"
|
||||
@@ -24,7 +22,7 @@ import (
|
||||
* AbstractResource is a struct that represents a resource
|
||||
* it defines the resource data
|
||||
*/
|
||||
type abstractResource struct {
|
||||
type AbsResource struct {
|
||||
utils.AbstractObject // AbstractObject contains the basic fields of an object (id, name)
|
||||
Logo string `json:"logo,omitempty" bson:"logo,omitempty" validate:"required"` // Logo is the logo of the resource
|
||||
Description string `json:"description,omitempty" bson:"description,omitempty"` // Description is the description of the resource
|
||||
@@ -34,71 +32,58 @@ type abstractResource struct {
|
||||
UsageRestrictions string `bson:"usage_restrictions,omitempty" json:"usage_restrictions,omitempty"`
|
||||
}
|
||||
|
||||
func (r *abstractResource) StoreDraftDefault() {
|
||||
func (r *AbsResource) StoreDraftDefault() {
|
||||
r.IsDraft = true
|
||||
}
|
||||
|
||||
func (r *AbstractCustomizedResource[T]) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||
func (r *AbsResource) CanUpdate(set utils.DBObject) (bool, utils.DBObject) {
|
||||
if r.IsDraft != set.IsDrafted() && set.IsDrafted() {
|
||||
return true, set // only state can be updated
|
||||
}
|
||||
return r.IsDraft != set.IsDrafted() && set.IsDrafted(), set
|
||||
}
|
||||
|
||||
func (r *abstractResource) CanDelete() bool {
|
||||
func (r *AbsResource) CanDelete() bool {
|
||||
return r.IsDraft // only draft bookings can be deleted
|
||||
}
|
||||
|
||||
func (ao *abstractResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
func (ao *AbsResource) GetAccessor(request *tools.APIRequest) utils.Accessor {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ao *abstractResource) GetCreatorID() string {
|
||||
return ao.CreatorID
|
||||
}
|
||||
|
||||
func (abs *abstractResource) SetResourceModel(model *resource_model.ResourceModel) {
|
||||
func (abs *AbsResource) SetResourceModel(model *resource_model.ResourceModel) {
|
||||
abs.ResourceModel = model
|
||||
}
|
||||
|
||||
type AbstractResource[T InstanceITF] struct {
|
||||
abstractResource
|
||||
type AbstractResource[T ResourceInstanceITF] struct {
|
||||
AbsResource
|
||||
Instances []T `json:"instances,omitempty" bson:"instances,omitempty"` // Bill is the bill of the resource // Bill is the bill of the resource
|
||||
}
|
||||
|
||||
func (abs *AbstractResource[T]) ConvertToPricedResource(
|
||||
t tools.DataType, request *tools.APIRequest) pricing.PricedItemITF {
|
||||
instances := map[string]string{}
|
||||
profiles := map[string][]pricing.PricingProfileITF{}
|
||||
for _, instance := range abs.Instances {
|
||||
instances[instance.GetID()] = instance.GetName()
|
||||
profiles[instance.GetID()] = instance.GetPricingsProfiles(request.PeerID, request.Groups)
|
||||
}
|
||||
return &PricedResource{
|
||||
Name: abs.Name,
|
||||
Logo: abs.Logo,
|
||||
ResourceID: abs.UUID,
|
||||
ResourceType: t,
|
||||
InstancesRefs: instances,
|
||||
PricingProfiles: profiles,
|
||||
CreatorID: abs.CreatorID,
|
||||
}
|
||||
}
|
||||
|
||||
func (abs *AbstractResource[T]) SetAllowedInstances(request *tools.APIRequest) {
|
||||
abs.Instances = verifyAuthAction[T](abs.Instances, request)
|
||||
}
|
||||
|
||||
func (abs *AbstractResource[T]) GetPartnership(request *tools.APIRequest) ResourcePartnerITF {
|
||||
for _, instance := range abs.Instances {
|
||||
partners, grps := instance.GetPeerGroups()
|
||||
for i, p := range grps {
|
||||
if request == nil {
|
||||
continue
|
||||
}
|
||||
if _, ok := p[request.PeerID]; ok {
|
||||
return partners[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (abs *AbstractResource[T]) VerifyPartnerships() bool {
|
||||
// a peer can be part of only one partnership by instance
|
||||
// may we need to define partnership in a different DB
|
||||
for _, instance := range abs.Instances {
|
||||
if !instance.VerifyPartnerships() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (d *AbstractResource[T]) Trim() {
|
||||
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: d.CreatorID}}).IsMySelf(); !ok {
|
||||
// TODO clean up the peer groups that are not in the allowed peers group
|
||||
for _, instance := range d.Instances {
|
||||
instance.ClearPeerGroups()
|
||||
}
|
||||
@@ -109,78 +94,7 @@ func (abs *AbstractResource[T]) VerifyAuth(request *tools.APIRequest) bool {
|
||||
return len(verifyAuthAction[T](abs.Instances, request)) > 0 || abs.AbstractObject.VerifyAuth(request)
|
||||
}
|
||||
|
||||
type AbstractCustomizedResource[T InstanceITF] struct {
|
||||
abstractResource
|
||||
ExplicitBookingDurationS float64 `json:"explicit_location_duration_s,omitempty" bson:"explicit_location_duration_s,omitempty"`
|
||||
UsageStart *time.Time `json:"start,omitempty" bson:"start,omitempty"`
|
||||
UsageEnd *time.Time `json:"end,omitempty" bson:"end,omitempty"`
|
||||
SelectedInstance T `json:"selected_instance,omitempty" bson:"selected_instance,omitempty"`
|
||||
SelectedPricing string `json:"selected_pricing,omitempty" bson:"selected_pricing,omitempty"`
|
||||
}
|
||||
|
||||
func (abs *AbstractCustomizedResource[T]) SetStartUsage(start time.Time) {
|
||||
if abs.UsageStart == nil {
|
||||
abs.UsageStart = &start
|
||||
}
|
||||
}
|
||||
|
||||
func (abs *AbstractCustomizedResource[T]) SetEndUsage(end time.Time) {
|
||||
if abs.UsageEnd == nil {
|
||||
abs.UsageEnd = &end
|
||||
}
|
||||
}
|
||||
|
||||
func (abs *AbstractCustomizedResource[T]) IsPurchased(request *tools.APIRequest) bool {
|
||||
return abs.GetPartnership(request).GetPricing(abs.SelectedPricing).IsPurchased()
|
||||
}
|
||||
|
||||
func (abs *AbstractCustomizedResource[T]) GetLocationEnd() *time.Time {
|
||||
return abs.UsageEnd
|
||||
}
|
||||
|
||||
func (abs *AbstractCustomizedResource[T]) GetLocationStart() *time.Time {
|
||||
return abs.UsageStart
|
||||
}
|
||||
|
||||
func (abs *AbstractCustomizedResource[T]) GetExplicitDurationInS() float64 {
|
||||
if abs.ExplicitBookingDurationS == 0 {
|
||||
if abs.UsageEnd == nil || abs.UsageStart == nil {
|
||||
return time.Duration(1 * time.Hour).Seconds()
|
||||
}
|
||||
return abs.UsageEnd.Sub(*abs.UsageStart).Seconds()
|
||||
}
|
||||
return abs.ExplicitBookingDurationS
|
||||
}
|
||||
|
||||
func (abs *AbstractCustomizedResource[T]) GetPricingID() string {
|
||||
return abs.SelectedPricing
|
||||
}
|
||||
|
||||
func (r *AbstractCustomizedResource[T]) GetPrice(request *tools.APIRequest) (float64, error) {
|
||||
if r.UsageStart == nil || r.UsageEnd == nil {
|
||||
return 0, errors.New("Usage start and end must be set")
|
||||
}
|
||||
partner := r.GetPartnership(request)
|
||||
if partner != nil && partner.GetPricing(r.SelectedPricing) != nil {
|
||||
return 0, errors.New("Pricing strategy not found")
|
||||
}
|
||||
return partner.GetPricing(r.SelectedPricing).GetPrice(1, 0, *r.UsageStart, *r.UsageEnd, request)
|
||||
}
|
||||
|
||||
func (abs *AbstractCustomizedResource[T]) GetPartnership(request *tools.APIRequest) ResourcePartnerITF {
|
||||
partners, grps := abs.SelectedInstance.GetPeerGroups()
|
||||
for i, p := range grps {
|
||||
if request == nil {
|
||||
continue
|
||||
}
|
||||
if _, ok := p[request.PeerID]; ok {
|
||||
return partners[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func verifyAuthAction[T InstanceITF](baseInstance []T, request *tools.APIRequest) []T {
|
||||
func verifyAuthAction[T ResourceInstanceITF](baseInstance []T, request *tools.APIRequest) []T {
|
||||
instances := []T{}
|
||||
for _, instance := range baseInstance {
|
||||
_, peerGroups := instance.GetPeerGroups()
|
||||
@@ -205,6 +119,7 @@ func verifyAuthAction[T InstanceITF](baseInstance []T, request *tools.APIRequest
|
||||
|
||||
type ResourceInstance[T ResourcePartnerITF] struct {
|
||||
UUID string `json:"id,omitempty" bson:"id,omitempty"`
|
||||
Name string `json:"name,omitempty" bson:"name,omitempty"`
|
||||
Location geopoint.GeoPoint `json:"location,omitempty" bson:"location,omitempty"`
|
||||
Country countries.CountryCode `json:"country,omitempty" bson:"country,omitempty"`
|
||||
// Url string `json:"url,omitempty" bson:"url,omitempty"`
|
||||
@@ -216,24 +131,16 @@ func (ri *ResourceInstance[T]) GetID() string {
|
||||
return ri.UUID
|
||||
}
|
||||
|
||||
func (r *ResourceInstance[T]) VerifyPartnerships() bool {
|
||||
peersMultiple := map[string]int{}
|
||||
for _, p := range r.Partnerships {
|
||||
for k, g := range p.GetPeerGroups() {
|
||||
for _, v := range g {
|
||||
if _, ok := peersMultiple[k+"_"+v]; !ok {
|
||||
peersMultiple[k+"_"+v] = 0
|
||||
}
|
||||
peersMultiple[k+"_"+v]++
|
||||
}
|
||||
}
|
||||
func (ri *ResourceInstance[T]) GetName() string {
|
||||
return ri.Name
|
||||
}
|
||||
|
||||
func (ri *ResourceInstance[T]) GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF {
|
||||
pricings := []pricing.PricingProfileITF{}
|
||||
for _, p := range ri.Partnerships {
|
||||
pricings = append(pricings, p.GetPricingsProfiles(peerID, groups)...)
|
||||
}
|
||||
for _, p := range peersMultiple {
|
||||
if p > 1 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
return pricings
|
||||
}
|
||||
|
||||
func (ri *ResourceInstance[T]) GetPeerGroups() ([]ResourcePartnerITF, []map[string][]string) {
|
||||
@@ -258,8 +165,19 @@ type ResourcePartnerShip[T pricing.PricingProfileITF] struct {
|
||||
PricingProfiles map[string]T `json:"pricing,omitempty" bson:"pricing,omitempty"`
|
||||
}
|
||||
|
||||
func (rp *ResourcePartnerShip[T]) GetPricing(id string) pricing.PricingProfileITF {
|
||||
return rp.PricingProfiles[id]
|
||||
func (ri *ResourcePartnerShip[T]) GetPricingsProfiles(peerID string, groups []string) []pricing.PricingProfileITF {
|
||||
if ri.PeerGroups[peerID] != nil {
|
||||
for _, p := range ri.PeerGroups[peerID] {
|
||||
if slices.Contains(groups, p) {
|
||||
profiles := []pricing.PricingProfileITF{}
|
||||
for _, ri := range ri.PricingProfiles {
|
||||
profiles = append(profiles, ri)
|
||||
}
|
||||
return profiles
|
||||
}
|
||||
}
|
||||
}
|
||||
return []pricing.PricingProfileITF{}
|
||||
}
|
||||
|
||||
func (rp *ResourcePartnerShip[T]) GetPeerGroups() map[string][]string {
|
||||
|
||||
Reference in New Issue
Block a user