Skip to content

Commit

Permalink
chore: sending email when free account creation code is created
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Jul 16, 2024
1 parent 9497dfb commit 80eb6af
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 46 deletions.
3 changes: 2 additions & 1 deletion src/lib/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
"Failed to create account: {reason}": "Failed to create account: {reason}",
"Error creating account": "Error creating account",
"Error creating account: {reason}": "Error creating account: {reason}",
"Unknown error": "Unknown error"
"Unknown error": "Unknown error",
"You've either already received your free account or an account creation code has been sent to your email. To create additional accounts, please proceed with the purchase below.": "You've either already received your free account or an account creation code has been sent to your email. To create additional accounts, please proceed with the purchase below."
}
76 changes: 36 additions & 40 deletions src/lib/sendgrid.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,41 @@
// /** Sendgrid email helpers, backend only. */
// This is currently being handled by the Whalesplainer hosted at https://create-api.anchor.link
// If this service ends up replacing Whalesplainer then, this will need to be uncommented.
import { SENDGRID_KEY, SENDGRID_TEMPLATE, SENDGRID_FROM } from '$env/static/private'

// import { SENDGRID_KEY, SENDGRID_TEMPLATE, SENDGRID_FROM} from '$env/static/private'
const key = SENDGRID_KEY
const templateId = SENDGRID_TEMPLATE || 'd-1106a932fc984f14be0230c670820b38'
const from = SENDGRID_FROM || '[email protected]'

// const key = SENDGRID_KEY
// const templateId = SENDGRID_TEMPLATE || 'd-1106a932fc984f14be0230c670820b38'
// const from = SENDGRID_FROM || '[email protected]'

if (!key) {
console.log('Missing SENDGRID_KEY, will not send emails')
}

// if (!key) {
// console.log('Missing SENDGRID_KEY, will not send emails')
// }
export async function sendEmail(to: string, data: Record<string, string>) {
if (!key) return

// export async function sendEmail(to: string, data: Record<string, string>) {
// if (!key) return

// const res = await fetch('https://api.sendgrid.com/v3/mail/send', {
// body: JSON.stringify({
// from: {
// email: from
// },
// personalizations: [
// {
// to: [{ email: to }],
// dynamic_template_data: data
// }
// ],
// template_id: templateId
// }),
// headers: {
// Authorization: `Bearer ${key}`,
// 'Content-Type': 'application/json'
// },
// method: 'POST'
// })
// if (res.status >= 400) {
// const body =
// parseInt(res.headers.get('content-length') || '0') > 0
// ? await res.text()
// : `${res.status} ${res.statusText}`
// throw new Error(`Sendgrid error: ${body}`)
// }
// }
const res = await fetch('https://api.sendgrid.com/v3/mail/send', {
body: JSON.stringify({
from: {
email: from
},
personalizations: [
{
to: [{ email: to }],
dynamic_template_data: data
}
],
template_id: templateId
}),
headers: {
Authorization: `Bearer ${key}`,
'Content-Type': 'application/json'
},
method: 'POST'
})
if (res.status >= 400) {
const body =
parseInt(res.headers.get('content-length') || '0') > 0
? await res.text()
: `${res.status} ${res.statusText}`
throw new Error(`Sendgrid error: ${body}`)
}
}
2 changes: 1 addition & 1 deletion src/routes/buy/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
{:else}
<div class="bg-yellow-100 border border-yellow-400 rounded-lg p-6 mb-4">
<h3 class="text-xl font-semibold mb-2">{$t('Free Account Not Available')}</h3>
<p>{$t('You\'ve already received your free account. To create additional accounts, please proceed with the purchase below.')}</p>
<p>{$t('You\'ve either already received your free account or an account creation code has been sent to your email. To create additional accounts, please proceed with the purchase below.')}</p>
</div>
{/if}

Expand Down
17 changes: 13 additions & 4 deletions src/routes/ticket/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,37 @@ import type { Actions } from './$types';
import { CreateRequest } from '@greymass/account-creation';
import { createTicket, generateCreationRequest, getSextantProductId } from '$lib/sextant';
import { PUBLIC_WHALESPLAINER_URL } from '$env/static/public';
import { sendEmail } from '$lib/sendgrid';

export const actions: Actions = {
default: async ({ url, locals, request }) => {
const session = await locals.auth();

if (!session) {
return fail(401, { error: 'Unauthorized' });
return fail(401, { error: 'Unauthorized' });
}

const formData = await request.formData();

const searchParams = new URLSearchParams(String(formData.get('searchParams')));

const createRequestArguments = {
login_scope: String(searchParams.get('scope') ?? ''),
return_path: String(searchParams.get('return_url') ?? ''),
login_scope: String(searchParams.get('scope') ?? ''),
return_path: String(searchParams.get('return_url') ?? ''),
};

if (!session.user?.email) {
return fail(403, { error: 'Session must contain an email address.' });
}

const createRequest: CreateRequest = generateCreationRequest(createRequestArguments)
const sextantProductId = await getSextantProductId();

await createTicket(createRequest.code, sextantProductId, 'free account - google login', session.user?.email ?? undefined);
await createTicket(createRequest.code, sextantProductId, 'free account', session.user.email);

await sendEmail(session.user.email, {
createurl: `${PUBLIC_WHALESPLAINER_URL}/activate/${createRequest.toString(false)}`,
});

if (searchParams.get('owner_key') || searchParams.get('active_key')) {
redirect(302, `/create?ticket=${createRequest.toString(false)}&${searchParams}`);
Expand Down

0 comments on commit 80eb6af

Please sign in to comment.