mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-20 05:21:57 +00:00
fix: internal server error because of date parsing
This commit is contained in:
@@ -272,6 +272,34 @@ func BuildWorkbook(moduleTitle string, query *Query, rows []Row) ([]byte, error)
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
func ParseActivityDate(value string) (time.Time, error) {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
if trimmed == "" {
|
||||
return time.Time{}, fmt.Errorf("empty activity date")
|
||||
}
|
||||
|
||||
layouts := []string{
|
||||
"2006-01-02",
|
||||
time.RFC3339,
|
||||
time.RFC3339Nano,
|
||||
"2006-01-02 15:04:05Z07:00",
|
||||
"2006-01-02 15:04:05.999999999Z07:00",
|
||||
}
|
||||
for _, layout := range layouts {
|
||||
if parsed, err := time.Parse(layout, trimmed); err == nil {
|
||||
return parsed, nil
|
||||
}
|
||||
}
|
||||
|
||||
if len(trimmed) >= len("2006-01-02") {
|
||||
if parsed, err := time.Parse("2006-01-02", trimmed[:10]); err == nil {
|
||||
return parsed, nil
|
||||
}
|
||||
}
|
||||
|
||||
return time.Time{}, fmt.Errorf("unsupported activity date format: %s", value)
|
||||
}
|
||||
|
||||
func buildStyles(file *excelize.File) (int, int, int, int, int, int, int, int, int, error) {
|
||||
titleStyle, err := file.NewStyle(&excelize.Style{
|
||||
Font: &excelize.Font{Bold: true, Size: 18, Color: "1F2937"},
|
||||
|
||||
@@ -141,7 +141,7 @@ func (r *ExpenseRepositoryImpl) GetProgressRows(ctx context.Context, startDate,
|
||||
'Expenses' AS module,
|
||||
COALESCE(pf.flock_name, loc.name, fallback_loc.name, 'Unknown Farm') AS farm_name,
|
||||
COALESCE(k.name, `+unassignedSQL+`, 'Unknown Kandang') AS kandang_name,
|
||||
DATE(e.transaction_date) AS activity_date,
|
||||
CAST(DATE(e.transaction_date) AS TEXT) AS activity_date,
|
||||
COUNT(*) AS count
|
||||
`).
|
||||
Joins("LEFT JOIN (SELECT DISTINCT expense_id, project_flock_kandang_id, kandang_id FROM expense_nonstocks) en ON en.expense_id = e.id").
|
||||
@@ -179,7 +179,7 @@ func (r *ExpenseRepositoryImpl) GetProgressRows(ctx context.Context, startDate,
|
||||
|
||||
rows := make([]exportprogress.Row, 0, len(scanned))
|
||||
for _, item := range scanned {
|
||||
activityDate, err := time.Parse("2006-01-02", item.ActivityDate)
|
||||
activityDate, err := exportprogress.ParseActivityDate(item.ActivityDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ func (r *MarketingRepositoryImpl) GetProgressRows(ctx context.Context, startDate
|
||||
'Marketings' AS module,
|
||||
COALESCE(pf.flock_name, loc.name, 'Unknown Farm') AS farm_name,
|
||||
COALESCE(k.name, `+unassignedSQL+`, 'Unknown Kandang') AS kandang_name,
|
||||
DATE(m.so_date) AS activity_date
|
||||
CAST(DATE(m.so_date) AS TEXT) AS activity_date
|
||||
`).
|
||||
Joins("JOIN marketing_products mp ON mp.marketing_id = m.id").
|
||||
Joins("JOIN product_warehouses pw ON pw.id = mp.product_warehouse_id").
|
||||
@@ -108,7 +108,7 @@ func (r *MarketingRepositoryImpl) GetProgressRows(ctx context.Context, startDate
|
||||
|
||||
rows := make([]exportprogress.Row, 0, len(scanned))
|
||||
for _, item := range scanned {
|
||||
activityDate, err := time.Parse("2006-01-02", item.ActivityDate)
|
||||
activityDate, err := exportprogress.ParseActivityDate(item.ActivityDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -260,7 +260,7 @@ func (r *RecordingRepositoryImpl) GetProgressRows(ctx context.Context, startDate
|
||||
'Recordings' AS module,
|
||||
COALESCE(pf.flock_name, loc.name, 'Unknown Farm') AS farm_name,
|
||||
COALESCE(k.name, `+unassignedSQL+`, 'Unknown Kandang') AS kandang_name,
|
||||
DATE(r.record_datetime) AS activity_date,
|
||||
CAST(DATE(r.record_datetime) AS TEXT) AS activity_date,
|
||||
COUNT(*) AS count
|
||||
`).
|
||||
Joins("JOIN project_flock_kandangs pfk ON pfk.id = r.project_flock_kandangs_id").
|
||||
@@ -296,7 +296,7 @@ func (r *RecordingRepositoryImpl) GetProgressRows(ctx context.Context, startDate
|
||||
|
||||
rows := make([]exportprogress.Row, 0, len(scanned))
|
||||
for _, item := range scanned {
|
||||
activityDate, err := time.Parse("2006-01-02", item.ActivityDate)
|
||||
activityDate, err := exportprogress.ParseActivityDate(item.ActivityDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -295,7 +295,7 @@ func (r *PurchaseRepositoryImpl) GetProgressRows(ctx context.Context, startDate,
|
||||
'Purchases' AS module,
|
||||
COALESCE(pf_explicit.flock_name, pf_active.flock_name, kandang_loc.name, warehouse_loc.name, 'Unknown Farm') AS farm_name,
|
||||
COALESCE(k_explicit.name, k_active.name, wk.name, `+unassignedSQL+`, 'Unknown Kandang') AS kandang_name,
|
||||
DATE(p.po_date) AS activity_date
|
||||
CAST(DATE(p.po_date) AS TEXT) AS activity_date
|
||||
`).
|
||||
Joins("JOIN purchase_items pi ON pi.purchase_id = p.id").
|
||||
Joins("JOIN warehouses w ON w.id = pi.warehouse_id").
|
||||
@@ -340,7 +340,7 @@ func (r *PurchaseRepositoryImpl) GetProgressRows(ctx context.Context, startDate,
|
||||
|
||||
rows := make([]exportprogress.Row, 0, len(scanned))
|
||||
for _, item := range scanned {
|
||||
activityDate, err := time.Parse("2006-01-02", item.ActivityDate)
|
||||
activityDate, err := exportprogress.ParseActivityDate(item.ActivityDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user