Skip to content

Commit

Permalink
fix(api): improve unit name creation
Browse files Browse the repository at this point in the history
The unit-name API must be called at least once
before sending all other metrics
  • Loading branch information
gsanchietti committed Sep 17, 2024
1 parent 8e8eb9a commit a213de9
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions api/methods/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ func SetUnitName(c *gin.Context) {
unitId := c.MustGet("UnitId").(string)
dbpool, dbctx := storage.ReportInstance()
// check if uuid is valid
_, err := dbpool.Exec(dbctx, "SELECT name FROM units WHERE uuid = $1", unitId)
if err != nil {
var uuid string
err := dbpool.QueryRow(dbctx, "SELECT uuid FROM units WHERE uuid = $1", unitId).Scan(&uuid)
if err != nil || uuid != unitId {
// insert a new unit and return the id
_, err := dbpool.Exec(dbctx, "INSERT INTO units (uuid, name) VALUES ($1, $2)", unitId, req.Name)
if err != nil {
Expand Down Expand Up @@ -72,13 +73,10 @@ func checkUnitId(unitId string) error {
dbpool, dbctx := storage.ReportInstance()

// check if uuid is valid
_, err := dbpool.Exec(dbctx, "SELECT uuid FROM units WHERE uuid = $1", unitId)
if err != nil {
// insert a new unit and return the id
_, err := dbpool.Exec(dbctx, "INSERT INTO units (uuid) VALUES ($1)", unitId)
if err != nil {
return errors.New("error inserting unit")
}
var uuid string
err := dbpool.QueryRow(dbctx, "SELECT uuid FROM units WHERE uuid = $1", unitId).Scan(&uuid)
if err != nil || uuid != unitId {
return errors.New("unit not found")
}

return nil
Expand Down

0 comments on commit a213de9

Please sign in to comment.