|
| 1 | +package campaigns |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/labstack/echo/v4" |
| 7 | + "github.com/keyxmakerx/chronicle/internal/apperror" |
| 8 | + "github.com/keyxmakerx/chronicle/internal/middleware" |
| 9 | + "github.com/keyxmakerx/chronicle/internal/plugins/auth" |
| 10 | +) |
| 11 | + |
| 12 | +// InviteHandler handles HTTP requests for campaign invites. |
| 13 | +type InviteHandler struct { |
| 14 | + service InviteService |
| 15 | + campaigns CampaignService |
| 16 | + baseURL string |
| 17 | +} |
| 18 | + |
| 19 | +// NewInviteHandler creates a new invite handler. |
| 20 | +func NewInviteHandler(service InviteService, campaigns CampaignService, baseURL string) *InviteHandler { |
| 21 | + return &InviteHandler{ |
| 22 | + service: service, |
| 23 | + campaigns: campaigns, |
| 24 | + baseURL: baseURL, |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// ListInvitesAPI returns all invites for a campaign as JSON. |
| 29 | +// GET /campaigns/:id/invites |
| 30 | +func (h *InviteHandler) ListInvitesAPI(c echo.Context) error { |
| 31 | + cc := GetCampaignContext(c) |
| 32 | + if cc == nil { |
| 33 | + return apperror.NewMissingContext() |
| 34 | + } |
| 35 | + |
| 36 | + invites, err := h.service.ListInvites(c.Request().Context(), cc.Campaign.ID) |
| 37 | + if err != nil { |
| 38 | + return apperror.NewInternal(err) |
| 39 | + } |
| 40 | + if invites == nil { |
| 41 | + invites = []Invite{} |
| 42 | + } |
| 43 | + return c.JSON(http.StatusOK, invites) |
| 44 | +} |
| 45 | + |
| 46 | +// CreateInviteAPI creates a new campaign invite. |
| 47 | +// POST /campaigns/:id/invites |
| 48 | +func (h *InviteHandler) CreateInviteAPI(c echo.Context) error { |
| 49 | + cc := GetCampaignContext(c) |
| 50 | + if cc == nil { |
| 51 | + return apperror.NewMissingContext() |
| 52 | + } |
| 53 | + |
| 54 | + var input CreateInviteInput |
| 55 | + if err := c.Bind(&input); err != nil { |
| 56 | + return apperror.NewBadRequest("invalid request body") |
| 57 | + } |
| 58 | + |
| 59 | + userID := auth.GetUserID(c) |
| 60 | + invite, err := h.service.CreateInvite(c.Request().Context(), cc.Campaign.ID, userID, input) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + return c.JSON(http.StatusCreated, invite) |
| 66 | +} |
| 67 | + |
| 68 | +// RevokeInviteAPI deletes a pending invite. |
| 69 | +// DELETE /campaigns/:id/invites/:inviteId |
| 70 | +func (h *InviteHandler) RevokeInviteAPI(c echo.Context) error { |
| 71 | + cc := GetCampaignContext(c) |
| 72 | + if cc == nil { |
| 73 | + return apperror.NewMissingContext() |
| 74 | + } |
| 75 | + _ = cc // Used for auth check via route middleware. |
| 76 | + |
| 77 | + inviteID := c.Param("inviteId") |
| 78 | + if inviteID == "" { |
| 79 | + return apperror.NewBadRequest("invite ID required") |
| 80 | + } |
| 81 | + |
| 82 | + if err := h.service.RevokeInvite(c.Request().Context(), inviteID); err != nil { |
| 83 | + return apperror.NewInternal(err) |
| 84 | + } |
| 85 | + |
| 86 | + return c.NoContent(http.StatusNoContent) |
| 87 | +} |
| 88 | + |
| 89 | +// AcceptInvitePage handles invite acceptance. |
| 90 | +// GET /invites/accept?token=xxx |
| 91 | +func (h *InviteHandler) AcceptInvitePage(c echo.Context) error { |
| 92 | + token := c.QueryParam("token") |
| 93 | + if token == "" { |
| 94 | + return apperror.NewBadRequest("missing invite token") |
| 95 | + } |
| 96 | + |
| 97 | + // Fetch the invite to show details. |
| 98 | + invite, err := h.service.GetInviteByToken(c.Request().Context(), token) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + // Fetch campaign name for display. |
| 104 | + campaign, _ := h.campaigns.GetByID(c.Request().Context(), invite.CampaignID) |
| 105 | + |
| 106 | + // Check if user is logged in. |
| 107 | + userID := auth.GetUserID(c) |
| 108 | + if userID == "" { |
| 109 | + // Not logged in — show the invite details and prompt to log in or register. |
| 110 | + campaignName := "" |
| 111 | + if campaign != nil { |
| 112 | + campaignName = campaign.Name |
| 113 | + } |
| 114 | + return middleware.Render(c, http.StatusOK, InviteAcceptPage(invite, campaignName, token, false, "")) |
| 115 | + } |
| 116 | + |
| 117 | + // User is logged in — accept the invite. |
| 118 | + accepted, err := h.service.AcceptInvite(c.Request().Context(), token, userID) |
| 119 | + if err != nil { |
| 120 | + // If it's a validation error (already member, expired), show it. |
| 121 | + campaignName := "" |
| 122 | + if campaign != nil { |
| 123 | + campaignName = campaign.Name |
| 124 | + } |
| 125 | + return middleware.Render(c, http.StatusOK, InviteAcceptPage(invite, campaignName, token, false, err.Error())) |
| 126 | + } |
| 127 | + |
| 128 | + // Success — redirect to the campaign. |
| 129 | + campaignName := "" |
| 130 | + if campaign != nil { |
| 131 | + campaignName = campaign.Name |
| 132 | + } |
| 133 | + _ = accepted |
| 134 | + return middleware.Render(c, http.StatusOK, InviteAcceptPage(invite, campaignName, token, true, "")) |
| 135 | +} |
| 136 | + |
| 137 | +// InvitesPage renders the invite management tab in campaign settings. |
| 138 | +// GET /campaigns/:id/invites/page |
| 139 | +func (h *InviteHandler) InvitesPage(c echo.Context) error { |
| 140 | + cc := GetCampaignContext(c) |
| 141 | + if cc == nil { |
| 142 | + return apperror.NewMissingContext() |
| 143 | + } |
| 144 | + |
| 145 | + invites, err := h.service.ListInvites(c.Request().Context(), cc.Campaign.ID) |
| 146 | + if err != nil { |
| 147 | + return apperror.NewInternal(err) |
| 148 | + } |
| 149 | + |
| 150 | + if middleware.IsHTMX(c) { |
| 151 | + return middleware.Render(c, http.StatusOK, InviteListFragment(cc, invites)) |
| 152 | + } |
| 153 | + return middleware.Render(c, http.StatusOK, InviteListFragment(cc, invites)) |
| 154 | +} |
0 commit comments