go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/worker/activities/stabilize.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package activities
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  
    14  	"go.charczuk.com/projects/nodes/pkg/dbmodel"
    15  	"go.charczuk.com/sdk/uuid"
    16  )
    17  
    18  type StabilizeArgs struct {
    19  	UserID  uuid.UUID
    20  	GraphID uuid.UUID
    21  }
    22  
    23  type StabilizeResult struct {
    24  	StabilizationNum uint64 `json:"stabilization_num"`
    25  }
    26  
    27  func (a Activities) Stabilize(ctx context.Context, args StabilizeArgs) (result StabilizeResult, err error) {
    28  	var graph dbmodel.Graph
    29  	var found bool
    30  	graph, found, err = a.Model.Graph(ctx, args.GraphID)
    31  	if err != nil {
    32  		return
    33  	}
    34  	if !found {
    35  		err = fmt.Errorf("graph not found with id: %v", args.GraphID)
    36  		return
    37  	}
    38  	if graph.UserID != args.UserID {
    39  		err = fmt.Errorf("graph not owned by user: graphID=%v userID=%v", args.GraphID, args.UserID)
    40  		return
    41  	}
    42  
    43  	store := &dbmodel.Store{
    44  		GraphID: graph.ID,
    45  		UserID:  graph.UserID,
    46  		Model:   &a.Model,
    47  	}
    48  	result.StabilizationNum, err = store.Stabilize(ctx, true)
    49  	if err != nil {
    50  		return
    51  	}
    52  	return
    53  }