-
-
Notifications
You must be signed in to change notification settings - Fork 290
Feature/send sms based on schedule #857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AchoArnold
wants to merge
13
commits into
main
Choose a base branch
from
feature/send-sms-based-on-schedule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f80acce
feat: Refactor send schedules based on review feedback
giresse19 df1642a
Merge remote-tracking branch 'upstream/main' into feature/send-schedu…
giresse19 a988dea
feat: Refactor send schedules based on review feedback
giresse19 6cfa208
feat: Refactor send schedules based on review feedback
giresse19 9de4156
fix: handle review feedback
giresse19 62b6031
fix: handle review feedback
giresse19 ec05b88
fix: handle review feedback
giresse19 79c2d0c
fix: handle review feedback
giresse19 0085695
fix: handle review feedback
giresse19 7c5cada
Merge upstream main into feature/send-schedule-review-fixes
giresse19 64c351a
Regenerate swagger after merging upstream main
giresse19 d7aa243
Merge pull request #841 from giresse19/feature/send-schedule-review-f…
AchoArnold d187f37
Remove sqlite
AchoArnold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package entities | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| ) | ||
|
|
||
| // MessageSendScheduleWindow represents a single availability window for a day of the week. | ||
| type MessageSendScheduleWindow struct { | ||
| DayOfWeek int `json:"day_of_week" example:"1"` | ||
| StartMinute int `json:"start_minute" example:"540"` | ||
| EndMinute int `json:"end_minute" example:"1020"` | ||
| } | ||
|
|
||
| // MessageSendSchedule controls when a phone is allowed to send outgoing SMS messages. | ||
| type MessageSendSchedule struct { | ||
| ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;" example:"32343a19-da5e-4b1b-a767-3298a73703cb"` | ||
| UserID UserID `json:"user_id" example:"WB7DRDWrJZRGbYrv2CKGkqbzvqdC"` | ||
| Name string `json:"name" example:"Business Hours"` | ||
| Timezone string `json:"timezone" example:"Europe/Tallinn"` | ||
| IsActive bool `json:"is_active" gorm:"default:true" example:"true"` | ||
| Windows []MessageSendScheduleWindow `json:"windows" gorm:"type:jsonb;serializer:json"` | ||
| CreatedAt time.Time `json:"created_at" example:"2022-06-05T14:26:02.302718+03:00"` | ||
| UpdatedAt time.Time `json:"updated_at" example:"2022-06-05T14:26:10.303278+03:00"` | ||
| } | ||
|
|
||
| // ResolveScheduledAt returns the next allowed send time based on the schedule. | ||
| // If the schedule is inactive, has no windows, or has an invalid timezone, | ||
| // the current time is returned in UTC. | ||
| func (schedule *MessageSendSchedule) ResolveScheduledAt(current time.Time) time.Time { | ||
| if schedule == nil || !schedule.IsActive || len(schedule.Windows) == 0 { | ||
| return current.UTC() | ||
| } | ||
|
|
||
| location, err := time.LoadLocation(schedule.Timezone) | ||
| if err != nil { | ||
| return current.UTC() | ||
| } | ||
|
|
||
| base := current.In(location) | ||
| var best time.Time | ||
|
|
||
| for dayOffset := 0; dayOffset <= 7; dayOffset++ { | ||
| day := base.AddDate(0, 0, dayOffset) | ||
| weekday := int(day.Weekday()) | ||
|
|
||
| for _, window := range schedule.Windows { | ||
| if window.DayOfWeek != weekday { | ||
| continue | ||
| } | ||
|
|
||
| start := time.Date( | ||
| day.Year(), | ||
| day.Month(), | ||
| day.Day(), | ||
| 0, | ||
| 0, | ||
| 0, | ||
| 0, | ||
| location, | ||
| ).Add(time.Duration(window.StartMinute) * time.Minute) | ||
|
|
||
| end := time.Date( | ||
| day.Year(), | ||
| day.Month(), | ||
| day.Day(), | ||
| 0, | ||
| 0, | ||
| 0, | ||
| 0, | ||
| location, | ||
| ).Add(time.Duration(window.EndMinute) * time.Minute) | ||
|
|
||
| var candidate time.Time | ||
|
|
||
| switch { | ||
| case dayOffset == 0 && base.Before(start): | ||
| candidate = start | ||
| case dayOffset == 0 && (base.Equal(start) || (base.After(start) && base.Before(end))): | ||
| candidate = base | ||
| case dayOffset > 0: | ||
| candidate = start | ||
| default: | ||
| continue | ||
| } | ||
|
|
||
| if best.IsZero() || candidate.Before(best) { | ||
| best = candidate | ||
| } | ||
| } | ||
|
|
||
| if !best.IsZero() { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if best.IsZero() { | ||
| return current.UTC() | ||
| } | ||
|
|
||
| return best.UTC() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
IsActiveistruebutWindowsis empty,ResolveScheduledAtreturnscurrent.UTC()(the early-exit path on line 32). This means messages are sent immediately instead of being held, which is the opposite of what a user who configured an active-but-window-less schedule might expect. Consider either disallowing an active schedule with zero windows (add a validator rule) or treating it the same asIsActive: false.