Skip to content

Commit

Permalink
DXCDT-549: Add logic to save branding data that gets sent by the web …
Browse files Browse the repository at this point in the history
…app (#872)
  • Loading branch information
sergiught authored Oct 10, 2023
1 parent 09b010c commit 3500101
Show file tree
Hide file tree
Showing 2 changed files with 229 additions and 175 deletions.
60 changes: 53 additions & 7 deletions internal/cli/universal_login_customize.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type (
Template *management.BrandingUniversalLogin `json:"template"`
Theme *management.BrandingTheme `json:"theme"`
Tenant *tenantData `json:"tenant"`
Prompt *promptData `json:"prompt"`
Prompts []*promptData `json:"prompts"`
}

tenantData struct {
Expand All @@ -43,9 +43,9 @@ type (
}

promptData struct {
Language string `json:"language"`
Prompt string `json:"prompt"`
CustomText map[string]map[string]interface{} `json:"custom_text,omitempty"`
Language string `json:"language"`
Prompt string `json:"prompt"`
CustomText map[string]interface{} `json:"custom_text,omitempty"`
}

webSocketHandler struct {
Expand Down Expand Up @@ -168,7 +168,7 @@ func fetchUniversalLoginBrandingData(
EnabledLocales: tenant.GetEnabledLocales(),
Domain: tenantDomain,
},
Prompt: prompt,
Prompts: []*promptData{prompt},
}, nil
}

Expand Down Expand Up @@ -269,10 +269,15 @@ func fetchPromptCustomTextWithDefaults(

brandingTextTranslations := mergeBrandingTextTranslations(defaultTranslations, customTranslations)

customText := make(map[string]interface{}, 0)
for key, value := range brandingTextTranslations {
customText[key] = value
}

return &promptData{
Language: language,
Prompt: promptName,
CustomText: brandingTextTranslations,
CustomText: customText,
}, nil
}

Expand Down Expand Up @@ -405,7 +410,16 @@ func (h *webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
continue
}
case saveBrandingMessageType:
h.display.Warnf("not yet implemented")
var saveBrandingMsg universalLoginBrandingData
if err := json.Unmarshal(message.Payload, &saveBrandingMsg); err != nil {
h.display.Errorf("failed to unmarshal save branding data payload: %v", err)
continue
}

if err := saveUniversalLoginBrandingData(r.Context(), h.api, &saveBrandingMsg); err != nil {
h.display.Errorf("failed to save branding data: %v", err)
continue
}
}
}
}
Expand All @@ -423,3 +437,35 @@ func checkOriginFunc(r *http.Request) bool {

return originURL.String() == webAppURL
}

func saveUniversalLoginBrandingData(ctx context.Context, api *auth0.API, data *universalLoginBrandingData) error {
group, ctx := errgroup.WithContext(ctx)

group.Go(func() (err error) {
return api.Branding.Update(ctx, data.Settings)
})

group.Go(func() (err error) {
return api.Branding.SetUniversalLogin(ctx, data.Template)
})

group.Go(func() (err error) {
themeID := data.Theme.GetID()
if themeID != "" {
data.Theme.ID = nil
return api.BrandingTheme.Update(ctx, themeID, data.Theme)
}

return api.BrandingTheme.Create(ctx, data.Theme)
})

for _, prompt := range data.Prompts {
prompt := prompt

group.Go(func() (err error) {
return api.Prompt.SetCustomText(ctx, prompt.Prompt, prompt.Language, prompt.CustomText)
})
}

return group.Wait()
}
Loading

0 comments on commit 3500101

Please sign in to comment.