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

Send emails when someone registers a new team #43

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
63 changes: 63 additions & 0 deletions supabase/functions/org-registration-email-trigger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Check out README.md for how to set up this function with Supabase
import {SMTPClient} from "https://deno.land/x/denomailer/mod.ts";
import {createClient} from 'https://esm.sh/@supabase/supabase-js'

const client = new SMTPClient({
connection: {
hostname: "smtp.gmail.com",
port: 465,
tls: true,
auth: {
username: Deno.env.get("SMTP_USERNAME")!,
password: Deno.env.get("SMTP_PASSWORD")!,
}
}
})

const emails = Deno.env.get("EMAILS")!.split(",")

console.log(emails)

Deno.serve(async (req) => {
try {
const {record} = await req.json();
if (record.organization_id == null) {
return new Response(
JSON.stringify({message: "No organization id"}),
{headers: {"Content-Type": "application/json"}},
)
}
const supabase = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_ANON_KEY') ?? '',
{global: {headers: {Authorization: req.headers.get('Authorization')!}}}
)
const {data, error} = await supabase
.rpc('does_org_lack_admin', {org_id_arg: record.organization_id});
if (error) {
throw error
}
if (data) {
await client.send({
from: "[email protected]",
to: emails,
subject: "Eaglescout Organization Registration Notification",
content: `A user on the organization with id ${record.organization_id} has requested to register.`,
})
return new Response(
JSON.stringify({message: "Email sent"}),
{headers: {"Content-Type": "application/json"}},
)
} else {
return new Response(
JSON.stringify({message: "Organization already has an admin"}),
{headers: {"Content-Type": "application/json"}},
)
}
} catch (e) {
return new Response(
JSON.stringify({message: "Error: " + e}),
{headers: {"Content-Type": "application/json"}},
)
}
})
22 changes: 22 additions & 0 deletions supabase/functions/register-team-email-trigger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Setting up the register team email trigger

This function is triggered when a new team is created and sends an email to a specified email list.

alaninnovates marked this conversation as resolved.
Show resolved Hide resolved
1. Go to the Database Webhooks page in the Supabase dashboard
2. Enable webhooks
3. Click on the "Add a new hook" button
4. Select the `register_team_requests` table on insert
5. Select "Supabase Edge Function" for the type of webhook
6. Select the register-team-email-trigger as the trigger
7. Click the dropdown next to "Add a new header", select "Auth header with service key"
8. Click "Create Webhook"

## Setting up Environment Variables

The following environment variables must be set up in Supabase's Secret manager:

SMTP_USERNAME: The username to the SMTP account

SMTP_PASSWORD: The password to the SMTP account

EMAILS: A comma seperated list of emails to send the info to
35 changes: 35 additions & 0 deletions supabase/functions/register-team-email-trigger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Check out README.md for how to set up this function with Supabase
import { SMTPClient } from "https://deno.land/x/denomailer/mod.ts";

const client = new SMTPClient({
connection: {
hostname: "smtp.gmail.com",
port: 465,
tls: true,
auth: {
username: Deno.env.get("SMTP_USERNAME")!,
password: Deno.env.get("SMTP_PASSWORD")!,
}
}
})

const emails = Deno.env.get("EMAILS")!.split(",")

console.log(emails)

Deno.serve(async (req) => {
const {record} = await req.json();
await client.send({
from: "[email protected]",
to: emails,
subject: "Eaglescout Team Registration Notification",
content: `A new team has requested to register with the following details:
Team: ${record.team}
Email: ${record.email}
`,
})
return new Response(
JSON.stringify({ message: "Email sent" }),
{ headers: { "Content-Type": "application/json" } },
)
})