105 lines
3.6 KiB
Go
105 lines
3.6 KiB
Go
package repositories
|
||
|
||
import (
|
||
"nearle/models"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
type StockRequestRepository interface {
|
||
CreateStockRequest(req *models.StockRequest) error
|
||
GetStockRequests(tenantID int, locationID int, status string, date string, pageNo int, pageSize int) ([]models.StockRequest, error)
|
||
GetStockRequestByID(requestID int) (*models.StockRequest, error)
|
||
UpdateStockRequest(requestID int, status string) error
|
||
}
|
||
|
||
type stockRequestRepository struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
func NewStockRequestRepository(db *gorm.DB) StockRequestRepository {
|
||
return &stockRequestRepository{db: db}
|
||
}
|
||
|
||
func (r *stockRequestRepository) CreateStockRequest(req *models.StockRequest) error {
|
||
// Fetch product details
|
||
var prod struct {
|
||
Productname string
|
||
Productimage string
|
||
}
|
||
r.db.Table("products").Select("productname, productimage").Where("productid = ?", req.Productid).Scan(&prod)
|
||
if req.Productname == "" {
|
||
req.Productname = prod.Productname
|
||
}
|
||
if req.Productimage == "" {
|
||
req.Productimage = prod.Productimage
|
||
}
|
||
|
||
// Fetch tenant name
|
||
var tenantName string
|
||
r.db.Table("tenants").Select("tenantname").Where("tenantid = ?", req.Tenantid).Scan(&tenantName)
|
||
if req.Tenantname == "" {
|
||
req.Tenantname = tenantName
|
||
}
|
||
|
||
return r.db.Create(req).Error
|
||
}
|
||
|
||
func (r *stockRequestRepository) GetStockRequests(tenantID int, locationID int, status string, date string, pageNo int, pageSize int) ([]models.StockRequest, error) {
|
||
var requests []models.StockRequest
|
||
query := r.db.Table("stockrequests").
|
||
Select("stockrequests.*, products.productname, products.productimage").
|
||
Joins("left join products on products.productid = stockrequests.productid").
|
||
Where("stockrequests.tenantid = ?", tenantID)
|
||
|
||
if locationID > 0 {
|
||
query = query.Where("stockrequests.locationid = ?", locationID)
|
||
}
|
||
|
||
if status != "" {
|
||
query = query.Where("stockrequests.status = ?", status)
|
||
}
|
||
|
||
if date != "" {
|
||
query = query.Where("DATE(stockrequests.created) = ?", date)
|
||
}
|
||
|
||
offset := (pageNo - 1) * pageSize
|
||
err := query.Order("stockrequests.created DESC").Offset(offset).Limit(pageSize).Find(&requests).Error
|
||
return requests, err
|
||
}
|
||
|
||
func (r *stockRequestRepository) GetStockRequestByID(requestID int) (*models.StockRequest, error) {
|
||
var req models.StockRequest
|
||
err := r.db.Table("stockrequests").
|
||
Select("stockrequests.*, products.productname, products.productimage, tenants.tenantname, tenantlocations.locationname").
|
||
Joins("left join products on products.productid = stockrequests.productid").
|
||
Joins("left join tenants on tenants.tenantid = stockrequests.tenantid").
|
||
Joins("left join tenantlocations on tenantlocations.locationid = stockrequests.locationid").
|
||
Where("requestid = ?", requestID).First(&req).Error
|
||
return &req, err
|
||
}
|
||
|
||
// UpdateStockRequest sets one request’s status, and refuses an id that is not
|
||
// there.
|
||
//
|
||
// An UPDATE that matches no row is not an SQL error, so this used to report
|
||
// success for a request that does not exist. Approving happened to catch it —
|
||
// the service reads the row first to move the stock — but rejecting went
|
||
// straight to the UPDATE and said it had worked.
|
||
//
|
||
// Harmless for one id typed by hand; not harmless in a batch, where the answer
|
||
// is a count. Measured on production 2026-09-02: rejecting [31,32,33,9999999]
|
||
// answered "4 updated" when only three requests existed. A merchant clearing a
|
||
// stale queue would be told every row was dealt with.
|
||
func (r *stockRequestRepository) UpdateStockRequest(requestID int, status string) error {
|
||
result := r.db.Model(&models.StockRequest{}).Where("requestid = ?", requestID).Update("status", status)
|
||
if result.Error != nil {
|
||
return result.Error
|
||
}
|
||
if result.RowsAffected == 0 {
|
||
return gorm.ErrRecordNotFound
|
||
}
|
||
return nil
|
||
}
|