Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

6.0 KB
Raw Blame
// Copyright (c) 2026 Jonas Kaninda.
//
// This file is part of Posta.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package repositories

import (
	"github.com/goposta/posta/internal/models"
	"gorm.io/gorm"
)

type PlanRepository struct {
	db *gorm.DB
}

func NewPlanRepository(db *gorm.DB) *PlanRepository {
	return &PlanRepository{db: db}
}

// Count counts the configured plans, active or not.
func (r *PlanRepository) Count() (int64, error) {
	var n int64
	err := r.db.Model(&models.Plan{}).Count(&n).Error
	return n, err
}

func (r *PlanRepository) Create(plan *models.Plan) error {
	return r.db.Create(plan).Error
}

func (r *PlanRepository) Update(plan *models.Plan) error {
	return r.db.Save(plan).Error
}

func (r *PlanRepository) Delete(id uint) error {
	return r.db.Delete(&models.Plan{}, id).Error
}

func (r *PlanRepository) FindByID(id uint) (*models.Plan, error) {
	var plan models.Plan
	if err := r.db.First(&plan, id).Error; err != nil {
		return nil, err
	}
	return &plan, nil
}

func (r *PlanRepository) FindAll(search string, limit, offset int) ([]models.Plan, int64, error) {
	var plans []models.Plan
	var total int64

	countQ := r.db.Model(&models.Plan{})
	findQ := r.db.Model(&models.Plan{})
	if search != "" {
		like := "%" + search + "%"
		countQ = countQ.Where("name ILIKE ? OR description ILIKE ?", like, like)
		findQ = findQ.Where("name ILIKE ? OR description ILIKE ?", like, like)
	}

	countQ.Count(&total)

	if err := findQ.Order("created_at DESC").
		Limit(limit).Offset(offset).
		Find(&plans).Error; err != nil {
		return nil, 0, err
	}
	return plans, total, nil
}

// FindDefault returns the active default plan, or gorm.ErrRecordNotFound if none exists.
func (r *PlanRepository) FindDefault() (*models.Plan, error) {
	var plan models.Plan
	if err := r.db.Where("is_default = ? AND is_active = ?", true, true).First(&plan).Error; err != nil {
		return nil, err
	}
	return &plan, nil
}

// FindByWorkspaceID returns the plan assigned to the given workspace.
func (r *PlanRepository) FindByWorkspaceID(workspaceID uint) (*models.Plan, error) {
	var ws models.Workspace
	if err := r.db.First(&ws, workspaceID).Error; err != nil {
		return nil, err
	}
	if ws.PlanID == nil {
		return nil, gorm.ErrRecordNotFound
	}
	var plan models.Plan
	if err := r.db.First(&plan, *ws.PlanID).Error; err != nil {
		return nil, err
	}
	return &plan, nil
}

// ClearDefault unsets is_default on all plans. Should be called within a transaction
// before setting a new default.
func (r *PlanRepository) ClearDefault() error {
	return r.db.Model(&models.Plan{}).Where("is_default = ?", true).Update("is_default", false).Error
}

// ClearDefaultTx unsets is_default on all plans within the given transaction.
func (r *PlanRepository) ClearDefaultTx(tx *gorm.DB) error {
	return tx.Model(&models.Plan{}).Where("is_default = ?", true).Update("is_default", false).Error
}

// CountWorkspaces returns the number of workspaces assigned to the given plan.
func (r *PlanRepository) CountWorkspaces(planID uint) (int64, error) {
	var count int64
	if err := r.db.Model(&models.Workspace{}).Where("plan_id = ?", planID).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

// AssignToWorkspace sets the plan_id on the given workspace.
func (r *PlanRepository) AssignToWorkspace(workspaceID, planID uint) error {
	return r.db.Model(&models.Workspace{}).Where("id = ?", workspaceID).Update("plan_id", planID).Error
}

// UnassignFromWorkspace removes the plan assignment from the given workspace.
func (r *PlanRepository) UnassignFromWorkspace(workspaceID uint) error {
	return r.db.Model(&models.Workspace{}).Where("id = ?", workspaceID).Update("plan_id", nil).Error
}

// UnassignAllFromPlan removes plan assignment from all workspaces using the given plan.
func (r *PlanRepository) UnassignAllFromPlan(planID uint) error {
	return r.db.Model(&models.Workspace{}).Where("plan_id = ?", planID).Update("plan_id", nil).Error
}

// FindByUserID returns the plan assigned to the given user.
func (r *PlanRepository) FindByUserID(userID uint) (*models.Plan, error) {
	var user models.User
	if err := r.db.First(&user, userID).Error; err != nil {
		return nil, err
	}
	if user.PlanID == nil {
		return nil, gorm.ErrRecordNotFound
	}
	var plan models.Plan
	if err := r.db.First(&plan, *user.PlanID).Error; err != nil {
		return nil, err
	}
	return &plan, nil
}

// AssignToUser sets the plan_id on the given user.
func (r *PlanRepository) AssignToUser(userID, planID uint) error {
	return r.db.Model(&models.User{}).Where("id = ?", userID).Update("plan_id", planID).Error
}

// UnassignFromUser removes the plan assignment from the given user.
func (r *PlanRepository) UnassignFromUser(userID uint) error {
	return r.db.Model(&models.User{}).Where("id = ?", userID).Update("plan_id", nil).Error
}

// CountUsers returns the number of users assigned to the given plan.
func (r *PlanRepository) CountUsers(planID uint) (int64, error) {
	var count int64
	if err := r.db.Model(&models.User{}).Where("plan_id = ?", planID).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

// UnassignAllUsersFromPlan removes plan assignment from all users using the given plan.
func (r *PlanRepository) UnassignAllUsersFromPlan(planID uint) error {
	return r.db.Model(&models.User{}).Where("plan_id = ?", planID).Update("plan_id", nil).Error
}

// DB returns the underlying database connection for use in transactions.
func (r *PlanRepository) DB() *gorm.DB {
	return r.db
}