Skip to content

Commit

Permalink
Send Prontogram message
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed Jul 4, 2024
1 parent 495d088 commit 68b2639
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- POSTGRES_USER
- POSTGRES_PASSWORD
- OFFER_VALIDATION_TIME
- PRONTOGRAM_ENDPOINT
- BANK_ENDPOINT
- BANK_CALLBACK
- BANK_PAYMENT_ENDPOINT
Expand Down
4 changes: 2 additions & 2 deletions bpmn/acmesky.bpmn
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@
<bpmn:messageEventDefinition id="MessageEventDefinition_0z59sko" />
</bpmn:endEvent>
</bpmn:process>
<bpmn:process id="Process_Prontogram" name="sd_prontogram" isExecutable="false">
<bpmn:process id="Process_Prontogram" name="sd_prontogram" isExecutable="true">
<bpmn:sequenceFlow id="Flow_0a080if" sourceRef="TM_Propagate_Message_From_Prontogram" targetRef="End_Prontogram" />
<bpmn:sequenceFlow id="Flow_1r7wwko" sourceRef="ST_Save_Info_On_Prontogram" targetRef="TM_Propagate_Message_From_Prontogram" />
<bpmn:serviceTask id="ST_Save_Info_On_Prontogram" name="Save info for historic purpose">
Expand Down Expand Up @@ -2118,4 +2118,4 @@
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
</bpmn:definitions>
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ services:
- SENTRY_DSN=${SENTRY_DSN}
- DATABASE_DSN=${DATABASE_DSN}
- OFFER_VALIDATION_TIME=${OFFER_VALIDATION_TIME}
- PRONTOGRAM_ENDPOINT=${PRONTOGRAM_ENDPOINT}
- BANK_ENDPOINT=${BANK_ENDPOINT}
- BANK_PAYMENT_ENDPOINT=${BANK_PAYMENT_ENDPOINT}
- BANK_CALLBACK=${BANK_CALLBACK}
Expand Down
8 changes: 8 additions & 0 deletions internal/handlers/acmesky/st_prepare_offer.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ func STPrepareOffer(client worker.JobClient, job entities.Job) {
}
}

// Preload user info
if err := db.Where("id = ?", offer.Id).Preload("User").First(&offer).Error; err != nil {
log.Errorf("[%s] [%d] Offer not found", job.Type, jobKey)
acmejob.FailJob(client, job)
return
}
variables["offer"] = offer

request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
Expand All @@ -104,5 +110,7 @@ func STPrepareOffer(client worker.JobClient, job entities.Job) {
}

log.Infof("[%s] [%d] Successfully completed job", job.Type, jobKey)
acmejob.JobVariables[job.Type] <- variables

acmejob.JobStatuses.Close(job.Type, 0)
}
2 changes: 1 addition & 1 deletion internal/handlers/acmesky/tm_send_offer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func TMSendOffer(client worker.JobClient, job entities.Job) {

log.Infof("[%s] [%d] Successfully completed job", job.Type, jobKey)

acmejob.JobVariables[job.Type] <- variables["offer"].(map[string]interface{})
acmejob.JobVariables[job.Type] <- variables
acmejob.JobStatuses.Close(job.Type, 0)
}
46 changes: 44 additions & 2 deletions internal/handlers/prontogram/st_save_info_on_prontogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ package handlers

import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"

"github.com/charmbracelet/log"

"github.com/acme-sky/workers/internal/config"
"github.com/acme-sky/workers/internal/http"
acmejob "github.com/acme-sky/workers/internal/job"
"github.com/acme-sky/workers/internal/models"
"github.com/camunda/zeebe/clients/go/v8/pkg/entities"
"github.com/camunda/zeebe/clients/go/v8/pkg/worker"
)

// Service used to save info into Prontogram backend service.
func STSaveInfoOnProntogram(client worker.JobClient, job entities.Job) {
jobKey := job.GetKey()

Expand All @@ -18,13 +27,46 @@ func STSaveInfoOnProntogram(client worker.JobClient, job entities.Job) {
return
}

request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
m := variables["offer"].(map[string]interface{})

jsonData, err := json.Marshal(m)
if err != nil {
fmt.Println("Error marshaling map to JSON:", err)
return
}

var offer models.Offer
err = json.Unmarshal(jsonData, &offer)
if err != nil {
fmt.Println("Error unmarshaling JSON to struct:", err)
return
}

conf, _ := config.GetConfig()
endpoint := fmt.Sprintf("%s/sendMessage", conf.String("prontogram.endpoint"))

fmt.Println(endpoint)
expirationInt, _ := strconv.ParseInt(offer.Expired, 10, 64)
expirationDate := time.Unix(expirationInt, 0)
payload := http.ProntogramMessageRequest{
Message: offer.Message,
Expiration: expirationDate.Format("2006-01-02T15:04:05Z"),
Username: *offer.User.ProntogramUsername,
Sid: " ",
}
_, err = http.MakeProntogramRequest(endpoint, payload)

if err != nil {
log.Errorf("[%s] [%d] Error for offer `%d`: %s", job.Type, jobKey, offer.Id, err.Error())
acmejob.FailJob(client, job)
return
}

log.Debug("Processing data:", variables)
request, err := client.NewCompleteJobCommand().JobKey(jobKey).VariablesFromMap(variables)
if err != nil {
acmejob.FailJob(client, job)
return
}

ctx := context.Background()
_, err = request.Send(ctx)
Expand Down
52 changes: 52 additions & 0 deletions internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ type AirportInfoResponseBody struct {
Longitude float32 `json:"longitude"`
}

type ProntogramMessageRequest struct {
Message string `json:"message"`
Expiration string `json:"expiration"`
Username string `json:"username"`
Sid string `json:"sid"`
}

type ProntogramMessageResponse struct {
Message string `json:"message"`
Sid string `json:"sid"`
Status int `json:"status"`
}

// Make a new request to an endpoint with a `body` and returns a response body
// or an error.
func MakeRequest(endpoint string, body map[string]interface{}) (*ResponseBody, error) {
Expand Down Expand Up @@ -227,3 +240,42 @@ func GetAirportInfo(endpoint string) (*AirportInfoResponseBody, error) {

return &responseBody, nil
}

// Make a new request for Prontogram and returns a ProntogramMessageResponse
func MakeProntogramRequest(endpoint string, body ProntogramMessageRequest) (*ProntogramMessageResponse, error) {
jsonBody, _ := json.Marshal(body)
bodyReader := bytes.NewReader(jsonBody)

req, err := http.NewRequest(http.MethodPost, endpoint, bodyReader)

if err != nil {
return nil, err
}

httpClient := http.Client{
Timeout: 30 * time.Second,
}

req.Header.Add("Content-Type", "application/json")

res, err := httpClient.Do(req)
if err != nil {
return nil, err
}

resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not read response body: %s", err.Error()))
}

if res.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("HTTP request returned a status %d and response `%s`", res.StatusCode, resBody))
}

var responseBody ProntogramMessageResponse
if err := json.Unmarshal(resBody, &responseBody); err != nil {
return nil, errors.New(fmt.Sprintf("Could not unmarshal response body: %s", err))
}

return &responseBody, nil
}

0 comments on commit 68b2639

Please sign in to comment.