From a1eecdd7f8b3a38d6eec50645a1bc28c434ce250 Mon Sep 17 00:00:00 2001 From: Preston Baxter Date: Thu, 23 Nov 2023 08:24:30 -0600 Subject: [PATCH] B: Add some more tests and better time finding --- service/controllers/pco_webhook.go | 6 ++++- service/vendors/pco/services.go | 7 +++-- .../{structs.go => event_delivery.go} | 0 .../pco/webhooks/event_delivery_test.go | 27 +++++++++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) rename service/vendors/pco/webhooks/{structs.go => event_delivery.go} (100%) create mode 100644 service/vendors/pco/webhooks/event_delivery_test.go diff --git a/service/controllers/pco_webhook.go b/service/controllers/pco_webhook.go index b77a97e..db8bf54 100644 --- a/service/controllers/pco_webhook.go +++ b/service/controllers/pco_webhook.go @@ -6,6 +6,7 @@ import ( "fmt" "regexp" "sync" + "time" "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/config" "git.preston-baxter.com/Preston_PLB/capstone/frontend-service/db/models" @@ -268,5 +269,8 @@ func scheduleNewBroadcastFromWebhook(c *gin.Context, plan *services.Plan, ytClie return nil, err } - return yt_helpers.InsertBroadcast(ytClient, plan.Title, times.StartsAt, yt_helpers.STATUS_PRIVATE) + startTime := times[0].StartsAt + // endTime := times[len(times) - 1].EndsAt TODO: this will be used later + + return yt_helpers.InsertBroadcast(ytClient, plan.Title, startTime, yt_helpers.STATUS_PRIVATE) } diff --git a/service/vendors/pco/services.go b/service/vendors/pco/services.go index 088ced4..f53da4a 100644 --- a/service/vendors/pco/services.go +++ b/service/vendors/pco/services.go @@ -34,7 +34,7 @@ func (api *PcoApiClient) GetPlan(service_type_id, plan_id string) (*services.Pla return plan, nil } -func (api *PcoApiClient) GetPlanTimes(service_type_id, plan_id string) (*services.PlanTime, error) { +func (api *PcoApiClient) GetPlanTimes(service_type_id, plan_id string) ([]services.PlanTime, error) { api.Url().Path = fmt.Sprintf("/services/v2/service_types/%s/plans/%s/plan_times", service_type_id, plan_id) req, err := http.NewRequest(http.MethodGet, api.Url().String(), nil) @@ -51,11 +51,10 @@ func (api *PcoApiClient) GetPlanTimes(service_type_id, plan_id string) (*service return nil, fmt.Errorf("Failed to retrieve plan with status code: %d", resp.StatusCode) } - planTime := &services.PlanTime{} - err = jsonapi.UnmarshalPayload(resp.Body, planTime) + planTimes, err := jsonapi.UnmarshalManyPayload[services.PlanTime](resp.Body) if err != nil { return nil, err } - return planTime, nil + return planTimes, nil } diff --git a/service/vendors/pco/webhooks/structs.go b/service/vendors/pco/webhooks/event_delivery.go similarity index 100% rename from service/vendors/pco/webhooks/structs.go rename to service/vendors/pco/webhooks/event_delivery.go diff --git a/service/vendors/pco/webhooks/event_delivery_test.go b/service/vendors/pco/webhooks/event_delivery_test.go new file mode 100644 index 0000000..4da2b37 --- /dev/null +++ b/service/vendors/pco/webhooks/event_delivery_test.go @@ -0,0 +1,27 @@ +package webhooks + +import ( + "strings" + "testing" + + "git.preston-baxter.com/Preston_PLB/capstone/webhook-service/vendors/pco/services" + "github.com/go-playground/assert/v2" + "github.com/google/jsonapi" +) + +func TestUnmarshallPayload(t *testing.T) { + raw := `{"data":[{"id":"87f49852-1a2a-45cb-b4d2-0fa30eda0823","type":"EventDelivery","attributes":{"name":"services.v2.events.plan.created","attempt":1,"payload":"{\"data\":{\"type\":\"Plan\",\"id\":\"69259663\",\"attributes\":{\"can_view_order\":true,\"created_at\":\"2023-11-23T13:34:09Z\",\"dates\":\"No dates\",\"files_expire_at\":null,\"items_count\":0,\"last_time_at\":null,\"multi_day\":false,\"needed_positions_count\":0,\"other_time_count\":0,\"permissions\":\"Administrator\",\"plan_notes_count\":0,\"plan_people_count\":0,\"planning_center_url\":\"https://services.planningcenteronline.com/plans/69259663\",\"prefers_order_view\":false,\"public\":false,\"rehearsable\":true,\"rehearsal_time_count\":0,\"reminders_disabled\":false,\"series_title\":null,\"service_time_count\":0,\"short_dates\":\"No dates\",\"sort_date\":\"2023-11-23T13:34:09Z\",\"title\":null,\"total_length\":0,\"updated_at\":\"2023-11-23T13:34:09Z\"},\"relationships\":{\"service_type\":{\"data\":{\"type\":\"ServiceType\",\"id\":\"1429991\"}},\"next_plan\":{\"data\":null},\"previous_plan\":{\"data\":null},\"attachment_types\":{\"data\":[]},\"series\":{\"data\":null},\"created_by\":{\"data\":{\"type\":\"Person\",\"id\":\"136901110\"}},\"updated_by\":{\"data\":{\"type\":\"Person\",\"id\":\"136901110\"}},\"linked_publishing_episode\":{\"data\":null}},\"links\":{\"all_attachments\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/all_attachments\",\"attachments\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/attachments\",\"attendances\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/attendances\",\"contributors\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/contributors\",\"import_template\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/import_template\",\"item_reorder\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/item_reorder\",\"items\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/items\",\"live\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/live\",\"my_schedules\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/my_schedules\",\"needed_positions\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/needed_positions\",\"next_plan\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/next_plan\",\"notes\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/notes\",\"plan_times\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/plan_times\",\"previous_plan\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/previous_plan\",\"series\":null,\"signup_teams\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/signup_teams\",\"team_members\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663/team_members\",\"self\":\"https://api.planningcenteronline.com/services/v2/service_types/1429991/plans/69259663\",\"html\":\"https://services.planningcenteronline.com/plans/69259663\"}},\"included\":[],\"meta\":{\"can_include\":[\"contributors\",\"my_schedules\",\"plan_times\",\"series\"],\"parent\":{\"id\":\"1429991\",\"type\":\"ServiceType\"},\"event_time\":\"2023-11-23T13:34:09Z\"}}"},"relationships":{"organization":{"data":{"type":"Organization","id":"456240"}}}}]}` + + deliveries, err := jsonapi.UnmarshalManyPayload[EventDelivery](strings.NewReader(raw)) + if err != nil { + t.Fatal(err) + } + + plan := &services.Plan{} + err = deliveries[0].UnmarshallPayload(plan) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, plan.Id, "69259663") +}