2026-02-08 01:44:44 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-03-04 14:52:34 +00:00
|
|
|
"errors"
|
2026-02-08 01:44:44 +00:00
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/models"
|
2026-03-19 15:31:04 +00:00
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
2026-02-08 01:44:44 +00:00
|
|
|
)
|
|
|
|
|
|
2026-02-09 19:13:42 +00:00
|
|
|
type UploadPoolDetail struct {
|
2026-02-09 19:17:33 +00:00
|
|
|
CountExisting int
|
|
|
|
|
CountNew int
|
2026-02-14 04:08:22 +00:00
|
|
|
CountOutside int
|
2026-02-09 19:17:33 +00:00
|
|
|
Created time.Time
|
2026-02-14 04:36:47 +00:00
|
|
|
Errors []UploadPoolError
|
2026-02-09 19:17:33 +00:00
|
|
|
ID int32
|
|
|
|
|
Name string
|
|
|
|
|
Pools []UploadPoolRow
|
|
|
|
|
Status string
|
2026-02-09 19:03:27 +00:00
|
|
|
}
|
2026-02-14 04:36:47 +00:00
|
|
|
type UploadPoolError struct {
|
|
|
|
|
Column uint
|
|
|
|
|
Line uint
|
|
|
|
|
Message string
|
|
|
|
|
}
|
2026-02-09 19:13:42 +00:00
|
|
|
type UploadPoolRow struct {
|
2026-03-19 15:31:04 +00:00
|
|
|
Address types.Address
|
|
|
|
|
Condition string
|
|
|
|
|
Errors []UploadPoolError
|
|
|
|
|
Status string
|
|
|
|
|
Tags map[string]string
|
2026-02-09 19:03:27 +00:00
|
|
|
}
|
2026-03-02 18:49:02 +00:00
|
|
|
type Upload struct {
|
2026-02-09 18:25:44 +00:00
|
|
|
Created time.Time `db:"created"`
|
|
|
|
|
ID int32 `db:"id"`
|
|
|
|
|
Status string `db:"status"`
|
2026-02-08 01:44:44 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-04 14:52:34 +00:00
|
|
|
func GetUploadDetail(ctx context.Context, organization_id int32, file_id int32) (UploadPoolDetail, error) {
|
2026-02-09 19:03:27 +00:00
|
|
|
file, err := models.FindFileuploadFile(ctx, db.PGInstance.BobDB, file_id)
|
|
|
|
|
if err != nil {
|
2026-02-09 19:13:42 +00:00
|
|
|
return UploadPoolDetail{}, fmt.Errorf("Failed to lookup file %d: %w", file_id, err)
|
2026-02-09 19:03:27 +00:00
|
|
|
}
|
2026-03-04 14:52:34 +00:00
|
|
|
csv, err := models.FindFileuploadCSV(ctx, db.PGInstance.BobDB, file_id)
|
2026-02-14 04:36:47 +00:00
|
|
|
if err != nil {
|
2026-03-04 14:52:34 +00:00
|
|
|
return UploadPoolDetail{}, fmt.Errorf("Failed to lookup csv %d: %w", file_id, err)
|
2026-02-14 04:36:47 +00:00
|
|
|
}
|
2026-03-04 14:52:34 +00:00
|
|
|
switch csv.Type {
|
|
|
|
|
case enums.FileuploadCsvtypeFlyover:
|
|
|
|
|
return getUploadPoollistDetail(ctx, file)
|
|
|
|
|
case enums.FileuploadCsvtypePoollist:
|
|
|
|
|
return getUploadPoollistDetail(ctx, file)
|
2026-02-14 04:36:47 +00:00
|
|
|
}
|
2026-03-04 14:52:34 +00:00
|
|
|
return UploadPoolDetail{}, errors.New("No idea what to do with upload type")
|
|
|
|
|
}
|
2026-02-14 04:36:47 +00:00
|
|
|
|
2026-03-04 14:52:34 +00:00
|
|
|
func getUploadPoollistDetail(ctx context.Context, file *models.FileuploadFile) (UploadPoolDetail, error) {
|
|
|
|
|
file_errors, errors_by_line, err := errorsByLine(ctx, file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return UploadPoolDetail{}, fmt.Errorf("get errors by line: %w", err)
|
|
|
|
|
}
|
2026-02-14 04:36:47 +00:00
|
|
|
pool_rows, err := models.FileuploadPools.Query(
|
2026-03-04 14:52:34 +00:00
|
|
|
models.SelectWhere.FileuploadPools.CSVFile.EQ(file.ID),
|
2026-02-09 19:03:27 +00:00
|
|
|
).All(ctx, db.PGInstance.BobDB)
|
|
|
|
|
if err != nil {
|
2026-03-04 14:52:34 +00:00
|
|
|
return UploadPoolDetail{}, fmt.Errorf("Failed to query pools for %d: %w", file.ID, err)
|
2026-02-09 19:03:27 +00:00
|
|
|
}
|
2026-02-14 04:36:47 +00:00
|
|
|
|
2026-02-09 19:13:42 +00:00
|
|
|
pools := make([]UploadPoolRow, 0)
|
2026-02-09 19:17:33 +00:00
|
|
|
count_existing := 0
|
|
|
|
|
count_new := 0
|
2026-02-14 04:08:22 +00:00
|
|
|
count_outside := 0
|
2026-02-14 04:36:47 +00:00
|
|
|
status := "unknown"
|
2026-02-16 17:59:18 +00:00
|
|
|
for _, r := range pool_rows {
|
2026-02-09 19:17:33 +00:00
|
|
|
if r.IsNew {
|
|
|
|
|
count_new = count_new + 1
|
2026-02-14 04:36:47 +00:00
|
|
|
status = "new"
|
2026-02-09 19:17:33 +00:00
|
|
|
} else {
|
|
|
|
|
count_existing = count_existing + 1
|
2026-02-14 04:36:47 +00:00
|
|
|
status = "existing"
|
2026-02-09 19:17:33 +00:00
|
|
|
}
|
2026-03-04 22:04:22 +00:00
|
|
|
if !r.IsInDistrict {
|
|
|
|
|
count_outside++
|
|
|
|
|
status = "outside"
|
|
|
|
|
}
|
2026-02-16 16:38:04 +00:00
|
|
|
tags := db.ConvertFromPGData(r.Tags)
|
2026-02-16 17:59:18 +00:00
|
|
|
// add 2 here because our file lines are 1-indexed and we skip the header line, but we are ranging 0-indexed
|
|
|
|
|
errors, ok := errors_by_line[r.LineNumber]
|
2026-02-16 16:38:04 +00:00
|
|
|
if !ok {
|
|
|
|
|
errors = []UploadPoolError{}
|
|
|
|
|
}
|
2026-02-09 19:13:42 +00:00
|
|
|
pools = append(pools, UploadPoolRow{
|
2026-03-19 15:31:04 +00:00
|
|
|
Address: types.Address{
|
|
|
|
|
Country: "usa",
|
|
|
|
|
Locality: r.AddressLocality,
|
|
|
|
|
Number: r.AddressNumber,
|
|
|
|
|
PostalCode: r.AddressPostalCode,
|
|
|
|
|
Region: r.AddressRegion,
|
|
|
|
|
Street: r.AddressStreet,
|
|
|
|
|
},
|
|
|
|
|
Condition: r.Condition.String(),
|
|
|
|
|
Errors: errors,
|
|
|
|
|
Status: status,
|
|
|
|
|
Tags: tags,
|
2026-02-09 19:03:27 +00:00
|
|
|
})
|
|
|
|
|
}
|
2026-02-09 19:13:42 +00:00
|
|
|
return UploadPoolDetail{
|
2026-02-09 19:17:33 +00:00
|
|
|
CountExisting: count_existing,
|
2026-02-14 04:08:22 +00:00
|
|
|
CountOutside: count_outside,
|
2026-02-09 19:17:33 +00:00
|
|
|
CountNew: count_new,
|
2026-02-16 16:38:04 +00:00
|
|
|
Errors: file_errors,
|
2026-02-09 19:17:33 +00:00
|
|
|
Name: file.Name,
|
|
|
|
|
Pools: pools,
|
|
|
|
|
Status: file.Status.String(),
|
2026-02-09 19:03:27 +00:00
|
|
|
}, nil
|
2026-02-09 18:25:44 +00:00
|
|
|
}
|
2026-03-04 14:52:34 +00:00
|
|
|
func errorsByLine(ctx context.Context, file *models.FileuploadFile) ([]UploadPoolError, map[int32][]UploadPoolError, error) {
|
|
|
|
|
file_errors := make([]UploadPoolError, 0)
|
|
|
|
|
errors_by_line := make(map[int32][]UploadPoolError, 0)
|
|
|
|
|
error_rows, err := models.FileuploadErrorCSVS.Query(
|
|
|
|
|
models.SelectWhere.FileuploadErrorCSVS.CSVFileID.EQ(file.ID),
|
|
|
|
|
).All(ctx, db.PGInstance.BobDB)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return file_errors, errors_by_line, fmt.Errorf("Failed to lookup errors in csv %d: %w", file.ID, err)
|
|
|
|
|
}
|
|
|
|
|
for _, row := range error_rows {
|
|
|
|
|
e := UploadPoolError{
|
|
|
|
|
Column: uint(row.Col),
|
|
|
|
|
Line: uint(row.Line),
|
|
|
|
|
Message: row.Message,
|
|
|
|
|
}
|
|
|
|
|
if row.Line == 0 {
|
|
|
|
|
file_errors = append(file_errors, e)
|
|
|
|
|
} else {
|
|
|
|
|
//log.Info().Int32("line", row.Line).Msg("Found error")
|
|
|
|
|
by_line, ok := errors_by_line[row.Line]
|
|
|
|
|
if !ok {
|
|
|
|
|
errors_by_line[row.Line] = []UploadPoolError{e}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
by_line = append(by_line, e)
|
|
|
|
|
errors_by_line[row.Line] = by_line
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return file_errors, errors_by_line, nil
|
|
|
|
|
}
|