Skip to content

Multitenancy #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 47 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
5306bb2
add: tenant id to schemas
Mar 26, 2025
d09e74d
update: user and user_repository to include tenant_id
Mar 26, 2025
9a364bf
update: core mappers
Mar 26, 2025
a878234
update: models to include tenant_id
Mar 26, 2025
ac23e2f
add: tenant composable and middleware
Mar 26, 2025
118ea84
add: tenant service, repository and update auth to get tenant entity
Mar 26, 2025
be0064f
Merge branch 'staging' into sdk-213
Mar 27, 2025
231cee2
Merge branch 'staging' into sdk-213
Mar 27, 2025
bcce1b1
add: tenant id to permission, role and tab repositories
Mar 28, 2025
063ae66
add: tenant id to authlog and passport repo
Mar 28, 2025
046d6bc
add: tenant id to session, upload and unit repos
Mar 28, 2025
5056e79
add: core repos tenant_id
Mar 29, 2025
7346cd7
add: bichat module tenant_id
Mar 29, 2025
1f1013f
add: crm module tenant_id
Mar 29, 2025
5c3b817
add: finance module tenant_id
Mar 29, 2025
777fa26
add: hrm module tenant_id
Mar 29, 2025
8513f0e
add: warehouse module tenant_id
Mar 29, 2025
e5f6f79
add: missing tenant_ids in Go code
Mar 29, 2025
e697173
Update pkg/middleware/auth.go
thelissimus-work Mar 29, 2025
499207e
fix: missing log import
Mar 29, 2025
b7b99b2
add: core missing tenant_id in Go code
Mar 30, 2025
4d25587
fix: NewWithID call sites
Mar 30, 2025
dcff0fc
fix: seed
Mar 30, 2025
0423802
format: warehouse-schema.sql
Mar 30, 2025
2c50151
format: logging-schema
Mar 30, 2025
2ec103c
format: hrm-schema
Mar 30, 2025
a572aa9
format: core-schema
Mar 30, 2025
815c573
format: crm-schema
Mar 30, 2025
a5c5591
format: hrm-schema
Mar 30, 2025
7f49594
format: schemas
Mar 30, 2025
171d741
fix: tests
Mar 31, 2025
35b3d25
fix: login error display
Mar 31, 2025
66d0e6d
fix: login to work across tenants
Mar 31, 2025
ca47337
fix: auth tenant context
Mar 31, 2025
6a29584
fix: auth conflict
Mar 31, 2025
a73ecbb
fix(user update): fix e2e tests
bektosh-iota Apr 2, 2025
460e76c
remove package-lock.json
bektosh-iota Apr 2, 2025
bbc5a7d
refactor(tenant): change id from uint to uuid
bektosh-iota Apr 4, 2025
9327ff7
Merge branch 'staging' into sdk-213
bektosh-iota Apr 6, 2025
6ed8920
fix migration typestamp
bektosh-iota Apr 6, 2025
04a235a
fix migrate collect
bektosh-iota Apr 6, 2025
5ed5b6b
fix sql format for the migration
bektosh-iota Apr 6, 2025
495c828
go format
bektosh-iota Apr 6, 2025
2ddac2d
Merge branch 'staging' into sdk-213
bektosh-iota Apr 7, 2025
6260d99
run make migrate collect
bektosh-iota Apr 7, 2025
6e1f790
migration fix after merge conflict
bektosh-iota Apr 7, 2025
6aba825
Merge branch 'staging' into sdk-213
bektosh-iota Apr 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions cmd/seed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"runtime/debug"
"time"

"github.com/google/uuid"
"github.com/iota-uz/iota-sdk/modules"
"github.com/iota-uz/iota-sdk/modules/core/domain/aggregates/user"
"github.com/iota-uz/iota-sdk/modules/core/domain/value_objects/internet"
coreseed "github.com/iota-uz/iota-sdk/modules/core/seed"
"github.com/iota-uz/iota-sdk/pkg/application"
"github.com/iota-uz/iota-sdk/pkg/composables"
"github.com/iota-uz/iota-sdk/pkg/configuration"
"github.com/iota-uz/iota-sdk/pkg/constants"
"github.com/iota-uz/iota-sdk/pkg/eventbus"

"github.com/jackc/pgx/v5/pgxpool"
Expand Down Expand Up @@ -67,14 +69,28 @@ func main() {
panicWithStack(err)
}
seeder.Register(
coreseed.CreateDefaultTenant,
coreseed.CreateCurrencies,
coreseed.CreatePermissions,
coreseed.UserSeedFunc(usr),
)
if err := seeder.Seed(composables.WithTx(ctx, tx), app); err != nil {

// Add default tenant to context
defaultTenant := &composables.Tenant{
ID: uuid.MustParse("00000000-0000-0000-0000-000000000001"),
Name: "Default",
Domain: "default.localhost",
}
ctxWithTenant := context.WithValue(
composables.WithTx(ctx, tx),
constants.TenantKey,
defaultTenant,
)

if err := seeder.Seed(ctxWithTenant, app); err != nil {
panicWithStack(err)
}
if err := tx.Commit(ctx); err != nil {
if err := tx.Commit(ctxWithTenant); err != nil {
panicWithStack(err)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ require (
cloud.google.com/go/auth v0.11.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.6 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect
github.com/PuerkitoBio/goquery v1.10.1 // indirect
github.com/agnivade/levenshtein v1.2.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ github.com/Oudwins/tailwind-merge-go v0.2.1 h1:jxRaEqGtwwwF48UuFIQ8g8XT7YSualNuG
github.com/Oudwins/tailwind-merge-go v0.2.1/go.mod h1:kkZodgOPvZQ8f7SIrlWkG/w1g9JTbtnptnePIh3V72U=
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.31 h1:SIkzqC6Nv+znY4NGbWlJceWdns8QVmf9cwAYXd7Cg8k=
github.com/PaulSonOfLars/gotgbot/v2 v2.0.0-rc.31/go.mod h1:kL1v4iIjlalwm3gCYGvF4NLa3hs+aKEfRkNJvj4aoDU=
github.com/PuerkitoBio/goquery v1.9.3 h1:mpJr/ikUA9/GNJB/DBZcGeFDXUtosHRyRrwh7KGdTG0=
github.com/PuerkitoBio/goquery v1.9.3/go.mod h1:1ndLHPdTz+DyQPICCWYlYQMPl0oXZj0G6D4LCYA6u4U=
github.com/PuerkitoBio/goquery v1.10.1 h1:Y8JGYUkXWTGRB6Ars3+j3kN0xg1YqqlwvdTV8WTFQcU=
github.com/PuerkitoBio/goquery v1.10.1/go.mod h1:IYiHrOMps66ag56LEH7QYDDupKXyo5A8qrjIx3ZtujY=
github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398/go.mod h1:a1uqRtAwp2Xwc6WNPJEufxJ7fx3npB4UV/JOLmbu5I0=
github.com/a-h/templ v0.3.857 h1:6EqcJuGZW4OL+2iZ3MD+NnIcG7nGkaQeF2Zq5kf9ZGg=
github.com/a-h/templ v0.3.857/go.mod h1:qhrhAkRFubE7khxLZHsBFHfX+gWwVNKbzKeF9GlPV4M=
Expand All @@ -34,8 +34,8 @@ github.com/agnivade/levenshtein v1.2.1/go.mod h1:QVVI16kDrtSuwcpd0p1+xMC6Z/VfhtC
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
Expand Down
37 changes: 22 additions & 15 deletions migrations/changes-1740741698.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
CREATE TABLE uploads (
id SERIAL8 PRIMARY KEY,
name VARCHAR(255) NOT NULL,
hash VARCHAR(255) NOT NULL UNIQUE,
hash VARCHAR(255) NOT NULL,
path VARCHAR(1024) DEFAULT '' NOT NULL,
size INT8 DEFAULT 0 NOT NULL,
mimetype VARCHAR(255) NOT NULL,
type VARCHAR(255) NOT NULL,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
updated_at TIMESTAMPTZ DEFAULT now(),
CONSTRAINT uploads_hash_key UNIQUE (hash)
);

-- Change CREATE_TABLE: clients
Expand Down Expand Up @@ -61,20 +62,22 @@ CREATE TABLE positions (
-- Change CREATE_TABLE: permissions
CREATE TABLE permissions (
id UUID DEFAULT gen_random_uuid() NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
resource VARCHAR(255) NOT NULL,
action VARCHAR(255) NOT NULL,
modifier VARCHAR(255) NOT NULL,
description TEXT
description TEXT,
CONSTRAINT permissions_name_key UNIQUE (name)
);

-- Change CREATE_TABLE: roles
CREATE TABLE roles (
id SERIAL8 PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
description TEXT,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
updated_at TIMESTAMPTZ DEFAULT now(),
CONSTRAINT roles_name_key UNIQUE (name)
);

-- Change CREATE_TABLE: warehouse_orders
Expand Down Expand Up @@ -122,11 +125,12 @@ CREATE TABLE inventory (
CREATE TABLE warehouse_positions (
id SERIAL8 PRIMARY KEY,
title VARCHAR(255) NOT NULL,
barcode VARCHAR(255) NOT NULL UNIQUE,
barcode VARCHAR(255) NOT NULL,
description TEXT,
unit_id INT8 REFERENCES warehouse_units (id) ON DELETE SET NULL,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
updated_at TIMESTAMPTZ DEFAULT now(),
CONSTRAINT warehouse_positions_barcode_key UNIQUE (barcode)
);

-- Change CREATE_TABLE: employees
Expand All @@ -135,15 +139,16 @@ CREATE TABLE employees (
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
middle_name VARCHAR(255),
email VARCHAR(255) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL,
phone VARCHAR(255),
salary DECIMAL(9,2) NOT NULL,
salary_currency_id VARCHAR(3) REFERENCES currencies (code) ON DELETE SET NULL,
hourly_rate DECIMAL(9,2) NOT NULL,
coefficient FLOAT8 NOT NULL,
avatar_id INT8 REFERENCES uploads (id) ON DELETE SET NULL,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
updated_at TIMESTAMPTZ DEFAULT now(),
CONSTRAINT employees_email_key UNIQUE (email)
);

-- Change CREATE_TABLE: warehouse_position_images
Expand All @@ -159,15 +164,16 @@ CREATE TABLE users (
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
middle_name VARCHAR(255),
email VARCHAR(255) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL,
password VARCHAR(255),
ui_language VARCHAR(3) NOT NULL,
avatar_id INT8 REFERENCES uploads (id) ON DELETE SET NULL,
last_login TIMESTAMP NULL,
last_ip VARCHAR(255) NULL,
last_action TIMESTAMPTZ NULL,
created_at TIMESTAMPTZ DEFAULT now() NOT NULL,
updated_at TIMESTAMPTZ DEFAULT now() NOT NULL
updated_at TIMESTAMPTZ DEFAULT now() NOT NULL,
CONSTRAINT users_email_key UNIQUE (email)
);

-- Change CREATE_TABLE: role_permissions
Expand Down Expand Up @@ -229,10 +235,11 @@ CREATE TABLE employee_meta (
CREATE TABLE warehouse_products (
id SERIAL8 PRIMARY KEY,
position_id INT8 NOT NULL REFERENCES warehouse_positions (id) ON DELETE CASCADE,
rfid VARCHAR(255) NULL UNIQUE,
rfid VARCHAR(255) NULL,
status VARCHAR(255) NOT NULL,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
updated_at TIMESTAMPTZ DEFAULT now(),
CONSTRAINT warehouse_products_rfid UNIQUE (rfid)
);

-- Change CREATE_TABLE: authentication_logs
Expand Down Expand Up @@ -292,7 +299,7 @@ CREATE TABLE tabs (
href VARCHAR(255) NOT NULL,
user_id INT8 NOT NULL REFERENCES users (id) ON DELETE CASCADE,
"position" INT8 DEFAULT 0 NOT NULL,
UNIQUE (href, user_id)
CONSTRAINT tabs_href_user_id_key UNIQUE (href, user_id)
);

-- Change CREATE_TABLE: counterparty_contacts
Expand Down
5 changes: 4 additions & 1 deletion migrations/changes-1740935243.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ ALTER TABLE clients

-- Change ADD_COLUMN: phone
ALTER TABLE users
ADD COLUMN phone VARCHAR(255) UNIQUE;
ADD COLUMN phone VARCHAR(255);

ALTER TABLE users
ADD CONSTRAINT users_phone_key UNIQUE (phone);

-- Change CREATE_TABLE: client_contacts
CREATE TABLE client_contacts (
Expand Down
5 changes: 3 additions & 2 deletions migrations/changes-1741630199.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
-- Change CREATE_TABLE: user_groups
CREATE TABLE user_groups (
id uuid DEFAULT gen_random_uuid () PRIMARY KEY,
name varchar(255) NOT NULL UNIQUE,
name varchar(255) NOT NULL,
description text,
created_at timestamp DEFAULT now(),
updated_at timestamp DEFAULT now()
updated_at timestamp DEFAULT now(),
CONSTRAINT user_groups_name_key UNIQUE (name)
);

-- Change CREATE_TABLE: group_roles
Expand Down
Loading