forked from VATUSA/TaskRunner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send emails when transfers are started, accepted, or rejected.
- Loading branch information
1 parent
c5e4deb
commit 2061b24
Showing
7 changed files
with
152 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package transfer | ||
|
||
import ( | ||
"JobScheduler/pkg/jobs/email" | ||
"bytes" | ||
"fmt" | ||
"html/template" | ||
) | ||
|
||
const ( | ||
Accepted = "ACCEPTED" | ||
Rejected = "REJECTED" | ||
) | ||
|
||
// TODO: should we minmize duplicated content here and only store/expect CID from the incoming event? | ||
type Transfer struct { | ||
FirstName string `json:"fname"` // Transferee's first name | ||
LastName string `json:"lname"` // Transferee's last name | ||
TransfereeAddress string `json:"email_address"` | ||
CID string `json:"cid"` // Transferee's CID | ||
TransferFrom string `json:"transfer_from"` // Transferring from (FAC ID) | ||
TransferTo string `json:"transfer_to"` // Transferring to (FAC ID) | ||
Reason string `json:"transfer_reason"` | ||
} | ||
|
||
// TODO: tracking these items on inbound events seems redundant. should they be stored from the initial transfer event | ||
// and fetched later (ie from a DB)? can they be looked up again on state changes (from an API)? | ||
type TransferStateChange struct { | ||
Transfer | ||
State string `json:"transfer_state"` // The state of the transfer | ||
Actor string `json:"transfer_actor"` // the person who acted on the transfer request | ||
StateChangeReason string `json:"transfer_state_reason"` // the reason why the request was accepted or rejected | ||
Contacts []string `json:"contacts"` // the emails to contact in case of questions | ||
} | ||
|
||
func transferEmail(transfer any, tName string) (string, error) { | ||
t, err := template.ParseFiles(tName) | ||
if nil != err { | ||
return "", err | ||
} | ||
b := new(bytes.Buffer) | ||
if err = t.Execute(b, transfer); err != nil { | ||
return "", err | ||
} | ||
return b.String(), nil | ||
} | ||
|
||
func HandleTransferStarted(transfer Transfer) error { | ||
payload, err := transferEmail(transfer, "transfer_started.html") | ||
if err != nil { | ||
return err | ||
} | ||
email.SendEmail(email.Payload{ | ||
DestinationAddress: transfer.TransfereeAddress, | ||
Subject: "VATSIM Transfer Initiatied", | ||
Body: payload, | ||
}) | ||
return nil | ||
} | ||
|
||
func HandleTransferStateChange(transfer TransferStateChange) error { | ||
var tName string | ||
if transfer.State == Accepted { | ||
tName = "transfer_accepted.html" | ||
} else if transfer.State == Rejected { | ||
tName = "transfer_rejected.html" | ||
} else { | ||
return fmt.Errorf("unexpected transfer state: %v", transfer.State) | ||
} | ||
payload, err := transferEmail(transfer, tName) | ||
if err != nil { | ||
return err | ||
} | ||
email.SendEmail(email.Payload{ | ||
DestinationAddress: transfer.TransfereeAddress, | ||
Subject: "VATSIM Transfer Initiatied", | ||
Body: payload, | ||
}) | ||
return nil | ||
} |
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 @@ | ||
<p>A transfer request for {{.FirstName}} {{.LastName}} ({{.CID}}) from {{.TransferFrom}} to {{.TransferTo}} as been <strong>accepted</strong>.<br> |
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,8 @@ | ||
Dear {{.FirstName}} {{.LastName}},<br> | ||
<br> | ||
This email is to inform you that your request to transfer to {{.TransferTo}} has been | ||
<strong>rejected</strong> by {{.Actor}}. | ||
<br><br> | ||
The reason specified for the rejection of your request is: <br><br><em>{{.Reason}}</em>.<br> | ||
<br> | ||
If you have any questions or wish to appeal this decision, please contact:{{range .Contacts}} {{.}}{{end}}. |
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,35 @@ | ||
<p><strong>{{.FirstName}} {{.LastName}} ({{.CID}})</strong> has requested a transfer to {{.TransferTo}}.</p> | ||
<table class="callout" | ||
style="Margin-bottom: 16px; border-collapse: collapse; border-spacing: 0; margin-bottom: 16px; padding: 0; text-align: left; vertical-align: top;"> | ||
<tbody> | ||
<tr style="padding: 0; text-align: left; vertical-align: top;"> | ||
<th class="callout-inner light-gray" | ||
style="Margin: 0; background: #fefefe; border: 1px solid #cbcbcb; color: #0a0a0a; font-family: Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 1.3; margin: 0; padding: 10px; text-align: left; width: 100%;"> | ||
<table class="row" | ||
style="border-collapse: collapse; border-spacing: 0; padding: 0; position: relative; text-align: left; vertical-align: top; width: 100%;"> | ||
<tbody> | ||
<tr style="padding: 0; text-align: left; vertical-align: top;"> | ||
<th class="small-12 large-12 columns first last" | ||
style="Margin: 0 auto; color: #0a0a0a; font-family: Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 1.3; margin: 0 auto; padding: 0; padding-bottom: 16px; padding-left: 16px; padding-right: 16px; text-align: left; width: 564px;"> | ||
<table | ||
style="border-collapse: collapse; border-spacing: 0; padding: 0; text-align: left; vertical-align: top; width: 100%;"> | ||
<tbody> | ||
<tr style="padding: 0; text-align: left; vertical-align: top;"> | ||
<th style="Margin: 0; color: #0a0a0a; font-family: Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 1.3; margin: 0; padding: 0; text-align: left;"> | ||
<p style="Margin: 0; Margin-bottom: 10px; color: #0a0a0a; font-family: Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 1.3; margin: 0; margin-bottom: 10px; padding: 0; text-align: left;">{{.Reason}}</p> | ||
</th> | ||
<th class="expander" | ||
style="Margin: 0; color: #0a0a0a; font-family: Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 1.3; margin: 0; padding: 0 !important; text-align: left; visibility: hidden; width: 0;"></th> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</th> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</th> | ||
<th class="expander" | ||
style="Margin: 0; color: #0a0a0a; font-family: Helvetica, Arial, sans-serif; font-size: 16px; font-weight: normal; line-height: 1.3; margin: 0; padding: 0 !important; text-align: left; visibility: hidden; width: 0;"></th> | ||
</tr> | ||
</tbody> | ||
</table> |
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