Skip to content

Commit

Permalink
feat: add absent existing check to discord absent command
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Trost <[email protected]>
  • Loading branch information
galexrt committed Nov 12, 2024
1 parent f0219a4 commit cdee342
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
86 changes: 66 additions & 20 deletions pkg/discord/commands/absent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"context"
"database/sql"
"errors"
"strings"
"time"

Expand All @@ -22,6 +23,7 @@ import (
timeutils "github.com/fivenet-app/fivenet/pkg/utils/time"
"github.com/fivenet-app/fivenet/query/fivenet/table"
jet "github.com/go-jet/jet/v2/mysql"
"github.com/go-jet/jet/v2/qrm"
"github.com/nicksnyder/go-i18n/v2/i18n"
)

Expand Down Expand Up @@ -188,7 +190,7 @@ func (c *AbsentCommand) HandleCommand(ctx context.Context, cmd cmdroute.CommandD
}
startDate = parsed

if !(startDate.After(now) || now.Equal(startDate)) {
if !(now.Equal(startDate) || startDate.After(now)) {
(*resp.Embeds)[0].Title = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "discord.commands.absent.results.invalid_date.title"})
(*resp.Embeds)[0].Description = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "discord.commands.absent.results.invalid_date.desc"})
return resp
Expand All @@ -210,10 +212,11 @@ func (c *AbsentCommand) HandleCommand(ctx context.Context, cmd cmdroute.CommandD
endDate := startDate.AddDate(0, 0, int(days))

reasonOption := cmd.Data.Options.Find("reason")
reason := reasonOption.String()
reason := strings.TrimSpace(reasonOption.String())
reason += " (via Discord Bot)"

if err := c.createAbsenceForUser(ctx, userId, job, timestamp.New(startDate), timestamp.New(endDate), reason); err != nil {
check, err := c.createAbsenceForUser(ctx, userId, job, startDate, endDate, reason)
if err != nil {
(*resp.Embeds)[0].Title = localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "discord.commands.absent.results.failed.title",
})
Expand All @@ -226,15 +229,28 @@ func (c *AbsentCommand) HandleCommand(ctx context.Context, cmd cmdroute.CommandD
return resp
}

(*resp.Embeds)[0].Title = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "discord.commands.absent.results.success.title"})
(*resp.Embeds)[0].Description = localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "discord.commands.absent.results.success.desc",
TemplateData: map[string]string{
"AbsenceBegin": startDate.Format(absentDateFormat),
"AbsenceEnd": endDate.Format(absentDateFormat),
},
})
(*resp.Embeds)[0].Color = embeds.ColorSuccess
if !check {
(*resp.Embeds)[0].Title = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "discord.commands.absent.results.success.title"})
(*resp.Embeds)[0].Description = localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "discord.commands.absent.results.success.desc",
TemplateData: map[string]string{
"AbsenceBegin": startDate.Format(absentDateFormat),
"AbsenceEnd": endDate.Format(absentDateFormat),
},
})
(*resp.Embeds)[0].Color = embeds.ColorSuccess
} else {
(*resp.Embeds)[0].Title = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "discord.commands.absent.results.already_absent.title"})
(*resp.Embeds)[0].Description = localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "discord.commands.absent.results.already_absent.desc",
TemplateData: map[string]string{
"AbsenceBegin": startDate.Format(absentDateFormat),
"AbsenceEnd": endDate.Format(absentDateFormat),
},
})
(*resp.Embeds)[0].Color = embeds.ColorInfo
}

return resp
}

Expand Down Expand Up @@ -272,11 +288,41 @@ func (c *AbsentCommand) getUserIDByJobAndDiscordID(ctx context.Context, job stri
return dest.UserID, dest.JobGrade, nil
}

func (c *AbsentCommand) createAbsenceForUser(ctx context.Context, charId int32, job string, absenceBegin *timestamp.Timestamp, absenceEnd *timestamp.Timestamp, reason string) error {
func (c *AbsentCommand) createAbsenceForUser(ctx context.Context, charId int32, job string, absenceBegin time.Time, absenceEnd time.Time, reason string) (bool, error) {
// TODO check if user has already been set absent
checkStmt := tJobsUserProps.
SELECT(
tJobsUserProps.AbsenceBegin,
tJobsUserProps.AbsenceEnd,
).
FROM(tJobsUserProps).
WHERE(jet.AND(
tJobsUserProps.UserID.EQ(jet.Int32(charId)),
tJobsUserProps.Job.EQ(jet.String(job)),
)).
LIMIT(1)

props := jobs.JobsUserProps{}
if err := checkStmt.QueryContext(ctx, c.db, &props); err != nil {
if !errors.Is(err, qrm.ErrNoRows) {
return false, err
}
}

if props.AbsenceBegin != nil && props.AbsenceEnd != nil {
begin := props.AbsenceBegin.AsTime()
end := props.AbsenceEnd.AsTime()

// Check if current absence is equal to requested one
if begin.Equal(absenceBegin) && end.Equal(absenceEnd) {
return true, nil
}
}

// Begin transaction
tx, err := c.db.BeginTx(ctx, nil)
if err != nil {
return err
return false, err
}
// Defer a rollback in case anything fails
defer tx.Rollback()
Expand All @@ -303,7 +349,7 @@ func (c *AbsentCommand) createAbsenceForUser(ctx context.Context, charId int32,
)

if _, err := stmt.ExecContext(ctx, c.db); err != nil {
return err
return false, err
}

activityStmt := tJobsUserActivity.
Expand All @@ -324,21 +370,21 @@ func (c *AbsentCommand) createAbsenceForUser(ctx context.Context, charId int32,
&jobs.JobsUserActivityData{
Data: &jobs.JobsUserActivityData_AbsenceDate{
AbsenceDate: &jobs.ColleagueAbsenceDate{
AbsenceBegin: absenceBegin,
AbsenceEnd: absenceEnd,
AbsenceBegin: timestamp.New(absenceBegin),
AbsenceEnd: timestamp.New(absenceEnd),
},
},
},
)

if _, err := activityStmt.ExecContext(ctx, tx); err != nil {
return err
return false, err
}

// Commit the transaction
if err := tx.Commit(); err != nil {
return err
return false, err
}

return nil
return false, nil
}
4 changes: 4 additions & 0 deletions pkg/lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
"success": {
"title": "Abwesenheit eingetragen",
"desc": "Abwesenheit erfolgreich eingetragen.\n({{.AbsenceBegin}} - {{.AbsenceEnd}})"
},
"already_absent": {
"title": "Abwesenheit bereits eingetragen",
"desc": "Eine Abwesenheit für den gleichen Zeitraum ist bereits hinterlegt.\n({{.AbsenceBegin}} - {{.AbsenceEnd}})"
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
"success": {
"title": "Successfully submitted your Absence",
"desc": "Your absence has been submitted successfully.\n({{.AbsenceBegin}} - {{.AbsenceEnd}})"
},
"already_absent": {
"title": "An Absence already been subbmited!",
"desc": "You already have an absence for the same dates.\n({{.AbsenceBegin}} - {{.AbsenceEnd}})"
}
}
}
Expand Down

0 comments on commit cdee342

Please sign in to comment.