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

[JN-1547] allow ternary in email templates #1421

Open
wants to merge 4 commits into
base: development
Choose a base branch
from

Conversation

connorlbark
Copy link
Collaborator

@connorlbark connorlbark commented Jan 27, 2025

DESCRIPTION (include screenshots, and mobile screenshots for participant UX)

Allow ternary e.g. {isSubject ? "your" : "your child's"} in email templates. Not super robust (doesn't allow escaping quotes, doesn't allow nesting) but should work for 99.9% of use cases.

TO TEST: (simple manual steps for confirming core behavior -- used for pre-release checks)

  • Sign up a non-proxy user; observe you get an email that says "your signed consent" when you consent
  • Sign up a proxy user; observe you get an email that says "your child's signed consent" when you consent

@@ -45,6 +45,12 @@ protected EnrolleeEmailSubstitutor(EnrolleeContext ruleData, NotificationContext
valueMap.put("siteMediaBaseUrl", getImageBaseUrl(contextInfo.portalEnv(), contextInfo.portalEnvConfig(), contextInfo.portal().getShortcode()));
valueMap.put("siteImageBaseUrl", getImageBaseUrl(contextInfo.portalEnv(), contextInfo.portalEnvConfig(), contextInfo.portal().getShortcode()));
valueMap.put("profile", enrolleeContext.getProfile());
if (enrolleeContext.getParticipantUser().getUsername().contains("-prox-")) {
Copy link
Collaborator Author

@connorlbark connorlbark Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find another way to glean this information from the enrollee context. I think this is probably OK but obviously not foolproof, so let me know if there's a better way to do this.

As an aside, seeing this (+ Matt's participant document PR where he does some profileId checks) makes me think we should have some kind of EnrolleeAccountType enum where the values are something like PROXY_ONLY GOVERNED_USER SELF_ENROLL and SELF_AND_PROXY

@connorlbark connorlbark marked this pull request as ready for review January 27, 2025 13:37
Copy link
Collaborator

@devonbush devonbush left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the implementation of the ternaries -- just needs some tweaks to handling the isProxy/Governed logic

@@ -45,6 +46,13 @@ protected EnrolleeEmailSubstitutor(EnrolleeContext ruleData, NotificationContext
valueMap.put("siteMediaBaseUrl", getImageBaseUrl(contextInfo.portalEnv(), contextInfo.portalEnvConfig(), contextInfo.portal().getShortcode()));
valueMap.put("siteImageBaseUrl", getImageBaseUrl(contextInfo.portalEnv(), contextInfo.portalEnvConfig(), contextInfo.portal().getShortcode()));
valueMap.put("profile", enrolleeContext.getProfile());
if (Objects.nonNull(enrolleeContext.getParticipantUser())
&& StringUtils.contains(enrolleeContext.getParticipantUser().getUsername(), "-prox-")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll want to make this more robust -- this might not work in the context of age-up protocols, there may be a period of time when they have their real email address as their account name but are still proxied.

I'd add an "isGoverned" field to EnrolleeContext, and a query in EnrolleeContextService to populate it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it simpler to just fetch all the enrollee relations and populate them - perhaps a heavyweight solution, though

&& StringUtils.contains(enrolleeContext.getParticipantUser().getUsername(), "-prox-")) {
valueMap.put("isProxy", "true");
} else {
valueMap.put("isSubject", "true");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's go ahead and just put the whole enrolleee on the valueMap -- that way we have subject and consented available

}

public static boolean isTernary(String key) {
return key.contains("?") && key.contains(":") && key.indexOf('?') < key.indexOf(':');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be just my personal preference, but I lean towards (commented) regexes for stuff like this -- something like/[^:]+\?.+:

@@ -80,6 +90,37 @@ public String lookup(String key) {
return "";
}

private record Ternary(String condition, String left, String right) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I like how you made this as a record -- good encapsulation of logic

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot

See analysis details on SonarQube Cloud

@connorlbark connorlbark requested a review from devonbush January 29, 2025 20:47
@connorlbark
Copy link
Collaborator Author

test failure is a fluke and sonarcloud is flagging the regex which shouldn't be a security problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants