generated from dipeshdulal/clean-gin
/
appointment.go
144 lines (128 loc) · 4.56 KB
/
appointment.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
package services
import (
"context"
"errors"
"fmt"
"time"
"github.com/idrpambudi/fita-appointment/api/dto"
"github.com/idrpambudi/fita-appointment/api/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type (
AppointmentService struct {
CoachAvailabilityCollection models.CoachAvailabilityCollection
AppointmentCollection models.AppointmentCollection
}
)
func NewAppointmentService(cac models.CoachAvailabilityCollection, ac models.AppointmentCollection) AppointmentService {
return AppointmentService{
CoachAvailabilityCollection: cac,
AppointmentCollection: ac,
}
}
func (as AppointmentService) CreateAppointment(appointment models.Appointment) (*models.Appointment, error) {
if err := as.validateAppointment(appointment); err != nil {
return nil, err
}
appointment.ID = primitive.NewObjectID().Hex()
appointment.Status = dto.Scheduled
_, err := as.AppointmentCollection.InsertOne(context.Background(), appointment)
if err != nil {
return nil, err
}
return &appointment, nil
}
func (as AppointmentService) DecideAppointment(id string, status dto.AppointmentStatus) error {
var find models.Appointment
if err := as.AppointmentCollection.FindOne(context.Background(), bson.M{
"_id": id,
"status": dto.Scheduled,
}).Decode(&find); err != nil {
return err
}
if _, err := as.AppointmentCollection.UpdateByID(context.Background(), id, bson.M{"$set": bson.M{"status": status}}); err != nil {
return err
}
return nil
}
func (as AppointmentService) RescheduleAppointment(id string, start time.Time, end time.Time) error {
var find models.Appointment
if err := as.AppointmentCollection.FindOne(context.Background(), bson.M{
"_id": id,
"status": dto.Scheduled,
}).Decode(&find); err != nil {
return err
}
if _, err := as.AppointmentCollection.UpdateByID(context.Background(), id, bson.M{"$set": bson.M{
"status": dto.Rescheduled,
"rescheduledstart": start,
"rescheduledend": end,
}}); err != nil {
return err
}
return nil
}
func (as AppointmentService) DecideRescheduledAppointment(id string, status dto.AppointmentStatus) error {
var find models.Appointment
if err := as.AppointmentCollection.FindOne(context.Background(), bson.M{
"_id": id,
"status": dto.Rescheduled,
}).Decode(&find); err != nil {
return err
}
_, err := as.AppointmentCollection.UpdateByID(context.Background(), id, bson.M{"$set": bson.M{"status": status}})
if err != nil {
return err
}
return nil
}
func (as AppointmentService) validateAppointment(appointment models.Appointment) error {
if !as.isCoachAvailable(appointment) {
return errors.New("Coach is unavailable at the chosen appointment time")
}
if as.isAppointmentCollide(appointment) {
return errors.New("The chosen appointment time collide with another appointment")
}
return nil
}
func (as *AppointmentService) isAppointmentCollide(appointment models.Appointment) bool {
return as.AppointmentCollection.FindOne(context.Background(), bson.M{
"coach": appointment.Coach,
"status": bson.M{"$ne": "Declined"},
"$or": bson.A{
bson.M{
"rescheduledstart": bson.M{"$in": bson.A{"", nil}},
"start": bson.M{"$lt": appointment.End},
"end": bson.M{"$gt": appointment.Start},
}, bson.M{
"rescheduledstart": bson.M{"$lt": appointment.End},
"rescheduledend": bson.M{"$gt": appointment.Start},
},
},
}).Err() == nil
}
func (as *AppointmentService) isCoachAvailable(appointment models.Appointment) bool {
startDay, startHourMinute := getWeeklyTimeInterval(appointment.Start)
endDay, endHourMinute := getWeeklyTimeInterval(appointment.End)
if startDay == endDay {
return as.findCoachAvailability(appointment.Coach, startDay, startHourMinute, endHourMinute).Err() == nil
}
return as.findCoachAvailability(appointment.Coach, startDay, startHourMinute, "24:00").Err() == nil && as.findCoachAvailability(appointment.Coach, endDay, "00:00", endHourMinute).Err() == nil
}
func (as *AppointmentService) findCoachAvailability(coach, day, startHourMinute, endHourMinute string) *mongo.SingleResult {
return as.CoachAvailabilityCollection.FindOne(context.Background(), bson.M{
"name": coach,
"dayofweek": day,
"start": bson.M{"$lte": startHourMinute},
"end": bson.M{"$gte": endHourMinute},
})
}
func getWeeklyTimeInterval(timestamp time.Time) (dayOfWeek, hourMinute string) {
dayOfWeek = timestamp.UTC().Weekday().String()
hour := timestamp.UTC().Hour()
minute := timestamp.UTC().Minute()
hourMinute = fmt.Sprintf("%02d:%02d", hour, minute)
return
}