forked from Ilhasoft/goflow
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from weni-ai/fix/marshal-trigger-msg-order
fix msg and resume order
- Loading branch information
Showing
6 changed files
with
204 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package flows | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/nyaruka/gocommon/jsonx" | ||
"github.com/nyaruka/goflow/assets" | ||
"github.com/nyaruka/goflow/envs" | ||
"github.com/nyaruka/goflow/excellent/types" | ||
"github.com/nyaruka/goflow/utils" | ||
"github.com/pkg/errors" | ||
"github.com/shopspring/decimal" | ||
) | ||
|
||
type Order struct { | ||
CatalogID string `json:"catalog_id,omitempty"` | ||
ProductItems []ProductItem `json:"product_items,omitempty"` | ||
Text string `json:"text,omitempty"` | ||
} | ||
|
||
type orderEnvelope struct { | ||
CatalogID string `json:"catalog_id,omitempty"` | ||
ProductItems []ProductItem `json:"product_items,omitempty"` | ||
Text string `json:"text,omitempty"` | ||
} | ||
|
||
type ProductItem struct { | ||
Currency string `json:"currency,omitempty"` | ||
ItemPrice decimal.Decimal `json:"item_price,omitempty"` | ||
ProductRetailerID string `json:"product_retailer_id"` | ||
Quantity int64 `json:"quantity,omitempty"` | ||
} | ||
|
||
func (o *Order) Context(env envs.Environment) map[string]types.XValue { | ||
array := make([]types.XValue, len(o.ProductItems)) | ||
for i, p := range o.ProductItems { | ||
array[i] = types.NewXObject(map[string]types.XValue{ | ||
"currency": types.NewXText(p.Currency), | ||
"item_price": types.NewXNumber(p.ItemPrice), | ||
"product_retailer_id": types.NewXText(p.ProductRetailerID), | ||
"quantity": types.NewXNumberFromInt64(p.Quantity), | ||
}) | ||
} | ||
|
||
return map[string]types.XValue{ | ||
"catalog_id": types.NewXText(o.CatalogID), | ||
"product_items": types.NewXArray(array...), | ||
"text": types.NewXText(o.Text), | ||
} | ||
} | ||
|
||
func ReadOrder(sa SessionAssets, data json.RawMessage, missing assets.MissingCallback) (*Order, error) { | ||
var envelope orderEnvelope | ||
var err error | ||
|
||
if err = utils.UnmarshalAndValidate(data, &envelope); err != nil { | ||
return nil, errors.Wrap(err, "unable to read order") | ||
} | ||
|
||
o := &Order{ | ||
CatalogID: envelope.CatalogID, | ||
ProductItems: envelope.ProductItems, | ||
Text: envelope.Text, | ||
} | ||
|
||
return o, nil | ||
} | ||
|
||
func (o *Order) MarshalJSON() ([]byte, error) { | ||
oe := &orderEnvelope{ | ||
CatalogID: o.CatalogID, | ||
ProductItems: o.ProductItems, | ||
Text: o.Text, | ||
} | ||
|
||
return jsonx.Marshal(oe) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package triggers | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"testing" | ||
|
||
"github.com/nyaruka/gocommon/jsonx" | ||
) | ||
|
||
func TestMsgTrigger(t *testing.T) { | ||
|
||
var msgTrigger MsgTrigger | ||
|
||
err := json.Unmarshal([]byte(triggerJSON), &msgTrigger) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
tm, err := msgTrigger.MarshalJSON() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
log.Println(string(tm)) | ||
} | ||
|
||
func TestMsgTriggerMarshallJSON(t *testing.T) { | ||
var mtEnvelop msgTriggerEnvelope | ||
|
||
err := json.Unmarshal([]byte(triggerJSON), &mtEnvelop) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
res, err := jsonx.Marshal(mtEnvelop) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
log.Println(string(res)) | ||
} | ||
|
||
const triggerJSON = `{ | ||
"type": "msg", | ||
"environment": { | ||
"date_format": "DD-MM-YYYY", | ||
"time_format": "tt:mm", | ||
"timezone": "America/Argentina/Buenos_Aires", | ||
"number_format": { | ||
"decimal_symbol": ".", | ||
"digit_grouping_symbol": "," | ||
}, | ||
"default_country": "BR", | ||
"redaction_policy": "none", | ||
"max_value_length": 3840 | ||
}, | ||
"flow": { | ||
"uuid": "7a2236bc-4c92-4ad6-b81a-0d86d2fdedc0", | ||
"name": "catalogo" | ||
}, | ||
"contact": { | ||
"uuid": "7eed1ae9-4f7d-4211-a788-8d73ebdd518c", | ||
"id": 389, | ||
"name": "Roberta Moreira", | ||
"status": "active", | ||
"timezone": "America/Argentina/Buenos_Aires", | ||
"created_on": "2023-10-05T19:35:25.351816Z", | ||
"urns": [ | ||
"whatsapp:5582999489287?channel=3065fa26-593b-4517-9318-6050165c78d7\\u0026id=387\\u0026priority=1000" | ||
] | ||
}, | ||
"params": { | ||
"order": { | ||
"catalog_id": "1729754620797120", | ||
"product_items": [ | ||
{ | ||
"currency": "BRL", | ||
"item_price": 2.46, | ||
"product_retailer_id": "1", | ||
"quantity": 1 | ||
} | ||
], | ||
"text": "" | ||
} | ||
}, | ||
"triggered_on": "2023-10-05T19:35:25.576204487Z", | ||
"msg": { | ||
"uuid": "22454ba0-b8db-440d-a1a6-1a6bb56e7add", | ||
"id": 162145, | ||
"urn": "whatsapp:5582999489287", | ||
"channel": { | ||
"uuid": "3065fa26-593b-4517-9318-6050165c78d7", | ||
"name": "Teste Weni Cloud 5" | ||
}, | ||
"text": "", | ||
"external_id": "wamid.HB1gMNTU4ODkzaaN2asaTasasYda1fa1asaOsaTA21FsQaa3I1AaaqEa2hssgWa23M0V2aCMEafY4NDYaswNDaRGOaUFBNzcwNjQysRgA=", | ||
"order": { | ||
"catalog_id": "1729754620797120", | ||
"product_items": [ | ||
{ | ||
"currency": "BRL", | ||
"item_price": 2.46, | ||
"product_retailer_id": "1", | ||
"quantity": 1 | ||
} | ||
], | ||
"text": "" | ||
} | ||
} | ||
}` |