Enforce only one domain per organization

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2025-10-02 00:18:50 +02:00
parent 064aa87ea5
commit 7d7acbcf14
3 changed files with 80 additions and 123 deletions

View File

@@ -36,6 +36,7 @@ type (
WebsiteURL *string `db:"website_url"`
Email *string `db:"email"`
HeadquarterAddress *string `db:"headquarter_address"`
CustomDomainID *gid.GID `db:"custom_domain_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
@@ -72,6 +73,7 @@ SELECT
website_url,
email,
headquarter_address,
custom_domain_id,
created_at,
updated_at
FROM
@@ -127,6 +129,7 @@ SELECT
website_url,
email,
headquarter_address,
custom_domain_id,
created_at,
updated_at
FROM
@@ -171,9 +174,10 @@ INSERT INTO organizations (
website_url,
email,
headquarter_address,
custom_domain_id,
created_at,
updated_at
) VALUES (@tenant_id, @id, @name, @logo_object_key, @description, @website_url, @email, @headquarter_address, @created_at, @updated_at)
) VALUES (@tenant_id, @id, @name, @logo_object_key, @description, @website_url, @email, @headquarter_address, @custom_domain_id, @created_at, @updated_at)
`
args := pgx.StrictNamedArgs{
@@ -185,6 +189,7 @@ INSERT INTO organizations (
"website_url": o.WebsiteURL,
"email": o.Email,
"headquarter_address": o.HeadquarterAddress,
"custom_domain_id": o.CustomDomainID,
"created_at": o.CreatedAt,
"updated_at": o.UpdatedAt,
}
@@ -211,6 +216,7 @@ SET
website_url = @website_url,
email = @email,
headquarter_address = @headquarter_address,
custom_domain_id = @custom_domain_id,
updated_at = @updated_at
WHERE
%s
@@ -227,6 +233,7 @@ WHERE
"website_url": o.WebsiteURL,
"email": o.Email,
"headquarter_address": o.HeadquarterAddress,
"custom_domain_id": o.CustomDomainID,
"updated_at": o.UpdatedAt,
}
@@ -264,3 +271,50 @@ WHERE
return nil
}
func (o *Organization) LoadByCustomDomainID(
ctx context.Context,
conn pg.Conn,
scope Scoper,
customDomainID gid.GID,
) error {
q := `
SELECT
tenant_id,
id,
name,
logo_object_key,
description,
website_url,
email,
headquarter_address,
custom_domain_id,
created_at,
updated_at
FROM
organizations
WHERE
%s
custom_domain_id = @custom_domain_id
LIMIT 1
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"custom_domain_id": customDomainID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query organization by custom domain: %w", err)
}
organization, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Organization])
if err != nil {
return fmt.Errorf("cannot collect organization: %w", err)
}
*o = organization
return nil
}