Merge pull request 'CUB-40: Add cost summary API endpoint' (#15) from agent/dex/CUB-40-cost-summary-api into dev
Some checks failed
Dev Build / build-test (push) Failing after 52s
Dev Build / deploy-dev (push) Has been skipped
Dev Build / notify-success (push) Has been skipped
Dev Build / notify-failure (push) Successful in 3s

Reviewed-on: #15
This commit was merged in pull request #15.
This commit is contained in:
2026-04-27 14:17:30 -04:00
2 changed files with 141 additions and 0 deletions

View File

@@ -413,6 +413,92 @@ public class PrintJobsController : ControllerBase
return NoContent();
}
// ── GET /api/printjobs/{id}/cost-summary ──────────────────────────
/// <summary>
/// Gets the material cost summary for a specific print job.
/// Calculates total material cost from filament usage (grams derived)
/// and the spool's purchase price. Returns warnings instead of errors
/// when cost data is unavailable.
/// </summary>
/// <param name="id">The unique identifier of the print job.</param>
/// <returns>A cost summary with breakdown and any warnings about missing data.</returns>
/// <response code="200">Returns the cost summary. Warnings field lists any missing data.</response>
/// <response code="404">If the print job with the given ID is not found.</response>
[HttpGet("{id:guid}/cost-summary")]
[ProducesResponseType(typeof(CostSummaryResponse), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<CostSummaryResponse>> GetCostSummary(Guid id)
{
_logger.LogDebug("Getting cost summary for print job {Id}", id);
var job = await _dbContext.PrintJobs
.Include(j => j.Spool)
.ThenInclude(s => s!.MaterialBase)
.FirstOrDefaultAsync(j => j.Id == id);
if (job is null)
{
_logger.LogWarning("Print job {Id} not found for cost summary", id);
return NotFound(new { error = $"Print job with ID '{id}' not found." });
}
var warnings = new List<string>();
var spool = job.Spool;
// Build response with what we have
var response = new CostSummaryResponse
{
PrintJobId = job.Id,
PrintName = job.PrintName,
SpoolId = job.SpoolId,
SpoolSerial = spool?.SpoolSerial ?? string.Empty,
SpoolBrand = spool?.Brand ?? string.Empty,
SpoolColorName = spool?.ColorName ?? string.Empty,
MmExtruded = job.MmExtruded,
GramsDerived = job.GramsDerived,
SpoolPurchasePrice = spool?.PurchasePrice,
SpoolWeightTotalGrams = spool?.WeightTotalGrams,
StoredCostPerPrint = job.CostPerPrint
};
// Validate spool data availability
if (spool is null)
{
warnings.Add("Spool data is not available for this print job. Cost cannot be calculated.");
response.Warnings = warnings;
return Ok(response);
}
// Check if we can calculate cost
if (!spool.PurchasePrice.HasValue)
{
warnings.Add("Spool purchase price is not set. Cost per gram and total material cost cannot be calculated.");
}
if (spool.WeightTotalGrams <= 0)
{
warnings.Add("Spool total weight is zero or invalid. Cost per gram and total material cost cannot be calculated.");
}
// If we have enough data, calculate the cost
if (spool.PurchasePrice.HasValue && spool.WeightTotalGrams > 0)
{
var pricePerGram = spool.PurchasePrice.Value / spool.WeightTotalGrams;
response.PricePerGram = Math.Round(pricePerGram, 4);
response.TotalMaterialCost = Math.Round(job.GramsDerived * pricePerGram, 4);
}
// Warn if grams derived is zero but mm extruded is non-zero
if (job.GramsDerived == 0 && job.MmExtruded > 0)
{
warnings.Add("GramsDerived is zero despite MmExtruded being non-zero. Cost may be inaccurate. Consider re-deriving grams from filament parameters.");
}
response.Warnings = warnings;
return Ok(response);
}
// ── Gram Derivation Formula ────────────────────────────────────
/// <summary>

View File

@@ -0,0 +1,55 @@
namespace Extrudex.API.DTOs.PrintJobs;
/// <summary>
/// Response DTO for the cost summary of a print job.
/// Provides a breakdown of material cost based on filament usage
/// and spool pricing data. If cost data is incomplete, warnings
/// are returned instead of throwing an error.
/// </summary>
public class CostSummaryResponse
{
/// <summary>Unique identifier of the print job.</summary>
public Guid PrintJobId { get; set; }
/// <summary>Human-readable name of the print job.</summary>
public string PrintName { get; set; } = string.Empty;
/// <summary>Foreign key to the spool used for this print job.</summary>
public Guid SpoolId { get; set; }
/// <summary>Serial number of the spool.</summary>
public string SpoolSerial { get; set; } = string.Empty;
/// <summary>Brand of the spool.</summary>
public string SpoolBrand { get; set; } = string.Empty;
/// <summary>Color name of the spool.</summary>
public string SpoolColorName { get; set; } = string.Empty;
/// <summary>Total millimeters of filament extruded during this print.</summary>
public decimal MmExtruded { get; set; }
/// <summary>Derived grams consumed for this print job.</summary>
public decimal GramsDerived { get; set; }
/// <summary>Purchase price of the full spool, if available.</summary>
public decimal? SpoolPurchasePrice { get; set; }
/// <summary>Total weight of the spool in grams when full.</summary>
public decimal? SpoolWeightTotalGrams { get; set; }
/// <summary>Calculated price per gram (purchase price / total weight), if available.</summary>
public decimal? PricePerGram { get; set; }
/// <summary>Calculated total material cost for this print job, if available.</summary>
public decimal? TotalMaterialCost { get; set; }
/// <summary>The CostPerPrint stored on the print job entity, if set.</summary>
public decimal? StoredCostPerPrint { get; set; }
/// <summary>
/// Warnings about missing data that prevent cost calculation.
/// Empty if all data is available and cost was calculated successfully.
/// </summary>
public List<string> Warnings { get; set; } = new();
}