## Add absences table and basic queries * Add migration scripts * Add queries to add, get and list absences * Add tests for the above * Add function to get the date of the next Monday Also fixes potential bug in the build where the output binary would be written to a file called `./bin` rather than a file named after the source directory in a _directory_ called `./bin`. ## Add /schedule command Adds the Discord slash command to get the schedule for the next few weeks. Also updates the required Go version to 1.21 to benefit from the new `time.DateOnly` format that's gonna be used in the absences table. ## Add scheduled message every Saturday 5pm Adds the `notify.go` file which manages the scheduling of the recurrent message that will ping the group every week about the following game. ## Add /absent command Adds command to register as absent for a specific date. The command takes in one optional parameter for the date, if none is specified, defaults to the next session.absences
parent
e9632dfaae
commit
638083a755
@ -1 +1,110 @@
|
||||
package themis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *Store) AddAbsence(ctx context.Context, session time.Time, userId string) error {
|
||||
if session.Weekday() != time.Monday {
|
||||
return fmt.Errorf("not a monday")
|
||||
}
|
||||
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
defer tx.Commit() //nolint:errcheck
|
||||
|
||||
stmt, err := s.db.PrepareContext(ctx, "INSERT INTO absences (session_date, userid) VALUES (?, ?)")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to prepare absence query: %w", err)
|
||||
}
|
||||
|
||||
_, err = stmt.ExecContext(ctx, session.Format(time.DateOnly), userId)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to insert absence: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) GetAbsentees(ctx context.Context, session time.Time) ([]string, error) {
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
defer tx.Commit() //nolint:errcheck
|
||||
|
||||
stmt, err := s.db.PrepareContext(ctx, `SELECT userid FROM absences WHERE session_date = ?`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to prepare query: %w", err)
|
||||
}
|
||||
|
||||
rows, err := stmt.QueryContext(ctx, session.Format(time.DateOnly))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute query: %w", err)
|
||||
}
|
||||
|
||||
absentees := make([]string, 0)
|
||||
for rows.Next() {
|
||||
var abs string
|
||||
err = rows.Scan(&abs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to scan row: %w", err)
|
||||
}
|
||||
|
||||
absentees = append(absentees, abs)
|
||||
}
|
||||
|
||||
return absentees, nil
|
||||
}
|
||||
|
||||
// map session_date -> list of absentees
|
||||
type Schedule map[string][]string
|
||||
|
||||
func (s *Store) GetSchedule(ctx context.Context, from, to time.Time) (Schedule, error) {
|
||||
schedule := make(Schedule)
|
||||
initSchedule(schedule, from, to)
|
||||
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
defer tx.Commit() //nolint:errcheck
|
||||
|
||||
stmt, err := s.db.PrepareContext(ctx, `SELECT session_date, userid FROM absences WHERE session_date BETWEEN ? AND ? ORDER BY session_date ASC`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to prepare query: %w", err)
|
||||
}
|
||||
|
||||
rows, err := stmt.QueryContext(ctx, from.Format(time.DateOnly), to.Format(time.DateOnly))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute query: %w", err)
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var date string
|
||||
var user string
|
||||
err = rows.Scan(&date, &user)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to scan row: %w", err)
|
||||
}
|
||||
|
||||
if _, ok := schedule[date]; ok {
|
||||
schedule[date] = append(schedule[date], user)
|
||||
} else {
|
||||
schedule[date] = []string{user}
|
||||
}
|
||||
}
|
||||
|
||||
return schedule, nil
|
||||
}
|
||||
|
||||
func initSchedule(schedule Schedule, from, to time.Time) {
|
||||
for from.Before(to) || from.Equal(to) {
|
||||
schedule[from.Format(time.DateOnly)] = []string{}
|
||||
from = from.AddDate(0, 0, 7)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,49 @@
|
||||
package themis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAddAbsence(t *testing.T) {
|
||||
store, err := NewStore(fmt.Sprintf(TEST_CONN_STRING_PATTERN, "TestAddAbsence"))
|
||||
require.NoError(t, err)
|
||||
|
||||
now := NextMonday()
|
||||
assert.NoError(t, store.AddAbsence(context.TODO(), now, "foobarbaz"))
|
||||
absentees, err := store.GetAbsentees(context.TODO(), now)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, len(absentees))
|
||||
assert.Equal(t, "foobarbaz", absentees[0])
|
||||
|
||||
assert.NoError(t, store.AddAbsence(context.TODO(), now, "foobarbaz"))
|
||||
absentees, err = store.GetAbsentees(context.TODO(), now)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, len(absentees))
|
||||
}
|
||||
|
||||
func TestGetSchedule(t *testing.T) {
|
||||
store, err := NewStore(fmt.Sprintf(TEST_CONN_STRING_PATTERN, "TestGetSchedule"))
|
||||
require.NoError(t, err)
|
||||
|
||||
now := NextMonday()
|
||||
|
||||
_ = store.AddAbsence(context.TODO(), now.Add(7*24*time.Hour), "foobar")
|
||||
|
||||
schedule, err := store.GetSchedule(context.TODO(), now, now.AddDate(0, 0, 14))
|
||||
assert.NoError(t, err)
|
||||
// reason being, the schedule should initialize to the desired time range
|
||||
assert.Equal(t, 3, len(schedule))
|
||||
for d, a := range schedule {
|
||||
if d == now.Add(7*24*time.Hour).Format(time.DateOnly) {
|
||||
assert.Equal(t, 1, len(a))
|
||||
} else {
|
||||
assert.Equal(t, 0, len(a))
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
DROP TABLE absences;
|
@ -0,0 +1,5 @@
|
||||
CREATE TABLE IF NOT EXISTS absences (
|
||||
session_date TEXT, -- 2006-11-23
|
||||
userid TEXT,
|
||||
UNIQUE(session_date, userid) ON CONFLICT IGNORE
|
||||
);
|
@ -0,0 +1,68 @@
|
||||
package themis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
var loc *time.Location
|
||||
|
||||
func init() {
|
||||
loc, _ = time.LoadLocation("America/New_York")
|
||||
}
|
||||
|
||||
type Notifier struct {
|
||||
c chan struct{}
|
||||
}
|
||||
|
||||
func NewNotifier(c chan struct{}) *Notifier {
|
||||
return &Notifier{
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notifier) Start(ctx context.Context) {
|
||||
m := NextMonday()
|
||||
sat := m.AddDate(0, 0, -2)
|
||||
if sat.Before(time.Now()) {
|
||||
sat = sat.AddDate(0, 0, 7)
|
||||
}
|
||||
|
||||
t, err := time.ParseInLocation(time.DateTime, fmt.Sprintf("%s 17:00:00", sat.Format(time.DateOnly)), loc)
|
||||
if err != nil {
|
||||
panic("failed to parse next monday notif time. this is likely a bug.")
|
||||
}
|
||||
|
||||
first := time.NewTimer(time.Until(t))
|
||||
<-first.C
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
default:
|
||||
n.c <- struct{}{}
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(time.Hour * 24 * 7)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
n.c <- struct{}{}
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notifier) NotifyFunc(ctx context.Context, f func()) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-n.c:
|
||||
f()
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package themis
|
||||
|
||||
import "time"
|
||||
|
||||
var now func() time.Time
|
||||
|
||||
func init() {
|
||||
now = time.Now
|
||||
}
|
||||
|
||||
func NextMonday() time.Time {
|
||||
return now().AddDate(0, 0, int((8-now().Weekday())%7))
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package themis
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNextMonday(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
seed string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "on monday",
|
||||
seed: "2023-11-13T15:04:05Z07:00",
|
||||
want: "2023-11-13T15:04:05Z07:00",
|
||||
},
|
||||
{
|
||||
name: "on sunday",
|
||||
seed: "2023-11-12T15:04:05Z07:00",
|
||||
want: "2023-11-13T15:04:05Z07:00",
|
||||
},
|
||||
{
|
||||
name: "on tuesday",
|
||||
seed: "2023-11-14T15:04:05Z07:00",
|
||||
want: "2023-11-20T15:04:05Z07:00",
|
||||
},
|
||||
{
|
||||
name: "on saturday",
|
||||
seed: "2023-11-18T15:04:05Z07:00",
|
||||
want: "2023-11-20T15:04:05Z07:00",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
seedt, _ := time.Parse(time.RFC3339, tt.seed)
|
||||
now = func() time.Time { return seedt }
|
||||
wantt, _ := time.Parse(time.RFC3339, tt.want)
|
||||
if got := NextMonday(); !reflect.DeepEqual(got, wantt) {
|
||||
t.Errorf("NextMonday() = %v, want %v", got, wantt)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue