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

close issue #103 #107

Merged
merged 1 commit into from
Dec 9, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-backend-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Prepare .env for tests
run: |
echo 'POSTGRES_PASSWORD="test_password"' > .env
echo 'READ_WRITE_PASSWORD="Tq43938!Tq43938!"' >> .env
echo 'READ_WRITE_PASSWORD="test_password"' >> .env
echo 'POSTGRES_DB="london_jam_sessions"' >> .env
- name: Run test suite
run: make test-ci
15 changes: 13 additions & 2 deletions backend/cmd/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func GetSessionsByVenueId(c *fuego.ContextNoBody) (types.SessionWithVenueFeature
// helper - https://github.com/golang/go/issues/63309
func ptr[T any](t T) *T { return &t }

func PostSession(c *fuego.ContextWithBody[types.SessionPropertiesWithVenue]) (types.SessionFeature[types.SessionProperties], error) {
func PostSession(c *fuego.ContextWithBody[types.SessionPropertiesWithVenuePOST]) (types.SessionFeature[types.SessionProperties], error) {
payload, err := c.Body()
slog.Info("PostSession", "payload", payload)
if err != nil {
Expand All @@ -241,8 +241,14 @@ func PostSession(c *fuego.ContextWithBody[types.SessionPropertiesWithVenue]) (ty
}
return types.SessionFeature[types.SessionProperties]{}, errors.New("an unknown error occured")
}
var submissionNotes *string = payload.SubmissionNotes
var submissionEmail *string = payload.SubmissionEmail
payload.SubmissionNotes = nil
payload.SubmissionEmail = nil

var cmd string
var title string

var sessionJson []byte
if payload.VenueName != nil { // if venue fields are present in the payload, we create a new venue in the same transaction
venueJson, err := json.Marshal(payload.VenueProperties)
Expand All @@ -259,7 +265,6 @@ func PostSession(c *fuego.ContextWithBody[types.SessionPropertiesWithVenue]) (ty
return types.SessionFeature[types.SessionProperties]{}, errors.New("an unknown error occured")
}
sessionJson = []byte(strings.Replace(string(sessionJson), "-999999", "$new_id", -1))

cmd = fmt.Sprintf(`new_id=$(dbcli insert venue "%s");`+"\n"+`dbcli insert session "%s";`, venueJson, sessionJson)
title = fmt.Sprintf("insert_venue_%v_session_%v", *payload.VenueName, *payload.SessionName)
slog.Info("PostSession", "mode", "sessionAndVenue", "cmd", cmd)
Expand All @@ -273,6 +278,12 @@ func PostSession(c *fuego.ContextWithBody[types.SessionPropertiesWithVenue]) (ty
title = fmt.Sprintf("insert_session_%v", *payload.SessionName)
slog.Info("PostSession", "mode", "sessionOnly", "cmd", cmd)
}
if submissionNotes != nil {
cmd += "\n# submission notes: " + *submissionNotes
}
if submissionEmail != nil {
cmd += "\n# email: " + *submissionEmail
}
if _, err := migrationutils.WriteMigration(cmd, title, migrationsDirectory); err != nil {
slog.Error("PostSession", "msg", err)
return types.SessionFeature[types.SessionProperties]{}, errors.New("an unknown error occurred")
Expand Down
58 changes: 56 additions & 2 deletions backend/cmd/server/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,61 @@ func TestHandlers(t *testing.T) {
t.Errorf("error reading file: %v", err)
}

matched, err := regexp.Match(fmt.Sprintf(`insert session "{\\"session_name\\":\\"%v\\",.*`, "TestInsert"), f)
matched, err := regexp.Match(fmt.Sprintf(`insert session "{\\"session_name\\":\\"%v\\",.*}"$`, "TestInsert"), f)
if err != nil {
t.Errorf("error when trying match with regex: %v", err)
}

if !matched {
t.Errorf("expected the regex to match. instead got file contents: %s", f)
}
})

t.Run("PostSessionWithSubmissionNotes", func(t *testing.T) {
// temp directory for migrations
migrationsDirectory = t.TempDir()

testBody, err := json.Marshal(types.SessionPropertiesWithVenuePOST{
SessionProperties: types.SessionProperties{SessionName: ptr("TestInsert"),
Venue: &testVenueId,
Description: ptr("Description."),
StartTimeUtc: ptr(time.Date(2024, 3, 4, 5, 5, 3, 5, time.UTC)),
DurationMinutes: ptr(int16(90)),
Interval: ptr(types.FirstOfMonth),
SessionWebsite: ptr("https://example.org"),
},
SubmissionNotes: ptr("I run the session"),
SubmissionEmail: ptr("[email protected]")})
if err != nil {
t.Error("could not marshal json:", err)
t.FailNow()
}

handler := fuego.HTTPHandler(s, PostSession)
req := httptest.NewRequest(http.MethodPost, "/jamsessions", bytes.NewReader(testBody))
w := httptest.NewRecorder()
handler(w, req)
res := w.Result()
if res.StatusCode != 201 {
t.Errorf("expected status code 201, got %v", res.StatusCode)
}

dir, err := os.ReadDir(migrationsDirectory)
if err != nil {
t.Errorf("couldn't read directory contents: %v", err)
t.FailNow()
}
if len(dir) != 1 {
t.Errorf("expected exactly 1 file in the directory, got %v", len(dir))
t.FailNow()
}

f, err := os.ReadFile(filepath.Join(migrationsDirectory, dir[0].Name()))
if err != nil {
t.Errorf("error reading file: %v", err)
}

matched, err := regexp.Match(fmt.Sprintf(`insert session "{\\"session_name\\":\\"%v\\",.*\n# submission notes: I run the session\n# email: [email protected]`, "TestInsert"), f)
if err != nil {
t.Errorf("error when trying match with regex: %v", err)
}
Expand All @@ -622,7 +676,7 @@ func TestHandlers(t *testing.T) {
// temp directory for migrations
migrationsDirectory = t.TempDir()

testBody, err := json.Marshal(types.SessionPropertiesWithVenue{SessionProperties: types.SessionProperties{
testBody, err := json.Marshal(types.SessionPropertiesWithVenuePOST{SessionProperties: types.SessionProperties{
SessionName: ptr("TestInsert2"),
Description: ptr("Description."),
StartTimeUtc: ptr(time.Date(2024, 3, 4, 5, 5, 3, 5, time.UTC)),
Expand Down
2 changes: 1 addition & 1 deletion backend/doc/openapi.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions backend/internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ type SessionPropertiesWithVenue struct {
VenueProperties
}

type SessionPropertiesWithVenuePOST struct {
SessionProperties
VenueProperties
SubmissionNotes *string `json:"submission_notes,omitempty"`
SubmissionEmail *string `json:"submission_email,omitempty"`
}

type SessionFeature[T SessionProperties | SessionPropertiesWithVenue] struct {
Type string `json:"type"`
Properties T `json:"properties"`
Expand Down
Loading
Loading