-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
50 lines (42 loc) · 1.58 KB
/
create.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
package api
import (
"context"
"fmt"
"time"
customerpb "tmprldemo/internal/pb/customer/v1"
"tmprldemo/internal/customer/workflows/verifyphone"
"go.temporal.io/api/enums/v1"
"go.temporal.io/sdk/client"
)
// CreateCustomer creates a new customer in the coffee shop system.
// It also executes a workflow responsible for the phone verification process for the newly created customer.
func (s *customerServiceGRPCServer) CreateCustomer(ctx context.Context, request *customerpb.CreateCustomerRequest) (*customerpb.CreateCustomerResponse, error) {
domainCustomer, err := ConvertFromPbToCustomer(request.Customer)
if err != nil {
return nil, err
}
// should place the request id on the context
createdCustomer, err := s.customerCreator.Create(ctx, *domainCustomer)
if err != nil {
return nil, err
}
workflowOptions := client.StartWorkflowOptions{
ID: createdCustomer.ID.String(),
TaskQueue: "TEMPORAL_COFFEE_SHOP_TASK_QUEUE",
WorkflowExecutionTimeout: time.Hour * 24,
WorkflowIDReusePolicy: enums.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE_FAILED_ONLY,
}
params := verifyphone.WorkflowParams{
PhoneNumber: createdCustomer.PhoneNumber,
MaximumAttempts: 3,
CodeLength: 4,
CodeValidityDuration: time.Minute * 2,
}
_, err = s.temporalClient.ExecuteWorkflow(ctx, workflowOptions, "VerifyPhoneWorkflow", params)
if err != nil {
return nil, fmt.Errorf("failed to execute verify phone workflow: %w", err)
}
return &customerpb.CreateCustomerResponse{
Customer: ConvertFromCustomerToPb(*createdCustomer),
}, nil
}