Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subscription metadata #738

Merged
merged 4 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions datastore/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,9 @@ type Subscription struct {
SourceID string `json:"-" bson:"source_id"`
EndpointID string `json:"-" bson:"endpoint_id"`

Source *Source `json:"source"`
Endpoint *Endpoint `json:"endpoint"`
Source *Source `json:"source_metadata,omitempty" bson:"-"`
Endpoint *Endpoint `json:"endpoint_metadata,omitempty" bson:"-"`
App *Application `json:"app_metadata,omitempty" bson:"-"`

// subscription config
AlertConfig *AlertConfiguration `json:"alert_config,omitempty" bson:"alert_config,omitempty"`
Expand Down Expand Up @@ -604,3 +605,8 @@ func (p *Password) Matches() (bool, error) {

return true, err
}

type EventMap map[string]*Event
type SourceMap map[string]*Source
type AppMap map[string]*Application
type EndpointMap map[string]*Endpoint
9 changes: 6 additions & 3 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// 2022-06-10 12:04:46.573241 +0100 WAT m=+76.692854293
// 2022-06-10 11:08:30.465468456 +0100 WAT m=+80.799317685
package docs

import (
Expand Down Expand Up @@ -7710,13 +7710,16 @@ var doc = `{
"description": "subscription config",
"$ref": "#/definitions/datastore.AlertConfiguration"
},
"app_metadata": {
"$ref": "#/definitions/datastore.Application"
},
"created_at": {
"type": "string"
},
"deleted_at": {
"type": "string"
},
"endpoint": {
"endpoint_metadata": {
"$ref": "#/definitions/datastore.Endpoint"
},
"filter_config": {
Expand All @@ -7728,7 +7731,7 @@ var doc = `{
"retry_config": {
"$ref": "#/definitions/datastore.RetryConfiguration"
},
"source": {
"source_metadata": {
"$ref": "#/definitions/datastore.Source"
},
"status": {
Expand Down
7 changes: 5 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7698,13 +7698,16 @@
"description": "subscription config",
"$ref": "#/definitions/datastore.AlertConfiguration"
},
"app_metadata": {
"$ref": "#/definitions/datastore.Application"
},
"created_at": {
"type": "string"
},
"deleted_at": {
"type": "string"
},
"endpoint": {
"endpoint_metadata": {
"$ref": "#/definitions/datastore.Endpoint"
},
"filter_config": {
Expand All @@ -7716,7 +7719,7 @@
"retry_config": {
"$ref": "#/definitions/datastore.RetryConfiguration"
},
"source": {
"source_metadata": {
"$ref": "#/definitions/datastore.Source"
},
"status": {
Expand Down
6 changes: 4 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -411,19 +411,21 @@ definitions:
alert_config:
$ref: '#/definitions/datastore.AlertConfiguration'
description: subscription config
app_metadata:
$ref: '#/definitions/datastore.Application'
created_at:
type: string
deleted_at:
type: string
endpoint:
endpoint_metadata:
$ref: '#/definitions/datastore.Endpoint'
filter_config:
$ref: '#/definitions/datastore.FilterConfiguration'
name:
type: string
retry_config:
$ref: '#/definitions/datastore.RetryConfiguration'
source:
source_metadata:
$ref: '#/definitions/datastore.Source'
status:
type: string
Expand Down
7 changes: 5 additions & 2 deletions docs/v3/openapi3.json
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,16 @@
"alert_config": {
"$ref": "#/components/schemas/datastore.AlertConfiguration"
},
"app_metadata": {
"$ref": "#/components/schemas/datastore.Application"
},
"created_at": {
"type": "string"
},
"deleted_at": {
"type": "string"
},
"endpoint": {
"endpoint_metadata": {
"$ref": "#/components/schemas/datastore.Endpoint"
},
"filter_config": {
Expand All @@ -627,7 +630,7 @@
"retry_config": {
"$ref": "#/components/schemas/datastore.RetryConfiguration"
},
"source": {
"source_metadata": {
"$ref": "#/components/schemas/datastore.Source"
},
"status": {
Expand Down
6 changes: 4 additions & 2 deletions docs/v3/openapi3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -410,19 +410,21 @@ components:
properties:
alert_config:
$ref: '#/components/schemas/datastore.AlertConfiguration'
app_metadata:
$ref: '#/components/schemas/datastore.Application'
created_at:
type: string
deleted_at:
type: string
endpoint:
endpoint_metadata:
$ref: '#/components/schemas/datastore.Endpoint'
filter_config:
$ref: '#/components/schemas/datastore.FilterConfiguration'
name:
type: string
retry_config:
$ref: '#/components/schemas/datastore.RetryConfiguration'
source:
source_metadata:
$ref: '#/components/schemas/datastore.Source'
status:
type: string
Expand Down
3 changes: 1 addition & 2 deletions server/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func newApplicationHandler(
gs := services.NewGroupService(appRepo, groupRepo, eventRepo, eventDeliveryRepo, limiter)
ss := services.NewSecurityService(groupRepo, apiKeyRepo)
os := services.NewOrganisationService(orgRepo, orgMemberRepo)
rs := services.NewSubscriptionService(subRepo)
rs := services.NewSubscriptionService(subRepo, appRepo, sourceRepo)
sos := services.NewSourceService(sourceRepo)
us := services.NewUserService(userRepo, cache)
ois := services.NewOrganisationInviteService(orgRepo, userRepo, orgMemberRepo, orgInviteRepo, queue)
Expand Down Expand Up @@ -293,7 +293,6 @@ func (a *applicationHandler) CreateAppEndpoint(w http.ResponseWriter, r *http.Re
// @Security ApiKeyAuth
// @Router /applications/{appID}/endpoints/{endpointID} [get]
func (a *applicationHandler) GetAppEndpoint(w http.ResponseWriter, r *http.Request) {

_ = render.Render(w, r, newServerResponse("App endpoint fetched successfully",
*getApplicationEndpointFromContext(r.Context()), http.StatusOK))
}
Expand Down
38 changes: 36 additions & 2 deletions server/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,12 @@ func requireOrganisationMemberRole(roleType auth.RoleType) func(next http.Handle
}
}

func requireEventDelivery(eventRepo datastore.EventDeliveryRepository) func(next http.Handler) http.Handler {
func requireEventDelivery(eventDeliveryRepo datastore.EventDeliveryRepository, appRepo datastore.ApplicationRepository, eventRepo datastore.EventRepository) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
eventDeliveryID := chi.URLParam(r, "eventDeliveryID")

eventDelivery, err := eventRepo.FindEventDeliveryByID(r.Context(), eventDeliveryID)
eventDelivery, err := eventDeliveryRepo.FindEventDeliveryByID(r.Context(), eventDeliveryID)
if err != nil {

eventDelivery := "an error occurred while retrieving event delivery details"
Expand All @@ -440,6 +440,40 @@ func requireEventDelivery(eventRepo datastore.EventDeliveryRepository) func(next
return
}

a, err := appRepo.FindApplicationByID(r.Context(), eventDelivery.AppID)
if err == nil {
app := &datastore.Application{
UID: a.UID,
Title: a.Title,
GroupID: a.GroupID,
SupportEmail: a.SupportEmail,
}
eventDelivery.App = app
}

ev, err := eventRepo.FindEventByID(r.Context(), eventDelivery.EventID)
if err == nil {
event := &datastore.Event{
UID: ev.UID,
EventType: ev.EventType,
}
eventDelivery.Event = event
}

en, err := appRepo.FindApplicationEndpointByID(r.Context(), eventDelivery.AppID, eventDelivery.EndpointID)
if err == nil {
endpoint := &datastore.Endpoint{
UID: en.UID,
TargetURL: en.TargetURL,
DocumentStatus: en.DocumentStatus,
Secret: en.Secret,
HttpTimeout: en.HttpTimeout,
RateLimit: en.RateLimit,
RateLimitDuration: en.RateLimitDuration,
}
eventDelivery.Endpoint = endpoint
}

r = r.WithContext(setEventDeliveryInContext(r.Context(), eventDelivery))
next.ServeHTTP(w, r)
})
Expand Down
2 changes: 1 addition & 1 deletion server/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ type Subscription struct {
Name string `json:"name" bson:"name" valid:"required~please provide a valid subscription name"`
Type string `json:"type" bson:"type" valid:"required~please provide a valid subscription type"`
AppID string `json:"app_id" bson:"app_id" valid:"required~please provide a valid app id"`
SourceID string `json:"source_id" bson:"source_id" valid:"required~please provide a valid source id"`
SourceID string `json:"source_id" bson:"source_id"`
EndpointID string `json:"endpoint_id" bson:"endpoint_id" valid:"required~please provide a valid endpoint id"`

AlertConfig *datastore.AlertConfiguration `json:"alert_config,omitempty" bson:"alert_config,omitempty"`
Expand Down
6 changes: 3 additions & 3 deletions server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func buildRoutes(app *applicationHandler) http.Handler {
eventDeliveryRouter.Get("/countbatchretryevents", app.CountAffectedEventDeliveries)

eventDeliveryRouter.Route("/{eventDeliveryID}", func(eventDeliverySubRouter chi.Router) {
eventDeliverySubRouter.Use(requireEventDelivery(app.eventDeliveryRepo))
eventDeliverySubRouter.Use(requireEventDelivery(app.eventDeliveryRepo, app.appRepo, app.eventRepo))

eventDeliverySubRouter.Get("/", app.GetEventDelivery)
eventDeliverySubRouter.Put("/resend", app.ResendEventDelivery)
Expand Down Expand Up @@ -336,7 +336,7 @@ func buildRoutes(app *applicationHandler) http.Handler {
eventDeliveryRouter.Get("/countbatchretryevents", app.CountAffectedEventDeliveries)

eventDeliveryRouter.Route("/{eventDeliveryID}", func(eventDeliverySubRouter chi.Router) {
eventDeliverySubRouter.Use(requireEventDelivery(app.eventDeliveryRepo))
eventDeliverySubRouter.Use(requireEventDelivery(app.eventDeliveryRepo, app.appRepo, app.eventRepo))

eventDeliverySubRouter.Get("/", app.GetEventDelivery)
eventDeliverySubRouter.Put("/resend", app.ResendEventDelivery)
Expand Down Expand Up @@ -443,7 +443,7 @@ func buildRoutes(app *applicationHandler) http.Handler {
eventDeliveryRouter.Get("/countbatchretryevents", app.CountAffectedEventDeliveries)

eventDeliveryRouter.Route("/{eventDeliveryID}", func(eventDeliverySubRouter chi.Router) {
eventDeliverySubRouter.Use(requireEventDelivery(app.eventDeliveryRepo))
eventDeliverySubRouter.Use(requireEventDelivery(app.eventDeliveryRepo, app.appRepo, app.eventRepo))

eventDeliverySubRouter.Get("/", app.GetEventDelivery)
eventDeliverySubRouter.Put("/resend", app.ResendEventDelivery)
Expand Down
62 changes: 32 additions & 30 deletions services/event_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ type EventService struct {
searcher searcher.Searcher
}

type EventMap map[string]*datastore.Event
type AppMap map[string]*datastore.Application
type EndpointMap map[string]*datastore.Endpoint

func NewEventService(appRepo datastore.ApplicationRepository, eventRepo datastore.EventRepository, eventDeliveryRepo datastore.EventDeliveryRepository,
queue queue.Queuer, cache cache.Cache, seacher searcher.Searcher, subRepo datastore.SubscriptionRepository) *EventService {
return &EventService{appRepo: appRepo, eventRepo: eventRepo, eventDeliveryRepo: eventDeliveryRepo, queue: queue, cache: cache, searcher: seacher, subRepo: subRepo}
Expand Down Expand Up @@ -233,7 +229,7 @@ func (e *EventService) GetEventsPaged(ctx context.Context, filter *datastore.Fil
return nil, datastore.PaginationData{}, NewServiceError(http.StatusInternalServerError, errors.New("an error occurred while fetching events"))
}

appMap := AppMap{}
appMap := datastore.AppMap{}
for i, event := range events {
if _, ok := appMap[event.AppID]; !ok {
a, _ := e.appRepo.FindApplicationByID(ctx, event.AppID)
Expand All @@ -259,43 +255,49 @@ func (e *EventService) GetEventDeliveriesPaged(ctx context.Context, filter *data
return nil, datastore.PaginationData{}, NewServiceError(http.StatusInternalServerError, errors.New("an error occurred while fetching event deliveries"))
}

appMap := AppMap{}
eventMap := EventMap{}
endpointMap := EndpointMap{}
appMap := datastore.AppMap{}
eventMap := datastore.EventMap{}
endpointMap := datastore.EndpointMap{}

for i, ed := range deliveries {
if _, ok := appMap[ed.AppID]; !ok {
a, _ := e.appRepo.FindApplicationByID(ctx, ed.AppID)
aa := &datastore.Application{
UID: a.UID,
Title: a.Title,
GroupID: a.GroupID,
SupportEmail: a.SupportEmail,
a, err := e.appRepo.FindApplicationByID(ctx, ed.AppID)
if err == nil {
aa := &datastore.Application{
UID: a.UID,
Title: a.Title,
GroupID: a.GroupID,
SupportEmail: a.SupportEmail,
}
appMap[ed.AppID] = aa
}
appMap[ed.AppID] = aa
}

if _, ok := eventMap[ed.EventID]; !ok {
ev, _ := e.eventRepo.FindEventByID(ctx, ed.EventID)
event := &datastore.Event{
UID: ev.UID,
EventType: ev.EventType,
ev, err := e.eventRepo.FindEventByID(ctx, ed.EventID)
if err == nil {
event := &datastore.Event{
UID: ev.UID,
EventType: ev.EventType,
}
eventMap[ed.EventID] = event
}
eventMap[ed.EventID] = event
}

if _, ok := endpointMap[ed.EndpointID]; !ok {
en, _ := e.appRepo.FindApplicationEndpointByID(ctx, ed.AppID, ed.EndpointID)
endpoint := &datastore.Endpoint{
UID: en.UID,
TargetURL: en.TargetURL,
DocumentStatus: en.DocumentStatus,
Secret: en.Secret,
HttpTimeout: en.HttpTimeout,
RateLimit: en.RateLimit,
RateLimitDuration: en.RateLimitDuration,
en, err := e.appRepo.FindApplicationEndpointByID(ctx, ed.AppID, ed.EndpointID)
if err == nil {
endpoint := &datastore.Endpoint{
UID: en.UID,
TargetURL: en.TargetURL,
DocumentStatus: en.DocumentStatus,
Secret: en.Secret,
HttpTimeout: en.HttpTimeout,
RateLimit: en.RateLimit,
RateLimitDuration: en.RateLimitDuration,
}
endpointMap[ed.EndpointID] = endpoint
}
endpointMap[ed.EndpointID] = endpoint
}

deliveries[i].App = appMap[ed.AppID]
Expand Down