| 1 | -- Workspace invitations are answered: nobody joins a workspace without |
| 2 | -- saying yes. An invite that names a workspace (invites.workspace_id) is a |
| 3 | -- workspace invitation for one account, which accepts or declines it |
| 4 | -- (src/invites.rs, `accept_invitation` and `decline_invitation`). An |
| 5 | -- account invite that names one makes the account first; the invitation |
| 6 | -- waits for its answer once the account confirms its address. |
| 7 | |
| 8 | -- The account it is for: the one invited by username or address, or the |
| 9 | -- one the invite made. Null until there is one. |
| 10 | ALTER TABLE invites ADD COLUMN invitee_id TEXT; |
| 11 | -- The role accepting joins with: member or owner. Null is member. |
| 12 | ALTER TABLE invites ADD COLUMN role TEXT; |
| 13 | -- The answer, RFC 3339 UTC. At most one is set. |
| 14 | ALTER TABLE invites ADD COLUMN accepted_at TEXT; |
| 15 | ALTER TABLE invites ADD COLUMN declined_at TEXT; |
| 16 | |
| 17 | -- Every invite used before now is for the account that used it, and one |
| 18 | -- that named a workspace and was applied joined it then: accepted. |
| 19 | UPDATE invites SET invitee_id = redeemed_by WHERE invitee_id IS NULL AND redeemed_by IS NOT NULL; |
| 20 | UPDATE invites SET accepted_at = applied_at |
| 21 | WHERE accepted_at IS NULL AND workspace_id IS NOT NULL AND applied_at IS NOT NULL; |
| 22 | -- A pending invitation already sent to an existing account's confirmed |
| 23 | -- address is for that account, so it shows among its invitations. |
| 24 | UPDATE invites SET invitee_id = ( |
| 25 | SELECT e.user_id FROM user_emails e WHERE e.email = invites.email AND e.verified_at IS NOT NULL LIMIT 1) |
| 26 | WHERE invitee_id IS NULL AND kind = 'workspace' AND email IS NOT NULL AND redeemed_at IS NULL AND revoked_at IS NULL; |
| 27 | |
| 28 | CREATE INDEX IF NOT EXISTS invites_invitee ON invites (invitee_id) |
| 29 | WHERE workspace_id IS NOT NULL AND accepted_at IS NULL AND declined_at IS NULL AND revoked_at IS NULL; |