puerta/internal/user/schedule.go

147 lines
2.9 KiB
Go
Raw Normal View History

2022-12-30 06:51:51 +00:00
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2022 Roberto Hidalgo <nidito@un.rob.mx>
2023-01-04 04:21:49 +00:00
package user
2022-12-30 06:51:51 +00:00
import (
2023-01-04 04:21:49 +00:00
"database/sql"
2022-12-30 06:51:51 +00:00
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/upper/db/v4"
2022-12-30 06:51:51 +00:00
)
func parseHour(src string) (float64, error) {
hm := strings.Split(src, ":")
if len(hm) == 1 {
return strconv.ParseFloat(hm[0], 32)
}
if len(hm) == 2 {
h, err := strconv.ParseFloat(hm[0], 32)
if err != nil {
return 0.0, err
}
m, err := strconv.ParseFloat(hm[1], 32)
if err != nil {
return 0.0, err
}
return h + (m / 60.0), nil
}
return 0.0, fmt.Errorf("unknown format for hour: %s", hm)
}
2023-01-04 04:21:49 +00:00
type Schedule struct {
2022-12-30 06:51:51 +00:00
src string
days []int
hours []float64
}
2023-01-04 04:21:49 +00:00
func (d Schedule) MarshalDB() (any, error) {
2022-12-30 06:51:51 +00:00
return json.Marshal(d.src)
}
2023-01-04 04:21:49 +00:00
func (d Schedule) MarshalJSON() ([]byte, error) {
return json.Marshal(d.src)
}
2023-01-04 04:21:49 +00:00
func (d *Schedule) UnmarshalJSON(value []byte) error {
var str string
if err := json.Unmarshal(value, &str); err != nil {
return err
}
parsed := Schedule{src: str}
if err := parsed.Parse(); err != nil {
return err
}
*d = parsed
return nil
}
func (d *Schedule) Parse() error {
2023-01-03 07:40:38 +00:00
for _, kv := range strings.Split(d.src, " ") {
2022-12-30 06:51:51 +00:00
kvSlice := strings.Split(kv, "=")
key := kvSlice[0]
values := strings.Split(kvSlice[1], "-")
switch key {
case "days":
from, err := strconv.Atoi(values[0])
if err != nil {
return err
}
until, err := strconv.Atoi(values[1])
if err != nil {
return err
}
2023-01-04 04:21:49 +00:00
logrus.Debugf("Parsed schedule days from: %d until %d", from, until)
2022-12-30 06:51:51 +00:00
d.days = []int{from, until}
case "hours":
from, err := parseHour(values[0])
if err != nil {
return err
}
until, err := parseHour(values[1])
if err != nil {
return err
}
2023-01-04 04:21:49 +00:00
logrus.Debugf("Parsed schedule hours from: %f until %f", from, until)
2022-12-30 06:51:51 +00:00
d.hours = []float64{from, until}
}
}
2023-01-03 07:40:38 +00:00
return nil
}
2023-01-04 04:21:49 +00:00
func (d *Schedule) Scan(value any) error {
if value == nil {
return nil
2023-01-03 07:40:38 +00:00
}
2023-01-04 04:21:49 +00:00
var src string
var ok bool
if src, ok = value.(string); !ok {
if err := json.Unmarshal(value.([]byte), &src); err != nil {
return err
}
2023-01-03 07:40:38 +00:00
}
2023-01-04 04:21:49 +00:00
d.src = src
2023-01-03 07:40:38 +00:00
2023-01-04 04:21:49 +00:00
// parsed := UserSchedule{src: src}
if err := d.Parse(); err != nil {
2023-01-03 07:40:38 +00:00
return err
}
2022-12-30 06:51:51 +00:00
return nil
}
2023-01-04 04:21:49 +00:00
func (sch *Schedule) AllowedAt(t time.Time) bool {
2022-12-30 06:51:51 +00:00
weekDay := int(t.Weekday())
h, m, s := t.Clock()
fractionalHour := float64(h) + (float64(m*60.0+s) / 3600.0)
logrus.Infof("Validating access at weekday %d, hour %f from rules: days=%v hours=%v at %s", weekDay, fractionalHour, sch.days, sch.hours, t.String())
if sch.days != nil {
if weekDay < sch.days[0] || weekDay > sch.days[1] {
return false
}
}
if sch.hours != nil {
if fractionalHour < sch.hours[0] || fractionalHour > sch.hours[1] {
return false
}
}
return true
}
2023-01-04 04:21:49 +00:00
var _ sql.Scanner = &Schedule{}
var _ db.Marshaler = &Schedule{}
var _ json.Marshaler = &Schedule{}
var _ json.Unmarshaler = &Schedule{}