Skip to content

Commit

Permalink
Merge pull request #19 from seatsio/steve/support_draft_chart_reports
Browse files Browse the repository at this point in the history
Support draft reports for charts
  • Loading branch information
schaloner authored Sep 11, 2023
2 parents c2b2186 + 9ac5f19 commit f4f07a0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 46 deletions.
90 changes: 48 additions & 42 deletions reports/chart_reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,98 +46,104 @@ type ChartReportItem struct {
HasRestrictedView bool `json:"hasRestrictedView,omitempty"`
}

type bookWholeTables struct {
mode map[string]string
type chartReportOptions struct {
options map[string]string
}

type bookWholeTablesModeOption func(params *bookWholeTables)
type chartReportOptionsOption func(params *chartReportOptions)

type bookWholeTablesNS struct{}
type ChartReportOptionsNS struct{}

var BookWholeTables bookWholeTablesNS
var ChartReportOptions ChartReportOptionsNS

func (reports *ChartReports) SummaryByObjectType(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byObjectType", chartKey, bookWholeTablesMode...)
func (reports *ChartReports) SummaryByObjectType(chartKey string, chartReportOptions ...chartReportOptionsOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byObjectType", chartKey, chartReportOptions...)
}

func (reports *ChartReports) SummaryByCategoryKey(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byCategoryKey", chartKey, bookWholeTablesMode...)
func (reports *ChartReports) SummaryByCategoryKey(chartKey string, chartReportOptions ...chartReportOptionsOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byCategoryKey", chartKey, chartReportOptions...)
}

func (reports *ChartReports) SummaryByCategoryLabel(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byCategoryLabel", chartKey, bookWholeTablesMode...)
func (reports *ChartReports) SummaryByCategoryLabel(chartKey string, chartReportOptions ...chartReportOptionsOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("byCategoryLabel", chartKey, chartReportOptions...)
}

func (reports *ChartReports) SummaryBySection(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("bySection", chartKey, bookWholeTablesMode...)
func (reports *ChartReports) SummaryBySection(chartKey string, chartReportOptions ...chartReportOptionsOption) (*ChartSummaryReport, error) {
return reports.fetchSummaryChartReport("bySection", chartKey, chartReportOptions...)
}

func (reports *ChartReports) ByLabel(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
return reports.fetchChartReport("byLabel", chartKey, bookWholeTablesMode...)
func (reports *ChartReports) ByLabel(chartKey string, chartReportOptions ...chartReportOptionsOption) (*ChartReport, error) {
return reports.fetchChartReport("byLabel", chartKey, chartReportOptions...)
}

func (reports *ChartReports) ByObjectType(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
return reports.fetchChartReport("byObjectType", chartKey, bookWholeTablesMode...)
func (reports *ChartReports) ByObjectType(chartKey string, chartReportOptions ...chartReportOptionsOption) (*ChartReport, error) {
return reports.fetchChartReport("byObjectType", chartKey, chartReportOptions...)
}

func (reports *ChartReports) ByCategoryKey(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
return reports.fetchChartReport("byCategoryKey", chartKey, bookWholeTablesMode...)
func (reports *ChartReports) ByCategoryKey(chartKey string, chartReportOptions ...chartReportOptionsOption) (*ChartReport, error) {
return reports.fetchChartReport("byCategoryKey", chartKey, chartReportOptions...)
}

func (reports *ChartReports) ByCategoryLabel(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
return reports.fetchChartReport("byCategoryLabel", chartKey, bookWholeTablesMode...)
func (reports *ChartReports) ByCategoryLabel(chartKey string, chartReportOptions ...chartReportOptionsOption) (*ChartReport, error) {
return reports.fetchChartReport("byCategoryLabel", chartKey, chartReportOptions...)
}

func (reports *ChartReports) BySection(chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
return reports.fetchChartReport("bySection", chartKey, bookWholeTablesMode...)
func (reports *ChartReports) BySection(chartKey string, chartReportOptions ...chartReportOptionsOption) (*ChartReport, error) {
return reports.fetchChartReport("bySection", chartKey, chartReportOptions...)
}

func (reports *ChartReports) fetchChartReport(reportType string, chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartReport, error) {
tableMode := bookWholeTables{mode: map[string]string{}}
for _, mode := range bookWholeTablesMode {
mode(&tableMode)
func (reports *ChartReports) fetchChartReport(reportType string, chartKey string, reportOptions ...chartReportOptionsOption) (*ChartReport, error) {
options := chartReportOptions{options: map[string]string{}}
for _, opt := range reportOptions {
opt(&options)
}
var report map[string][]ChartReportItem
result, err := reports.Client.R().
SetSuccessResult(&report).
SetPathParam("reportItemType", "charts").
SetPathParam("key", chartKey).
SetPathParam("reportType", reportType).
SetQueryParams(tableMode.mode).
SetQueryParams(options.options).
Get("/reports/{reportItemType}/{key}/{reportType}")
return shared.AssertOk(result, err, &ChartReport{Items: report})
}

func (reports *ChartReports) fetchSummaryChartReport(reportType string, chartKey string, bookWholeTablesMode ...bookWholeTablesModeOption) (*ChartSummaryReport, error) {
tableMode := bookWholeTables{mode: map[string]string{}}
for _, mode := range bookWholeTablesMode {
mode(&tableMode)
func (reports *ChartReports) fetchSummaryChartReport(reportType string, chartKey string, reportOptions ...chartReportOptionsOption) (*ChartSummaryReport, error) {
options := chartReportOptions{options: map[string]string{}}
for _, opt := range reportOptions {
opt(&options)
}
var report map[string]ChartSummaryReportItem
result, err := reports.Client.R().
SetSuccessResult(&report).
SetPathParam("reportItemType", "charts").
SetPathParam("key", chartKey).
SetPathParam("reportType", reportType).
SetQueryParams(tableMode.mode).
SetQueryParams(options.options).
Get("/reports/{reportItemType}/{key}/{reportType}/summary")
return shared.AssertOk(result, err, &ChartSummaryReport{Items: report})
}

func (bookWholeTablesNS) Chart() bookWholeTablesModeOption {
return func(mode *bookWholeTables) {
mode.mode["bookWholeTables"] = "chart"
func (ChartReportOptionsNS) BookWholeTablesChart() chartReportOptionsOption {
return func(options *chartReportOptions) {
options.options["bookWholeTables"] = "chart"
}
}

func (bookWholeTablesNS) True() bookWholeTablesModeOption {
return func(mode *bookWholeTables) {
mode.mode["bookWholeTables"] = "true"
func (ChartReportOptionsNS) BookWholeTablesTrue() chartReportOptionsOption {
return func(options *chartReportOptions) {
options.options["bookWholeTables"] = "true"
}
}

func (bookWholeTablesNS) False() bookWholeTablesModeOption {
return func(mode *bookWholeTables) {
mode.mode["bookWholeTables"] = "false"
func (ChartReportOptionsNS) BookWholeTablesFalse() chartReportOptionsOption {
return func(options *chartReportOptions) {
options.options["bookWholeTables"] = "false"
}
}

func (ChartReportOptionsNS) UseDraftVersion() chartReportOptionsOption {
return func(options *chartReportOptions) {
options.options["version"] = "draft"
}
}
33 changes: 32 additions & 1 deletion reports_test/chart_reports_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package reports_test

import (
"github.com/seatsio/seatsio-go"
"github.com/seatsio/seatsio-go/charts"
"github.com/seatsio/seatsio-go/events"
"github.com/seatsio/seatsio-go/reports"
"github.com/seatsio/seatsio-go/test_util"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -59,7 +61,7 @@ func TestSummaryByObjectType_BookWholeTablesTrue(t *testing.T) {
client := seatsio.NewSeatsioClient(test_util.BaseUrl, company.Admin.SecretKey)
chartKey := test_util.CreateTestChartWithTables(t, company.Admin.SecretKey)

summaryChartReport, err := client.ChartReports.SummaryByObjectType(chartKey, reports.BookWholeTables.True())
summaryChartReport, err := client.ChartReports.SummaryByObjectType(chartKey, reports.ChartReportOptions.BookWholeTablesTrue())

require.NoError(t, err)
tableReportItem := reports.ChartSummaryReportItem{
Expand Down Expand Up @@ -192,3 +194,32 @@ func TestSummaryBySection(t *testing.T) {
}
require.Equal(t, noSectionReport, summaryChartReport.Items["NO_SECTION"])
}

func TestSummaryDraftVersion(t *testing.T) {
t.Parallel()
company := test_util.CreateTestCompany(t)
client := seatsio.NewSeatsioClient(test_util.BaseUrl, company.Admin.SecretKey)
chartKey := test_util.CreateTestChart(t, company.Admin.SecretKey)
_, _ = client.Events.Create(&events.CreateEventParams{ChartKey: chartKey})
_ = client.Charts.Update(chartKey, &charts.UpdateChartParams{Name: "Foo"})

summaryChartReport, err := client.ChartReports.SummaryBySection(chartKey, reports.ChartReportOptionsNS{}.UseDraftVersion())

require.NoError(t, err)
noSectionReport := reports.ChartSummaryReportItem{
Count: 232,
ByCategoryKey: map[string]interface{}{
"9": float64(116),
"10": float64(116),
},
ByCategoryLabel: map[string]interface{}{
"Cat1": float64(116),
"Cat2": float64(116),
},
ByObjectType: map[string]interface{}{
"seat": float64(32),
"generalAdmission": float64(200),
},
}
require.Equal(t, noSectionReport, summaryChartReport.Items["NO_SECTION"])
}
21 changes: 18 additions & 3 deletions reports_test/chart_reports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package reports

import (
"github.com/seatsio/seatsio-go"
"github.com/seatsio/seatsio-go/charts"
"github.com/seatsio/seatsio-go/events"
"github.com/seatsio/seatsio-go/reports"
"github.com/seatsio/seatsio-go/test_util"
Expand Down Expand Up @@ -60,7 +61,7 @@ func TestReportItemPropertiesForTable(t *testing.T) {
client := seatsio.NewSeatsioClient(test_util.BaseUrl, company.Admin.SecretKey)
chartKey := test_util.CreateTestChartWithTables(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByLabel(chartKey, reports.BookWholeTables.True())
chartReport, err := client.ChartReports.ByLabel(chartKey, reports.ChartReportOptions.BookWholeTablesTrue())
require.NoError(t, err)
require.Len(t, chartReport.Items["T1"], 1)
item := chartReport.Items["T1"][0]
Expand All @@ -86,7 +87,7 @@ func TestByLabel_BookWholeTablesTrue(t *testing.T) {
client := seatsio.NewSeatsioClient(test_util.BaseUrl, company.Admin.SecretKey)
chartKey := test_util.CreateTestChartWithTables(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByLabel(chartKey, reports.BookWholeTables.True())
chartReport, err := client.ChartReports.ByLabel(chartKey, reports.ChartReportOptions.BookWholeTablesTrue())
require.NoError(t, err)
require.Len(t, chartReport.Items, 2)
require.Nil(t, chartReport.Items["T1-1"])
Expand All @@ -101,7 +102,7 @@ func TestByLabel_BookWholeTablesChart(t *testing.T) {
client := seatsio.NewSeatsioClient(test_util.BaseUrl, company.Admin.SecretKey)
chartKey := test_util.CreateTestChartWithTables(t, company.Admin.SecretKey)

chartReport, err := client.ChartReports.ByLabel(chartKey, reports.BookWholeTables.Chart())
chartReport, err := client.ChartReports.ByLabel(chartKey, reports.ChartReportOptions.BookWholeTablesChart())
require.NoError(t, err)
require.Len(t, chartReport.Items, 7)
require.NotNil(t, chartReport.Items["T1-1"])
Expand Down Expand Up @@ -160,3 +161,17 @@ func TestBySection(t *testing.T) {
require.Len(t, chartReport.Items["Section A"], 36)
require.Len(t, chartReport.Items["Section B"], 35)
}

func TestDraftVersion(t *testing.T) {
t.Parallel()
company := test_util.CreateTestCompany(t)
client := seatsio.NewSeatsioClient(test_util.BaseUrl, company.Admin.SecretKey)
chartKey := test_util.CreateTestChartWithSections(t, company.Admin.SecretKey)
_, _ = client.Events.Create(&events.CreateEventParams{ChartKey: chartKey})
_ = client.Charts.Update(chartKey, &charts.UpdateChartParams{Name: "Foo"})

chartReport, err := client.ChartReports.BySection(chartKey, reports.ChartReportOptions.UseDraftVersion())
require.NoError(t, err)
require.Len(t, chartReport.Items["Section A"], 36)
require.Len(t, chartReport.Items["Section B"], 35)
}

0 comments on commit f4f07a0

Please sign in to comment.