|
| 1 | +package caller |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "go.temporal.io/sdk/workflow" |
| 7 | + |
| 8 | + "github.com/temporalio/samples-go/nexus-messaging/callerpattern/service" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + CallerTaskQueue = "nexus-messaging-caller-task-queue" |
| 13 | + endpointName = "my-nexus-endpoint-name" |
| 14 | +) |
| 15 | + |
| 16 | +// CallerWorkflow calls the NexusGreetingService operations for a given userID |
| 17 | +// and returns a log of actions taken. |
| 18 | +func CallerWorkflow(ctx workflow.Context, userID string) ([]string, error) { |
| 19 | + var log []string |
| 20 | + c := workflow.NewNexusClient(endpointName, service.ServiceName) |
| 21 | + |
| 22 | + // 1. Get supported languages. |
| 23 | + fut := c.ExecuteOperation(ctx, service.GetLanguagesOperationName, service.GetLanguagesInput{ |
| 24 | + IncludeUnsupported: false, |
| 25 | + UserID: userID, |
| 26 | + }, workflow.NexusOperationOptions{}) |
| 27 | + var langsOut service.GetLanguagesOutput |
| 28 | + if err := fut.Get(ctx, &langsOut); err != nil { |
| 29 | + return nil, fmt.Errorf("getLanguages failed: %w", err) |
| 30 | + } |
| 31 | + log = append(log, fmt.Sprintf("getLanguages returned %d languages", len(langsOut.Languages))) |
| 32 | + |
| 33 | + // 2. Set language to Arabic. |
| 34 | + fut = c.ExecuteOperation(ctx, service.SetLanguageOperationName, service.SetLanguageInput{ |
| 35 | + Language: service.Arabic, |
| 36 | + UserID: userID, |
| 37 | + }, workflow.NexusOperationOptions{}) |
| 38 | + var prevLang service.Language |
| 39 | + if err := fut.Get(ctx, &prevLang); err != nil { |
| 40 | + return nil, fmt.Errorf("setLanguage failed: %w", err) |
| 41 | + } |
| 42 | + log = append(log, fmt.Sprintf("setLanguage(Arabic) returned previous language: %s", prevLang)) |
| 43 | + |
| 44 | + // 3. Get current language (assert it is Arabic). |
| 45 | + fut = c.ExecuteOperation(ctx, service.GetLanguageOperationName, service.GetLanguageInput{ |
| 46 | + UserID: userID, |
| 47 | + }, workflow.NexusOperationOptions{}) |
| 48 | + var currentLang service.Language |
| 49 | + if err := fut.Get(ctx, ¤tLang); err != nil { |
| 50 | + return nil, fmt.Errorf("getLanguage failed: %w", err) |
| 51 | + } |
| 52 | + if currentLang != service.Arabic { |
| 53 | + return nil, fmt.Errorf("expected Arabic, got %s", currentLang) |
| 54 | + } |
| 55 | + log = append(log, fmt.Sprintf("getLanguage returned: %s (confirmed Arabic)", currentLang)) |
| 56 | + |
| 57 | + // 4. Approve the workflow. |
| 58 | + fut = c.ExecuteOperation(ctx, service.ApproveOperationName, service.ApproveInput{ |
| 59 | + Name: "CallerWorkflow", |
| 60 | + UserID: userID, |
| 61 | + }, workflow.NexusOperationOptions{}) |
| 62 | + var approveOut service.ApproveOutput |
| 63 | + if err := fut.Get(ctx, &approveOut); err != nil { |
| 64 | + return nil, fmt.Errorf("approve failed: %w", err) |
| 65 | + } |
| 66 | + log = append(log, "approve sent successfully") |
| 67 | + |
| 68 | + return log, nil |
| 69 | +} |
0 commit comments