Skip to content

Commit

Permalink
feat: allow override completed task
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Feb 14, 2023
1 parent 1351b83 commit 75ac78c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/actor/subscription/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (a *Actor) handleTaskCompletedEvent(data map[string]interface{}) {
return
}

if err := a.store.ManualCompleteTask(a.ctx, e.Validator, e.Phase, e.Task, e.Points); err != nil {
if err := a.store.ManualCompleteTask(a.ctx, e.Validator, e.Phase, e.Task, e.Points, e.Override); err != nil {
log.Err(err).Interface("data", data).Msg("🤕 Couldn't manually complete task")
}
}
Expand Down
34 changes: 34 additions & 0 deletions app/nemeton/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ func (s *Store) ManualCompleteTask(
phaseNB int,
taskID string,
rewards *uint64,
override bool,
) error {
phase := s.GetPhase(phaseNB)
var task *Task
Expand All @@ -531,6 +532,12 @@ func (s *Store) ManualCompleteTask(
return fmt.Errorf("task '%s' not found in phase '%d'", taskID, phaseNB)
}

if override {
if err := s.resetTaskPoints(ctx, valoper, phaseNB, taskID); err != nil {
return err
}
}

points := uint64(0)
if rewards != nil {
points = *rewards
Expand Down Expand Up @@ -564,6 +571,33 @@ func (s *Store) ensureTaskCompleted(ctx context.Context, filter bson.M, phase in
return err
}

func (s *Store) resetTaskPoints(ctx context.Context, valoper types.ValAddress, phase int, task string) error {
val, err := s.GetValidatorByValoper(ctx, valoper)
if err != nil {
return err
}

taskState := val.Task(phase, task)
if taskState != nil && taskState.Completed {
_, err := s.db.Collection(validatorsCollectionName).UpdateOne(
ctx,
bson.M{
"valoper": valoper,
},
bson.M{
"$unset": bson.M{
fmt.Sprintf("tasks.%d.%s", phase, task): 1,
},
"$inc": bson.M{
"points": int64(taskState.EarnedPoints) * -1,
},
},
)
return err
}
return nil
}

// getTaskPhaseByType return the current phase at the given time and the current **first** task for the given task type.
func (s *Store) getTaskPhaseByType(id string, at time.Time) (*Phase, *Task) {
phase, tasks := s.getTasksPhaseByType(id, at)
Expand Down
1 change: 1 addition & 0 deletions graphql/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ type TaskCompletedEvent struct {
Phase int `json:"phase"`
Task string `json:"task"`
Points *uint64 `json:"points,omitempty"`
Override bool `json:"override"`
}

func (e *TaskCompletedEvent) Marshal() (map[string]interface{}, error) {
Expand Down
41 changes: 22 additions & 19 deletions graphql/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion graphql/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions graphql/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ type Mutation {
The points to attribute, if applicable. The priority will be given to the static rewards amount specified in the task definition.
"""
points: UInt64

"""
Allow updating points even if the task was already completed.
"""
override: Boolean = False
): Void @auth

"""
Expand Down
20 changes: 12 additions & 8 deletions graphql/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 75ac78c

Please sign in to comment.