|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "time" |
| 6 | + |
| 7 | + amodels "github.com/abhinavxd/libredesk/internal/auth/models" |
| 8 | + "github.com/abhinavxd/libredesk/internal/envelope" |
| 9 | + "github.com/valyala/fasthttp" |
| 10 | + "github.com/zerodha/fastglue" |
| 11 | +) |
| 12 | + |
| 13 | +// reminderCreateReq is the payload accepted by handleCreateReminder. |
| 14 | +type reminderCreateReq struct { |
| 15 | + // ISO 8601 / RFC 3339 timestamp. Must be in the future. |
| 16 | + RemindAt string `json:"remind_at"` |
| 17 | + Note string `json:"note"` |
| 18 | +} |
| 19 | + |
| 20 | +// handleListConversationReminders returns the requesting agent's pending |
| 21 | +// reminders for a specific conversation (by UUID). Private to the agent — |
| 22 | +// each user only sees their own. |
| 23 | +func handleListConversationReminders(r *fastglue.Request) error { |
| 24 | + var ( |
| 25 | + app = r.Context.(*App) |
| 26 | + user = r.RequestCtx.UserValue("user").(amodels.User) |
| 27 | + uuid = r.RequestCtx.UserValue("uuid").(string) |
| 28 | + ) |
| 29 | + if uuid == "" { |
| 30 | + return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`uuid`"), nil, envelope.InputError) |
| 31 | + } |
| 32 | + out, err := app.reminder.ListForConversation(user.ID, uuid) |
| 33 | + if err != nil { |
| 34 | + return sendErrorEnvelope(r, err) |
| 35 | + } |
| 36 | + return r.SendEnvelope(out) |
| 37 | +} |
| 38 | + |
| 39 | +// handleListMyReminders returns every pending reminder owned by the |
| 40 | +// requesting agent across all conversations. Used by a future "My |
| 41 | +// reminders" sidebar view. |
| 42 | +func handleListMyReminders(r *fastglue.Request) error { |
| 43 | + var ( |
| 44 | + app = r.Context.(*App) |
| 45 | + user = r.RequestCtx.UserValue("user").(amodels.User) |
| 46 | + ) |
| 47 | + out, err := app.reminder.ListForUser(user.ID) |
| 48 | + if err != nil { |
| 49 | + return sendErrorEnvelope(r, err) |
| 50 | + } |
| 51 | + return r.SendEnvelope(out) |
| 52 | +} |
| 53 | + |
| 54 | +// handleCreateReminder sets a personal reminder on a conversation. |
| 55 | +func handleCreateReminder(r *fastglue.Request) error { |
| 56 | + var ( |
| 57 | + app = r.Context.(*App) |
| 58 | + user = r.RequestCtx.UserValue("user").(amodels.User) |
| 59 | + uuid = r.RequestCtx.UserValue("uuid").(string) |
| 60 | + req reminderCreateReq |
| 61 | + ) |
| 62 | + if uuid == "" { |
| 63 | + return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`uuid`"), nil, envelope.InputError) |
| 64 | + } |
| 65 | + if err := r.Decode(&req, "json"); err != nil { |
| 66 | + return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.errorParsing", "name", "{globals.terms.request}"), err.Error(), envelope.InputError) |
| 67 | + } |
| 68 | + if req.RemindAt == "" { |
| 69 | + return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.empty", "name", "`remind_at`"), nil, envelope.InputError) |
| 70 | + } |
| 71 | + |
| 72 | + // Parse remind_at; require strict RFC 3339 so we don't accept timezone-less |
| 73 | + // strings that would later get misinterpreted by the worker's NOW() compare. |
| 74 | + remindAt, err := time.Parse(time.RFC3339, req.RemindAt) |
| 75 | + if err != nil { |
| 76 | + return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`remind_at`"), nil, envelope.InputError) |
| 77 | + } |
| 78 | + // Reject obviously stale picks — gives a 1-minute grace so a slow agent |
| 79 | + // can still set "in 1 minute" without server-clock skew kicking it back. |
| 80 | + if remindAt.Before(time.Now().Add(-1 * time.Minute)) { |
| 81 | + return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`remind_at`"), nil, envelope.InputError) |
| 82 | + } |
| 83 | + if len(req.Note) > 500 { |
| 84 | + return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`note`"), nil, envelope.InputError) |
| 85 | + } |
| 86 | + |
| 87 | + convID, err := app.reminder.ResolveConversationIDByUUID(uuid) |
| 88 | + if err != nil { |
| 89 | + return r.SendErrorEnvelope(fasthttp.StatusNotFound, app.i18n.Ts("globals.messages.notFound", "name", "{globals.terms.conversation}"), nil, envelope.InputError) |
| 90 | + } |
| 91 | + |
| 92 | + created, err := app.reminder.Create(user.ID, convID, remindAt, req.Note) |
| 93 | + if err != nil { |
| 94 | + return sendErrorEnvelope(r, err) |
| 95 | + } |
| 96 | + return r.SendEnvelope(created) |
| 97 | +} |
| 98 | + |
| 99 | +// handleDeleteReminder removes a reminder. Only the owning user may delete |
| 100 | +// theirs — the query enforces user_id == caller. |
| 101 | +func handleDeleteReminder(r *fastglue.Request) error { |
| 102 | + var ( |
| 103 | + app = r.Context.(*App) |
| 104 | + user = r.RequestCtx.UserValue("user").(amodels.User) |
| 105 | + ) |
| 106 | + id, err := strconv.Atoi(r.RequestCtx.UserValue("id").(string)) |
| 107 | + if err != nil || id <= 0 { |
| 108 | + return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.Ts("globals.messages.invalid", "name", "`id`"), nil, envelope.InputError) |
| 109 | + } |
| 110 | + if err := app.reminder.Delete(user.ID, id); err != nil { |
| 111 | + return sendErrorEnvelope(r, err) |
| 112 | + } |
| 113 | + return r.SendEnvelope(true) |
| 114 | +} |
0 commit comments