fix on stocks on store catalogue
This commit is contained in:
@@ -3,6 +3,8 @@ package repositories
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nearle/models"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -124,3 +126,44 @@ func (r *productRepository) UnpublishProduct(tenantID, productID int) (int, erro
|
||||
tenantID, productID)
|
||||
return int(res.RowsAffected), res.Error
|
||||
}
|
||||
|
||||
// PublishPricedLocations releases the rows an import has just created, at the
|
||||
// one branch it imported into.
|
||||
//
|
||||
// The publish gate exists to stop an unpriced product reaching a till that
|
||||
// would ring it up at zero. An import from a spreadsheet CARRIES the price — it
|
||||
// is on the row this writes — so the gate's condition is already met, and
|
||||
// leaving these rows unpublished asks somebody to click a button that can only
|
||||
// answer "yes" seventeen times.
|
||||
//
|
||||
// It was not a harmless extra step. Nothing in the backend filters on
|
||||
// `publishedat`, so the products were already on sale in the customer app while
|
||||
// the merchant's own store catalogue — the one screen that does read it —
|
||||
// showed nothing. The shopkeeper could not see what their shoppers could buy.
|
||||
//
|
||||
// Three things keep this honest:
|
||||
//
|
||||
// - `price > 0` is checked in SQL, not by the caller. A row that somehow
|
||||
// arrived unpriced stays unpublished, which is the rule the gate is for.
|
||||
// - COALESCE keeps the first release date, so re-importing does not rewrite
|
||||
// the history of a product that has been on sale for months.
|
||||
// - Only the named branch. PublishProduct releases to every outlet a tenant
|
||||
// runs and that is right for a deliberate release; an import names one
|
||||
// branch, and quietly stocking the others would put products in shops
|
||||
// nobody chose.
|
||||
func (r *productRepository) PublishPricedLocations(refs []models.ProductLocationRef) error {
|
||||
if len(refs) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, ref := range refs {
|
||||
if err := r.db.Exec(`
|
||||
UPDATE productlocations
|
||||
SET publishedat = COALESCE(publishedat, NOW()), updated = NOW()
|
||||
WHERE tenantid = ? AND locationid = ? AND productid = ?
|
||||
AND COALESCE(price, 0) > 0`,
|
||||
ref.Tenantid, ref.Locationid, ref.Productid).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user