-
Notifications
You must be signed in to change notification settings - Fork 4
/
cron.go
133 lines (110 loc) · 3.95 KB
/
cron.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package giftcard
import (
"time"
"github.com/ottemo/commerce/app"
"github.com/ottemo/commerce/app/models/order"
"github.com/ottemo/commerce/db"
"github.com/ottemo/commerce/env"
"github.com/ottemo/commerce/utils"
)
// SendTask will send email with purchased gift cards info
func SendTask(params map[string]interface{}) error {
giftCardCollection, err := db.GetCollection(ConstCollectionNameGiftCard)
if err != nil {
return env.ErrorDispatch(err)
}
// send an email if we find gift cards in the order
// use the email template from the configuration value
giftCardTemplateEmail := utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathGiftEmailTemplate))
if giftCardTemplateEmail == "" {
return env.ErrorDispatch(env.ErrorNew(ConstErrorModule, ConstErrorLevel, "cccda57c-7f13-48be-a1fb-d29e545051ce", "No giftcard email template found."))
}
currentTime := time.Now()
ignoreDeliveryDate := false
// handle usage of params
// key "giftCards" is required and should be an array of giftCard ids
// key "ignoreDeliveryDate" is optional and can be true or false
if params != nil {
if giftCardsToSend, present := params["giftCards"]; present {
giftCardsToSend := utils.InterfaceToArray(giftCardsToSend)
if len(giftCardsToSend) >= 1 {
if err := giftCardCollection.AddFilter("_id", "in", giftCardsToSend); err != nil {
return env.ErrorDispatch(err)
}
}
}
if ignoreDateValue, present := params["ignoreDeliveryDate"]; present {
ignoreDeliveryDate = utils.InterfaceToBool(ignoreDateValue)
}
}
if !ignoreDeliveryDate {
if err := giftCardCollection.AddFilter("delivery_date", "<=", currentTime); err != nil {
return env.ErrorDispatch(err)
}
}
if err := giftCardCollection.AddFilter("status", "=", ConstGiftCardStatusNew); err != nil {
return env.ErrorDispatch(err)
}
records, err := giftCardCollection.Load()
if err != nil {
return env.ErrorDispatch(err)
}
if len(records) < 1 {
return nil
}
customInfo := map[string]interface{}{
"Url": app.GetStorefrontURL(""),
}
giftCardEmailSubject := utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathGiftEmailSubject))
if len(giftCardEmailSubject) < 2 {
giftCardEmailSubject = "Your giftcard has arrived"
}
for _, record := range records {
giftCardRecipientEmail := utils.InterfaceToString(record["recipient_mailbox"])
giftCardInfo := map[string]interface{}{
"Amount": utils.RoundPrice(utils.InterfaceToFloat64(record["amount"])),
"Code": utils.InterfaceToString(record["code"]),
"RecipientName": utils.InterfaceToString(record["name"]),
"RecipientEmail": giftCardRecipientEmail,
"Message": utils.InterfaceToString(record["message"]),
}
buyerInfo := make(map[string]interface{})
if orderID, present := record["order_id"]; present {
currentOrder, err := order.LoadOrderByID(utils.InterfaceToString(orderID))
if err == nil {
buyerInfo["Name"] = currentOrder.Get("customer_name")
buyerInfo["Email"] = currentOrder.Get("customer_email")
}
} else {
// set default name and email
buyerInfo["Name"] = utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathGiftCardAdminBuyerName))
buyerInfo["Email"] = utils.InterfaceToString(env.ConfigGetValue(ConstConfigPathGiftCardAdminBuyerEmail))
}
recipientInfo := map[string]interface{}{
"Name": utils.InterfaceToString(record["name"]),
"Email": giftCardRecipientEmail,
}
giftCardEmail, err := utils.TextTemplate(giftCardTemplateEmail,
map[string]interface{}{
"Recipient": recipientInfo,
"Buyer": buyerInfo,
"GiftCard": giftCardInfo,
"Site": customInfo,
})
if err != nil {
_ = env.ErrorDispatch(err)
continue
}
err = app.SendMail(giftCardRecipientEmail, giftCardEmailSubject, giftCardEmail)
if err != nil {
_ = env.ErrorDispatch(err)
continue
}
record["status"] = ConstGiftCardStatusDelivered
_, err = giftCardCollection.Save(record)
if err != nil {
_ = env.ErrorDispatch(err)
}
}
return nil
}