Skip to content

Commit

Permalink
Handle sending email for manually setting invoice paid
Browse files Browse the repository at this point in the history
  • Loading branch information
alexopenline committed May 27, 2024
1 parent 0bb6c9b commit 233d97c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
WorkflowIdOrgOwnerUpdateEmail = "org-owner-update-email"
WorkflowIdOrgOwnerUpdateAppNotification = "org-owner-update-in-app-notification"
WorkflowInvoicePaid = "invoice-paid"
WorkflowInvoicePaymentReceived = "invoice-payment-received"
WorkflowInvoiceReadyWithPaymentLink = "invoice-ready"
WorkflowInvoiceReadyNoPaymentLink = "invoice-ready-nolink"
WorkflowInvoiceVoided = "invoice-voided"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
WorkflowFailedWebhookSubject = "[Action Required] Webhook %s is offline"
WorkflowInvoiceVoidedSubject = "Voided Invoice %s"
WorkflowInvoicePaidSubject = "Paid Invoice %s from %s"
WorkflowInvoicePaymentReceivedSubject = "Payment Received for Invoice %s from %s"
WorkflowInvoiceReadySubject = "New invoice %s"
WorkflowReminderNotificationSubject = "Reminder, %s"
)
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ func (np *PostmarkProvider) GetFileName(workflowId, fileExtension string) string
switch workflowId {
case notifications.WorkflowInvoicePaid:
fileName = "invoice.paid." + fileExtension
case notifications.WorkflowInvoicePaymentReceived:
fileName = "invoice.payment.received." + fileExtension
case notifications.WorkflowInvoiceReadyWithPaymentLink:
fileName = "invoice.ready." + fileExtension
case notifications.WorkflowInvoiceReadyNoPaymentLink:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ import (
"golang.org/x/net/context"
)

type eventMetadata struct {
UserId string `json:"user-id"`
}

type RequestBodyInvoiceFinalized struct {
Tenant string `json:"tenant"`
Currency string `json:"currency"`
Expand Down Expand Up @@ -955,6 +959,12 @@ func (h *InvoiceEventHandler) onInvoicePaidV1(ctx context.Context, evt eventstor
span.SetTag(tracing.SpanTagEntityId, invoiceId)
span.SetTag(tracing.SpanTagTenant, eventData.Tenant)

evtMetadata := eventMetadata{}
if err := json.Unmarshal(evt.Metadata, &evtMetadata); err != nil {
tracing.TraceErr(span, err)
}
eventTriggeredByUser := evtMetadata.UserId != ""

var invoiceEntity *neo4jentity.InvoiceEntity
var contractEntity neo4jentity.ContractEntity

Expand Down Expand Up @@ -1002,22 +1012,27 @@ func (h *InvoiceEventHandler) onInvoicePaidV1(ctx context.Context, evt eventstor
bcc = utils.RemoveDuplicates(bcc)

postmarkEmail := postmark.PostmarkEmail{
WorkflowId: notifications.WorkflowInvoicePaid,
MessageStream: postmark.PostmarkMessageStreamInvoice,
From: invoiceEntity.Provider.Email,
To: contractEntity.InvoiceEmail,
CC: cc,
BCC: bcc,
Subject: fmt.Sprintf(notifications.WorkflowInvoicePaidSubject, invoiceEntity.Number, invoiceEntity.Provider.Name),
TemplateData: map[string]string{
"{{userFirstName}}": invoiceEntity.Customer.Name,
"{{invoiceNumber}}": invoiceEntity.Number,
"{{currencySymbol}}": invoiceEntity.Currency.Symbol(),
"{{amtDue}}": fmt.Sprintf("%.2f", invoiceEntity.TotalAmount),
"{{paymentDate}}": invoiceEntity.DueDate.Format("02 Jan 2006"),
"{{paymentDate}}": utils.Now().Format("02 Jan 2006"),
},
Attachments: []postmark.PostmarkEmailAttachment{},
}
if eventTriggeredByUser {
postmarkEmail.WorkflowId = notifications.WorkflowInvoicePaymentReceived
postmarkEmail.Subject = fmt.Sprintf(notifications.WorkflowInvoicePaymentReceivedSubject, invoiceEntity.Number, invoiceEntity.Provider.Name)
} else {
postmarkEmail.WorkflowId = notifications.WorkflowInvoicePaid
postmarkEmail.Subject = fmt.Sprintf(notifications.WorkflowInvoicePaidSubject, invoiceEntity.Number, invoiceEntity.Provider.Name)
}

err = h.appendInvoiceFileToEmailAsAttachment(ctx, eventData.Tenant, *invoiceEntity, &postmarkEmail)
if err != nil {
Expand Down

0 comments on commit 233d97c

Please sign in to comment.