-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathteam.gno
222 lines (192 loc) · 5.88 KB
/
team.gno
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package zentasktic_project
import (
"errors"
"gno.land/p/demo/zentasktic"
)
// AddTeam adds a new team to the Teams tree
func (t *Team) AddTeam() error {
if _, terr := GetTeamById(t.Id); terr == nil {
// terr == nil means we found a team
return errors.New("team ID already exists")
}
Teams.Set(t.Id, t)
return nil
}
// RemoveTeam removes a team from the Teams tree and cleans up associated data
func (t *Team) RemoveTeam() error {
// Check if the team exists
team, terr := GetTeamById(t.Id)
if terr != nil {
return errors.New("team ID not found")
}
// Remove the team from the Teams tree
Teams.Remove(team.Id)
// Remove the team's tasks and projects
TeamTasks.Remove(team.Id)
TeamProjects.Remove(team.Id)
return nil
}
// EditTeam updates an existing team's details in the Teams tree
func (t *Team) EditTeam() error {
_, terr := GetTeamById(t.Id)
if terr != nil {
return errors.New("team ID not found")
}
Teams.Set(t.Id, t)
return nil
}
// AssignTeamToTask assigns a task to a team
func (t *Team) AssignTeamToTask(wt WorkableTask) error {
// Add the task to the team's task list
tasks, _ := TeamTasks.Get(t.Id)
var updatedTasks []WorkableTask
if tasks != nil {
updatedTasks = tasks.([]WorkableTask)
}
for _, task := range updatedTasks {
if task.Id == wt.Id {
return nil // Task already assigned to team
}
}
updatedTasks = append(updatedTasks, wt)
TeamTasks.Set(t.Id, updatedTasks)
return nil
}
// UnassignTeamFromTask unassigns a task from a team
func (t *Team) UnassignTeamFromTask(wt WorkableTask) error {
tasks, exist := TeamTasks.Get(t.Id)
if !exist {
return errors.New("team task not found")
}
updatedTasks := tasks.([]WorkableTask)
for i, task := range updatedTasks {
if task.Id == wt.Id {
updatedTasks = append(updatedTasks[:i], updatedTasks[i+1:]...)
if len(updatedTasks) == 0 {
TeamTasks.Remove(t.Id)
} else {
TeamTasks.Set(t.Id, updatedTasks)
}
return nil
}
}
return errors.New("team task not found")
}
// AssignTeamToProject assigns a project to a team
func (t *Team) AssignTeamToProject(wp *WorkableProject) error {
// Add the project to the team's project list
projects, _ := TeamProjects.Get(t.Id)
var updatedProjects []*WorkableProject
if projects != nil {
updatedProjects = projects.([]*WorkableProject)
}
for _, project := range updatedProjects {
if project.Id == wp.Id {
return nil // Project already assigned to team
}
}
updatedProjects = append(updatedProjects, wp)
TeamProjects.Set(t.Id, updatedProjects)
return nil
}
// UnassignTeamFromProject unassigns a project from a team
func (t *Team) UnassignTeamFromProject(wp *WorkableProject) error {
projects, exist := TeamProjects.Get(t.Id)
if !exist {
return errors.New("team project not found")
}
updatedProjects := projects.([]*WorkableProject)
for i, project := range updatedProjects {
if project.Id == wp.Id {
updatedProjects = append(updatedProjects[:i], updatedProjects[i+1:]...)
if len(updatedProjects) == 0 {
TeamProjects.Remove(t.Id)
} else {
TeamProjects.Set(t.Id, updatedProjects)
}
return nil
}
}
return errors.New("team project not found")
}
// GetTeamById retrieves a team by ID
func GetTeamById(teamId string) (*Team, error) {
team, exist := Teams.Get(teamId)
if exist {
return team.(*Team), nil
}
return nil, errors.New("team ID not found")
}
// GetAllTeams retrieves all teams and returns them as a JSON string
func GetAllTeams() string {
teams := make([]*Team, 0)
Teams.Iterate("", "", func(key string, value interface{}) bool {
teams = append(teams, value.(*Team))
return false // Continue iteration
})
teamsObject := &TeamsObject{Teams: teams}
marshalledTeams, err := teamsObject.MarshalJSON()
if err != nil {
return ""
}
return string(marshalledTeams)
}
// GetTeamTasks retrieves all tasks associated with a team
func (t *Team) GetTeamTasks() (string, error) {
tasks, exist := TeamTasks.Get(t.Id)
if !exist {
return "", errors.New("team tasks not found")
}
tasksObject := zentasktic.TasksObject{Tasks: make([]zentasktic.Task, 0)}
for _, task := range tasks.([]WorkableTask) {
tasksObject.Tasks = append(tasksObject.Tasks, task.Task)
}
marshalledTasks, err := tasksObject.MarshalJSON()
if err != nil {
return "", err
}
return string(marshalledTasks), nil
}
// GetTeamProjects retrieves all projects associated with a team
func (t *Team) GetTeamProjects() (string, error) {
projects, exist := TeamProjects.Get(t.Id)
if !exist {
return "", errors.New("team projects not found")
}
projectsObject := zentasktic.ProjectsObject{Projects: make([]zentasktic.Project, 0)}
for _, project := range projects.([]WorkableProject) {
projectsObject.Projects = append(projectsObject.Projects, project.Project)
}
marshalledProjects, err := projectsObject.MarshalJSON()
if err != nil {
return "", err
}
return string(marshalledProjects), nil
}
// GetTeamsWithAssignedTasks retrieves all teams with assigned tasks from TeamTasks
func GetTeamsWithAssignedTasks() (string) {
teamsWithAssignedTasks := make([]TeamTasksObject, 0)
TeamTasks.Iterate("", "", func(key string, value interface{}) bool {
tasks, ok := value.([]WorkableTask)
if !ok {
return false // Continue iteration if the value is not of the expected type
}
// Cast []*WorkableTask to []zentasktic.Task
taskList := make([]zentasktic.Task, len(tasks))
for i, task := range tasks {
taskList[i] = task.Task
}
teamTaskObject := TeamTasksObject{
TeamId: key,
TeamTasks: taskList,
}
teamsWithAssignedTasks = append(teamsWithAssignedTasks, teamTaskObject)
return false // Continue iteration
})
teamsTasksObject := TeamsTasksObject{TeamsTasks: teamsWithAssignedTasks}
marshalledTeamsTasks, err := teamsTasksObject.MarshalJSON()
if err != nil {
return ""
}
return string(marshalledTeamsTasks)
}