/
activation.go
140 lines (134 loc) · 4.15 KB
/
activation.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package crudService
import (
"strconv"
"github.com/dotenx/dotenx/ao-api/models"
"github.com/dotenx/dotenx/ao-api/pkg/utils"
)
func (cm *crudManager) ActivatePipeline(accountId, pipelineId string) (err error) {
id, _ := strconv.Atoi(pipelineId)
hasAccess, err := cm.CheckAccess(accountId, id)
if err != nil {
return err
}
if !hasAccess {
return utils.ErrReachLimitationOfPlan
}
pipeline, err := cm.Store.GetById(noContext, id)
if err != nil {
return
}
// the comment codes used for change trigger frequency based on user plan
// if !pipeline.IsTemplate && !pipeline.IsInteraction {
// plan, err := utils.GetUserPlan(accountId)
// if err != nil {
// return err
// }
// planTriggerFrequency := fmt.Sprint(plan["plan_trigger_frequency"])
// redisKey := "ao-api-trigger-frequency-" + planTriggerFrequency
// redisValue := pipeline.Endpoint
// err = cm.RedisStore.AddToRedisSortedSet(redisKey, []interface{}{redisValue})
// if err != nil {
// return err
// }
// }
err = cm.Store.ActivatePipeline(noContext, accountId, pipelineId)
if err != nil {
return
}
err = cm.EnableEventBridgeScheduler(pipeline.Endpoint)
if err != nil {
return err
}
err = cm.TriggerService.EnableAllScheduledTriggers(pipeline.Endpoint)
if err != nil {
return err
}
return cm.NotifyPlanmanageForActivation(accountId, "activate", pipelineId, false)
}
func (cm *crudManager) CheckAccess(accId string, excutionId int) (bool, error) {
// NOTE: currently we ignore limitation because body of this request should change based on project type
// dt := automationDto{AccountId: accId, AutomationId: strconv.Itoa(excutionId)}
// json_data, err := json.Marshal(dt)
// if err != nil {
// return false, errors.New("bad input body")
// }
// requestBody := bytes.NewBuffer(json_data)
// token, err := utils.GeneratToken()
// if err != nil {
// return false, err
// }
// Requestheaders := []utils.Header{
// {
// Key: "Authorization",
// Value: token,
// },
// {
// Key: "Content-Type",
// Value: "application/json",
// },
// }
// httpHelper := utils.NewHttpHelper(utils.NewHttpClient())
// out, err, status, _ := httpHelper.HttpRequest(http.MethodPost, config.Configs.Endpoints.Admin+"/internal/user/access/automation", requestBody, Requestheaders, time.Minute, true)
// if err != nil {
// return false, err
// }
// //fmt.Println(string(out))
// if status != http.StatusOK && status != http.StatusAccepted {
// return false, errors.New("not ok with status: " + strconv.Itoa(status))
// }
// var res struct {
// Access bool `json:"access"`
// }
// if err := json.Unmarshal(out, &res); err != nil {
// return false, err
// }
// return res.Access, nil
return true, nil
}
func (cm *crudManager) DeActivatePipeline(accountId, pipelineId string, deleteRecord bool) (err error) {
id, _ := strconv.Atoi(pipelineId)
pipeline, err := cm.Store.GetById(noContext, id)
if err != nil {
return
}
// the comment codes used for change trigger frequency based on user plan
// if !pipeline.IsTemplate && !pipeline.IsInteraction {
// plan, err := utils.GetUserPlan(accountId)
// if err != nil {
// return err
// }
// planTriggerFrequency := fmt.Sprint(plan["plan_trigger_frequency"])
// redisKey := "ao-api-trigger-frequency-" + planTriggerFrequency
// redisValue := pipeline.Endpoint
// err = cm.RedisStore.RemoveFromRedisSortedSet(redisKey, []interface{}{redisValue})
// if err != nil {
// return err
// }
// }
err = cm.Store.DeActivatePipeline(noContext, accountId, pipelineId)
if err != nil {
return
}
err = cm.DisableEventBridgeScheduler(pipeline.Endpoint)
if err != nil {
return err
}
err = cm.TriggerService.DisableAllScheduledTriggers(pipeline.Endpoint)
if err != nil {
return err
}
return cm.NotifyPlanmanageForActivation(accountId, "deactivate", pipelineId, deleteRecord)
}
func (cm *crudManager) GetActivePipelines(accountId, projectName string) ([]models.Pipeline, error) {
pipelines, err := cm.Store.GetPipelines(noContext, accountId)
if err != nil {
return nil, err
}
actives := make([]models.Pipeline, 0)
for _, p := range pipelines {
if p.IsActive {
actives = append(actives, p)
}
}
return actives, nil
}