Skip to content

Commit

Permalink
list tenant users
Browse files Browse the repository at this point in the history
  • Loading branch information
briskt committed Nov 19, 2023
1 parent e0a4a51 commit f190439
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
1 change: 1 addition & 0 deletions app/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type TenantService interface {
type Tenant struct {
ID string
Name string
UserIDs []string
CreatedAt time.Time
UpdatedAt time.Time
}
Expand Down
18 changes: 16 additions & 2 deletions assets/pages/admin/tenants/[id].svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
export let id: string
let newTenantName = ''
let tenant = {} as Tenant
let showAddTenantUserModal = false
Expand All @@ -33,7 +32,22 @@
<dd>{localeTime(tenant.UpdatedAt)}</dd>
</dl>


<h2>Tenant Users</h2>

{#if tenant.UserIDs && tenant.UserIDs.length}
<table>
<tr>
<th>ID</th>
</tr>
{#each tenant.UserIDs as id (id)}
<tr>
<td>{id}</td>
</tr>
{/each}
</table>
{:else}
<em>No users</em>
{/if}

<style>
dd {
Expand Down
15 changes: 12 additions & 3 deletions db/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,20 @@ func findTenantByID(ctx echo.Context, id string) (Tenant, error) {
return tenant, result.Error
}

func convertTenant(_ echo.Context, t Tenant) (app.Tenant, error) {
return app.Tenant{
func convertTenant(c echo.Context, t Tenant) (app.Tenant, error) {
tenant := app.Tenant{
ID: t.ID,
Name: t.Name,
CreatedAt: t.CreatedAt,
UpdatedAt: t.UpdatedAt,
}, nil
}
users, n, err := findUsers(c, app.UserFilter{TenantID: &t.ID})
if err != nil {
return app.Tenant{}, err
}
tenant.UserIDs = make([]string, n)
for i, user := range users {
tenant.UserIDs[i] = user.ID
}
return tenant, nil
}
17 changes: 4 additions & 13 deletions migrations/20230617074340_add_tenants_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,13 @@ CREATE TABLE "tenants" (
PRIMARY KEY(id),
UNIQUE (name, deleted)
);
CREATE TABLE "tenant_users" (
id text NOT NULL,
tenant_id varchar(255) NOT NULL,
user_id varchar(255) NOT NULL,
created_at timestamp NOT NULL,
updated_at timestamp NOT NULL,
deleted timestamp,
PRIMARY KEY(id),
CONSTRAINT FK_tenant_users_tenants FOREIGN KEY (tenant_id) REFERENCES tenants(id),
CONSTRAINT FK_tenant_users_user FOREIGN KEY (user_id) REFERENCES users(id),
UNIQUE (tenant_id, user_id, deleted)
);
ALTER TABLE "users" ADD "tenant_id" text NULL;
ALTER TABLE "users" ADD FOREIGN KEY ("tenant_id") REFERENCES "tenants" ("id") ON DELETE SET NULL ON UPDATE SET NULL;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE "tenant_users";
ALTER TABLE "users" DROP CONSTRAINT "users_tenant_id_fkey"
ALTER TABLE "users" DROP "tenant_id";
DROP TABLE "tenants";
-- +goose StatementEnd

0 comments on commit f190439

Please sign in to comment.