Head of Product & Engineering 5312eb0ca2
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat: per-location sender ID with admin API
Allows each GHL sub-account to use a different Cast sender ID instead of
the global CAST_SENDER_ID default.

- store.TokenRecord gains a sender_id field (MongoDB)
- store.UpdateSenderID method to set it per location
- cast.Client.SendSMS accepts a senderID override param (empty = use
  client-level default)
- webhook.processOutbound reads the location's sender_id from the token
  record and passes it to Cast
- new admin handler: PUT /api/admin/locations/{locationId}/sender-id
  protected by Authorization: Bearer <INBOUND_API_KEY>

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-04-06 12:47:00 +02:00

112 lines
3.2 KiB
Go

package store
import (
"context"
"errors"
"time"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
type TokenRecord struct {
LocationID string `bson:"location_id"`
CompanyID string `bson:"company_id"`
AccessToken string `bson:"access_token"`
RefreshToken string `bson:"refresh_token"`
ExpiresAt time.Time `bson:"expires_at"`
InstalledAt time.Time `bson:"installed_at"`
UpdatedAt time.Time `bson:"updated_at"`
SenderID string `bson:"sender_id,omitempty"` // per-location Cast sender ID; overrides global default
}
type Store struct {
client *mongo.Client
collection *mongo.Collection
}
func NewStore(ctx context.Context, uri string) (*Store, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
client, err := mongo.Connect(options.Client().ApplyURI(uri))
if err != nil {
return nil, err
}
if err := client.Ping(ctx, nil); err != nil {
return nil, err
}
col := client.Database("cast-ghl").Collection("oauth_tokens")
indexModel := mongo.IndexModel{
Keys: bson.D{{Key: "location_id", Value: 1}},
Options: options.Index().SetUnique(true),
}
if _, err := col.Indexes().CreateOne(ctx, indexModel); err != nil {
return nil, err
}
return &Store{client: client, collection: col}, nil
}
func (s *Store) SaveToken(ctx context.Context, record *TokenRecord) error {
record.UpdatedAt = time.Now()
filter := bson.D{{Key: "location_id", Value: record.LocationID}}
opts := options.Replace().SetUpsert(true)
_, err := s.collection.ReplaceOne(ctx, filter, record, opts)
return err
}
func (s *Store) GetToken(ctx context.Context, locationID string) (*TokenRecord, error) {
filter := bson.D{{Key: "location_id", Value: locationID}}
var record TokenRecord
err := s.collection.FindOne(ctx, filter).Decode(&record)
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, nil
}
if err != nil {
return nil, err
}
return &record, nil
}
func (s *Store) UpdateToken(ctx context.Context, locationID, accessToken, refreshToken string, expiresAt time.Time) error {
filter := bson.D{{Key: "location_id", Value: locationID}}
update := bson.D{{Key: "$set", Value: bson.D{
{Key: "access_token", Value: accessToken},
{Key: "refresh_token", Value: refreshToken},
{Key: "expires_at", Value: expiresAt},
{Key: "updated_at", Value: time.Now()},
}}}
_, err := s.collection.UpdateOne(ctx, filter, update)
return err
}
func (s *Store) UpdateSenderID(ctx context.Context, locationID, senderID string) error {
filter := bson.D{{Key: "location_id", Value: locationID}}
update := bson.D{{Key: "$set", Value: bson.D{
{Key: "sender_id", Value: senderID},
{Key: "updated_at", Value: time.Now()},
}}}
res, err := s.collection.UpdateOne(ctx, filter, update)
if err != nil {
return err
}
if res.MatchedCount == 0 {
return errors.New("location not found")
}
return nil
}
func (s *Store) DeleteToken(ctx context.Context, locationID string) error {
filter := bson.D{{Key: "location_id", Value: locationID}}
_, err := s.collection.DeleteOne(ctx, filter)
return err
}
func (s *Store) Close(ctx context.Context) error {
return s.client.Disconnect(ctx)
}