mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 13:31:56 +00:00
feat(BE): fix delete project flock budget and uniformity, and fix uniformity with update purchase document
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -305,6 +306,56 @@ func (s *documentService) PresignURL(ctx context.Context, document entity.Docume
|
||||
return s.storage.PresignURL(ctx, document.Path, expires)
|
||||
}
|
||||
|
||||
// ResolveDocumentURL normalizes a stored path or URL into a presigned URL.
|
||||
func ResolveDocumentURL(
|
||||
ctx context.Context,
|
||||
svc DocumentService,
|
||||
rawPath string,
|
||||
expires time.Duration,
|
||||
) (string, error) {
|
||||
if svc == nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
rawPath = strings.TrimSpace(rawPath)
|
||||
if rawPath == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
key := rawPath
|
||||
lower := strings.ToLower(rawPath)
|
||||
if strings.HasPrefix(lower, "http://") || strings.HasPrefix(lower, "https://") {
|
||||
key = extractS3KeyFromURL(rawPath)
|
||||
if key == "" {
|
||||
return "", nil
|
||||
}
|
||||
}
|
||||
|
||||
return svc.PresignURL(ctx, entity.Document{Path: key}, expires)
|
||||
}
|
||||
|
||||
func extractS3KeyFromURL(raw string) string {
|
||||
parsed, err := url.Parse(strings.TrimSpace(raw))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
path := strings.TrimPrefix(parsed.Path, "/")
|
||||
if path == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
host := strings.ToLower(strings.TrimSpace(parsed.Host))
|
||||
if strings.HasPrefix(host, "s3.") || strings.HasPrefix(host, "s3-") {
|
||||
parts := strings.SplitN(path, "/", 2)
|
||||
if len(parts) == 2 {
|
||||
return parts[1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
func (s *documentService) generateObjectKey(ext string) (string, error) {
|
||||
normalizedExt := strings.TrimSpace(ext)
|
||||
if normalizedExt != "" && !strings.HasPrefix(normalizedExt, ".") {
|
||||
|
||||
@@ -192,17 +192,6 @@ func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*St
|
||||
if req.Quantity < 0 {
|
||||
return nil, errors.New("quantity must be zero or greater")
|
||||
}
|
||||
if s.logger.IsLevelEnabled(logrus.DebugLevel) {
|
||||
s.logger.WithFields(logrus.Fields{
|
||||
"usable_key": req.UsableKey.String(),
|
||||
"usable_id": req.UsableID,
|
||||
"requested_quantity": req.Quantity,
|
||||
"allow_pending": req.AllowPending,
|
||||
"product_warehouse_id": req.ProductWarehouseID,
|
||||
}).Debug("fifo consume request")
|
||||
}
|
||||
|
||||
|
||||
cfg, ok := fifo.Usable(req.UsableKey)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("usable %q is not registered", req.UsableKey)
|
||||
@@ -230,20 +219,6 @@ func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*St
|
||||
currentPending := ctxRow.PendingQty
|
||||
currentTotal := currentUsage + currentPending
|
||||
delta := req.Quantity - currentTotal
|
||||
if s.logger.IsLevelEnabled(logrus.DebugLevel) {
|
||||
s.logger.WithFields(logrus.Fields{
|
||||
"usable_key": req.UsableKey.String(),
|
||||
"usable_id": req.UsableID,
|
||||
"product_warehouse_id": productWarehouseID,
|
||||
"current_usage_qty": currentUsage,
|
||||
"current_pending_qty": currentPending,
|
||||
"current_total_qty": currentTotal,
|
||||
"requested_quantity": req.Quantity,
|
||||
"calculated_delta": delta,
|
||||
"input_warehouse_match": req.ProductWarehouseID == 0 || req.ProductWarehouseID == productWarehouseID,
|
||||
}).Debug("fifo consume context")
|
||||
}
|
||||
|
||||
var (
|
||||
usageDelta float64
|
||||
pendingDelta float64
|
||||
@@ -308,21 +283,6 @@ func (s *fifoService) Consume(ctx context.Context, req StockConsumeRequest) (*St
|
||||
result.ReleasedQuantity = releasedAmount
|
||||
result.UsageQuantity = currentUsage + usageDelta
|
||||
result.PendingQuantity = currentPending + pendingDelta
|
||||
if s.logger.IsLevelEnabled(logrus.DebugLevel) {
|
||||
s.logger.WithFields(logrus.Fields{
|
||||
"usable_key": req.UsableKey.String(),
|
||||
"usable_id": req.UsableID,
|
||||
"product_warehouse_id": productWarehouseID,
|
||||
"usage_delta": usageDelta,
|
||||
"pending_delta": pendingDelta,
|
||||
"released_quantity": releasedAmount,
|
||||
"added_allocations": len(addedAlloc),
|
||||
"final_usage_qty": result.UsageQuantity,
|
||||
"final_pending_qty": result.PendingQuantity,
|
||||
"final_requested_qty": result.RequestedQuantity,
|
||||
}).Debug("fifo consume result")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -336,14 +296,6 @@ func (s *fifoService) ReleaseUsage(ctx context.Context, req StockReleaseRequest)
|
||||
if req.UsableID == 0 || strings.TrimSpace(req.UsableKey.String()) == "" {
|
||||
return errors.New("usable key and id are required")
|
||||
}
|
||||
if s.logger.IsLevelEnabled(logrus.DebugLevel) {
|
||||
s.logger.WithFields(logrus.Fields{
|
||||
"usable_key": req.UsableKey.String(),
|
||||
"usable_id": req.UsableID,
|
||||
"reason": req.Reason,
|
||||
}).Debug("fifo release request")
|
||||
}
|
||||
|
||||
return s.withTransaction(ctx, req.Tx, func(tx *gorm.DB) error {
|
||||
cfg, ok := fifo.Usable(req.UsableKey)
|
||||
if !ok {
|
||||
@@ -354,17 +306,6 @@ func (s *fifoService) ReleaseUsage(ctx context.Context, req StockReleaseRequest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if s.logger.IsLevelEnabled(logrus.DebugLevel) {
|
||||
s.logger.WithFields(logrus.Fields{
|
||||
"usable_key": req.UsableKey.String(),
|
||||
"usable_id": req.UsableID,
|
||||
"product_warehouse_id": ctxRow.ProductWarehouseID,
|
||||
"current_usage_qty": ctxRow.UsageQty,
|
||||
"current_pending_qty": ctxRow.PendingQty,
|
||||
"current_total_qty": ctxRow.UsageQty + ctxRow.PendingQty,
|
||||
}).Debug("fifo release context")
|
||||
}
|
||||
|
||||
var usageDelta, pendingDelta float64
|
||||
if ctxRow.UsageQty > 0 {
|
||||
if _, err := s.releaseUsagePortion(ctx, tx, req.UsableKey, req.UsableID, ctxRow.UsageQty); err != nil {
|
||||
@@ -380,15 +321,6 @@ func (s *fifoService) ReleaseUsage(ctx context.Context, req StockReleaseRequest)
|
||||
return err
|
||||
}
|
||||
|
||||
if s.logger.IsLevelEnabled(logrus.DebugLevel) {
|
||||
s.logger.WithFields(logrus.Fields{
|
||||
"usable_key": req.UsableKey.String(),
|
||||
"usable_id": req.UsableID,
|
||||
"usage_delta": usageDelta,
|
||||
"pending_delta": pendingDelta,
|
||||
}).Debug("fifo release applied")
|
||||
}
|
||||
|
||||
return s.allocations.ReleaseByUsable(ctx, req.UsableKey.String(), req.UsableID, req.Reason, func(db *gorm.DB) *gorm.DB {
|
||||
return s.txOrDB(tx, db)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user