feat(models): add new models for public API, event log, hook, session, user, and whatsapp account
Some checks failed
CI / Test (1.23) (push) Failing after -22m31s
CI / Test (1.22) (push) Failing after -22m34s
CI / Build (push) Failing after -22m43s
CI / Lint (push) Failing after -22m27s

* Introduced ModelPublicAPIKey, ModelPublicEventLog, ModelPublicHook, ModelPublicSession, ModelPublicUser, and ModelPublicWhatsappAccount
* Removed deprecated ModelPublicUsers
* Updated database schema definitions to reflect new model structure
* Adjusted seed data to use correct types for timestamps
This commit is contained in:
Hein
2026-02-20 16:19:49 +02:00
parent 524bc4179b
commit 3b6d0f81d2
10 changed files with 157 additions and 167 deletions

View File

@@ -7,8 +7,8 @@ import (
"github.com/uptrace/bun"
)
type ModelPublicAPIKeys struct {
bun.BaseModel `bun:"table:public.api_keys,alias:api_keys"`
type ModelPublicAPIKey struct {
bun.BaseModel `bun:"table:public.api_key,alias:api_key"`
ID resolvespec_common.SqlString `bun:"id,type:varchar(36),pk," json:"id"` // UUID
Active bool `bun:"active,type:boolean,default:true,notnull," json:"active"`
CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamp,default:now(),notnull," json:"created_at"`
@@ -21,50 +21,49 @@ type ModelPublicAPIKeys struct {
Permissions resolvespec_common.SqlString `bun:"permissions,type:text,nullzero," json:"permissions"` // JSON array of permissions
UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamp,default:now(),notnull," json:"updated_at"`
UserID resolvespec_common.SqlString `bun:"user_id,type:varchar(36),notnull," json:"user_id"`
RelUserID *ModelPublicUsers `bun:"rel:has-one,join:user_id=id" json:"reluserid,omitempty"` // Has one ModelPublicUsers
}
// TableName returns the table name for ModelPublicAPIKeys
func (m ModelPublicAPIKeys) TableName() string {
return "public.api_keys"
// TableName returns the table name for ModelPublicAPIKey
func (m ModelPublicAPIKey) TableName() string {
return "public.api_key"
}
// TableNameOnly returns the table name without schema for ModelPublicAPIKeys
func (m ModelPublicAPIKeys) TableNameOnly() string {
return "api_keys"
// TableNameOnly returns the table name without schema for ModelPublicAPIKey
func (m ModelPublicAPIKey) TableNameOnly() string {
return "api_key"
}
// SchemaName returns the schema name for ModelPublicAPIKeys
func (m ModelPublicAPIKeys) SchemaName() string {
// SchemaName returns the schema name for ModelPublicAPIKey
func (m ModelPublicAPIKey) SchemaName() string {
return "public"
}
// GetID returns the primary key value
func (m ModelPublicAPIKeys) GetID() int64 {
func (m ModelPublicAPIKey) GetID() int64 {
return m.ID.Int64()
}
// GetIDStr returns the primary key as a string
func (m ModelPublicAPIKeys) GetIDStr() string {
func (m ModelPublicAPIKey) GetIDStr() string {
return fmt.Sprintf("%d", m.ID)
}
// SetID sets the primary key value
func (m ModelPublicAPIKeys) SetID(newid int64) {
func (m ModelPublicAPIKey) SetID(newid int64) {
m.UpdateID(newid)
}
// UpdateID updates the primary key value
func (m *ModelPublicAPIKeys) UpdateID(newid int64) {
func (m *ModelPublicAPIKey) UpdateID(newid int64) {
m.ID.FromString(fmt.Sprintf("%d", newid))
}
// GetIDName returns the name of the primary key column
func (m ModelPublicAPIKeys) GetIDName() string {
func (m ModelPublicAPIKey) GetIDName() string {
return "id"
}
// GetPrefix returns the table prefix
func (m ModelPublicAPIKeys) GetPrefix() string {
func (m ModelPublicAPIKey) GetPrefix() string {
return "AKP"
}

View File

@@ -7,8 +7,8 @@ import (
"github.com/uptrace/bun"
)
type ModelPublicEventLogs struct {
bun.BaseModel `bun:"table:public.event_logs,alias:event_logs"`
type ModelPublicEventLog struct {
bun.BaseModel `bun:"table:public.event_log,alias:event_log"`
ID resolvespec_common.SqlString `bun:"id,type:varchar(36),pk," json:"id"` // UUID
Action resolvespec_common.SqlString `bun:"action,type:varchar(50),nullzero," json:"action"` // create
CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamp,default:now(),notnull," json:"created_at"`
@@ -20,51 +20,50 @@ type ModelPublicEventLogs struct {
IpAddress resolvespec_common.SqlString `bun:"ip_address,type:varchar(50),nullzero," json:"ip_address"`
Success bool `bun:"success,type:boolean,default:true,notnull," json:"success"`
UserAgent resolvespec_common.SqlString `bun:"user_agent,type:text,nullzero," json:"user_agent"`
UserID resolvespec_common.SqlString `bun:"user_id,type:varchar(36),nullzero," json:"user_id"` // Optional user reference
RelUserID *ModelPublicUsers `bun:"rel:has-one,join:user_id=id" json:"reluserid,omitempty"` // Has one ModelPublicUsers
UserID resolvespec_common.SqlString `bun:"user_id,type:varchar(36),nullzero," json:"user_id"` // Optional user reference
}
// TableName returns the table name for ModelPublicEventLogs
func (m ModelPublicEventLogs) TableName() string {
return "public.event_logs"
// TableName returns the table name for ModelPublicEventLog
func (m ModelPublicEventLog) TableName() string {
return "public.event_log"
}
// TableNameOnly returns the table name without schema for ModelPublicEventLogs
func (m ModelPublicEventLogs) TableNameOnly() string {
return "event_logs"
// TableNameOnly returns the table name without schema for ModelPublicEventLog
func (m ModelPublicEventLog) TableNameOnly() string {
return "event_log"
}
// SchemaName returns the schema name for ModelPublicEventLogs
func (m ModelPublicEventLogs) SchemaName() string {
// SchemaName returns the schema name for ModelPublicEventLog
func (m ModelPublicEventLog) SchemaName() string {
return "public"
}
// GetID returns the primary key value
func (m ModelPublicEventLogs) GetID() int64 {
func (m ModelPublicEventLog) GetID() int64 {
return m.ID.Int64()
}
// GetIDStr returns the primary key as a string
func (m ModelPublicEventLogs) GetIDStr() string {
func (m ModelPublicEventLog) GetIDStr() string {
return fmt.Sprintf("%d", m.ID)
}
// SetID sets the primary key value
func (m ModelPublicEventLogs) SetID(newid int64) {
func (m ModelPublicEventLog) SetID(newid int64) {
m.UpdateID(newid)
}
// UpdateID updates the primary key value
func (m *ModelPublicEventLogs) UpdateID(newid int64) {
func (m *ModelPublicEventLog) UpdateID(newid int64) {
m.ID.FromString(fmt.Sprintf("%d", newid))
}
// GetIDName returns the name of the primary key column
func (m ModelPublicEventLogs) GetIDName() string {
func (m ModelPublicEventLog) GetIDName() string {
return "id"
}
// GetPrefix returns the table prefix
func (m ModelPublicEventLogs) GetPrefix() string {
func (m ModelPublicEventLog) GetPrefix() string {
return "ELV"
}

View File

@@ -7,8 +7,8 @@ import (
"github.com/uptrace/bun"
)
type ModelPublicHooks struct {
bun.BaseModel `bun:"table:public.hooks,alias:hooks"`
type ModelPublicHook struct {
bun.BaseModel `bun:"table:public.hook,alias:hook"`
ID resolvespec_common.SqlString `bun:"id,type:varchar(36),pk," json:"id"` // UUID
Active bool `bun:"active,type:boolean,default:true,notnull," json:"active"`
AllowInsecure bool `bun:"allow_insecure,type:boolean,default:false,notnull," json:"allow_insecure"` // Skip TLS certificate verification
@@ -25,50 +25,49 @@ type ModelPublicHooks struct {
UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamp,default:now(),notnull," json:"updated_at"`
URL resolvespec_common.SqlString `bun:"url,type:text,notnull," json:"url"`
UserID resolvespec_common.SqlString `bun:"user_id,type:varchar(36),notnull," json:"user_id"`
RelUserID *ModelPublicUsers `bun:"rel:has-one,join:user_id=id" json:"reluserid,omitempty"` // Has one ModelPublicUsers
}
// TableName returns the table name for ModelPublicHooks
func (m ModelPublicHooks) TableName() string {
return "public.hooks"
// TableName returns the table name for ModelPublicHook
func (m ModelPublicHook) TableName() string {
return "public.hook"
}
// TableNameOnly returns the table name without schema for ModelPublicHooks
func (m ModelPublicHooks) TableNameOnly() string {
return "hooks"
// TableNameOnly returns the table name without schema for ModelPublicHook
func (m ModelPublicHook) TableNameOnly() string {
return "hook"
}
// SchemaName returns the schema name for ModelPublicHooks
func (m ModelPublicHooks) SchemaName() string {
// SchemaName returns the schema name for ModelPublicHook
func (m ModelPublicHook) SchemaName() string {
return "public"
}
// GetID returns the primary key value
func (m ModelPublicHooks) GetID() int64 {
func (m ModelPublicHook) GetID() int64 {
return m.ID.Int64()
}
// GetIDStr returns the primary key as a string
func (m ModelPublicHooks) GetIDStr() string {
func (m ModelPublicHook) GetIDStr() string {
return fmt.Sprintf("%d", m.ID)
}
// SetID sets the primary key value
func (m ModelPublicHooks) SetID(newid int64) {
func (m ModelPublicHook) SetID(newid int64) {
m.UpdateID(newid)
}
// UpdateID updates the primary key value
func (m *ModelPublicHooks) UpdateID(newid int64) {
func (m *ModelPublicHook) UpdateID(newid int64) {
m.ID.FromString(fmt.Sprintf("%d", newid))
}
// GetIDName returns the name of the primary key column
func (m ModelPublicHooks) GetIDName() string {
func (m ModelPublicHook) GetIDName() string {
return "id"
}
// GetPrefix returns the table prefix
func (m ModelPublicHooks) GetPrefix() string {
func (m ModelPublicHook) GetPrefix() string {
return "HOO"
}

View File

@@ -7,8 +7,8 @@ import (
"github.com/uptrace/bun"
)
type ModelPublicSessions struct {
bun.BaseModel `bun:"table:public.sessions,alias:sessions"`
type ModelPublicSession struct {
bun.BaseModel `bun:"table:public.session,alias:session"`
ID resolvespec_common.SqlString `bun:"id,type:varchar(36),pk," json:"id"` // UUID
CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamp,default:now(),notnull," json:"created_at"`
ExpiresAt resolvespec_common.SqlTimeStamp `bun:"expires_at,type:timestamp,notnull," json:"expires_at"`
@@ -17,50 +17,49 @@ type ModelPublicSessions struct {
UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamp,default:now(),notnull," json:"updated_at"`
UserAgent resolvespec_common.SqlString `bun:"user_agent,type:text,nullzero," json:"user_agent"`
UserID resolvespec_common.SqlString `bun:"user_id,type:varchar(36),notnull," json:"user_id"`
RelUserID *ModelPublicUsers `bun:"rel:has-one,join:user_id=id" json:"reluserid,omitempty"` // Has one ModelPublicUsers
}
// TableName returns the table name for ModelPublicSessions
func (m ModelPublicSessions) TableName() string {
return "public.sessions"
// TableName returns the table name for ModelPublicSession
func (m ModelPublicSession) TableName() string {
return "public.session"
}
// TableNameOnly returns the table name without schema for ModelPublicSessions
func (m ModelPublicSessions) TableNameOnly() string {
return "sessions"
// TableNameOnly returns the table name without schema for ModelPublicSession
func (m ModelPublicSession) TableNameOnly() string {
return "session"
}
// SchemaName returns the schema name for ModelPublicSessions
func (m ModelPublicSessions) SchemaName() string {
// SchemaName returns the schema name for ModelPublicSession
func (m ModelPublicSession) SchemaName() string {
return "public"
}
// GetID returns the primary key value
func (m ModelPublicSessions) GetID() int64 {
func (m ModelPublicSession) GetID() int64 {
return m.ID.Int64()
}
// GetIDStr returns the primary key as a string
func (m ModelPublicSessions) GetIDStr() string {
func (m ModelPublicSession) GetIDStr() string {
return fmt.Sprintf("%d", m.ID)
}
// SetID sets the primary key value
func (m ModelPublicSessions) SetID(newid int64) {
func (m ModelPublicSession) SetID(newid int64) {
m.UpdateID(newid)
}
// UpdateID updates the primary key value
func (m *ModelPublicSessions) UpdateID(newid int64) {
func (m *ModelPublicSession) UpdateID(newid int64) {
m.ID.FromString(fmt.Sprintf("%d", newid))
}
// GetIDName returns the name of the primary key column
func (m ModelPublicSessions) GetIDName() string {
func (m ModelPublicSession) GetIDName() string {
return "id"
}
// GetPrefix returns the table prefix
func (m ModelPublicSessions) GetPrefix() string {
func (m ModelPublicSession) GetPrefix() string {
return "SES"
}

View File

@@ -0,0 +1,67 @@
// Code generated by relspecgo. DO NOT EDIT.
package models
import (
"fmt"
resolvespec_common "github.com/bitechdev/ResolveSpec/pkg/spectypes"
"github.com/uptrace/bun"
)
type ModelPublicUser struct {
bun.BaseModel `bun:"table:public.user,alias:user"`
ID resolvespec_common.SqlString `bun:"id,type:varchar(36),pk," json:"id"` // UUID
Active bool `bun:"active,type:boolean,default:true,notnull," json:"active"`
CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamp,default:now(),notnull," json:"created_at"`
DeletedAt resolvespec_common.SqlTimeStamp `bun:"deleted_at,type:timestamp,nullzero," json:"deleted_at"` // Soft delete
Email resolvespec_common.SqlString `bun:"email,type:varchar(255),notnull," json:"email"`
FullName resolvespec_common.SqlString `bun:"full_name,type:varchar(255),nullzero," json:"full_name"`
Password resolvespec_common.SqlString `bun:"password,type:varchar(255),notnull," json:"password"` // Bcrypt hashed password
Role resolvespec_common.SqlString `bun:"role,type:varchar(50),default:'user',notnull," json:"role"` // admin
UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamp,default:now(),notnull," json:"updated_at"`
Username resolvespec_common.SqlString `bun:"username,type:varchar(255),notnull," json:"username"`
}
// TableName returns the table name for ModelPublicUser
func (m ModelPublicUser) TableName() string {
return "public.user"
}
// TableNameOnly returns the table name without schema for ModelPublicUser
func (m ModelPublicUser) TableNameOnly() string {
return "user"
}
// SchemaName returns the schema name for ModelPublicUser
func (m ModelPublicUser) SchemaName() string {
return "public"
}
// GetID returns the primary key value
func (m ModelPublicUser) GetID() int64 {
return m.ID.Int64()
}
// GetIDStr returns the primary key as a string
func (m ModelPublicUser) GetIDStr() string {
return fmt.Sprintf("%d", m.ID)
}
// SetID sets the primary key value
func (m ModelPublicUser) SetID(newid int64) {
m.UpdateID(newid)
}
// UpdateID updates the primary key value
func (m *ModelPublicUser) UpdateID(newid int64) {
m.ID.FromString(fmt.Sprintf("%d", newid))
}
// GetIDName returns the name of the primary key column
func (m ModelPublicUser) GetIDName() string {
return "id"
}
// GetPrefix returns the table prefix
func (m ModelPublicUser) GetPrefix() string {
return "USE"
}

View File

@@ -1,72 +0,0 @@
// Code generated by relspecgo. DO NOT EDIT.
package models
import (
"fmt"
resolvespec_common "github.com/bitechdev/ResolveSpec/pkg/spectypes"
"github.com/uptrace/bun"
)
type ModelPublicUsers struct {
bun.BaseModel `bun:"table:public.users,alias:users"`
ID resolvespec_common.SqlString `bun:"id,type:varchar(36),pk," json:"id"` // UUID
Active bool `bun:"active,type:boolean,default:true,notnull," json:"active"`
CreatedAt resolvespec_common.SqlTimeStamp `bun:"created_at,type:timestamp,default:now(),notnull," json:"created_at"`
DeletedAt resolvespec_common.SqlTimeStamp `bun:"deleted_at,type:timestamp,nullzero," json:"deleted_at"` // Soft delete
Email resolvespec_common.SqlString `bun:"email,type:varchar(255),notnull," json:"email"`
FullName resolvespec_common.SqlString `bun:"full_name,type:varchar(255),nullzero," json:"full_name"`
Password resolvespec_common.SqlString `bun:"password,type:varchar(255),notnull," json:"password"` // Bcrypt hashed password
Role resolvespec_common.SqlString `bun:"role,type:varchar(50),default:'user',notnull," json:"role"` // admin
UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamp,default:now(),notnull," json:"updated_at"`
Username resolvespec_common.SqlString `bun:"username,type:varchar(255),notnull," json:"username"`
RelUserIDPublicAPIKeys []*ModelPublicAPIKeys `bun:"rel:has-many,join:id=user_id" json:"reluseridpublicapikeys,omitempty"` // Has many ModelPublicAPIKeys
RelUserIDPublicHooks []*ModelPublicHooks `bun:"rel:has-many,join:id=user_id" json:"reluseridpublichooks,omitempty"` // Has many ModelPublicHooks
RelUserIDPublicWhatsappAccounts []*ModelPublicWhatsappAccounts `bun:"rel:has-many,join:id=user_id" json:"reluseridpublicwhatsappaccounts,omitempty"` // Has many ModelPublicWhatsappAccounts
RelUserIDPublicEventLogs []*ModelPublicEventLogs `bun:"rel:has-many,join:id=user_id" json:"reluseridpubliceventlogs,omitempty"` // Has many ModelPublicEventLogs
RelUserIDPublicSessions []*ModelPublicSessions `bun:"rel:has-many,join:id=user_id" json:"reluseridpublicsessions,omitempty"` // Has many ModelPublicSessions
}
// TableName returns the table name for ModelPublicUsers
func (m ModelPublicUsers) TableName() string {
return "public.users"
}
// TableNameOnly returns the table name without schema for ModelPublicUsers
func (m ModelPublicUsers) TableNameOnly() string {
return "users"
}
// SchemaName returns the schema name for ModelPublicUsers
func (m ModelPublicUsers) SchemaName() string {
return "public"
}
// GetID returns the primary key value
func (m ModelPublicUsers) GetID() int64 {
return m.ID.Int64()
}
// GetIDStr returns the primary key as a string
func (m ModelPublicUsers) GetIDStr() string {
return fmt.Sprintf("%d", m.ID)
}
// SetID sets the primary key value
func (m ModelPublicUsers) SetID(newid int64) {
m.UpdateID(newid)
}
// UpdateID updates the primary key value
func (m *ModelPublicUsers) UpdateID(newid int64) {
m.ID.FromString(fmt.Sprintf("%d", newid))
}
// GetIDName returns the name of the primary key column
func (m ModelPublicUsers) GetIDName() string {
return "id"
}
// GetPrefix returns the table prefix
func (m ModelPublicUsers) GetPrefix() string {
return "USE"
}

View File

@@ -7,8 +7,8 @@ import (
"github.com/uptrace/bun"
)
type ModelPublicWhatsappAccounts struct {
bun.BaseModel `bun:"table:public.whatsapp_accounts,alias:whatsapp_accounts"`
type ModelPublicWhatsappAccount struct {
bun.BaseModel `bun:"table:public.whatsapp_account,alias:whatsapp_account"`
ID resolvespec_common.SqlString `bun:"id,type:varchar(36),pk," json:"id"` // UUID
AccountType resolvespec_common.SqlString `bun:"account_type,type:varchar(50),notnull," json:"account_type"` // whatsmeow or business-api
Active bool `bun:"active,type:boolean,default:true,notnull," json:"active"`
@@ -22,50 +22,49 @@ type ModelPublicWhatsappAccounts struct {
Status resolvespec_common.SqlString `bun:"status,type:varchar(50),default:'disconnected',notnull," json:"status"` // connected
UpdatedAt resolvespec_common.SqlTimeStamp `bun:"updated_at,type:timestamp,default:now(),notnull," json:"updated_at"`
UserID resolvespec_common.SqlString `bun:"user_id,type:varchar(36),notnull," json:"user_id"`
RelUserID *ModelPublicUsers `bun:"rel:has-one,join:user_id=id" json:"reluserid,omitempty"` // Has one ModelPublicUsers
}
// TableName returns the table name for ModelPublicWhatsappAccounts
func (m ModelPublicWhatsappAccounts) TableName() string {
return "public.whatsapp_accounts"
// TableName returns the table name for ModelPublicWhatsappAccount
func (m ModelPublicWhatsappAccount) TableName() string {
return "public.whatsapp_account"
}
// TableNameOnly returns the table name without schema for ModelPublicWhatsappAccounts
func (m ModelPublicWhatsappAccounts) TableNameOnly() string {
return "whatsapp_accounts"
// TableNameOnly returns the table name without schema for ModelPublicWhatsappAccount
func (m ModelPublicWhatsappAccount) TableNameOnly() string {
return "whatsapp_account"
}
// SchemaName returns the schema name for ModelPublicWhatsappAccounts
func (m ModelPublicWhatsappAccounts) SchemaName() string {
// SchemaName returns the schema name for ModelPublicWhatsappAccount
func (m ModelPublicWhatsappAccount) SchemaName() string {
return "public"
}
// GetID returns the primary key value
func (m ModelPublicWhatsappAccounts) GetID() int64 {
func (m ModelPublicWhatsappAccount) GetID() int64 {
return m.ID.Int64()
}
// GetIDStr returns the primary key as a string
func (m ModelPublicWhatsappAccounts) GetIDStr() string {
func (m ModelPublicWhatsappAccount) GetIDStr() string {
return fmt.Sprintf("%d", m.ID)
}
// SetID sets the primary key value
func (m ModelPublicWhatsappAccounts) SetID(newid int64) {
func (m ModelPublicWhatsappAccount) SetID(newid int64) {
m.UpdateID(newid)
}
// UpdateID updates the primary key value
func (m *ModelPublicWhatsappAccounts) UpdateID(newid int64) {
func (m *ModelPublicWhatsappAccount) UpdateID(newid int64) {
m.ID.FromString(fmt.Sprintf("%d", newid))
}
// GetIDName returns the name of the primary key column
func (m ModelPublicWhatsappAccounts) GetIDName() string {
func (m ModelPublicWhatsappAccount) GetIDName() string {
return "id"
}
// GetPrefix returns the table prefix
func (m ModelPublicWhatsappAccounts) GetPrefix() string {
func (m ModelPublicWhatsappAccount) GetPrefix() string {
return "WAH"
}

View File

@@ -40,8 +40,8 @@ func SeedData(ctx context.Context) error {
FullName: resolvespec_common.NewSqlString("System Administrator"),
Role: resolvespec_common.NewSqlString("admin"),
Active: true,
CreatedAt: resolvespec_common.NewSqlTime(now),
UpdatedAt: resolvespec_common.NewSqlTime(now),
CreatedAt: resolvespec_common.NewSqlTimeStamp(now),
UpdatedAt: resolvespec_common.NewSqlTimeStamp(now),
}
if err := userRepo.Create(ctx, adminUser); err != nil {

View File

@@ -165,7 +165,7 @@ func (wh *WhatsHooked) connectFromDatabase(ctx context.Context) error {
for _, account := range accounts {
// Skip if account_id is not set
accountID := account.AccountID.String()
accountID := account.ID.String()
if accountID == "" {
accountID = account.ID.String() // Fall back to UUID if account_id not set
}