init push
This commit is contained in:
33
internal/domain/fleet_status/const.go
Normal file
33
internal/domain/fleet_status/const.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package fleetstatus
|
||||
|
||||
const (
|
||||
InspectionTypeAF = "A/F"
|
||||
InspectionTypeENG1 = "ENG1"
|
||||
InspectionTypeENG2 = "ENG2"
|
||||
InspectionTypeMAIN1 = "MAIN1"
|
||||
InspectionTypeMAIN2 = "MAIN2"
|
||||
|
||||
FleetStatusFileRefType = "fleet_status"
|
||||
|
||||
StatusActive = "active"
|
||||
StatusServiced = "serviced"
|
||||
FileCategoryWB = "wb"
|
||||
FileCategoryBFI = "bfi"
|
||||
)
|
||||
|
||||
func IsValidFileCategory(s string) bool {
|
||||
switch s {
|
||||
case FileCategoryWB, FileCategoryBFI:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var InspectionTypeOrder = []string{
|
||||
InspectionTypeAF,
|
||||
InspectionTypeENG1,
|
||||
InspectionTypeENG2,
|
||||
InspectionTypeMAIN1,
|
||||
InspectionTypeMAIN2,
|
||||
}
|
||||
107
internal/domain/fleet_status/model.go
Normal file
107
internal/domain/fleet_status/model.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package fleetstatus
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
filemanager "wucher/internal/domain/file_manager"
|
||||
"wucher/internal/domain/helicopter"
|
||||
"wucher/internal/shared/pkg/uuidv7"
|
||||
)
|
||||
|
||||
type FleetStatus struct {
|
||||
ID []byte `gorm:"type:binary(16);primaryKey;column:id"`
|
||||
HelicopterID []byte `gorm:"type:binary(16);not null;index;column:helicopter_id"`
|
||||
Helicopter *helicopter.Helicopter `gorm:"foreignKey:HelicopterID;references:ID"`
|
||||
Status string `gorm:"type:varchar(16);not null;default:active;index;column:status"`
|
||||
ServicedAt *time.Time `gorm:"column:serviced_at;index"`
|
||||
|
||||
MaintenanceSchedules []MaintenanceSchedule `gorm:"foreignKey:FleetStatusID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
||||
Files []FleetStatusFile `gorm:"foreignKey:FleetStatusID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
||||
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
CreatedBy []byte `gorm:"type:binary(16);column:created_by"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
UpdatedBy []byte `gorm:"type:binary(16);column:updated_by"`
|
||||
DeletedAt *time.Time `gorm:"column:deleted_at;index"`
|
||||
DeletedBy []byte `gorm:"type:binary(16);column:deleted_by"`
|
||||
}
|
||||
|
||||
func (FleetStatus) TableName() string { return "fleet_statuses" }
|
||||
|
||||
func (f *FleetStatus) BeforeCreate(_ *gorm.DB) error {
|
||||
if len(f.ID) == 0 {
|
||||
f.ID = uuidv7.MustBytes()
|
||||
}
|
||||
if f.Status == "" {
|
||||
f.Status = StatusActive
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type MaintenanceSchedule struct {
|
||||
ID []byte `gorm:"type:binary(16);primaryKey;column:id"`
|
||||
FleetStatusID []byte `gorm:"type:binary(16);not null;index;column:fleet_status_id"`
|
||||
InspectionType string `gorm:"type:varchar(16);not null;column:inspection_type"`
|
||||
Type string `gorm:"type:varchar(255);not null;column:type"`
|
||||
Due string `gorm:"type:varchar(255);column:due"`
|
||||
Ext string `gorm:"type:varchar(255);column:ext"`
|
||||
|
||||
FleetStatus *FleetStatus `gorm:"foreignKey:FleetStatusID;references:ID"`
|
||||
}
|
||||
|
||||
func (MaintenanceSchedule) TableName() string { return "maintenance_schedules" }
|
||||
|
||||
func (m *MaintenanceSchedule) BeforeCreate(_ *gorm.DB) error {
|
||||
if len(m.ID) == 0 {
|
||||
m.ID = uuidv7.MustBytes()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type FleetStatusFile struct {
|
||||
ID []byte `gorm:"type:binary(16);primaryKey;column:id"`
|
||||
IsMandatory bool `gorm:"not null;default:false;column:is_mandatory"`
|
||||
FleetStatusID []byte `gorm:"type:binary(16);not null;index;column:fleet_status_id"`
|
||||
FileAttachmentID []byte `gorm:"type:binary(16);not null;index;column:file_attachment_id"`
|
||||
Category string `gorm:"type:varchar(64);index;column:category"`
|
||||
|
||||
FileID []byte `gorm:"-"`
|
||||
|
||||
FleetStatus *FleetStatus `gorm:"foreignKey:FleetStatusID;references:ID"`
|
||||
Attachment *filemanager.Attachment `gorm:"foreignKey:FileAttachmentID;references:ID"`
|
||||
}
|
||||
|
||||
func (FleetStatusFile) TableName() string { return "fleet_status_files" }
|
||||
|
||||
func (f *FleetStatusFile) BeforeCreate(_ *gorm.DB) error {
|
||||
if len(f.ID) == 0 {
|
||||
f.ID = uuidv7.MustBytes()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type FleetStatusServiceLog struct {
|
||||
ID []byte `gorm:"type:binary(16);primaryKey;column:id"`
|
||||
FleetStatusID []byte `gorm:"type:binary(16);not null;index;column:fleet_status_id"`
|
||||
HelicopterID []byte `gorm:"type:binary(16);not null;index;column:helicopter_id"`
|
||||
Helicopter *helicopter.Helicopter `gorm:"foreignKey:HelicopterID;references:ID"`
|
||||
InspectionType string `gorm:"type:varchar(16);not null;column:inspection_type"`
|
||||
Type string `gorm:"type:varchar(255);not null;column:type"`
|
||||
Due string `gorm:"type:varchar(255);column:due"`
|
||||
Ext string `gorm:"type:varchar(255);column:ext"`
|
||||
ServicedAt time.Time `gorm:"column:serviced_at;index"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
CreatedBy []byte `gorm:"type:binary(16);column:created_by"`
|
||||
}
|
||||
|
||||
func (FleetStatusServiceLog) TableName() string { return "fleet_status_service_logs" }
|
||||
|
||||
func (f *FleetStatusServiceLog) BeforeCreate(_ *gorm.DB) error {
|
||||
if len(f.ID) == 0 {
|
||||
f.ID = uuidv7.MustBytes()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
68
internal/domain/fleet_status/model_test.go
Normal file
68
internal/domain/fleet_status/model_test.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package fleetstatus
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestTableNames(t *testing.T) {
|
||||
if (FleetStatus{}).TableName() != "fleet_statuses" {
|
||||
t.Error("FleetStatus table name")
|
||||
}
|
||||
if (MaintenanceSchedule{}).TableName() != "maintenance_schedules" {
|
||||
t.Error("MaintenanceSchedule table name")
|
||||
}
|
||||
if (FleetStatusFile{}).TableName() != "fleet_status_files" {
|
||||
t.Error("FleetStatusFile table name")
|
||||
}
|
||||
if (FleetStatusServiceLog{}).TableName() != "fleet_status_service_logs" {
|
||||
t.Error("FleetStatusServiceLog table name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFleetStatusBeforeCreate(t *testing.T) {
|
||||
f := &FleetStatus{}
|
||||
if err := f.BeforeCreate(nil); err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if len(f.ID) == 0 {
|
||||
t.Fatal("ID should be set")
|
||||
}
|
||||
if f.Status != StatusActive {
|
||||
t.Fatalf("Status default = %q, want %q", f.Status, StatusActive)
|
||||
}
|
||||
// existing ID + status preserved
|
||||
f2 := &FleetStatus{Status: "serviced"}
|
||||
_ = f2.BeforeCreate(nil)
|
||||
if f2.Status != "serviced" {
|
||||
t.Fatal("BeforeCreate must not overwrite an existing status")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChildBeforeCreate(t *testing.T) {
|
||||
m := &MaintenanceSchedule{}
|
||||
_ = m.BeforeCreate(nil)
|
||||
if len(m.ID) == 0 {
|
||||
t.Error("MaintenanceSchedule ID not set")
|
||||
}
|
||||
fsf := &FleetStatusFile{}
|
||||
_ = fsf.BeforeCreate(nil)
|
||||
if len(fsf.ID) == 0 {
|
||||
t.Error("FleetStatusFile ID not set")
|
||||
}
|
||||
sl := &FleetStatusServiceLog{}
|
||||
_ = sl.BeforeCreate(nil)
|
||||
if len(sl.ID) == 0 {
|
||||
t.Error("FleetStatusServiceLog ID not set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidFileCategory(t *testing.T) {
|
||||
for _, c := range []string{FileCategoryWB, FileCategoryBFI} {
|
||||
if !IsValidFileCategory(c) {
|
||||
t.Fatalf("%q should be valid", c)
|
||||
}
|
||||
}
|
||||
for _, c := range []string{"WB", "", "nope"} {
|
||||
if IsValidFileCategory(c) {
|
||||
t.Fatalf("%q should be invalid", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
19
internal/domain/fleet_status/repository.go
Normal file
19
internal/domain/fleet_status/repository.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package fleetstatus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
Create(ctx context.Context, row *FleetStatus) error
|
||||
Update(ctx context.Context, row *FleetStatus) error
|
||||
Delete(ctx context.Context, id []byte, deletedBy []byte) error
|
||||
GetByID(ctx context.Context, id []byte) (*FleetStatus, error)
|
||||
List(ctx context.Context, filter, sort string, limit, offset int) ([]FleetStatus, int64, error)
|
||||
LatestByHelicopterIDs(ctx context.Context, helicopterIDs [][]byte) (map[string]*FleetStatus, error)
|
||||
MarkServiced(ctx context.Context, id []byte, servicedAt time.Time, actorID []byte) error
|
||||
ListServiceHistoryByHelicopterID(ctx context.Context, helicopterID []byte, limit, offset int) ([]FleetStatusServiceLog, int64, error)
|
||||
HelicopterIDByMissionID(ctx context.Context, missionID []byte) ([]byte, error)
|
||||
HelicopterIDsWithFleetStatus(ctx context.Context) ([][]byte, error)
|
||||
}
|
||||
28
internal/domain/fleet_status/service.go
Normal file
28
internal/domain/fleet_status/service.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package fleetstatus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
helicopterusage "wucher/internal/domain/helicopter_usage"
|
||||
shareddto "wucher/internal/transport/http/dto/shared"
|
||||
)
|
||||
|
||||
type OverdueInspection struct {
|
||||
InspectionType string
|
||||
Detail string
|
||||
}
|
||||
|
||||
type Service interface {
|
||||
Create(ctx context.Context, row *FleetStatus) error
|
||||
Update(ctx context.Context, row *FleetStatus) error
|
||||
Delete(ctx context.Context, id []byte, deletedBy []byte) error
|
||||
GetByID(ctx context.Context, id []byte) (*FleetStatus, error)
|
||||
List(ctx context.Context, filter, sort string, limit, offset int) ([]FleetStatus, int64, error)
|
||||
LatestByHelicopterIDs(ctx context.Context, helicopterIDs [][]byte) (map[string]*FleetStatus, error)
|
||||
MarkServiced(ctx context.Context, id []byte, servicedAt time.Time, actorID []byte) error
|
||||
ListServiceHistoryByHelicopterID(ctx context.Context, helicopterID []byte, limit, offset int) ([]FleetStatusServiceLog, int64, error)
|
||||
GetSummaryByHelicopterID(ctx context.Context, helicopterID []byte) (*shareddto.FleetStatusSummary, error)
|
||||
ReviseSummary(ctx context.Context, helicopterID []byte, in helicopterusage.ManualSummaryInput, actor []byte) error
|
||||
OverdueInspections(ctx context.Context, helicopterID []byte) ([]OverdueInspection, error)
|
||||
}
|
||||
Reference in New Issue
Block a user