feat: Add new entities for maintenance management and user roles
- Created M_Location, M_MaintenanceGroup, M_MaintenanceGroupRole, M_MaintenanceGroupUser, M_MaintenanceScheduleHeader, M_MaintenanceScheduleLine, M_MaintenanceStaff, M_Menu_Client, M_Menu, M_Permissions, M_Role, M_Role_Menu, M_Role_Permission, M_User, M_User_Role entities. - Implemented relationships and foreign keys for user roles and permissions. - Added JSON seed files for roles and user roles, along with corresponding seed functions. - Updated migration and seeder files to include new entities and ensure proper database setup. - Commented out legacy product and tenant entities and their seed functions for future reference.
This commit is contained in:
parent
3cf87f7527
commit
465908965e
|
|
@ -0,0 +1,19 @@
|
|||
package entities
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type M_Client struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
PIC string `gorm:"type:varchar(100)" json:"pic"`
|
||||
Phone string `gorm:"type:varchar(20)" json:"phone"`
|
||||
Email string `gorm:"type:varchar(100)" json:"email"`
|
||||
Address string `gorm:"type:text" json:"address"`
|
||||
LogoUrl string `gorm:"type:varchar(255)" json:"logo_url"`
|
||||
|
||||
Users []M_User `gorm:"foreignKey:ClientID;references:ID" json:"users"`
|
||||
MaintenanceGroups []M_MaintenanceGroup `gorm:"foreignKey:ClientID;references:ID" json:"maintenance_groups"`
|
||||
Roles []M_Role `gorm:"foreignKey:ClientID;references:ID" json:"roles"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type M_Location struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
ClientID uuid.UUID `gorm:"type:uuid;not null" json:"client_id"`
|
||||
SiteID uuid.UUID `gorm:"type:uuid;not null" json:"site_id"`
|
||||
CategoryID uuid.UUID `gorm:"type:uuid;not null" json:"category_id"`
|
||||
PatchCode string `gorm:"type:varchar(50)" json:"patch_code"`
|
||||
DisposalLocation bool `gorm:"default:false" json:"disposal_location"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package entities
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type M_MaintenanceGroup struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
Code string `gorm:"type:varchar(50);uniqueIndex;not null" json:"code"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
ClientID uuid.UUID `gorm:"type:uuid;not null;index" json:"client_id"`
|
||||
|
||||
Client M_Client `gorm:"foreignKey:ClientID;references:ID"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package entities
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type M_MaintenanceGroupRole struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
MaintenanceGroupID uuid.UUID `gorm:"type:uuid;not null;index" json:"maintenance_group_id"`
|
||||
RoleID uuid.UUID `gorm:"type:uuid;not null;index" json:"role_id"`
|
||||
ClientID uuid.UUID `gorm:"type:uuid;not null;index" json:"client_id"`
|
||||
Level int `gorm:"not null" json:"level"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package entities
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type M_MaintenanceGroupUser struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
MaintenanceGroupRoleID uuid.UUID `gorm:"type:uuid;not null;index" json:"maintenance_group_role_id"`
|
||||
UserID uuid.UUID `gorm:"type:uuid;not null;index" json:"user_id"`
|
||||
ClientID uuid.UUID `gorm:"type:uuid;not null;index" json:"client_id"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type M_MaintenanceScheduleHeader struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
MaintenanceGroupID uuid.UUID `gorm:"type:uuid;not null;index" json:"maintenance_group_id"`
|
||||
CategoryID uuid.UUID `gorm:"type:uuid;not null;index" json:"category_id"`
|
||||
LocationID uuid.UUID `gorm:"type:uuid;not null;index" json:"location_id"`
|
||||
SpvCreatedId uuid.UUID `gorm:"type:uuid;not null;index" json:"spv_created_id"`
|
||||
SpvAssignId uuid.UUID `gorm:"type:uuid;not null;index" json:"spv_assign_id"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
Notes string `gorm:"type:text" json:"notes"`
|
||||
AttachmentUrl string `gorm:"type:varchar(255)" json:"attachment_url"`
|
||||
IntervalNumber int `gorm:"not null" json:"interval_number"`
|
||||
IntervalUom uuid.UUID `gorm:"type:uuid;not null;index" json:"interval_uom"`
|
||||
ApprovalStatus string `gorm:"type:varchar(50);not null" json:"approval_status"`
|
||||
EndDate time.Time `json:"end_date"`
|
||||
StartDate time.Time `json:"start_date"`
|
||||
DocumentNo string `gorm:"type:varchar(50);uniqueIndex;not null" json:"document_no"`
|
||||
OnDay int `json:"on_day"`
|
||||
OnMonth int `json:"on_month"`
|
||||
Repeat string `gorm:"type:varchar(50);not null" json:"repeat"`
|
||||
RepeatEvery int `json:"repeat_every"`
|
||||
Weeks string `gorm:"type:varchar(100)" json:"weeks"`
|
||||
AssetID uuid.UUID `gorm:"type:uuid;index" json:"asset_id"`
|
||||
StandardMaintenanceID uuid.UUID `gorm:"type:uuid;index" json:"standard_maintenance_id"`
|
||||
ClientID uuid.UUID `gorm:"type:uuid;not null;index" json:"client_id"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type M_MaintenanceScheduleLine struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
MaintenanceScheduleHeaderID uuid.UUID `gorm:"type:uuid;not null;index" json:"maintenance_schedule_header_id"`
|
||||
StaffID uuid.UUID `gorm:"type:uuid;not null;index" json:"staff_id"`
|
||||
Notes string `gorm:"type:text" json:"notes"`
|
||||
PlannedDate time.Time `gorm:"type:timestamp;not null" json:"planned_date"`
|
||||
DueDate time.Time `gorm:"type:timestamp;not null" json:"due_date"`
|
||||
AssetID uuid.UUID `gorm:"type:uuid;not null;index" json:"asset_id"`
|
||||
TaskDescription string `gorm:"type:text" json:"task_description"`
|
||||
CategoryID uuid.UUID `gorm:"type:uuid;not null;index" json:"category_id"`
|
||||
LocationID uuid.UUID `gorm:"type:uuid;not null;index" json:"location_id"`
|
||||
ClientID uuid.UUID `gorm:"type:uuid;not null;index" json:"client_id"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type M_MaintenanceStaff struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
MaintenanceSpvID uuid.UUID `gorm:"type:uuid;not null;index" json:"maintenance_spv_id"`
|
||||
StaffID uuid.UUID `gorm:"type:uuid;not null;index" json:"staff_id"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
CategoryID uuid.UUID `gorm:"type:uuid;not null;index" json:"category_id"`
|
||||
SiteID uuid.UUID `gorm:"type:uuid;not null;index" json:"site_id"`
|
||||
ClientID uuid.UUID `gorm:"type:uuid;not null;index" json:"client_id"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type M_Menu_Client struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
MenuID uuid.UUID `gorm:"type:uuid;not null;index" json:"menu_id"`
|
||||
ClientID uuid.UUID `gorm:"type:uuid;not null;index" json:"client_id"`
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type M_Menu struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
IconUrl string `gorm:"type:varchar(255)" json:"icon_url"`
|
||||
Url string `gorm:"type:varchar(255);not null" json:"url"`
|
||||
Sequence int `gorm:"type:int;not null" json:"sequence"`
|
||||
Mode string `gorm:"type:varchar(50);not null" json:"mode"`
|
||||
TableName string `gorm:"type:varchar(100)" json:"table_name"`
|
||||
Status string `gorm:"type:varchar(50);not null" json:"status"`
|
||||
ParentID *uuid.UUID `gorm:"type:uuid" json:"parent_id"`
|
||||
|
||||
Children []M_Menu `gorm:"foreignKey:ParentID;references:ID" json:"children"`
|
||||
RoleMenus []M_Role_Menu `gorm:"foreignKey:MenuID;references:ID" json:"role_menus"`
|
||||
Permissions []M_Permissions `gorm:"foreignKey:MenuID;references:ID" json:"permissions"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type M_Permissions struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
MenuID uuid.UUID `gorm:"type:uuid;not null;index" json:"menu_id"`
|
||||
RoleID uuid.UUID `gorm:"type:uuid;not null;index" json:"role_id"` // <-- Add this line
|
||||
|
||||
Menu M_Menu `gorm:"foreignKey:MenuID;references:ID"`
|
||||
Role M_Role `gorm:"foreignKey:RoleID;references:ID"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
package entities
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type M_Role struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
Description string `gorm:"type:text" json:"description"`
|
||||
IconUrl string `gorm:"type:varchar(255)" json:"icon_url"`
|
||||
Type string `gorm:"type:varchar(50);not null" json:"type"`
|
||||
HomeUrl string `gorm:"type:varchar(255)" json:"home_url"`
|
||||
ClientID uuid.UUID `gorm:"type:uuid;not null;index" json:"client_id"`
|
||||
|
||||
Client M_Client `gorm:"foreignKey:ClientID;references:ID"`
|
||||
RoleMenus []M_Role_Menu `gorm:"foreignKey:RoleID;references:ID" json:"role_menus"`
|
||||
RolePermissions []M_Role_Permission `gorm:"foreignKey:RoleID;references:ID" json:"role_permissions"`
|
||||
UserRoles []M_User_Role `gorm:"foreignKey:RoleID;references:ID" json:"user_roles"`
|
||||
Users []M_User `gorm:"many2many:m_user_roles;" json:"users"`
|
||||
Permissions []M_Permissions `gorm:"many2many:m_role_permissions;joinForeignKey:RoleID;JoinReferences:PermissionID" json:"permissions"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
||||
// Assign role ke user
|
||||
// userRole := M_User_Role{
|
||||
// UserID: userID,
|
||||
// RoleID: roleID,
|
||||
// }
|
||||
// db.Create(&userRole)
|
||||
|
||||
// // Get user dengan roles
|
||||
// var user M_User
|
||||
// db.Preload("Roles").First(&user, userID)
|
||||
|
||||
// // Get role dengan users
|
||||
// var role M_Role
|
||||
// db.Preload("Users").First(&role, roleID)
|
||||
|
||||
// // Check apakah user memiliki role tertentu
|
||||
// var count int64
|
||||
// db.Model(&M_User_Role{}).Where("user_id = ? AND role_id = ?", userID, roleID).Count(&count)
|
||||
// hasRole := count > 0
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type M_Role_Menu struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
RoleID uuid.UUID `gorm:"type:uuid;not null;index" json:"role_id"`
|
||||
MenuID uuid.UUID `gorm:"type:uuid;not null;index" json:"menu_id"`
|
||||
|
||||
Role M_Role `gorm:"foreignKey:RoleID;references:ID"`
|
||||
Menu M_Menu `gorm:"foreignKey:MenuID;references:ID"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type M_Role_Permission struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
RoleID uuid.UUID `gorm:"type:uuid;not null;index" json:"role_id"`
|
||||
PermissionID uuid.UUID `gorm:"type:uuid;not null;index" json:"permission_id"`
|
||||
|
||||
Role M_Role `gorm:"foreignKey:RoleID;references:ID"`
|
||||
Permission M_Permissions `gorm:"foreignKey:PermissionID;references:ID"`
|
||||
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package entities
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type M_User struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
Username string `gorm:"type:varchar(100);uniqueIndex;not null" json:"username"`
|
||||
Password string `gorm:"type:varchar(255);not null" json:"password"`
|
||||
Gender string `gorm:"type:varchar(10)" json:"gender"`
|
||||
Address string `gorm:"type:text" json:"address"`
|
||||
Phone string `gorm:"type:varchar(20);index" json:"phone"`
|
||||
Email string `gorm:"type:varchar(255);uniqueIndex;not null" json:"email"`
|
||||
PhotoUrl string `gorm:"type:varchar(255)" json:"photo_url"`
|
||||
ClientID uuid.UUID `gorm:"type:uuid;not null;index" json:"client_id"`
|
||||
MaintenanceGroupUserID uuid.UUID `gorm:"type:uuid;index" json:"maintenance_group_user_id"`
|
||||
LocationID uuid.UUID `gorm:"type:uuid;index" json:"location_id"`
|
||||
|
||||
Client M_Client `gorm:"foreignKey:ClientID;references:ID"`
|
||||
UserRoles []M_User_Role `gorm:"foreignKey:UserID;references:ID" json:"user_roles"`
|
||||
Roles []M_Role `gorm:"many2many:m_user_roles;foreignKey:ID;joinForeignKey:UserID;References:ID;JoinReferences:RoleID" json:"roles"`
|
||||
FullAuditTrail
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package entities
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type M_User_Role struct {
|
||||
// Timestamp
|
||||
UserID uuid.UUID `gorm:"type:uuid;not null;index" json:"user_id"`
|
||||
RoleID uuid.UUID `gorm:"type:uuid;not null;index" json:"role_id"`
|
||||
|
||||
User M_User `gorm:"foreignKey:UserID;references:ID"`
|
||||
Role M_Role `gorm:"foreignKey:RoleID;references:ID"`
|
||||
|
||||
Timestamp
|
||||
}
|
||||
|
|
@ -1,16 +1,16 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
// import (
|
||||
// "github.com/google/uuid"
|
||||
// )
|
||||
|
||||
type Product struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
Price float64 `gorm:"type:numeric;not null" json:"price"`
|
||||
// type Product struct {
|
||||
// ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
// Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
// Price float64 `gorm:"type:numeric;not null" json:"price"`
|
||||
|
||||
TenantID uuid.UUID `gorm:"type:uuid;null;index" json:"tenant_id"`
|
||||
Tenant Tenant `gorm:"foreignKey:TenantID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"tenant"`
|
||||
// TenantID uuid.UUID `gorm:"type:uuid;null;index" json:"tenant_id"`
|
||||
// Tenant Tenant `gorm:"foreignKey:TenantID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"tenant"`
|
||||
|
||||
Timestamp
|
||||
}
|
||||
// Timestamp
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
// import (
|
||||
// "github.com/google/uuid"
|
||||
// )
|
||||
|
||||
type Tenant struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
// type Tenant struct {
|
||||
// ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
// Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
|
||||
Products []Product `gorm:"foreignKey:TenantID" json:"products"`
|
||||
Users []User `gorm:"foreignKey:TenantID" json:"users"`
|
||||
Timestamp
|
||||
}
|
||||
// Products []Product `gorm:"foreignKey:TenantID" json:"products"`
|
||||
// Users []User `gorm:"foreignKey:TenantID" json:"users"`
|
||||
// Timestamp
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -1,58 +1,59 @@
|
|||
package entities
|
||||
|
||||
import (
|
||||
"github.com/Caknoooo/go-gin-clean-starter/pkg/helpers"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
// import (
|
||||
// "github.com/Caknoooo/go-gin-clean-starter/pkg/helpers"
|
||||
// "github.com/google/uuid"
|
||||
// "gorm.io/gorm"
|
||||
// )
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
Email string `gorm:"type:varchar(255);uniqueIndex;not null" json:"email"`
|
||||
TelpNumber string `gorm:"type:varchar(20);index" json:"telp_number"`
|
||||
Password string `gorm:"type:varchar(255);not null" json:"password"`
|
||||
Role string `gorm:"type:varchar(50);not null;default:'user'" json:"role"`
|
||||
ImageUrl string `gorm:"type:varchar(255)" json:"image_url"`
|
||||
IsVerified bool `gorm:"default:false" json:"is_verified"`
|
||||
// type User struct {
|
||||
// ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id"`
|
||||
// Name string `gorm:"type:varchar(100);not null" json:"name"`
|
||||
// Email string `gorm:"type:varchar(255);uniqueIndex;not null" json:"email"`
|
||||
// TelpNumber string `gorm:"type:varchar(20);index" json:"telp_number"`
|
||||
// Password string `gorm:"type:varchar(255);not null" json:"password"`
|
||||
// Role string `gorm:"type:varchar(50);not null;default:'user'" json:"role"`
|
||||
// ImageUrl string `gorm:"type:varchar(255)" json:"image_url"`
|
||||
// IsVerified bool `gorm:"default:false" json:"is_verified"`
|
||||
|
||||
TenantID uuid.UUID `gorm:"type:uuid;null;index" json:"tenant_id"`
|
||||
Tenant Tenant `gorm:"foreignKey:TenantID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"tenant"`
|
||||
// TenantID uuid.UUID `gorm:"type:uuid;null;index" json:"tenant_id"`
|
||||
// Tenant Tenant `gorm:"foreignKey:TenantID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"tenant"`
|
||||
// UserRoles []M_User_Role `gorm:"foreignKey:UserID"` // atau relasi many-to-many
|
||||
|
||||
Timestamp
|
||||
}
|
||||
// FullAuditTrail
|
||||
// }
|
||||
|
||||
// BeforeCreate hook to hash password and set defaults
|
||||
func (u *User) BeforeCreate(_ *gorm.DB) (err error) {
|
||||
// Hash password
|
||||
if u.Password != "" {
|
||||
u.Password, err = helpers.HashPassword(u.Password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// // BeforeCreate hook to hash password and set defaults
|
||||
// func (u *User) BeforeCreate(_ *gorm.DB) (err error) {
|
||||
// // Hash password
|
||||
// if u.Password != "" {
|
||||
// u.Password, err = helpers.HashPassword(u.Password)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
|
||||
// Ensure UUID is set
|
||||
if u.ID == uuid.Nil {
|
||||
u.ID = uuid.New()
|
||||
}
|
||||
// // Ensure UUID is set
|
||||
// if u.ID == uuid.Nil {
|
||||
// u.ID = uuid.New()
|
||||
// }
|
||||
|
||||
// Set default role if not specified
|
||||
if u.Role == "" {
|
||||
u.Role = "user"
|
||||
}
|
||||
// // Set default role if not specified
|
||||
// if u.Role == "" {
|
||||
// u.Role = "user"
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// BeforeUpdate hook to handle password updates
|
||||
func (u *User) BeforeUpdate(_ *gorm.DB) (err error) {
|
||||
// Only hash password if it has been changed
|
||||
if u.Password != "" {
|
||||
u.Password, err = helpers.HashPassword(u.Password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// // BeforeUpdate hook to handle password updates
|
||||
// func (u *User) BeforeUpdate(_ *gorm.DB) (err error) {
|
||||
// // Only hash password if it has been changed
|
||||
// if u.Password != "" {
|
||||
// u.Password, err = helpers.HashPassword(u.Password)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -11,10 +11,11 @@ func Migrate(db *gorm.DB) error {
|
|||
&entities.M_User{},
|
||||
&entities.RefreshToken{},
|
||||
&entities.M_Menu{},
|
||||
&entities.M_MaintenanceGroup{},
|
||||
&entities.M_Role{},
|
||||
&entities.M_Role_Menu{},
|
||||
&entities.M_Permissions{},
|
||||
&entities.M_MaintenanceGroup{},
|
||||
&entities.M_User_Role{},
|
||||
&entities.M_Role_Menu{},
|
||||
&entities.M_Role_Permission{},
|
||||
); err != nil {
|
||||
return err
|
||||
|
|
@ -28,16 +29,14 @@ func MigrateFresh(db *gorm.DB) error {
|
|||
if err := db.Migrator().DropTable(
|
||||
&entities.M_Client{},
|
||||
&entities.M_User{},
|
||||
&entities.M_Menu{},
|
||||
&entities.M_MaintenanceGroup{},
|
||||
&entities.M_Role{},
|
||||
&entities.M_Role_Menu{},
|
||||
&entities.M_Permissions{},
|
||||
&entities.M_Role_Permission{},
|
||||
&entities.Tenant{},
|
||||
&entities.User{},
|
||||
&entities.RefreshToken{},
|
||||
&entities.Product{},
|
||||
&entities.M_Menu{},
|
||||
&entities.M_Role{},
|
||||
&entities.M_Permissions{},
|
||||
&entities.M_MaintenanceGroup{},
|
||||
&entities.M_User_Role{},
|
||||
&entities.M_Role_Menu{},
|
||||
&entities.M_Role_Permission{},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,8 @@ func Seeder(db *gorm.DB) error {
|
|||
seeders := []func(*gorm.DB) error{
|
||||
seeds.ListClientSeeder,
|
||||
seeds.ListUserSeeder,
|
||||
seeds.ListRoleSeeder,
|
||||
seeds.ListUserRoleSeeder,
|
||||
}
|
||||
|
||||
for _, seeder := range seeders {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
[
|
||||
{
|
||||
"id": "a9e7c8b2-4f1a-4e6d-9c3e-1b2a3c4d5e6f",
|
||||
"name": "Super Admin",
|
||||
"description": "Super Administrator role",
|
||||
"icon_url": "https://example.com/logo-super-admin.png",
|
||||
"type": "system",
|
||||
"home_url": "/admin/dashboard",
|
||||
"client_name": "PT Teknologi Maju Indonesia"
|
||||
},
|
||||
{
|
||||
"id": "a9e7c8b2-4f1a-4e6d-9c3e-1b2a3c4d5e6f",
|
||||
"name": "Admin",
|
||||
"description": "Administrator role",
|
||||
"icon_url": "https://example.com/logo-admin.png",
|
||||
"type": "system",
|
||||
"home_url": "/admin/dashboard",
|
||||
"client_name": "PT Teknologi Maju Indonesia"
|
||||
},
|
||||
{
|
||||
"id": "b7d6e5c4-3a2b-4c1d-8e9f-0a1b2c3d4e5f",
|
||||
"name": "User",
|
||||
"description": "Regular user role",
|
||||
"icon_url": "https://example.com/logo-user.png",
|
||||
"type": "system",
|
||||
"home_url": "/user/home",
|
||||
"client_name": "PT Teknologi Maju Indonesia"
|
||||
}
|
||||
]
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
[
|
||||
{
|
||||
"name": "Super Admin",
|
||||
"role_name": "Super Admin"
|
||||
},
|
||||
{
|
||||
"name": "Admin",
|
||||
"role_name": "Admin"
|
||||
},
|
||||
{
|
||||
"name": "User",
|
||||
"role_name": "User"
|
||||
}
|
||||
]
|
||||
|
|
@ -1,74 +1,23 @@
|
|||
package seeds
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
// import (
|
||||
// "encoding/json"
|
||||
// "errors"
|
||||
// "io"
|
||||
// "os"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
// "github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
// "gorm.io/gorm"
|
||||
// )
|
||||
|
||||
type ProductSeed struct {
|
||||
Name string `json:"name"`
|
||||
Price float64 `json:"price"`
|
||||
TenantName string `json:"tenant_name"`
|
||||
}
|
||||
// type ProductSeed struct {
|
||||
// Name string `json:"name"`
|
||||
// Price float64 `json:"price"`
|
||||
// TenantName string `json:"tenant_name"`
|
||||
// }
|
||||
|
||||
func ListProductSeeder(db *gorm.DB) error {
|
||||
jsonFile, err := os.Open("./database/seeders/json/products.json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
jsonData, err := io.ReadAll(jsonFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var listProduct []ProductSeed
|
||||
if err := json.Unmarshal(jsonData, &listProduct); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hasTable := db.Migrator().HasTable(&entities.Product{})
|
||||
if !hasTable {
|
||||
if err := db.Migrator().CreateTable(&entities.Product{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, data := range listProduct {
|
||||
var tenant entities.Tenant
|
||||
if err := db.Where("name = ?", data.TenantName).First(&tenant).Error; err != nil {
|
||||
return err // tenant tidak ditemukan
|
||||
}
|
||||
|
||||
var product entities.Product
|
||||
err := db.Where(&entities.Product{Name: data.Name}).First(&product).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
|
||||
isData := db.Find(&product, "name = ?", data.Name).RowsAffected
|
||||
if isData == 0 {
|
||||
newProduct := entities.Product{
|
||||
Name: data.Name,
|
||||
Price: data.Price,
|
||||
TenantID: tenant.ID,
|
||||
}
|
||||
if err := db.Create(&newProduct).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// func ListUserSeeder(db *gorm.DB) error {
|
||||
// jsonFile, err := os.Open("./database/seeders/json/users.json")
|
||||
// func ListProductSeeder(db *gorm.DB) error {
|
||||
// jsonFile, err := os.Open("./database/seeders/json/products.json")
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
|
@ -78,28 +27,38 @@ func ListProductSeeder(db *gorm.DB) error {
|
|||
// return err
|
||||
// }
|
||||
|
||||
// var listUser []entities.User
|
||||
// if err := json.Unmarshal(jsonData, &listUser); err != nil {
|
||||
// var listProduct []ProductSeed
|
||||
// if err := json.Unmarshal(jsonData, &listProduct); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// hasTable := db.Migrator().HasTable(&entities.User{})
|
||||
// hasTable := db.Migrator().HasTable(&entities.Product{})
|
||||
// if !hasTable {
|
||||
// if err := db.Migrator().CreateTable(&entities.User{}); err != nil {
|
||||
// if err := db.Migrator().CreateTable(&entities.Product{}); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
|
||||
// for _, data := range listUser {
|
||||
// var user entities.User
|
||||
// err := db.Where(&entities.User{Email: data.Email}).First(&user).Error
|
||||
// for _, data := range listProduct {
|
||||
// var tenant entities.Tenant
|
||||
// if err := db.Where("name = ?", data.TenantName).First(&tenant).Error; err != nil {
|
||||
// return err // tenant tidak ditemukan
|
||||
// }
|
||||
|
||||
// var product entities.Product
|
||||
// err := db.Where(&entities.Product{Name: data.Name}).First(&product).Error
|
||||
// if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// isData := db.Find(&user, "email = ?", data.Email).RowsAffected
|
||||
// isData := db.Find(&product, "name = ?", data.Name).RowsAffected
|
||||
// if isData == 0 {
|
||||
// if err := db.Create(&data).Error; err != nil {
|
||||
// newProduct := entities.Product{
|
||||
// Name: data.Name,
|
||||
// Price: data.Price,
|
||||
// TenantID: tenant.ID,
|
||||
// }
|
||||
// if err := db.Create(&newProduct).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
|
|
@ -107,3 +66,44 @@ func ListProductSeeder(db *gorm.DB) error {
|
|||
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// // func ListUserSeeder(db *gorm.DB) error {
|
||||
// // jsonFile, err := os.Open("./database/seeders/json/users.json")
|
||||
// // if err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
|
||||
// // jsonData, err := io.ReadAll(jsonFile)
|
||||
// // if err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
|
||||
// // var listUser []entities.User
|
||||
// // if err := json.Unmarshal(jsonData, &listUser); err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
|
||||
// // hasTable := db.Migrator().HasTable(&entities.User{})
|
||||
// // if !hasTable {
|
||||
// // if err := db.Migrator().CreateTable(&entities.User{}); err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// // for _, data := range listUser {
|
||||
// // var user entities.User
|
||||
// // err := db.Where(&entities.User{Email: data.Email}).First(&user).Error
|
||||
// // if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// // return err
|
||||
// // }
|
||||
|
||||
// // isData := db.Find(&user, "email = ?", data.Email).RowsAffected
|
||||
// // if isData == 0 {
|
||||
// // if err := db.Create(&data).Error; err != nil {
|
||||
// // return err
|
||||
// // }
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// // return nil
|
||||
// // }
|
||||
|
|
|
|||
|
|
@ -0,0 +1,79 @@
|
|||
package seeds
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type RoleSeed struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
IconUrl string `json:"icon_url"`
|
||||
Type string `json:"type"`
|
||||
HomeUrl string `json:"home_url"`
|
||||
ClientName string `json:"client_name"`
|
||||
}
|
||||
|
||||
func ListRoleSeeder(db *gorm.DB) error {
|
||||
jsonFile, err := os.Open("./database/seeders/json/roles.json")
|
||||
if err != nil {
|
||||
fmt.Println("Error opening file:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
jsonData, err := io.ReadAll(jsonFile)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading file:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
var listRole []RoleSeed
|
||||
if err := json.Unmarshal(jsonData, &listRole); err != nil {
|
||||
fmt.Println("Error unmarshalling JSON:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
hasTable := db.Migrator().HasTable(&entities.M_Role{})
|
||||
if !hasTable {
|
||||
fmt.Println("Creating table: M_Role")
|
||||
if err := db.Migrator().CreateTable(&entities.M_Role{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for _, data := range listRole {
|
||||
var role entities.M_Role
|
||||
var client entities.M_Client
|
||||
|
||||
if err := db.Where("name = ?", data.ClientName).First(&client).Error; err != nil {
|
||||
fmt.Println("Client not found:", err)
|
||||
return err // client tidak ditemukan
|
||||
}
|
||||
|
||||
// err := db.Where(&entities.M_Role{Name: data.Name}).First(&role).Error
|
||||
// if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// fmt.Println("Error querying role:", err)
|
||||
// return err
|
||||
// }
|
||||
|
||||
if err := db.FirstOrCreate(&role, entities.M_Role{
|
||||
Name: data.Name,
|
||||
Description: data.Description,
|
||||
IconUrl: data.IconUrl,
|
||||
Type: data.Type,
|
||||
HomeUrl: data.HomeUrl,
|
||||
ClientID: client.ID,
|
||||
}).Error; err != nil {
|
||||
fmt.Println("Error seeding role:", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -1,52 +1,52 @@
|
|||
package seeds
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
// import (
|
||||
// "encoding/json"
|
||||
// "errors"
|
||||
// "io"
|
||||
// "os"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
// "github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
// "gorm.io/gorm"
|
||||
// )
|
||||
|
||||
func ListTenantSeeder(db *gorm.DB) error {
|
||||
jsonFile, err := os.Open("./database/seeders/json/tenants.json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// func ListTenantSeeder(db *gorm.DB) error {
|
||||
// jsonFile, err := os.Open("./database/seeders/json/tenants.json")
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
jsonData, err := io.ReadAll(jsonFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// jsonData, err := io.ReadAll(jsonFile)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
var listTenant []entities.Tenant
|
||||
if err := json.Unmarshal(jsonData, &listTenant); err != nil {
|
||||
return err
|
||||
}
|
||||
// var listTenant []entities.Tenant
|
||||
// if err := json.Unmarshal(jsonData, &listTenant); err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
hasTable := db.Migrator().HasTable(&entities.Tenant{})
|
||||
if !hasTable {
|
||||
if err := db.Migrator().CreateTable(&entities.Tenant{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// hasTable := db.Migrator().HasTable(&entities.Tenant{})
|
||||
// if !hasTable {
|
||||
// if err := db.Migrator().CreateTable(&entities.Tenant{}); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
|
||||
for _, data := range listTenant {
|
||||
var tenant entities.Tenant
|
||||
err := db.Where(&entities.Tenant{Name: data.Name}).First(&tenant).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
// for _, data := range listTenant {
|
||||
// var tenant entities.Tenant
|
||||
// err := db.Where(&entities.Tenant{Name: data.Name}).First(&tenant).Error
|
||||
// if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
// return err
|
||||
// }
|
||||
|
||||
isData := db.Find(&tenant, "name = ?", data.Name).RowsAffected
|
||||
if isData == 0 {
|
||||
if err := db.Create(&data).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
// isData := db.Find(&tenant, "name = ?", data.Name).RowsAffected
|
||||
// if isData == 0 {
|
||||
// if err := db.Create(&data).Error; err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
// return nil
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -0,0 +1,58 @@
|
|||
package seeds
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserRoleSeed struct {
|
||||
UserName string `json:"name"`
|
||||
RoleName string `json:"role_name"`
|
||||
}
|
||||
|
||||
func ListUserRoleSeeder(db *gorm.DB) error {
|
||||
jsonFile, err := os.Open("./database/seeders/json/users_roles.json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
jsonData, err := io.ReadAll(jsonFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var listUserRole []UserRoleSeed
|
||||
if err := json.Unmarshal(jsonData, &listUserRole); err != nil {
|
||||
return err
|
||||
}
|
||||
hasTable := db.Migrator().HasTable(&entities.M_User_Role{})
|
||||
if !hasTable {
|
||||
if err := db.Migrator().CreateTable(&entities.M_User_Role{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, data := range listUserRole {
|
||||
var user entities.M_User
|
||||
var role entities.M_Role
|
||||
|
||||
if err := db.Where("name = ?", data.UserName).First(&user).Error; err != nil {
|
||||
return err // user tidak ditemukan
|
||||
}
|
||||
if err := db.Where("name = ?", data.RoleName).First(&role).Error; err != nil {
|
||||
return err // role tidak ditemukan
|
||||
}
|
||||
var userRole entities.M_User_Role
|
||||
if err := db.FirstOrCreate(&userRole, entities.M_User_Role{
|
||||
UserID: user.ID,
|
||||
RoleID: role.ID,
|
||||
}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -3,7 +3,6 @@ package seeds
|
|||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
|
|
@ -49,22 +48,18 @@ func ListUserSeeder(db *gorm.DB) error {
|
|||
|
||||
for _, data := range listUser {
|
||||
var client entities.M_Client
|
||||
fmt.Println("Finding client: ", data.ClientName)
|
||||
if err := db.Where("name = ?", data.ClientName).First(&client).Error; err != nil {
|
||||
fmt.Println("Client not found:", err)
|
||||
return err // client tidak ditemukan
|
||||
}
|
||||
|
||||
var user entities.M_User
|
||||
err := db.Where(&entities.M_User{Email: data.Email}).First(&user).Error
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
fmt.Println("Error querying user:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
isData := db.Find(&user, "email = ?", data.Email).RowsAffected
|
||||
if isData == 0 {
|
||||
fmt.Println("Seeding user:", data.Email)
|
||||
passwordEncrypted, err := utils.HashPassword(data.Password)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
Loading…
Reference in New Issue