mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
91b320d489
- Extend DB schema with product_warehouses and stock_logs tables - Implement stock adjustment API (increase/decrease operations) - Add comprehensive validation for all adjustment operations - Implement audit log system for each adjustment with history tracking - Include transaction handling, DTOs, seeders, and proper error handling - Add adjustment history API with pagination and filtering TODO: Integration testing pending
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type StockLogRepository interface {
|
|
repository.BaseRepository[entity.StockLog]
|
|
GetByFlaggable(ctx context.Context, logType string, logId uint) ([]*entity.StockLog, error)
|
|
GetByProductWarehouse(ctx context.Context, productWarehouseId uint, limit int) ([]*entity.StockLog, error)
|
|
GetByTransactionType(ctx context.Context, transactionType string, limit int) ([]*entity.StockLog, error)
|
|
}
|
|
|
|
type StockLogRepositoryImpl struct {
|
|
*repository.BaseRepositoryImpl[entity.StockLog]
|
|
}
|
|
|
|
func NewStockLogRepository(db *gorm.DB) StockLogRepository {
|
|
return &StockLogRepositoryImpl{
|
|
BaseRepositoryImpl: repository.NewBaseRepository[entity.StockLog](db),
|
|
}
|
|
}
|
|
|
|
func (r *StockLogRepositoryImpl) GetByFlaggable(ctx context.Context, logType string, logId uint) ([]*entity.StockLog, error) {
|
|
var stockLogs []*entity.StockLog
|
|
|
|
err := r.DB().WithContext(ctx).
|
|
Where("log_type = ? AND log_id = ?", logType, logId).
|
|
Preload("ProductWarehouse").
|
|
Preload("ProductWarehouse.Product").
|
|
Preload("ProductWarehouse.Warehouse").
|
|
Order("created_at DESC").
|
|
Find(&stockLogs).Error
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return stockLogs, nil
|
|
}
|
|
|
|
func (r *StockLogRepositoryImpl) GetByProductWarehouse(ctx context.Context, productWarehouseId uint, limit int) ([]*entity.StockLog, error) {
|
|
var stockLogs []*entity.StockLog
|
|
|
|
query := r.DB().WithContext(ctx).
|
|
Where("product_warehouse_id = ?", productWarehouseId).
|
|
Preload("ProductWarehouse").
|
|
Preload("ProductWarehouse.Product").
|
|
Preload("ProductWarehouse.Warehouse").
|
|
Order("created_at DESC")
|
|
|
|
if limit > 0 {
|
|
query = query.Limit(limit)
|
|
}
|
|
|
|
err := query.Find(&stockLogs).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return stockLogs, nil
|
|
}
|
|
|
|
func (r *StockLogRepositoryImpl) GetByTransactionType(ctx context.Context, transactionType string, limit int) ([]*entity.StockLog, error) {
|
|
var stockLogs []*entity.StockLog
|
|
|
|
query := r.DB().WithContext(ctx).
|
|
Where("transaction_type = ?", transactionType).
|
|
Preload("ProductWarehouse").
|
|
Preload("ProductWarehouse.Product").
|
|
Preload("ProductWarehouse.Warehouse").
|
|
Order("created_at DESC")
|
|
|
|
if limit > 0 {
|
|
query = query.Limit(limit)
|
|
}
|
|
|
|
err := query.Find(&stockLogs).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return stockLogs, nil
|
|
}
|