-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
161 lines (147 loc) · 3.98 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package models
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/msik-404/micro-appoint-scheduler/internal/database"
)
type Order struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
CustomerID primitive.ObjectID `bson:"customer_id,omitempty"`
CompanyID primitive.ObjectID `bson:"company_id,omitempty"`
ServiceID primitive.ObjectID `bson:"service_id,omitempty"`
EmployeeID primitive.ObjectID `bson:"employee_id,omitempty"`
OrderTime primitive.DateTime `bson:"order_time,omitempty"`
IsCanceled bool `bson:"is_canceled"`
StartTime primitive.DateTime `bson:"start_time,omitempty"`
EndTime primitive.DateTime `bson:"end_time,omitempty"`
}
// todo needs fixing
func FindManyOrders(
ctx context.Context,
client *mongo.Client,
customerID *primitive.ObjectID,
companyID *primitive.ObjectID,
nPerPage *int64,
startDate *primitive.DateTime,
startValue *primitive.ObjectID,
isCanceled *bool,
) (*mongo.Cursor, error) {
db := client.Database(database.DBName)
coll := db.Collection(database.CollName)
opts := options.Find()
var n int64 = 30
if nPerPage != nil {
if *nPerPage > 0 {
n = *nPerPage
}
}
opts.SetLimit(n)
projection := bson.D{
{Key: "start_time", Value: 0},
{Key: "end_time", Value: 0},
}
if customerID != nil {
projection = append(projection, bson.E{Key: "customer_id", Value: 0})
} else if companyID != nil {
projection = append(projection, bson.E{Key: "company_id", Value: 0})
}
opts.SetProjection(projection)
filter := bson.M{}
if isCanceled != nil {
filter["is_canceled"] = *isCanceled
}
if customerID != nil {
filter["customer_id"] = customerID
} else if companyID != nil {
filter["company_id"] = companyID
}
if startValue != nil {
opts.SetSort(bson.M{
"order_id": -1,
"order_time": -1,
})
filter["order_id"] = bson.M{"lt": startValue}
filter["order_time"] = bson.M{"lte": startDate}
}
return coll.Find(ctx, filter, opts)
}
var BookedError = errors.New("this Date is already booked")
func IsBookedTimeFrame(
ctx context.Context,
client *mongo.Client,
employeeID primitive.ObjectID,
startTime primitive.DateTime,
endTime primitive.DateTime,
) error {
db := client.Database(database.DBName)
coll := db.Collection(database.CollName)
matches, err := coll.CountDocuments(ctx, bson.M{
"employee_id": employeeID,
"start_time": bson.M{"$lt": endTime},
"end_time": bson.M{"$gt": startTime},
"is_canceled": false,
})
if err != nil {
return err
}
if matches > 0 {
return BookedError
}
return nil
}
// todo: check if this employee can perfom this service and
// whether this service and employee is in this company
func (order *Order) InsertOneOrder(
ctx context.Context,
client *mongo.Client,
) (*mongo.InsertOneResult, error) {
session, err := client.StartSession()
if err != nil {
return nil, err
}
defer session.EndSession(ctx)
db := client.Database(database.DBName)
coll := db.Collection(database.CollName)
callback := func(mongoCtx mongo.SessionContext) (any, error) {
err := IsBookedTimeFrame(
ctx,
client,
order.EmployeeID,
order.StartTime,
order.EndTime,
)
if err != nil {
return nil, err
}
return coll.InsertOne(ctx, order)
}
transactionResult, err := session.WithTransaction(ctx, callback)
if err != nil {
return nil, err
}
result := transactionResult.(*mongo.InsertOneResult)
return result, nil
}
type CancelRequest struct {
CustomerID string `json:"customer_id"`
OrderID string `json:"order_id"`
}
func CancelOneOrder(
ctx context.Context,
client *mongo.Client,
customerID primitive.ObjectID,
id primitive.ObjectID,
) (*mongo.UpdateResult, error) {
db := client.Database(database.DBName)
coll := db.Collection(database.CollName)
filter := bson.M{
"_id": id,
"customer_id": customerID,
}
update := bson.M{"$set": bson.M{"is_canceled": true}}
return coll.UpdateOne(ctx, filter, update)
}