book in workflow

This commit is contained in:
mr 2024-08-08 15:56:48 +02:00
parent 5ac4492369
commit 4688d9c681
2 changed files with 19 additions and 1 deletions

View File

@ -3,10 +3,13 @@ package oclib
import ( import (
"encoding/json" "encoding/json"
"slices" "slices"
"time"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/models/resources" "cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/resources/workflow/graph" "cloud.o-forge.io/core/oc-lib/models/resources/workflow/graph"
"cloud.o-forge.io/core/oc-lib/models/utils" "cloud.o-forge.io/core/oc-lib/models/utils"
"go.mongodb.org/mongo-driver/bson/primitive"
) )
type AbstractWorkflow struct { type AbstractWorkflow struct {
@ -31,6 +34,21 @@ func (d *Workflow) GetName() string {
return d.Name return d.Name
} }
func (d *Workflow) CheckBooking() bool {
if d.Schedule != nil && d.Schedule.Start != nil {
sd := primitive.NewDateTimeFromTime(d.Schedule.Start.Add(time.Second * -10))
ed := primitive.NewDateTimeFromTime(d.Schedule.Start.Add(time.Second * 10))
f := dbs.Filters{
And: map[string][]dbs.Filter{
"execution_date": {{Operator: "gte", Value: sd}, {Operator: "lte", Value: ed}},
},
}
res, _, _ := d.GetAccessor().Search(&f, "")
return len(res) == 0
}
return true
}
func (d *Workflow) GetAccessor() utils.Accessor { func (d *Workflow) GetAccessor() utils.Accessor {
data := New() data := New()
data.SetLogger(utils.WORKFLOW) data.SetLogger(utils.WORKFLOW)

View File

@ -87,7 +87,7 @@ func (wfa *workflowMongoAccessor) execution(id string, realData *Workflow, delet
if r.Schedule.Start == realData.Schedule.Start && r.Schedule.End == realData.Schedule.End && r.Schedule.Cron == realData.Schedule.Cron { if r.Schedule.Start == realData.Schedule.Start && r.Schedule.End == realData.Schedule.End && r.Schedule.Cron == realData.Schedule.Cron {
return 200, nil return 200, nil
} }
if !realData.CheckBooking(realData.Schedule.Start) { if !realData.CheckBooking() {
return 409, errors.New("the booking is already taken.") return 409, errors.New("the booking is already taken.")
} }
if delete { if delete {