Skip to content

Commit

Permalink
chore: add support for custom email templates
Browse files Browse the repository at this point in the history
  • Loading branch information
psanders committed Dec 22, 2023
1 parent 8a7a3df commit 038cc37
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/goodtok
CLOAK_ENCRYPTION_KEY=k1.aesgcm256.MmPSvzCG9fk654bAbl30tsqq4h9d3N4F11hlue8bGAY=

# Uncomment to enable custom email templates
# See api/src/notifications/templates for available templates
# If not set, the default templates will be used
# EMAIL_TEMPLATES_DIR=/path/to/email/templates
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ SMTP_SENDER=Goodtok Info <[email protected]>
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
DATABASE_URL=postgresql://postgres:postgres@postgres:5432/goodtok
CLOAK_ENCRYPTION_KEY=k1.aesgcm256.MmPSvzCG9fk654bAbl30tsqq4h9d3N4F11hlue8bGAY=
CLOAK_ENCRYPTION_KEY=k1.aesgcm256.MmPSvzCG9fk654bAbl30tsqq4h9d3N4F11hlue8bGAY=

# Uncomment to enable custom email templates
# See api/src/notifications/templates for available templates
# If not set, the default templates will be used
# EMAIL_TEMPLATES_DIR=/path/to/email/templates
```

Finally, run the following command to start the application:
Expand Down Expand Up @@ -120,6 +125,7 @@ A customer token is a [JSON Web Token](https://jwt.io/) with necessay claims to
"calendarUrl": "https://cal.com/placeholder",
"signalingHost": "peerjs.goodtok.io",
"signalingPort": 443,
"iceServers": { ... }
"metadata": {
"name": "Peter",
"email": "[email protected]",
Expand Down
51 changes: 51 additions & 0 deletions docs/docs/self-hosting/deploy-with-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
DATABASE_URL=postgresql://postgres:postgres@postgres:5432/goodtok
CLOAK_ENCRYPTION_KEY=k1.aesgcm256.MmPSvzCG9fk654bAbl30tsqq4h9d3N4F11hlue8bGAY=

# Uncomment to enable custom email templates
# See api/src/notifications/templates for available templates
# If not set, the default templates will be used
# EMAIL_TEMPLATES_DIR=/path/to/email/templates
```

Few important things to note:
Expand All @@ -74,3 +79,49 @@ The previous command will start all the services, including the Front Office. Yo
## Securing the application

Comming soon...

## Custom Email Templates

Goodtok use handlebars to render email templates. You can customize the templates by setting the `EMAIL_TEMPLATES_DIR` variable to the path where your templates are located. The following templates are available:

The `inviteExistingUserTemplate.hbs` and `inviteNewUserTemplate.hbs` templates are used to send invitations to users. The `inviteExistingUserTemplate.hbs` template is used when the user already exists in the system. The `inviteNewUserTemplate.hbs` template is used when the user does not exist in the system.

If a template is not set at the path specified by the `EMAIL_TEMPLATES_DIR` variable, the default template will be used.

Available variables for the `inviteExistingUserTemplate.hbs` and `inviteNewUserTemplate.hbs` templates:

- `{{workspaceName}}`: The name of the store
- `{{inviteUrl}}`: The URL where the user can accept the invitation
- `{{oneTimePassword}}`: The one-time password that the user can use to accept the invitation (only available in the `inviteNewUserTemplate.hbs` template)

An example of the `inviteExistingUserTemplate.hbs` template:

```html
<html>
<head>
<title>Invite</title>
</head>
<body>
<p>Welcome to Goodtok</p>
<p>To accept the invitation, please click the following link:
<a href="{{inviteUrl}}">{{inviteUrl}}</a>
</p>
</body>
</html>
```

An example of the `inviteNewUserTemplate.hbs` template:

```html
<html>
<head>
<title>Invite</title>
</head>
<body>
<p>Welcome to Goodtok</p>
<p>Your one-time password is: <b>{{oneTimePassword}}</b></p>
<p>You can use it at the following URL:
<a href="{{inviteUrl}}">{{inviteUrl}}</a></p>
</body>
</html>
```
3 changes: 3 additions & 0 deletions mods/apiserver/src/envs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ export const SMTP_SECURE = e.SMTP_SECURE ?? true;
export const SMTP_AUTH_USER = e.SMTP_AUTH_USER;
export const SMTP_AUTH_PASS = e.SMTP_AUTH_PASS;
export const SMTP_SENDER = e.SMTP_SENDER;

// Custom email templates
export const EMAIL_TEMPLATES_DIR = e.EMAIL_TEMPLATES_DIR
18 changes: 9 additions & 9 deletions mods/apiserver/src/notifications/createInviteBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { EMAIL_TEMPLATES_DIR } from "../envs";
import handlebars from "handlebars";
import fs from "fs";
import path from "path";

const TEMPLATE_DIR = path.join(__dirname, "templates");

enum TemplateName {
INVITE_NEW_USER = "inviteNewUserTemplate",
INVITE_EXISTING_USER = "inviteExistingUserTemplate"
}

const compileTemplate = (
templateName: string,
data: Record<string, string>
) => {
const filePath = path.join(TEMPLATE_DIR, `${templateName}.hbs`);
function compileTemplate(templateName: string, data: Record<string, string>) {
let filePath = path.join(EMAIL_TEMPLATES_DIR, `${templateName}.hbs`);
if (!fs.existsSync(filePath)) {
filePath = path.join(__dirname, "templates", `${templateName}.hbs`);
}

const source = fs.readFileSync(filePath, "utf-8").toString();
const template = handlebars.compile(source);
return template(data);
};
}

export function createInviteBody(data: Record<string, string>) {
if (data.oneTimePassword) {
return compileTemplate(TemplateName.INVITE_NEW_USER, data);
} else {
return compileTemplate(TemplateName.INVITE_EXISTING_USER, data);
}
}
}

0 comments on commit 038cc37

Please sign in to comment.