Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
Factor out Eliona notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
zdevaty committed May 4, 2024
1 parent f430da8 commit 1e5ff1f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
33 changes: 2 additions & 31 deletions apiservices/api_booking_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ package apiservices
import (
"booking-app/apiserver"
"booking-app/conf"
"booking-app/eliona"
"context"
"errors"
"fmt"
"net/http"
"time"

api "github.com/eliona-smart-building-assistant/go-eliona-api-client/v2"
"github.com/eliona-smart-building-assistant/go-eliona/client"
"github.com/eliona-smart-building-assistant/go-utils/log"
)

Expand Down Expand Up @@ -53,7 +52,7 @@ func (s *BookingAPIService) BookingsBookingIdDelete(ctx context.Context, booking
case "", "cancelled":
// Event cancelled by user, no need to notify
case "conflict":
if err := notifyUser(ctx, booking); err != nil {
if err := eliona.NotifyUser(ctx, booking, reason); err != nil {
log.Error("apiservices", "notifying user %v: %v", booking.OrganizerID, err)
return apiserver.ImplResponse{Code: http.StatusInternalServerError}, err
}
Expand All @@ -74,34 +73,6 @@ func (s *BookingAPIService) BookingsBookingIdDelete(ctx context.Context, booking
return apiserver.Response(http.StatusNoContent, nil), nil
}

func notifyUser(ctx context.Context, booking apiserver.Booking) error {
ctx = client.AuthenticationContextWrap(ctx)
for _, assetID := range booking.AssetIds {
asset, _, err := client.NewClient().AssetsAPI.GetAssetById(ctx, assetID).Execute()
if err != nil {
return fmt.Errorf("fetching asset id %v: %v", assetID, err)
}
assetName := *asset.Name.Get()
bookingStart := booking.Start.Format("Mon 02.01 15:04")
_, _, err = client.NewClient().CommunicationAPI.
PostNotification(client.AuthenticationContext()).
Notification(
api.Notification{
User: booking.OrganizerID,
ProjectId: *api.NewNullableString(&asset.ProjectId),
Message: *api.NewNullableTranslation(&api.Translation{
De: api.PtrString(fmt.Sprintf("Ihre Buchung von Asset %s ab %s wurde aufgrund eines Zeitkonflikts storniert. Bitte buchen Sie neu.", assetName, bookingStart)),
En: api.PtrString(fmt.Sprintf("Your booking of asset %s starting at %s got cancelled due to a time conflict. Please rebook.", assetName, bookingStart)),
}),
}).
Execute()
if err != nil {
return fmt.Errorf("posting cancel notification: %v", err)
}
}
return nil
}

// BookingsGet - List bookings
func (s *BookingAPIService) BookingsGet(ctx context.Context, start string, end string, assetId int32) (apiserver.ImplResponse, error) {
since, err := time.Parse(time.RFC3339, start)
Expand Down
51 changes: 51 additions & 0 deletions eliona/notify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package eliona

import (
"booking-app/apiserver"
"context"
"fmt"

api "github.com/eliona-smart-building-assistant/go-eliona-api-client/v2"
"github.com/eliona-smart-building-assistant/go-eliona/client"
)

func NotifyUser(ctx context.Context, booking apiserver.Booking, reason string) error {
ctx = client.AuthenticationContextWrap(ctx)
for _, assetID := range booking.AssetIds {
asset, _, err := client.NewClient().AssetsAPI.GetAssetById(ctx, assetID).Execute()
if err != nil {
return fmt.Errorf("fetching asset id %v: %v", assetID, err)
}
assetName := *asset.Name.Get()
bookingStart := booking.Start.Format("Mon 02.01 15:04")
var message api.Translation
switch reason {
case "conflict":
message = api.Translation{
De: api.PtrString(fmt.Sprintf(
"Ihre Buchung von Asset %s ab %s wurde aufgrund eines Zeitkonflikts storniert. Bitte buchen Sie neu.",
assetName, bookingStart,
)),
En: api.PtrString(fmt.Sprintf(
"Your booking of asset %s starting at %s got cancelled due to a time conflict. Please rebook.",
assetName, bookingStart,
)),
}
default:
return fmt.Errorf("Unknown reason: %v", reason)
}
_, _, err = client.NewClient().CommunicationAPI.
PostNotification(client.AuthenticationContext()).
Notification(
api.Notification{
User: booking.OrganizerID,
ProjectId: *api.NewNullableString(&asset.ProjectId),
Message: *api.NewNullableTranslation(&message),
}).
Execute()
if err != nil {
return fmt.Errorf("posting cancel notification: %v", err)
}
}
return nil
}

0 comments on commit 1e5ff1f

Please sign in to comment.