feat: Add maintenance groups and roles with corresponding seeders and migrations
This commit is contained in:
parent
04cfb9719b
commit
b4554ff5f2
|
|
@ -10,6 +10,7 @@ type M_MaintenanceGroup struct {
|
||||||
ClientID uuid.UUID `gorm:"type:uuid;not null;index" json:"client_id"`
|
ClientID uuid.UUID `gorm:"type:uuid;not null;index" json:"client_id"`
|
||||||
|
|
||||||
Client M_Client `gorm:"foreignKey:ClientID;references:ID"`
|
Client M_Client `gorm:"foreignKey:ClientID;references:ID"`
|
||||||
|
MaintenanceGroupRoles []M_MaintenanceGroupRole `gorm:"foreignKey:MaintenanceGroupID;references:ID" json:"maintenance_group_roles"`
|
||||||
|
|
||||||
FullAuditTrail
|
FullAuditTrail
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,12 @@ package entities
|
||||||
import "github.com/google/uuid"
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
type M_MaintenanceGroupRole struct {
|
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"`
|
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"`
|
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"`
|
Level int `gorm:"not null" json:"level"`
|
||||||
|
|
||||||
|
MaintenanceGroup M_MaintenanceGroup `gorm:"foreignKey:MaintenanceGroupID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"maintenance_group"`
|
||||||
|
Role M_Role `gorm:"foreignKey:RoleID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"role"`
|
||||||
|
|
||||||
FullAuditTrail
|
FullAuditTrail
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ type M_Role struct {
|
||||||
UserRoles []M_User_Role `gorm:"foreignKey:RoleID;references:ID" json:"user_roles"`
|
UserRoles []M_User_Role `gorm:"foreignKey:RoleID;references:ID" json:"user_roles"`
|
||||||
Users []M_User `gorm:"many2many:m_user_roles;" json:"users"`
|
Users []M_User `gorm:"many2many:m_user_roles;" json:"users"`
|
||||||
Permissions []M_Permissions `gorm:"many2many:m_role_permissions;joinForeignKey:RoleID;JoinReferences:PermissionID" json:"permissions"`
|
Permissions []M_Permissions `gorm:"many2many:m_role_permissions;joinForeignKey:RoleID;JoinReferences:PermissionID" json:"permissions"`
|
||||||
|
MaintenanceGroupRoles []M_MaintenanceGroupRole `gorm:"foreignKey:RoleID;references:ID" json:"maintenance_group_roles"`
|
||||||
|
|
||||||
FullAuditTrail
|
FullAuditTrail
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,12 @@ func Migrate(db *gorm.DB) error {
|
||||||
&entities.M_Menu{},
|
&entities.M_Menu{},
|
||||||
&entities.M_Role{},
|
&entities.M_Role{},
|
||||||
&entities.M_Permissions{},
|
&entities.M_Permissions{},
|
||||||
&entities.M_MaintenanceGroup{},
|
|
||||||
&entities.M_User_Role{},
|
&entities.M_User_Role{},
|
||||||
&entities.M_Role_Menu{},
|
&entities.M_Role_Menu{},
|
||||||
|
&entities.M_Menu_Client{},
|
||||||
&entities.M_Role_Permission{},
|
&entities.M_Role_Permission{},
|
||||||
|
&entities.M_MaintenanceGroup{},
|
||||||
|
&entities.M_MaintenanceGroupRole{},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -33,10 +35,12 @@ func MigrateFresh(db *gorm.DB) error {
|
||||||
&entities.M_Menu{},
|
&entities.M_Menu{},
|
||||||
&entities.M_Role{},
|
&entities.M_Role{},
|
||||||
&entities.M_Permissions{},
|
&entities.M_Permissions{},
|
||||||
&entities.M_MaintenanceGroup{},
|
|
||||||
&entities.M_User_Role{},
|
&entities.M_User_Role{},
|
||||||
&entities.M_Role_Menu{},
|
&entities.M_Role_Menu{},
|
||||||
|
&entities.M_Menu_Client{},
|
||||||
&entities.M_Role_Permission{},
|
&entities.M_Role_Permission{},
|
||||||
|
&entities.M_MaintenanceGroup{},
|
||||||
|
&entities.M_MaintenanceGroupRole{},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@ func Seeder(db *gorm.DB) error {
|
||||||
seeds.ListRolePermissionSeeder,
|
seeds.ListRolePermissionSeeder,
|
||||||
seeds.ListRoleMenuSeeder,
|
seeds.ListRoleMenuSeeder,
|
||||||
seeds.ListMenuClientSeeder,
|
seeds.ListMenuClientSeeder,
|
||||||
|
seeds.ListMaintenanceGroupSeeder,
|
||||||
|
seeds.ListMaintenanceGroupRoleSeeder,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, seeder := range seeders {
|
for _, seeder := range seeders {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"id": "9a7c4b2e-1111-4f3a-b88a-111111111111",
|
||||||
|
"code": "GRP-FORKLIFT",
|
||||||
|
"name": "Perawatan Forklift & Pallet Jack",
|
||||||
|
"description": "Pemeliharaan rutin unit forklift, pallet jack, dan battery charger",
|
||||||
|
"client_name": "PT Teknologi Maju Indonesia"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "8b9f2d4a-2222-4c5b-b777-222222222222",
|
||||||
|
"code": "GRP-CONVEYOR",
|
||||||
|
"name": "Perawatan Conveyor & Sortation System",
|
||||||
|
"description": "Perawatan belt conveyor, sensor, dan motor drive area inbound dan outbound",
|
||||||
|
"client_name": "PT Teknologi Maju Indonesia"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "7d8e1f3a-3333-4a9c-b666-333333333333",
|
||||||
|
"code": "GRP-RACK",
|
||||||
|
"name": "Perawatan Rak & Struktur Gudang",
|
||||||
|
"description": "Pemeriksaan integritas struktur rak, sambungan baut, dan sistem safety",
|
||||||
|
"client_name": "PT Teknologi Maju Indonesia"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "6e9f2c1b-4444-4d8b-b555-444444444444",
|
||||||
|
"code": "GRP-IT",
|
||||||
|
"name": "Perawatan Perangkat IT Gudang",
|
||||||
|
"description": "Scanner, printer label, RFID reader, dan komputer stasiun kerja",
|
||||||
|
"client_name": "PT Teknologi Maju Indonesia"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "5f1a3e2c-5555-4b9d-b444-555555555555",
|
||||||
|
"code": "GRP-FACILITY",
|
||||||
|
"name": "Maintenance Fasilitas Umum",
|
||||||
|
"description": "Perawatan sistem pendingin, penerangan, dan jalur listrik gudang",
|
||||||
|
"client_name": "PT Teknologi Maju Indonesia"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"maintenance_group_id": "9a7c4b2e-1111-4f3a-b88a-111111111111",
|
||||||
|
"role_id": "a9e7c8b2-4f1a-4e6d-9c3e-1b2a3c4d5e6f",
|
||||||
|
"level": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"maintenance_group_id": "9a7c4b2e-1111-4f3a-b88a-111111111111",
|
||||||
|
"role_id": "b7d6e5c4-3a2b-4c1d-8e9f-0a1b2c3d4e5f",
|
||||||
|
"level": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"maintenance_group_id": "8b9f2d4a-2222-4c5b-b777-222222222222",
|
||||||
|
"role_id": "b7d6e5c4-3a2b-4c1d-8e9f-0a1b2c3d4e5f",
|
||||||
|
"level": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"maintenance_group_id": "7d8e1f3a-3333-4a9c-b666-333333333333",
|
||||||
|
"role_id": "a9e7c8b2-4f1a-4e6d-9c3e-1b2a3c4d5e6f",
|
||||||
|
"level": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"maintenance_group_id": "6e9f2c1b-4444-4d8b-b555-444444444444",
|
||||||
|
"role_id": "a9e7c8b2-4f1a-4e6d-9c3e-1b2a3c4d5e6f",
|
||||||
|
"level": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"id": "a9e7c8b2-4f1a-4e6d-9c3e-1b2a3c4d5e6f",
|
"id": "a9e7c8b2-4f1a-4e6d-9c3e-1b2a3c4d5e4f",
|
||||||
"name": "Super Admin",
|
"name": "Super Admin",
|
||||||
"description": "Super Administrator role",
|
"description": "Super Administrator role",
|
||||||
"icon_url": "https://example.com/logo-super-admin.png",
|
"icon_url": "https://example.com/logo-super-admin.png",
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
package seeds
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MaintenanceGroupRoleSeed struct {
|
||||||
|
MaintenanceGroupID string `json:"maintenance_group_id"`
|
||||||
|
RoleID string `json:"role_id"`
|
||||||
|
Level int `json:"level"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListMaintenanceGroupRoleSeeder(db *gorm.DB) error {
|
||||||
|
jsonFile, err := os.Open("./database/seeders/json/maintenance_group_roles.json")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := io.ReadAll(jsonFile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var listMaintenanceGroupRole []MaintenanceGroupRoleSeed
|
||||||
|
if err := json.Unmarshal(jsonData, &listMaintenanceGroupRole); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
hasTable := db.Migrator().HasTable(&entities.M_MaintenanceGroupRole{})
|
||||||
|
if !hasTable {
|
||||||
|
if err := db.Migrator().CreateTable(&entities.M_MaintenanceGroupRole{}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, data := range listMaintenanceGroupRole {
|
||||||
|
var maintenanceGroup entities.M_MaintenanceGroup
|
||||||
|
var role entities.M_Role
|
||||||
|
if err := db.Where("id = ?", data.MaintenanceGroupID).First(&maintenanceGroup).Error; err != nil {
|
||||||
|
return err // maintenance group tidak ditemukan
|
||||||
|
}
|
||||||
|
if err := db.Where("id = ?", data.RoleID).First(&role).Error; err != nil {
|
||||||
|
return err // role tidak ditemukan
|
||||||
|
}
|
||||||
|
var maintenanceGroupRole entities.M_MaintenanceGroupRole
|
||||||
|
if err := db.FirstOrCreate(&maintenanceGroupRole, entities.M_MaintenanceGroupRole{
|
||||||
|
MaintenanceGroupID: maintenanceGroup.ID,
|
||||||
|
RoleID: role.ID,
|
||||||
|
Level: data.Level,
|
||||||
|
}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,77 @@
|
||||||
|
package seeds
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MaintenanceGroupSeed struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Code string `json:"code"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
ClientName string `json:"client_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListMaintenanceGroupSeeder(db *gorm.DB) error {
|
||||||
|
jsonFile, err := os.Open("./database/seeders/json/maintenance_group.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 listMaintenanceGroup []MaintenanceGroupSeed
|
||||||
|
if err := json.Unmarshal(jsonData, &listMaintenanceGroup); err != nil {
|
||||||
|
fmt.Println("Error unmarshalling JSON:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hasTable := db.Migrator().HasTable(&entities.M_MaintenanceGroup{})
|
||||||
|
if !hasTable {
|
||||||
|
fmt.Println("Creating table: M_MaintenanceGroup")
|
||||||
|
if err := db.Migrator().CreateTable(&entities.M_MaintenanceGroup{}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, data := range listMaintenanceGroup {
|
||||||
|
var maintenanceGroup entities.M_MaintenanceGroup
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedUUID, err := uuid.Parse(data.ID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Invalid UUID:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.FirstOrCreate(&maintenanceGroup, entities.M_MaintenanceGroup{
|
||||||
|
ID: parsedUUID,
|
||||||
|
Code: data.Code,
|
||||||
|
Name: data.Name,
|
||||||
|
Description: data.Description,
|
||||||
|
ClientID: client.ID,
|
||||||
|
}).Error; err != nil {
|
||||||
|
fmt.Println("Error seeding maintenance group:", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||||
|
"github.com/google/uuid"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -56,13 +57,14 @@ func ListRoleSeeder(db *gorm.DB) error {
|
||||||
return err // client tidak ditemukan
|
return err // client tidak ditemukan
|
||||||
}
|
}
|
||||||
|
|
||||||
// err := db.Where(&entities.M_Role{Name: data.Name}).First(&role).Error
|
parsedUUID, err := uuid.Parse(data.ID)
|
||||||
// if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
if err != nil {
|
||||||
// fmt.Println("Error querying role:", err)
|
fmt.Println("Invalid UUID:", err)
|
||||||
// return err
|
return err
|
||||||
// }
|
}
|
||||||
|
|
||||||
if err := db.FirstOrCreate(&role, entities.M_Role{
|
if err := db.FirstOrCreate(&role, entities.M_Role{
|
||||||
|
ID: parsedUUID,
|
||||||
Name: data.Name,
|
Name: data.Name,
|
||||||
Description: data.Description,
|
Description: data.Description,
|
||||||
IconUrl: data.IconUrl,
|
IconUrl: data.IconUrl,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue