diff --git a/api/config.go b/api/config.go index a783a3fd..22a974b5 100644 --- a/api/config.go +++ b/api/config.go @@ -10,18 +10,18 @@ import ( func NewRepos(config APIConfig) repository.Repositories { // TODO: Make sure all of the repos are initialized here return repository.Repositories{ - Auth: repository.NewAuth(config.Redis, config.DB), - User: repository.NewUser(config.DB), - Contact: repository.NewContact(config.DB), - Instrument: repository.NewInstrument(config.DB), - Device: repository.NewDevice(config.DB), - UserPlatform: repository.NewUserPlatform(config.DB), - Asset: repository.NewAsset(config.DB), - Network: repository.NewNetwork(config.DB), - Platform: repository.NewPlatform(config.DB), - Transaction: repository.NewTransaction(config.DB), - TxLeg: repository.NewTxLeg(config.DB), - Location: repository.NewLocation(config.DB), + Auth: repository.NewAuth(config.Redis, config.DB), + User: repository.NewUser(config.DB), + Contact: repository.NewContact(config.DB), + Instrument: repository.NewInstrument(config.DB), + Device: repository.NewDevice(config.DB), + UserToPlatform: repository.NewUserToPlatform(config.DB), + Asset: repository.NewAsset(config.DB), + Network: repository.NewNetwork(config.DB), + Platform: repository.NewPlatform(config.DB), + Transaction: repository.NewTransaction(config.DB), + TxLeg: repository.NewTxLeg(config.DB), + Location: repository.NewLocation(config.DB), } } diff --git a/docker-compose.yml b/docker-compose.yml index 49edbe1b..7108b7bc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,8 +8,10 @@ services: ports: - 5555:5555 depends_on: - - db - - redis + redis: + condition: service_started + db: + condition: service_healthy volumes: - ./:/string_api db: @@ -22,6 +24,11 @@ services: - '5432:5432' volumes: - db:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U string_db"] + interval: 5s + timeout: 5s + retries: 5 redis: image: redis:7.0-alpine restart: always diff --git a/entrypoint.sh b/entrypoint.sh index 72b1a1fb..c630ff82 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -6,12 +6,9 @@ export $(grep -v '^#' .env | xargs) # run db migrations echo "----- Running migrations..." cd migrations -cd mocks DB_CONFIG="host=$DB_HOST user=$DB_USERNAME dbname=$DB_NAME sslmode=disable password=$DB_PASSWORD" goose postgres "$DB_CONFIG" reset -cd .. -goose postgres "$DB_CONFIG" reset goose postgres "$DB_CONFIG" up cd .. echo "----- ...Migrations done" diff --git a/migrations/0001_string-user_platform_asset_network.sql b/migrations/0001_string-user_platform_asset_network.sql index 06590b6d..d816e3c3 100644 --- a/migrations/0001_string-user_platform_asset_network.sql +++ b/migrations/0001_string-user_platform_asset_network.sql @@ -10,11 +10,11 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; ------------------------------------------------------------------------- -- +goose StatementBegin CREATE OR REPLACE FUNCTION update_updated_at_column() - RETURNS TRIGGER AS + RETURNS TRIGGER AS $$ BEGIN - NEW.updated_at = now(); - RETURN NEW; + NEW.updated_at = now(); + RETURN NEW; END; $$ language 'plpgsql'; -- +goose StatementEnd @@ -36,9 +36,9 @@ CREATE TABLE string_user ( ); CREATE OR REPLACE TRIGGER update_string_user_updated_at - BEFORE UPDATE - ON string_user - FOR EACH ROW + BEFORE UPDATE + ON string_user + FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); ------------------------------------------------------------------------- @@ -55,9 +55,9 @@ CREATE TABLE platform ( authentication TEXT DEFAULT '' --enum [email, phone, wallet] ); CREATE OR REPLACE TRIGGER update_platform_updated_at - BEFORE UPDATE - ON platform - FOR EACH ROW + BEFORE UPDATE + ON platform + FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); ------------------------------------------------------------------------- @@ -76,9 +76,9 @@ CREATE TABLE network ( explorer_url TEXT DEFAULT '' -- The Block Explorer URL used to view transactions and entities in the browser ); CREATE OR REPLACE TRIGGER update_network_updated_at - BEFORE UPDATE - ON network - FOR EACH ROW + BEFORE UPDATE + ON network + FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); ------------------------------------------------------------------------- @@ -97,9 +97,9 @@ CREATE TABLE asset ( -- We will write sql commands to add/update these in bulk. ); CREATE OR REPLACE TRIGGER update_asset_updated_at - BEFORE UPDATE - ON asset - FOR EACH ROW + BEFORE UPDATE + ON asset + FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); CREATE INDEX network_gas_token_id_fk ON network (gas_token_id); @@ -121,7 +121,7 @@ DROP TABLE IF EXISTS network; ------------------------------------------------------------------------- -- PLATFORM ------------------------------------------------------------- -DROP TRIGGER IF EXISTS update_platform_updated_at ON platfom; +DROP TRIGGER IF EXISTS update_platform_updated_at ON platform; DROP TABLE IF EXISTS platform; ------------------------------------------------------------------------- diff --git a/migrations/0002_user-platform_device_contact_location_instrument.sql b/migrations/0002_user-to-platform_device_contact_location_instrument.sql similarity index 96% rename from migrations/0002_user-platform_device_contact_location_instrument.sql rename to migrations/0002_user-to-platform_device_contact_location_instrument.sql index 8d7e009b..4315b060 100644 --- a/migrations/0002_user-platform_device_contact_location_instrument.sql +++ b/migrations/0002_user-to-platform_device_contact_location_instrument.sql @@ -27,9 +27,9 @@ CREATE TABLE device ( ); CREATE OR REPLACE TRIGGER update_device_updated_at - BEFORE UPDATE - ON device - FOR EACH ROW + BEFORE UPDATE + ON device + FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); CREATE UNIQUE INDEX device_fingerprint_id_idx ON device(fingerprint, user_id); @@ -49,9 +49,9 @@ CREATE TABLE contact ( ); CREATE OR REPLACE TRIGGER update_contact_updated_at - BEFORE UPDATE - ON contact - FOR EACH ROW + BEFORE UPDATE + ON contact + FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); ------------------------------------------------------------------------- @@ -74,9 +74,9 @@ CREATE TABLE location ( ); CREATE OR REPLACE TRIGGER update_location_updated_at - BEFORE UPDATE - ON location - FOR EACH ROW + BEFORE UPDATE + ON location + FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); ------------------------------------------------------------------------- @@ -97,9 +97,9 @@ CREATE TABLE instrument ( ); CREATE OR REPLACE TRIGGER update_instrument_updated_at - BEFORE UPDATE - ON instrument - FOR EACH ROW + BEFORE UPDATE + ON instrument + FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); ------------------------------------------------------------------------- diff --git a/migrations/0003_contact-platform_device-instrument_tx-leg_transaction.sql b/migrations/0003_contact-to-platform_device-to-instrument_tx-leg_transaction.sql similarity index 98% rename from migrations/0003_contact-platform_device-instrument_tx-leg_transaction.sql rename to migrations/0003_contact-to-platform_device-to-instrument_tx-leg_transaction.sql index a981ef0b..99d8a319 100644 --- a/migrations/0003_contact-platform_device-instrument_tx-leg_transaction.sql +++ b/migrations/0003_contact-to-platform_device-to-instrument_tx-leg_transaction.sql @@ -50,9 +50,9 @@ CREATE TABLE tx_leg ( ); CREATE OR REPLACE TRIGGER update_tx_leg_updated_at - BEFORE UPDATE - ON tx_leg - FOR EACH ROW + BEFORE UPDATE + ON tx_leg + FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); ------------------------------------------------------------------------- @@ -84,9 +84,9 @@ CREATE TABLE transaction ( ); CREATE OR REPLACE TRIGGER update_transaction_updated_at - BEFORE UPDATE - ON transaction - FOR EACH ROW + BEFORE UPDATE + ON transaction + FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); ------------------------------------------------------------------------- diff --git a/migrations/0004_auth_key.sql b/migrations/0004_auth_key.sql index 40bfa569..9295d939 100644 --- a/migrations/0004_auth_key.sql +++ b/migrations/0004_auth_key.sql @@ -11,9 +11,9 @@ CREATE TABLE auth_strategy ( ); CREATE OR REPLACE TRIGGER update_auth_strategy_updated_at - BEFORE UPDATE - ON auth_strategy - FOR EACH ROW + BEFORE UPDATE + ON auth_strategy + FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column(); CREATE INDEX auth_strategy_status_idx ON auth_strategy(status); diff --git a/migrations/0005_user-to-platform_contact-to-platform_device-to-instrument_platform.sql b/migrations/0005_user-to-platform_contact-to-platform_device-to-instrument_platform.sql new file mode 100644 index 00000000..0568dda5 --- /dev/null +++ b/migrations/0005_user-to-platform_contact-to-platform_device-to-instrument_platform.sql @@ -0,0 +1,94 @@ +------------------------------------------------------------------------- +-- +goose Up + +------------------------------------------------------------------------- +-- USER_TO_PLATFORM ----------------------------------------------------- +ALTER TABLE user_platform + RENAME TO user_to_platform; + +DROP INDEX IF EXISTS user_platform_user_id_platform_id_idx; + +CREATE UNIQUE INDEX user_to_platform_user_id_platform_id_idx ON user_to_platform(user_id, platform_id); + + +------------------------------------------------------------------------- +-- CONTACT_TO_PLATFORM -------------------------------------------------- +ALTER TABLE contact_platform + RENAME TO contact_to_platform; + +DROP INDEX IF EXISTS contact_platform_contact_id_platform_id_idx; + +CREATE UNIQUE INDEX contact_to_platform_contact_id_platform_id_idx ON contact_to_platform(contact_id, platform_id); + + +------------------------------------------------------------------------- +-- DEVICE_TO_INSTRUMENT ------------------------------------------------- +ALTER TABLE device_instrument + RENAME TO device_to_instrument; + +DROP INDEX IF EXISTS device_instrument_device_id_instrument_id_idx; + +CREATE UNIQUE INDEX device_to_instrument_device_id_instrument_id_idx ON device_to_instrument(device_id, instrument_id); + + +------------------------------------------------------------------------- +-- PLATFORM ------------------------------------------------------------- +ALTER TABLE platform + DROP COLUMN IF EXISTS type, + DROP COLUMN IF EXISTS status, + DROP COLUMN IF EXISTS name, + DROP COLUMN IF EXISTS api_key, + DROP COLUMN IF EXISTS authentication, + ADD COLUMN activated_at TIMESTAMP WITH TIME ZONE DEFAULT NULL, -- for activating prod users + ADD COLUMN name TEXT NOT NULL, + ADD COLUMN description TEXT NOT NULL, + ADD COLUMN domains TEXT[] DEFAULT NULL, -- define which domains can make calls to API (web-to-API) + ADD COLUMN ip_addresses TEXT[] DEFAULT NULL; -- define which API ips can make calls (API-to-API) + + +------------------------------------------------------------------------- +-- +goose Down + +------------------------------------------------------------------------- +-- PLATFORM ------------------------------------------------------------- +ALTER TABLE platform + DROP COLUMN IF EXISTS activated_at, + DROP COLUMN IF EXISTS name, + DROP COLUMN IF EXISTS description, + DROP COLUMN IF EXISTS domains, + DROP COLUMN IF EXISTS ip_addresses, + ADD COLUMN type TEXT DEFAULT '', -- enum: to be defined at struct level in Go + ADD COLUMN status TEXT DEFAULT '', -- enum: to be defined at struct level in Go + ADD COLUMN name TEXT DEFAULT '', + ADD COLUMN api_key TEXT DEFAULT '', + ADD COLUMN authentication TEXT DEFAULT ''; --enum [email, phone, wallet] + + +------------------------------------------------------------------------- +-- USER_PLATFORM ----------------------------------------------------- +ALTER TABLE user_to_platform + RENAME TO user_platform; + +DROP INDEX IF EXISTS user_to_platform_user_id_platform_id_idx; + +CREATE UNIQUE INDEX user_platform_user_id_platform_id_idx ON user_platform(user_id, platform_id); + + +------------------------------------------------------------------------- +-- CONTACT_PLATFORM -------------------------------------------------- +ALTER TABLE contact_to_platform + RENAME TO contact_platform; + +DROP INDEX IF EXISTS contact_to_platform_contact_id_platform_id_idx; + +CREATE UNIQUE INDEX contact_platform_contact_id_platform_id_idx ON contact_platform(contact_id, platform_id); + + +------------------------------------------------------------------------- +-- DEVICE_INSTRUMENT ------------------------------------------------- +ALTER TABLE device_to_instrument + RENAME TO device_instrument; + +DROP INDEX IF EXISTS device_to_instrument_device_id_instrument_id_idx; + +CREATE UNIQUE INDEX device_instrument_device_id_instrument_id_idx ON device_instrument(device_id, instrument_id); \ No newline at end of file diff --git a/migrations/0006_platform-member_member-to-platform_member-role_member-to-role_member-invite_apikey.sql b/migrations/0006_platform-member_member-to-platform_member-role_member-to-role_member-invite_apikey.sql new file mode 100644 index 00000000..43f665d9 --- /dev/null +++ b/migrations/0006_platform-member_member-to-platform_member-role_member-to-role_member-invite_apikey.sql @@ -0,0 +1,100 @@ +------------------------------------------------------------------------- +-- +goose Up + +------------------------------------------------------------------------- +-- PLATFORM_MEMBER ------------------------------------------------------ +CREATE TABLE platform_member ( + id UUID PRIMARY KEY NOT NULL DEFAULT UUID_GENERATE_V4(), + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + deactivated_at TIMESTAMP WITH TIME ZONE DEFAULT NULL, + email TEXT NOT NULL, + password TEXT NOT NULL -- how do we maintain this? +); + +------------------------------------------------------------------------- +-- PLATFORM_MEMBER ------------------------------------------------------ +CREATE TABLE member_to_platform ( + member_id UUID REFERENCES platform_member (id), + platform_id UUID REFERENCES platform (id) +); + +CREATE UNIQUE INDEX member_to_platform_platform_id_member_id_idx ON member_to_platform(platform_id, member_id); + +------------------------------------------------------------------------- +-- MEMBER_ROLE ---------------------------------------------------------- +CREATE TABLE member_role ( + id UUID PRIMARY KEY NOT NULL DEFAULT UUID_GENERATE_V4(), + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + deactivated_at TIMESTAMP WITH TIME ZONE DEFAULT NULL, + name TEXT NOT NULL +); + +------------------------------------------------------------------------- +-- MEMBER_TO_ROLE ------------------------------------------------------- +CREATE TABLE member_to_role ( + member_id UUID REFERENCES platform_member (id), + role_id UUID REFERENCES member_role (id) +); + +CREATE UNIQUE INDEX member_to_role_member_id_role_id_idx ON member_to_role(member_id, role_id); + +------------------------------------------------------------------------- +-- MEMBER_INVITE -------------------------------------------------------- +CREATE TABLE member_invite ( + id UUID PRIMARY KEY NOT NULL DEFAULT UUID_GENERATE_V4(), + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + deactivated_at TIMESTAMP WITH TIME ZONE DEFAULT NULL, + expired_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + accepted_at TIMESTAMP WITH TIME ZONE DEFAULT NULL, + email TEXT NOT NULL, + invited_by UUID REFERENCES platform_member (id), + platform_id UUID REFERENCES platform (id) +); + +------------------------------------------------------------------------- +-- APIKEY --------------------------------------------------------------- +CREATE TABLE apikey ( + id UUID PRIMARY KEY NOT NULL DEFAULT UUID_GENERATE_V4(), + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + deactivated_at TIMESTAMP WITH TIME ZONE DEFAULT NULL, + type TEXT NOT NULL, -- [public,private] for now all public? + data TEXT NOT NULL, -- the key itself + description TEXT NOT NULL, + created_by UUID REFERENCES platform_member (id), + platform_id UUID REFERENCES platform (id) +); + + +------------------------------------------------------------------------- +-- +goose Down + +------------------------------------------------------------------------- +-- APIKEY --------------------------------------------------------------- +DROP TABLE IF EXISTS apikey; + +------------------------------------------------------------------------- +-- MEMBER_INVITE -------------------------------------------------------- +DROP TABLE IF EXISTS member_invite; + +------------------------------------------------------------------------- +-- MEMBER_TO_ROLE ------------------------------------------------------- +DROP TABLE IF EXISTS member_to_role; + +------------------------------------------------------------------------- +-- MEMBER_ROLE ---------------------------------------------------------- +DROP TABLE IF EXISTS member_role; + +------------------------------------------------------------------------- +-- PLATFORM_TO_MEMBER --------------------------------------------------- +DROP TABLE IF EXISTS member_to_platform; + +------------------------------------------------------------------------- +-- PLATFORM_MEMBER ------------------------------------------------------ +DROP TABLE IF EXISTS platform_member; + + + diff --git a/migrations/mocks/0005_mocks.sql b/migrations/mocks/0005_mocks.sql deleted file mode 100644 index f8f199ca..00000000 --- a/migrations/mocks/0005_mocks.sql +++ /dev/null @@ -1,46 +0,0 @@ -------------------------------------------------------------------------- --- +goose Up -------------------------------------------------------------------------- --- STRING_USER ---------------------------------------------------------- -INSERT INTO string_user (id, created_at, updated_at, type, status, tags, first_name, last_name) -VALUES ('0e837b73-55cf-43ff-9b1e-0d8258eec978', '2022-10-19 00:17:01.837572+00', '2022-10-19 00:17:01.837572+00', 'Developer', 'Developing', '{}', 'Deve', 'Loper'); - -------------------------------------------------------------------------- --- DEVICE --------------------------------------------------------------- -INSERT INTO device (id, created_at, updated_at, last_used_at, validated_at, description, user_id) -VALUES ('073f5a88-9223-4554-a7ce-11d358123a21', '2022-10-19 00:23:10.405595+00', '2022-10-19 00:23:10.405595+00', '2022-10-19 00:17:01.837572+00', '2022-10-19 00:17:01.837572+00', 'Developer Laptop', '0e837b73-55cf-43ff-9b1e-0d8258eec978'); - -------------------------------------------------------------------------- --- INSTRUMENT ----------------------------------------------------------- -INSERT INTO instrument (id, created_at, updated_at, type, status, tags, network, public_key, last_4, user_id) -VALUES ('13438963-f5e7-47c4-a790-ebca3e3bf915', '2022-10-19 00:53:36.538289+00', '2022-10-19 00:53:36.538289+00', 'Credit Card', 'Ephemeral', '{}', 'Mastercard', '', '4242', '0e837b73-55cf-43ff-9b1e-0d8258eec978'), -('ab6a2d66-ad4c-43f4-adf9-c0cd3282492c', '2022-10-19 00:55:26.166175+00', '2022-10-19 00:55:26.166175+00', 'Crypto Wallet', 'Ephemeral', '{}', 'Ethereum', '0x44A4b9E2A69d86BA382a511f845CbF2E31286770', '', '0e837b73-55cf-43ff-9b1e-0d8258eec978'); - -------------------------------------------------------------------------- --- NETWORK -------------------------------------------------------------- -INSERT INTO network (id, created_at, updated_at, name, network_id, chain_id, gas_token_id, gas_oracle, rpc_url, explorer_url) -VALUES ('ea34e526-ec6e-4f2b-89b4-acc08db80d63', '2022-10-14 20:18:09.555645+00', '2022-10-14 20:18:09.555645+00', 'Fuji Testnet', '43113', '43113', '19611d0e-a42f-4cee-a35a-b34eb5c08a7f', 'avax', 'https://api.avax-test.network/ext/bc/C/rpc', 'https://testnet.snowtrace.io'), -('b21d6cd6-5d8a-49a6-bac6-e6323316dc01', '2022-10-14 20:41:39.962327+00', '2022-10-14 20:41:39.962327+00', 'Goerli Testnet', '5', '5', '3ef72571-c2e1-4ca3-991c-0df17cef7535', 'eth', 'https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161', 'https://goerli.etherscan.io'), -('6cea71b3-b287-4680-ad9d-e631d0bc84ba', '2022-10-14 20:41:39.962327+00', '2022-10-14 20:41:39.962327+00', 'Polygon Mainnet', '137', '137', 'c06986d8-cc2c-4cdc-9728-16a45698b3e7', 'poly', 'https://rpc-mainnet.matic.quiknode.pro', 'https://polygonscan.com'), -('cd42c066-554c-42ad-994b-48fed371931c', '2022-10-14 20:41:39.962327+00', '2022-10-14 20:41:39.962327+00', 'Avalanche Mainnet', '43114', '43114', '19611d0e-a42f-4cee-a35a-b34eb5c08a7f', 'avax', 'https://api.avax.network/ext/bc/C/rpc', 'https://snowtrace.io'), -('491d46e2-18e0-45ec-8209-faf0ec5d278c', '2022-10-14 20:41:39.962327+00', '2022-10-14 20:41:39.962327+00', 'Mumbai Testnet', '80001', '80001', 'c06986d8-cc2c-4cdc-9728-16a45698b3e7', 'poly', 'https://matic-mumbai.chainstacklabs.com', 'https://mumbai.polygonscan.com/'), -('60a02818-4e7d-4b84-b673-e2376fdbfbf9', '2022-10-14 20:41:39.962327+00', '2022-10-30 01:07:37.237054+00', 'Ethereum Mainnet', '1', '1', '3ef72571-c2e1-4ca3-991c-0df17cef7535', 'eth', 'https://rpc.ankr.com/eth', 'https://etherscan.io/'); - -------------------------------------------------------------------------- --- PLATFORM ------------------------------------------------------------- -INSERT INTO platform (id, created_at, updated_at, type, status, name, api_key, authentication) -VALUES ('54a7e062-4cec-44f3-9d89-99498d0eb6ef', '2022-10-19 00:37:16.965408+00', '2022-10-19 00:37:16.965408+00', 'Game', 'Verified', 'Nintendo', 'developer', 'email'); - -------------------------------------------------------------------------- --- ASSET ------------------------------------------------------------- -INSERT INTO asset (id, created_at, updated_at, name, description, decimals, is_crypto, network_id, value_oracle) -VALUES ('19611d0e-a42f-4cee-a35a-b34eb5c08a7f', '2022-10-14 20:17:06.460812+00', '2022-10-15 02:41:02.270712+00', 'AVAX', 'Avalanche', 18, TRUE, 'cd42c066-554c-42ad-994b-48fed371931c', 'avalanche-2'), -('c06986d8-cc2c-4cdc-9728-16a45698b3e7', '2022-10-14 20:17:06.460812+00', '2022-10-15 02:41:02.270712+00', 'MATIC', 'Matic', 18, TRUE, '6cea71b3-b287-4680-ad9d-e631d0bc84ba', 'matic-network'), -('3ef72571-c2e1-4ca3-991c-0df17cef7535', '2022-10-14 20:17:06.460812+00', '2022-10-15 02:41:02.270712+00', 'ETH', 'Ethereum', 18, TRUE, '60a02818-4e7d-4b84-b673-e2376fdbfbf9', 'ethereum'), -('bc376c3a-6481-49d0-83ef-34ba80937ba8', '2022-10-18 03:59:05.042924+00', '2022-10-18 03:59:05.042924+00', 'USD', 'United States Dollar', 6, FALSE, null, null); - - -------------------------------------------------------------------------- --- +goose Down - --- Can't delete rows due to foreign key constraint \ No newline at end of file diff --git a/pkg/internal/unit21/entity.go b/pkg/internal/unit21/entity.go index 958ba091..3437e593 100644 --- a/pkg/internal/unit21/entity.go +++ b/pkg/internal/unit21/entity.go @@ -17,9 +17,9 @@ type Entity interface { } type EntityRepos struct { - Device repository.Device - Contact repository.Contact - UserPlatform repository.UserPlatform + Device repository.Device + Contact repository.Contact + UserToPlatform repository.UserToPlatform } type entity struct { @@ -176,7 +176,7 @@ func (e entity) getEntityDigitalData(userId string) (deviceData entityDigitalDat } func (e entity) getCustomData(userId string) (customData entityCustomData, err error) { - devices, err := e.repo.UserPlatform.ListByUserId(userId, 100, 0) + devices, err := e.repo.UserToPlatform.ListByUserId(userId, 100, 0) if err != nil { log.Printf("Failed to get user platforms: %s", err) err = common.StringError(err) diff --git a/pkg/model/entity.go b/pkg/model/entity.go index 0908e048..d8a8e54f 100644 --- a/pkg/model/entity.go +++ b/pkg/model/entity.go @@ -22,17 +22,17 @@ type User struct { LastName string `json:"lastName" db:"last_name"` } -// See PLATFORM in Migrations 0001 +// See PLATFORM in Migrations 0005 type Platform struct { - ID string `json:"id" db:"id"` - CreatedAt time.Time `json:"createdAt" db:"created_at"` - UpdatedAt time.Time `json:"updatedAt" db:"updated_at"` - DeactivatedAt *time.Time `json:"deactivatedAt,omitempty" db:"deactivated_at"` - Type string `json:"type" db:"type"` - Status string `json:"status" db:"status"` - Name string `json:"name" db:"name"` - ApiKey string `json:"apiKey" db:"api_key"` - Authentication AuthType `json:"authentication" db:"authentication"` + ID string `json:"id,omitempty" db:"id"` + CreatedAt time.Time `json:"createdAt,omitempty" db:"created_at"` + UpdatedAt time.Time `json:"updatedAt,omitempty" db:"updated_at"` + DeactivatedAt *time.Time `json:"deactivatedAt,omitempty" db:"deactivated_at"` + ActivatedAt *time.Time `json:"activatedAt,omitempty" db:"activated_at"` + Name string `json:"name" db:"name"` + Description string `json:"description" db:"description"` + Domains pq.StringArray `json:"domains" db:"domains"` + IPAddresses pq.StringArray `json:"ipAddresses" db:"ip_addresses"` } // See NETWORK in Migrations 0001 @@ -65,7 +65,7 @@ type Asset struct { } // See USER_PLATFORM in Migrations 0002 -type UserPlatform struct { +type UserToPlatform struct { UserID string `json:"userId" db:"user_id"` PlatformID string `json:"platformId" db:"platform_id"` } @@ -135,13 +135,13 @@ type Instrument struct { } // See CONTACT_PLATFORM in Migrations 0003 -type ContactPlatform struct { +type ContactToPlatform struct { ContactID string `json:"contactId" db:"contact_id"` PlatformID string `json:"platformId" db:"platform_id"` } // See DEVICE_INSTRUMENT in Migrations 0003 -type DeviceInstrument struct { +type DeviceToInstrument struct { DeviceID string `json:"deviceId" db:"device_id"` InstrumentID string `json:"instrumentId" db:"instrument_id"` } diff --git a/pkg/repository/base.go b/pkg/repository/base.go index 87b5ccb9..8f78d12a 100644 --- a/pkg/repository/base.go +++ b/pkg/repository/base.go @@ -15,18 +15,18 @@ import ( var ErrNotFound = errors.New("not found") type Repositories struct { - Auth AuthStrategy - User User - Contact Contact - Instrument Instrument - Device Device - UserPlatform UserPlatform - Asset Asset - Network Network - Platform Platform - Transaction Transaction - TxLeg TxLeg - Location Location + Auth AuthStrategy + User User + Contact Contact + Instrument Instrument + Device Device + UserToPlatform UserToPlatform + Asset Asset + Network Network + Platform Platform + Transaction Transaction + TxLeg TxLeg + Location Location } type Queryable interface { diff --git a/pkg/repository/contact_platform.go b/pkg/repository/contact_platform.go deleted file mode 100644 index 8f4a29d0..00000000 --- a/pkg/repository/contact_platform.go +++ /dev/null @@ -1,42 +0,0 @@ -package repository - -import ( - "github.com/String-xyz/string-api/pkg/internal/common" - "github.com/String-xyz/string-api/pkg/model" - "github.com/jmoiron/sqlx" -) - -type ContactPlatform interface { - Transactable - Readable - Create(model.ContactPlatform) (model.ContactPlatform, error) - GetById(ID string) (model.ContactPlatform, error) - List(limit int, offset int) ([]model.ContactPlatform, error) - Update(ID string, updates any) error -} - -type contactPlatform[T any] struct { - base[T] -} - -func NewContactPlatform(db *sqlx.DB) ContactPlatform { - return &contactPlatform[model.ContactPlatform]{base: base[model.ContactPlatform]{store: db, table: "contact_platform"}} -} - -func (u contactPlatform[T]) Create(insert model.ContactPlatform) (model.ContactPlatform, error) { - m := model.ContactPlatform{} - rows, err := u.store.NamedQuery(` - INSERT INTO contact_platform (contact_id, platform_id) - VALUES(:contact_id, :platform_id) RETURNING *`, insert) - if err != nil { - return m, common.StringError(err) - } - for rows.Next() { - err = rows.StructScan(&m) - if err != nil { - return m, common.StringError(err) - } - } - defer rows.Close() - return m, nil -} diff --git a/pkg/repository/contact_to_platform.go b/pkg/repository/contact_to_platform.go new file mode 100644 index 00000000..ca0d1543 --- /dev/null +++ b/pkg/repository/contact_to_platform.go @@ -0,0 +1,42 @@ +package repository + +import ( + "github.com/String-xyz/string-api/pkg/internal/common" + "github.com/String-xyz/string-api/pkg/model" + "github.com/jmoiron/sqlx" +) + +type ContactToPlatform interface { + Transactable + Readable + Create(model.ContactToPlatform) (model.ContactToPlatform, error) + GetById(ID string) (model.ContactToPlatform, error) + List(limit int, offset int) ([]model.ContactToPlatform, error) + Update(ID string, updates any) error +} + +type contactToPlatform[T any] struct { + base[T] +} + +func NewContactPlatform(db *sqlx.DB) ContactToPlatform { + return &contactToPlatform[model.ContactToPlatform]{base: base[model.ContactToPlatform]{store: db, table: "contact_to_platform"}} +} + +func (u contactToPlatform[T]) Create(insert model.ContactToPlatform) (model.ContactToPlatform, error) { + m := model.ContactToPlatform{} + rows, err := u.store.NamedQuery(` + INSERT INTO contact_to_platform (contact_id, platform_id) + VALUES(:contact_id, :platform_id) RETURNING *`, insert) + if err != nil { + return m, common.StringError(err) + } + for rows.Next() { + err = rows.StructScan(&m) + if err != nil { + return m, common.StringError(err) + } + } + defer rows.Close() + return m, nil +} diff --git a/pkg/repository/platform.go b/pkg/repository/platform.go index 8313cbee..8d21f93d 100644 --- a/pkg/repository/platform.go +++ b/pkg/repository/platform.go @@ -1,8 +1,6 @@ package repository import ( - "database/sql" - "fmt" "time" "github.com/String-xyz/string-api/pkg/internal/common" @@ -24,7 +22,6 @@ type Platform interface { GetById(ID string) (model.Platform, error) List(limit int, offset int) ([]model.Platform, error) Update(ID string, updates any) error - GetByApiKey(key string) (model.Platform, error) } type platform[T any] struct { @@ -38,8 +35,8 @@ func NewPlatform(db *sqlx.DB) Platform { func (p platform[T]) Create(m model.Platform) (model.Platform, error) { plat := model.Platform{} rows, err := p.store.NamedQuery(` - INSERT INTO platform (type, authentication, api_key, status) - VALUES(:type, :authentication, :api_key, :status) RETURNING *`, m) + INSERT INTO platform (name, description) + VALUES(:name, :description) RETURNING *`, m) if err != nil { return plat, common.StringError(err) @@ -55,13 +52,3 @@ func (p platform[T]) Create(m model.Platform) (model.Platform, error) { return plat, nil } -func (p platform[T]) GetByApiKey(key string) (model.Platform, error) { - m := model.Platform{} - err := p.store.Get(&m, fmt.Sprintf("SELECT * FROM %s WHERE api_key = $1", p.table), key) - if err != nil && err == sql.ErrNoRows { - return m, common.StringError(ErrNotFound) - } else if err != nil { - return m, common.StringError(err) - } - return m, nil -} diff --git a/pkg/repository/user_platform.go b/pkg/repository/user_platform.go deleted file mode 100644 index b098150d..00000000 --- a/pkg/repository/user_platform.go +++ /dev/null @@ -1,43 +0,0 @@ -package repository - -import ( - "github.com/String-xyz/string-api/pkg/internal/common" - "github.com/String-xyz/string-api/pkg/model" - "github.com/jmoiron/sqlx" -) - -type UserPlatform interface { - Transactable - Readable - Create(model.UserPlatform) (model.UserPlatform, error) - GetById(ID string) (model.UserPlatform, error) - List(limit int, offset int) ([]model.UserPlatform, error) - ListByUserId(userID string, imit int, offset int) ([]model.UserPlatform, error) - Update(ID string, updates any) error -} - -type userPlatform[T any] struct { - base[T] -} - -func NewUserPlatform(db *sqlx.DB) UserPlatform { - return &userPlatform[model.UserPlatform]{base: base[model.UserPlatform]{store: db, table: "user_platform"}} -} - -func (u userPlatform[T]) Create(insert model.UserPlatform) (model.UserPlatform, error) { - m := model.UserPlatform{} - rows, err := u.store.NamedQuery(` - INSERT INTO user_platform (user_id, platform_id) - VALUES(:user_id, :platform_id) RETURNING *`, insert) - if err != nil { - return m, common.StringError(err) - } - for rows.Next() { - err = rows.StructScan(&m) - if err != nil { - return m, common.StringError(err) - } - } - defer rows.Close() - return m, nil -} diff --git a/pkg/repository/user_to_platform.go b/pkg/repository/user_to_platform.go new file mode 100644 index 00000000..c3fc066b --- /dev/null +++ b/pkg/repository/user_to_platform.go @@ -0,0 +1,43 @@ +package repository + +import ( + "github.com/String-xyz/string-api/pkg/internal/common" + "github.com/String-xyz/string-api/pkg/model" + "github.com/jmoiron/sqlx" +) + +type UserToPlatform interface { + Transactable + Readable + Create(model.UserToPlatform) (model.UserToPlatform, error) + GetById(ID string) (model.UserToPlatform, error) + List(limit int, offset int) ([]model.UserToPlatform, error) + ListByUserId(userID string, imit int, offset int) ([]model.UserToPlatform, error) + Update(ID string, updates any) error +} + +type userToPlatform[T any] struct { + base[T] +} + +func NewUserToPlatform(db *sqlx.DB) UserToPlatform { + return &userToPlatform[model.UserToPlatform]{base: base[model.UserToPlatform]{store: db, table: "user_to_platform"}} +} + +func (u userToPlatform[T]) Create(insert model.UserToPlatform) (model.UserToPlatform, error) { + m := model.UserToPlatform{} + rows, err := u.store.NamedQuery(` + INSERT INTO user_to_platform (user_id, platform_id) + VALUES(:user_id, :platform_id) RETURNING *`, insert) + if err != nil { + return m, common.StringError(err) + } + for rows.Next() { + err = rows.StructScan(&m) + if err != nil { + return m, common.StringError(err) + } + } + defer rows.Close() + return m, nil +} diff --git a/pkg/service/platform.go b/pkg/service/platform.go index 71999271..aae5753f 100644 --- a/pkg/service/platform.go +++ b/pkg/service/platform.go @@ -23,12 +23,7 @@ func NewPlatform(repos repository.Repositories) Platform { func (a platform) Create(c CreatePlatform) (model.Platform, error) { uuiKey := "str." + uuidWithoutHyphens() hashed := common.ToSha256(uuiKey) - m := model.Platform{ - Type: c.Type, - Authentication: c.Authentication, - ApiKey: hashed, - Status: "pending", - } + m := model.Platform{} plat, err := a.repos.Platform.Create(m) if err != nil { @@ -37,7 +32,6 @@ func (a platform) Create(c CreatePlatform) (model.Platform, error) { _, err = a.repos.Auth.CreateAPIKey(plat.ID, c.Authentication, hashed, false) pt := &plat - pt.ApiKey = uuiKey if err != nil { return *pt, common.StringError(err) } diff --git a/pkg/service/user.go b/pkg/service/user.go index 13d485f9..f30e53e1 100644 --- a/pkg/service/user.go +++ b/pkg/service/user.go @@ -173,9 +173,9 @@ func (u user) Update(userID string, request UserUpdates) (model.User, error) { func (u user) createUnit21Entity(user model.User) { // Createing a User Entity in Unit21 u21Repo := unit21.EntityRepos{ - Device: u.repos.Device, - Contact: u.repos.Contact, - UserPlatform: u.repos.UserPlatform, + Device: u.repos.Device, + Contact: u.repos.Contact, + UserToPlatform: u.repos.UserToPlatform, } u21Entity := unit21.NewEntity(u21Repo) // TODO: Make it an injected dependency @@ -188,9 +188,9 @@ func (u user) createUnit21Entity(user model.User) { func (u user) updateUnit21Entity(user model.User) { // Createing a User Entity in Unit21 u21Repo := unit21.EntityRepos{ - Device: u.repos.Device, - Contact: u.repos.Contact, - UserPlatform: u.repos.UserPlatform, + Device: u.repos.Device, + Contact: u.repos.Contact, + UserToPlatform: u.repos.UserToPlatform, } u21Entity := unit21.NewEntity(u21Repo) diff --git a/scripts/data_seeding.go b/scripts/data_seeding.go index 805bfdf5..2dd9a52d 100644 --- a/scripts/data_seeding.go +++ b/scripts/data_seeding.go @@ -186,7 +186,8 @@ func DataSeeding() { // Platforms, placeholder /*platformDeveloper*/ - placeholderPlatform, err := repos.Platform.Create(model.Platform{Type: "Game", Status: "Verified", Name: "Nintendo", ApiKey: "Internal", Authentication: "Email"}) + placeholderPlatform, err := repos.Platform.Create(model.Platform{Name: "Nintendo", Description: "Fun"}) + if err != nil { panic(err) } @@ -377,7 +378,7 @@ func MockSeeding() { // Platforms, placeholder /*platformDeveloper*/ - placeholderPlatform, err := repos.Platform.Create(model.Platform{Type: "Game", Status: "Verified", Name: "Nintendo", ApiKey: "Internal", Authentication: "Email"}) + placeholderPlatform, err := repos.Platform.Create(model.Platform{Name: "Nintendo", Description: "Fun"}) if err != nil { panic(err) }