mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
codex: initiated changes
This commit is contained in:
+58
-32
@@ -84,31 +84,28 @@ func (r *ProductWarehouseRepositoryImpl) ProductWarehouseExistByProductAndWareho
|
||||
}
|
||||
|
||||
func (r *ProductWarehouseRepositoryImpl) GetProductWarehouseByProductAndWarehouseID(ctx context.Context, productId, warehouseId uint) (*entity.ProductWarehouse, error) {
|
||||
var productWarehouse entity.ProductWarehouse
|
||||
|
||||
err := r.DB().WithContext(ctx).
|
||||
Where("product_id = ? AND warehouse_id = ? AND project_flock_kandang_id IS NOT NULL", productId, warehouseId).
|
||||
Order("id DESC").
|
||||
Preload("ProjectFlockKandang").
|
||||
First(&productWarehouse).Error
|
||||
|
||||
if err == nil {
|
||||
|
||||
if productWarehouse.ProjectFlockKandang.ClosedAt == nil {
|
||||
return &productWarehouse, nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
err = r.DB().WithContext(ctx).
|
||||
Where("product_id = ? AND warehouse_id = ? AND project_flock_kandang_id IS NULL", productId, warehouseId).
|
||||
First(&productWarehouse).Error
|
||||
|
||||
warehouseIsKandang, err := r.isKandangWarehouse(ctx, warehouseId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &productWarehouse, nil
|
||||
if warehouseIsKandang {
|
||||
if productWarehouse, err := r.findOpenKandangOwnedWarehouse(ctx, productId, warehouseId); err == nil {
|
||||
return productWarehouse, nil
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r.findSharedWarehouse(ctx, productId, warehouseId)
|
||||
}
|
||||
|
||||
if productWarehouse, err := r.findSharedWarehouse(ctx, productId, warehouseId); err == nil {
|
||||
return productWarehouse, nil
|
||||
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r.findOpenKandangOwnedWarehouse(ctx, productId, warehouseId)
|
||||
}
|
||||
|
||||
func (r *ProductWarehouseRepositoryImpl) FindByProductWarehouseAndPfk(ctx context.Context, productID uint, warehouseID uint, projectFlockKandangID *uint) (*entity.ProductWarehouse, error) {
|
||||
@@ -266,18 +263,8 @@ func (r *ProductWarehouseRepositoryImpl) EnsureProductWarehouse(
|
||||
projectFlockKandangID *uint,
|
||||
createdBy uint,
|
||||
) (uint, error) {
|
||||
record, err := r.GetProductWarehouseByProductAndWarehouseID(ctx, productID, warehouseID)
|
||||
record, err := r.FindByProductWarehouseAndPfk(ctx, productID, warehouseID, projectFlockKandangID)
|
||||
if err == nil {
|
||||
// Backfill project_flock_kandang_id when it's missing and caller provides one.
|
||||
if projectFlockKandangID != nil && (record.ProjectFlockKandangId == nil || *record.ProjectFlockKandangId == 0) {
|
||||
if err := r.DB().WithContext(ctx).
|
||||
Model(&entity.ProductWarehouse{}).
|
||||
Where("id = ?", record.Id).
|
||||
Update("project_flock_kandang_id", *projectFlockKandangID).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
record.ProjectFlockKandangId = projectFlockKandangID
|
||||
}
|
||||
return record.Id, nil
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -301,6 +288,45 @@ func (r *ProductWarehouseRepositoryImpl) EnsureProductWarehouse(
|
||||
return entity.Id, nil
|
||||
}
|
||||
|
||||
func (r *ProductWarehouseRepositoryImpl) isKandangWarehouse(ctx context.Context, warehouseID uint) (bool, error) {
|
||||
var kandangID *uint
|
||||
if err := r.DB().WithContext(ctx).
|
||||
Table("warehouses").
|
||||
Select("kandang_id").
|
||||
Where("id = ?", warehouseID).
|
||||
Scan(&kandangID).Error; err != nil {
|
||||
return false, err
|
||||
}
|
||||
return kandangID != nil && *kandangID != 0, nil
|
||||
}
|
||||
|
||||
func (r *ProductWarehouseRepositoryImpl) findOpenKandangOwnedWarehouse(ctx context.Context, productID uint, warehouseID uint) (*entity.ProductWarehouse, error) {
|
||||
var productWarehouse entity.ProductWarehouse
|
||||
err := r.DB().WithContext(ctx).
|
||||
Where("product_id = ? AND warehouse_id = ? AND project_flock_kandang_id IS NOT NULL", productID, warehouseID).
|
||||
Order("id DESC").
|
||||
Preload("ProjectFlockKandang").
|
||||
First(&productWarehouse).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if productWarehouse.ProjectFlockKandang != nil && productWarehouse.ProjectFlockKandang.ClosedAt == nil {
|
||||
return &productWarehouse, nil
|
||||
}
|
||||
return nil, gorm.ErrRecordNotFound
|
||||
}
|
||||
|
||||
func (r *ProductWarehouseRepositoryImpl) findSharedWarehouse(ctx context.Context, productID uint, warehouseID uint) (*entity.ProductWarehouse, error) {
|
||||
var productWarehouse entity.ProductWarehouse
|
||||
if err := r.DB().WithContext(ctx).
|
||||
Where("product_id = ? AND warehouse_id = ? AND project_flock_kandang_id IS NULL", productID, warehouseID).
|
||||
Preload("ProjectFlockKandang").
|
||||
First(&productWarehouse).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &productWarehouse, nil
|
||||
}
|
||||
|
||||
func (r *ProductWarehouseRepositoryImpl) GetByProductWarehouseAndProjectFlockKandang(
|
||||
ctx context.Context,
|
||||
productId uint,
|
||||
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestGetProductWarehouseByProductAndWarehouseIDPrefersSharedForFarmWarehouse(t *testing.T) {
|
||||
db := setupProductWarehouseRepoTestDB(t)
|
||||
repo := NewProductWarehouseRepository(db)
|
||||
ctx := context.Background()
|
||||
|
||||
insertProductWarehouseTestFixtures(t, db)
|
||||
|
||||
got, err := repo.GetProductWarehouseByProductAndWarehouseID(ctx, 1, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got.Id != 2 {
|
||||
t.Fatalf("expected shared farm warehouse id 2, got %d", got.Id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetProductWarehouseByProductAndWarehouseIDPrefersOpenKandangOwnedForKandangWarehouse(t *testing.T) {
|
||||
db := setupProductWarehouseRepoTestDB(t)
|
||||
repo := NewProductWarehouseRepository(db)
|
||||
ctx := context.Background()
|
||||
|
||||
insertProductWarehouseTestFixtures(t, db)
|
||||
|
||||
got, err := repo.GetProductWarehouseByProductAndWarehouseID(ctx, 1, 2)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got.Id != 3 {
|
||||
t.Fatalf("expected kandang-owned warehouse id 3, got %d", got.Id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureProductWarehouseDoesNotBackfillSharedWarehouse(t *testing.T) {
|
||||
db := setupProductWarehouseRepoTestDB(t)
|
||||
repo := NewProductWarehouseRepository(db)
|
||||
ctx := context.Background()
|
||||
|
||||
insertProductWarehouseTestFixtures(t, db)
|
||||
|
||||
projectFlockKandangID := uint(101)
|
||||
createdID, err := repo.EnsureProductWarehouse(ctx, 1, 1, &projectFlockKandangID, 9)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if createdID == 2 {
|
||||
t.Fatalf("expected new kandang-attributed row instead of reusing shared row")
|
||||
}
|
||||
|
||||
var sharedPfkID *uint
|
||||
if err := db.WithContext(ctx).
|
||||
Table("product_warehouses").
|
||||
Select("project_flock_kandang_id").
|
||||
Where("id = ?", 2).
|
||||
Scan(&sharedPfkID).Error; err != nil {
|
||||
t.Fatalf("failed to load shared warehouse row: %v", err)
|
||||
}
|
||||
if sharedPfkID != nil {
|
||||
t.Fatalf("expected shared row attribution to stay nil, got %v", *sharedPfkID)
|
||||
}
|
||||
}
|
||||
|
||||
func setupProductWarehouseRepoTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
|
||||
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=private"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("failed opening sqlite db: %v", err)
|
||||
}
|
||||
|
||||
statements := []string{
|
||||
`CREATE TABLE warehouses (id INTEGER PRIMARY KEY, kandang_id INTEGER NULL)`,
|
||||
`CREATE TABLE project_flock_kandangs (id INTEGER PRIMARY KEY, closed_at TIMESTAMP NULL)`,
|
||||
`CREATE TABLE product_warehouses (
|
||||
id INTEGER PRIMARY KEY,
|
||||
product_id INTEGER NOT NULL,
|
||||
warehouse_id INTEGER NOT NULL,
|
||||
project_flock_kandang_id INTEGER NULL,
|
||||
qty NUMERIC(15,3) NOT NULL DEFAULT 0
|
||||
)`,
|
||||
}
|
||||
for _, stmt := range statements {
|
||||
if err := db.Exec(stmt).Error; err != nil {
|
||||
t.Fatalf("failed preparing schema: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
|
||||
func insertProductWarehouseTestFixtures(t *testing.T, db *gorm.DB) {
|
||||
t.Helper()
|
||||
|
||||
statements := []string{
|
||||
`INSERT INTO warehouses (id, kandang_id) VALUES (1, NULL), (2, 7)`,
|
||||
`INSERT INTO project_flock_kandangs (id, closed_at) VALUES (101, NULL)`,
|
||||
`INSERT INTO product_warehouses (id, product_id, warehouse_id, project_flock_kandang_id, qty) VALUES
|
||||
(1, 1, 1, 101, 10),
|
||||
(2, 1, 1, NULL, 20),
|
||||
(3, 1, 2, 101, 30),
|
||||
(4, 1, 2, NULL, 40)`,
|
||||
}
|
||||
for _, stmt := range statements {
|
||||
if err := db.Exec(stmt).Error; err != nil {
|
||||
t.Fatalf("failed seeding fixtures: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user