aboutsummaryrefslogtreecommitdiffstats
path: root/internal/thing/helper.go
blob: c3941ee45cff49a71b2f746af6094ead50fdab26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package thing

import (
	"strconv"

	tModel "donetick.com/core/internal/thing/model"
)

func isValidThingState(thing *tModel.Thing) bool {
	switch thing.Type {
	case "number":
		_, err := strconv.Atoi(thing.State)
		return err == nil
	case "text":
		return true
	case "boolean":
		return thing.State == "true" || thing.State == "false"
	default:
		return false
	}
}

func EvaluateThingChore(tchore *tModel.ThingChore, newState string) bool {
	if tchore.Condition == "" {
		return newState == tchore.TriggerState
	}

	switch tchore.Condition {
	case "eq":
		return newState == tchore.TriggerState
	case "neq":
		return newState != tchore.TriggerState
	}

	newStateInt, err := strconv.Atoi(newState)
	if err != nil {
		return false
	}
	TargetStateInt, err := strconv.Atoi(tchore.TriggerState)
	if err != nil {
		return false
	}

	switch tchore.Condition {
	case "gt":
		return newStateInt > TargetStateInt
	case "lt":
		return newStateInt < TargetStateInt
	case "gte":
		return newStateInt >= TargetStateInt
	case "lte":
		return newStateInt <= TargetStateInt
	default:
		return newState == tchore.TriggerState
	}

}