Imported files header validation

This commit is contained in:
J Sen Ooi 2023-12-17 19:13:25 +08:00
parent 3ee150e7d6
commit 704b5d27d8
2 changed files with 30 additions and 0 deletions

View file

@ -41,6 +41,12 @@ func (s *IOSheet) indexHeaders() {
}
}
// Ooi J Sen
// function to return headers from excel sheet
func (s *IOSheet) GetHeaders() []string {
return s.headers
}
func (s *IOSheet) GetColumn(str string) (col int, ok bool) {
if s.index == nil {
s.indexHeaders()

View file

@ -89,6 +89,20 @@ func serializeLocation[T ~[]string](location T) string {
return strings.Join(location, "/")
}
// Ooi J Sen
// Function to validate headers
func validateHeaders(expected, actual []string) bool {
if len(expected) != len(actual) {
return false
}
for i := range expected {
if expected[i] != actual[i] {
return false
}
}
return true
}
// CsvImport imports items from a CSV file. using the standard defined format.
//
// CsvImport applies the following rules/operations
@ -103,6 +117,16 @@ func (svc *ItemService) CsvImport(ctx context.Context, GID uuid.UUID, data io.Re
if err != nil {
return 0, err
}
// Ooi J Sen
// Access excel sheet headers
headers := sheet.GetHeaders()
// Validate column headers
expectedHeaders := []string{"HB.import_ref", "HB.location", "HB.labels", "HB.asset_id", "HB.archived", "HB.name", "HB.quantity", "HB.description", "HB.insured", "HB.notes", "HB.purchase_price", "HB.purchase_from", "HB.purchase_time", "HB.manufacturer", "HB.model_number", "HB.serial_number", "HB.lifetime_warranty", "HB.warranty_expires", "HB.warranty_details", "HB.sold_to", "HB.sold_price", "HB.sold_time", "HB.sold_notes",}
if !validateHeaders(expectedHeaders, headers) {
return 0, fmt.Errorf("CSV columns do not match the expected format")
}
// ========================================
// Labels