2025-05-08 07:42:53 +02:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2025-07-14 21:27:14 +02:00
|
|
|
"errors"
|
2025-05-08 07:42:53 +02:00
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
|
|
|
2025-05-08 07:42:53 +02:00
|
|
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// repository
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
var _ domain.OrganizationRepository = (*org)(nil)
|
|
|
|
|
2025-05-08 07:42:53 +02:00
|
|
|
type org struct {
|
2025-07-16 09:26:46 +02:00
|
|
|
shouldJoinDomains bool
|
2025-05-08 07:42:53 +02:00
|
|
|
repository
|
2025-07-16 09:26:46 +02:00
|
|
|
domainRepo domain.OrganizationDomainRepository
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
func OrganizationRepository(client database.QueryExecutor) domain.OrganizationRepository {
|
2025-05-08 07:42:53 +02:00
|
|
|
return &org{
|
|
|
|
repository: repository{
|
|
|
|
client: client,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-15 20:20:53 +02:00
|
|
|
const queryOrganizationStmt = `SELECT id, name, instance_id, state, created_at, updated_at` +
|
2025-07-14 21:27:14 +02:00
|
|
|
` FROM zitadel.organizations`
|
2025-05-08 07:42:53 +02:00
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// Get implements [domain.OrganizationRepository].
|
|
|
|
func (o *org) Get(ctx context.Context, id domain.OrgIdentifierCondition, instanceID string, conditions ...database.Condition) (*domain.Organization, error) {
|
|
|
|
builder := database.StatementBuilder{}
|
|
|
|
|
|
|
|
builder.WriteString(queryOrganizationStmt)
|
|
|
|
|
|
|
|
instanceIDCondition := o.InstanceIDCondition(instanceID)
|
|
|
|
|
2025-07-15 20:20:53 +02:00
|
|
|
conditions = append(conditions, id, instanceIDCondition)
|
2025-07-14 21:27:14 +02:00
|
|
|
writeCondition(&builder, database.And(conditions...))
|
|
|
|
|
|
|
|
return scanOrganization(ctx, o.client, &builder)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// List implements [domain.OrganizationRepository].
|
2025-07-15 20:20:53 +02:00
|
|
|
func (o *org) List(ctx context.Context, conditions ...database.Condition) ([]*domain.Organization, error) {
|
2025-07-14 21:27:14 +02:00
|
|
|
builder := database.StatementBuilder{}
|
|
|
|
|
|
|
|
builder.WriteString(queryOrganizationStmt)
|
|
|
|
|
2025-07-15 20:20:53 +02:00
|
|
|
if conditions != nil {
|
|
|
|
writeCondition(&builder, database.And(conditions...))
|
|
|
|
}
|
2025-07-14 21:27:14 +02:00
|
|
|
|
|
|
|
orderBy := database.OrderBy(o.CreatedAtColumn())
|
|
|
|
orderBy.Write(&builder)
|
|
|
|
|
|
|
|
return scanOrganizations(ctx, o.client, &builder)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
const createOrganizationStmt = `INSERT INTO zitadel.organizations (id, name, instance_id, state)` +
|
|
|
|
` VALUES ($1, $2, $3, $4)` +
|
|
|
|
` RETURNING created_at, updated_at`
|
|
|
|
|
|
|
|
// Create implements [domain.OrganizationRepository].
|
|
|
|
func (o *org) Create(ctx context.Context, organization *domain.Organization) error {
|
|
|
|
builder := database.StatementBuilder{}
|
|
|
|
builder.AppendArgs(organization.ID, organization.Name, organization.InstanceID, organization.State)
|
|
|
|
builder.WriteString(createOrganizationStmt)
|
|
|
|
|
|
|
|
err := o.client.QueryRow(ctx, builder.String(), builder.Args()...).Scan(&organization.CreatedAt, &organization.UpdatedAt)
|
|
|
|
if err != nil {
|
|
|
|
return checkCreateOrgErr(err)
|
|
|
|
}
|
|
|
|
return nil
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
func checkCreateOrgErr(err error) error {
|
|
|
|
var pgErr *pgconn.PgError
|
|
|
|
if !errors.As(err, &pgErr) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// constraint violation
|
|
|
|
if pgErr.Code == "23514" {
|
|
|
|
if pgErr.ConstraintName == "organizations_name_check" {
|
|
|
|
return errors.New("organization name not provided")
|
|
|
|
}
|
|
|
|
if pgErr.ConstraintName == "organizations_id_check" {
|
|
|
|
return errors.New("organization id not provided")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// duplicate
|
|
|
|
if pgErr.Code == "23505" {
|
|
|
|
if pgErr.ConstraintName == "organizations_pkey" {
|
|
|
|
return errors.New("organization id already exists")
|
|
|
|
}
|
|
|
|
if pgErr.ConstraintName == "org_unique_instance_id_name_idx" {
|
|
|
|
return errors.New("organization name already exists for instance")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// invalid instance id
|
|
|
|
if pgErr.Code == "23503" {
|
|
|
|
if pgErr.ConstraintName == "organizations_instance_id_fkey" {
|
|
|
|
return errors.New("invalid instance id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// Update implements [domain.OrganizationRepository].
|
|
|
|
func (o org) Update(ctx context.Context, id domain.OrgIdentifierCondition, instanceID string, changes ...database.Change) (int64, error) {
|
|
|
|
if changes == nil {
|
|
|
|
return 0, errors.New("Update must contain a condition") // (otherwise ALL organizations will be updated)
|
|
|
|
}
|
|
|
|
builder := database.StatementBuilder{}
|
|
|
|
builder.WriteString(`UPDATE zitadel.organizations SET `)
|
|
|
|
|
|
|
|
instanceIDCondition := o.InstanceIDCondition(instanceID)
|
|
|
|
|
2025-07-15 20:20:53 +02:00
|
|
|
conditions := []database.Condition{id, instanceIDCondition}
|
2025-07-14 21:27:14 +02:00
|
|
|
database.Changes(changes).Write(&builder)
|
|
|
|
writeCondition(&builder, database.And(conditions...))
|
|
|
|
|
|
|
|
stmt := builder.String()
|
|
|
|
|
|
|
|
rowsAffected, err := o.client.Exec(ctx, stmt, builder.Args()...)
|
|
|
|
return rowsAffected, err
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// Delete implements [domain.OrganizationRepository].
|
|
|
|
func (o org) Delete(ctx context.Context, id domain.OrgIdentifierCondition, instanceID string) (int64, error) {
|
|
|
|
builder := database.StatementBuilder{}
|
|
|
|
|
2025-07-15 20:20:53 +02:00
|
|
|
builder.WriteString(`DELETE FROM zitadel.organizations`)
|
2025-07-14 21:27:14 +02:00
|
|
|
|
|
|
|
instanceIDCondition := o.InstanceIDCondition(instanceID)
|
|
|
|
|
2025-07-15 20:20:53 +02:00
|
|
|
conditions := []database.Condition{id, instanceIDCondition}
|
2025-07-14 21:27:14 +02:00
|
|
|
writeCondition(&builder, database.And(conditions...))
|
|
|
|
|
|
|
|
return o.client.Exec(ctx, builder.String(), builder.Args()...)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// changes
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// SetName implements [domain.organizationChanges].
|
|
|
|
func (o org) SetName(name string) database.Change {
|
2025-05-08 07:42:53 +02:00
|
|
|
return database.NewChange(o.NameColumn(), name)
|
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// SetState implements [domain.organizationChanges].
|
|
|
|
func (o org) SetState(state domain.OrgState) database.Change {
|
2025-05-08 07:42:53 +02:00
|
|
|
return database.NewChange(o.StateColumn(), state)
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// conditions
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// IDCondition implements [domain.organizationConditions].
|
|
|
|
func (o org) IDCondition(id string) domain.OrgIdentifierCondition {
|
|
|
|
return database.NewTextCondition(o.IDColumn(), database.TextOperationEqual, id)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// NameCondition implements [domain.organizationConditions].
|
|
|
|
func (o org) NameCondition(name string) domain.OrgIdentifierCondition {
|
|
|
|
return database.NewTextCondition(o.NameColumn(), database.TextOperationEqual, name)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// InstanceIDCondition implements [domain.organizationConditions].
|
|
|
|
func (o org) InstanceIDCondition(instanceID string) database.Condition {
|
|
|
|
return database.NewTextCondition(o.InstanceIDColumn(), database.TextOperationEqual, instanceID)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// StateCondition implements [domain.organizationConditions].
|
|
|
|
func (o org) StateCondition(state domain.OrgState) database.Condition {
|
2025-07-16 09:28:57 +02:00
|
|
|
return database.NewTextCondition(o.StateColumn(), database.TextOperationEqual, state)
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// columns
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// IDColumn implements [domain.organizationColumns].
|
|
|
|
func (org) IDColumn() database.Column {
|
|
|
|
return database.NewColumn("id")
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// NameColumn implements [domain.organizationColumns].
|
|
|
|
func (org) NameColumn() database.Column {
|
|
|
|
return database.NewColumn("name")
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// InstanceIDColumn implements [domain.organizationColumns].
|
|
|
|
func (org) InstanceIDColumn() database.Column {
|
2025-05-08 07:42:53 +02:00
|
|
|
return database.NewColumn("instance_id")
|
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// StateColumn implements [domain.organizationColumns].
|
|
|
|
func (org) StateColumn() database.Column {
|
|
|
|
return database.NewColumn("state")
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// CreatedAtColumn implements [domain.organizationColumns].
|
|
|
|
func (org) CreatedAtColumn() database.Column {
|
|
|
|
return database.NewColumn("created_at")
|
2025-05-08 07:42:53 +02:00
|
|
|
}
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
// UpdatedAtColumn implements [domain.organizationColumns].
|
|
|
|
func (org) UpdatedAtColumn() database.Column {
|
2025-05-08 07:42:53 +02:00
|
|
|
return database.NewColumn("updated_at")
|
|
|
|
}
|
|
|
|
|
2025-07-16 09:26:46 +02:00
|
|
|
// -------------------------------------------------------------
|
|
|
|
// scanners
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
2025-07-14 21:27:14 +02:00
|
|
|
func scanOrganization(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) (*domain.Organization, error) {
|
|
|
|
rows, err := querier.Query(ctx, builder.String(), builder.Args()...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
organization := &domain.Organization{}
|
|
|
|
if err := rows.(database.CollectableRows).CollectExactlyOneRow(organization); err != nil {
|
|
|
|
if err.Error() == "no rows in result set" {
|
|
|
|
return nil, ErrResourceDoesNotExist
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return organization, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func scanOrganizations(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) ([]*domain.Organization, error) {
|
|
|
|
rows, err := querier.Query(ctx, builder.String(), builder.Args()...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
organizations := []*domain.Organization{}
|
|
|
|
if err := rows.(database.CollectableRows).Collect(&organizations); err != nil {
|
|
|
|
// if no results returned, this is not a error
|
|
|
|
// it just means the organization was not found
|
|
|
|
// the caller should check if the returned organization is nil
|
|
|
|
if err.Error() == "no rows in result set" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return organizations, nil
|
|
|
|
}
|
2025-07-16 09:26:46 +02:00
|
|
|
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
// sub repositories
|
|
|
|
// -------------------------------------------------------------
|
|
|
|
|
|
|
|
func (o *org) Domains() domain.OrganizationDomainRepository {
|
|
|
|
o.shouldJoinDomains = true
|
|
|
|
|
|
|
|
if o.domainRepo != nil {
|
|
|
|
return o.domainRepo
|
|
|
|
}
|
|
|
|
|
|
|
|
o.domainRepo = &orgDomain{
|
|
|
|
repository: o.repository,
|
|
|
|
org: o,
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.domainRepo
|
|
|
|
}
|