From 26b32a8b93ce41f78ed2bdc20146c4b8ceaf82e1 Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 14 Jan 2025 15:34:17 +0000 Subject: [PATCH 1/2] Introduce initial language code setting --- handlers/application/menuhandler.go | 19 ++++++++++++++++++- store/db/db.go | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index d82a417..fa0c532 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -2187,6 +2187,23 @@ func (h *MenuHandlers) resetIncorrectPINAttempts(ctx context.Context, sessionId return nil } +func (h *MenuHandlers) persistInitialLanguageCode(ctx context.Context, sessionId string, code string) error { + store := h.userdataStore + _, err := store.ReadEntry(ctx, sessionId, storedb.DATA_INITIAL_LANGUAGE_CODE) + if err == nil { + return nil + } + if !db.IsNotFound(err) { + return err + } + err = store.WriteEntry(ctx, sessionId, storedb.DATA_SELECTED_LANGUAGE_CODE, []byte(code)) + if err != nil { + logg.ErrorCtxf(ctx, "failed to persist initial language code", "key", storedb.DATA_INITIAL_LANGUAGE_CODE, "value", code, "error", err) + return err + } + return nil +} + // persistLanguageCode persists the selected ISO 639 language code func (h *MenuHandlers) persistLanguageCode(ctx context.Context, code string) error { store := h.userdataStore @@ -2199,5 +2216,5 @@ func (h *MenuHandlers) persistLanguageCode(ctx context.Context, code string) err logg.ErrorCtxf(ctx, "failed to persist language code", "key", storedb.DATA_SELECTED_LANGUAGE_CODE, "value", code, "error", err) return err } - return nil + return h.persistInitialLanguageCode(ctx, sessionId, code) } diff --git a/store/db/db.go b/store/db/db.go index b70cc5e..2ccd9e6 100644 --- a/store/db/db.go +++ b/store/db/db.go @@ -59,6 +59,8 @@ const ( DATA_INCORRECT_PIN_ATTEMPTS //ISO 639 code for the selected language. DATA_SELECTED_LANGUAGE_CODE + //ISO 639 code for the language initially selected. + DATA_INITIAL_LANGUAGE_CODE ) const ( From 604f3103c7f5dd40d6c35f3b2c9f526e5cc0aa3f Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 14 Jan 2025 15:44:14 +0000 Subject: [PATCH 2/2] Fix wrong key for initial language write, add test --- handlers/application/menuhandler.go | 2 +- handlers/application/menuhandler_test.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index fa0c532..28f6854 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -2196,7 +2196,7 @@ func (h *MenuHandlers) persistInitialLanguageCode(ctx context.Context, sessionId if !db.IsNotFound(err) { return err } - err = store.WriteEntry(ctx, sessionId, storedb.DATA_SELECTED_LANGUAGE_CODE, []byte(code)) + err = store.WriteEntry(ctx, sessionId, storedb.DATA_INITIAL_LANGUAGE_CODE, []byte(code)) if err != nil { logg.ErrorCtxf(ctx, "failed to persist initial language code", "key", storedb.DATA_INITIAL_LANGUAGE_CODE, "value", code, "error", err) return err diff --git a/handlers/application/menuhandler_test.go b/handlers/application/menuhandler_test.go index cf5b4a5..3244ce4 100644 --- a/handlers/application/menuhandler_test.go +++ b/handlers/application/menuhandler_test.go @@ -825,6 +825,17 @@ func TestSetLanguage(t *testing.T) { // Assert that the Result FlagSet has the required flags after language switch assert.Equal(t, res, tt.expectedResult, "Result should match expected result") + code, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SELECTED_LANGUAGE_CODE) + if err != nil { + t.Error(err) + } + + assert.Equal(t, string(code), tt.expectedResult.Content) + code, err = store.ReadEntry(ctx, sessionId, storedb.DATA_INITIAL_LANGUAGE_CODE) + if err != nil { + t.Error(err) + } + assert.Equal(t, string(code), "eng") }) } }