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

feat(hackathon): automated email setup #87

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 8 additions & 1 deletion apps/hackathon/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ DATABASE_URL="postgresql://postgres:password@localhost:5432/hackathon"
# NEXTAUTH_SECRET=""
NEXTAUTH_URL="http://localhost:8000"
GOOGLE_CLIENT_ID="YOUR_GOOGLE_CLIENT_ID"
GOOGLE_CLIENT_SECRET="YOUR_GOOGLE_CLIENT_SECRET"
GOOGLE_CLIENT_SECRET="YOUR_GOOGLE_CLIENT_SECRET"


# AWS CONFIG
AWS_ACCESS_KEY_ID="YOUR_AWS_ACCESS_KEY_ID"
AWS_SECRET_ACCESS_KEY="YOUR_AWS_SECRET_ACCESS_KEY"
AWS_REGION="YOUR_AWS_REGION"
SES_SOURCE_EMAIL="YOUR_SES_SOURCE_EMAIL"
4 changes: 4 additions & 0 deletions apps/hackathon/.github/workflows/BUILD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jobs:
NEXTAUTH_URL: ${{ secrets.NEXTAUTH_URL }}
GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
SES_SOURCE_EMAIL: ${{ secrets.SES_SOURCE_EMAIL }}
run: bun run --bun build

- name: 💰 Profit
Expand Down
4 changes: 4 additions & 0 deletions apps/hackathon/.github/workflows/RELEASE.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ jobs:
NEXTAUTH_URL: ${{ secrets.NEXTAUTH_URL }}
GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
SES_SOURCE_EMAIL: ${{ secrets.SES_SOURCE_EMAIL }}
run: bun run --bun semantic-release

- name: 💰 Profit
Expand Down
4 changes: 4 additions & 0 deletions apps/hackathon/.github/workflows/TEST_e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ jobs:
NEXTAUTH_URL: ${{ secrets.NEXTAUTH_URL }}
GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ${{ secrets.AWS_REGION }}
SES_SOURCE_EMAIL: ${{ secrets.SES_SOURCE_EMAIL }}
run: bun run --bun playwright test

- name: 📄 Upload Artifact
Expand Down
164 changes: 164 additions & 0 deletions apps/hackathon/src/app/_components/userInformation/ApplicationForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
'use client'
import type { FormEvent } from 'react'
import React, { useState } from 'react'
import type { UserInformation } from '@prisma/client'
import { api } from '~/trpc/react'

interface ApplicationFormProps {
session: {
user: {
id: string
}
}
}

export function ApplicationForm(props: ApplicationFormProps) {
const [subscribe, setSubscribe] = useState<boolean>(false)
const [formData, setFormData] = useState<UserInformation>({
id: '1',
email: '',
first_name: '',
last_name: '',
levels_of_study: '',
major: '',
date_of_birth: new Date(0),
gender: '',
phone_number: '',
school: '',
userId: props.session?.user.id || '',
})

const submitApplicationForm = api.userInformation.useMutation()
const subscribeToNewsletter = api.subscribeToNewsletter.useMutation()
const handleSubmit = async (e: FormEvent) => {
e.preventDefault()
try {
if (subscribe && formData.email) {
subscribeToNewsletter.mutateAsync({ email: formData.email })
}
await submitApplicationForm.mutateAsync(formData)
}
catch (error) {
console.error('Error submitting form:', error)
}
}

const handleSubscribe = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target && formData.email) {
setSubscribe(!subscribe)
}
else {
e.target.checked = false
}
}

const handleOnChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target) {
setFormData({
...formData,
[e.target.name]: e.target.value,
})
if (e.target.name === 'email' && subscribe) {
handleSubscribe(e)
}
}
}

return (
<form onSubmit={handleSubmit}>
<label htmlFor="firstName">First Name</label>
<input
type="text"
id="firstName"
name="first_name"
value={formData.first_name || ''}
onChange={handleOnChange}
/>

<label htmlFor="lastName">Last Name</label>
<input
type="text"
id="lastName"
name="last_name"
value={formData.last_name || ''}
onChange={handleOnChange}
/>

<label htmlFor="email">Email</label>
<input
type="email"
id="email"
name="email"
value={formData.email || ''}
onChange={handleOnChange}
/>

<input
type="checkbox"
id="subscribe"
name="subscribe"
value={subscribe ? 'true' : 'false'}
checked={subscribe}
onChange={handleSubscribe}
/>
subscribe to newsletter with email

<label htmlFor="school">School</label>
<input
type="text"
id="school"
value={formData.school || ''}
onChange={handleOnChange}
/>

<label htmlFor="major">Major</label>
<input
type="text"
id="major"
name="major"
value={formData.major || ''}
onChange={handleOnChange}
/>

<label htmlFor="yearOfStudy">Year of Study</label>
<input
type="text"
id="yearOfStudy"
name="levels_of_study"
value={formData.levels_of_study || ''}
onChange={handleOnChange}
/>

<label htmlFor="gender">Gender</label>
<input
type="text"
id="gender"
name="gender"
value={formData.gender || ''}
onChange={handleOnChange}
/>

<label htmlFor="dateOfBirth">Date of Birth</label>
<input
type="date"
id="dateOfBirth"
name="date_of_birth"
value={formData.date_of_birth ? formData.date_of_birth.toISOString().split('T')[0] : ''}
onChange={handleOnChange}
/>

<label htmlFor="phoneNumber">Phone Number</label>
<input
type="tel"
id="phoneNumber"
name="phone_number"
value={formData.phone_number || ''}
onChange={handleOnChange}
/>

<button type="submit">Submit</button>
</form>
)
};

export default ApplicationForm
8 changes: 8 additions & 0 deletions apps/hackathon/src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export const env = createEnv({
),
GOOGLE_CLIENT_ID: z.string(),
GOOGLE_CLIENT_SECRET: z.string(),
AWS_ACCESS_KEY_ID: z.string(),
AWS_SECRET_ACCESS_KEY: z.string(),
AWS_REGION: z.string(),
SES_SOURCE_EMAIL: z.string(),
},

/**
Expand All @@ -47,6 +51,10 @@ export const env = createEnv({
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET: process.env.GOOGLE_CLIENT_SECRET,
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,
AWS_REGION: process.env.AWS_REGION,
SES_SOURCE_EMAIL: process.env.SES_SOURCE_EMAIL,
},
/**
* Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially
Expand Down
4 changes: 4 additions & 0 deletions apps/hackathon/src/server/api/root.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { postRouter } from '~/server/api/routers/post'
import { createCallerFactory, createTRPCRouter } from '~/server/api/trpc'
import { userInformationRouter } from '~/server/api/routers/applicationForm/procedures/setUserInformation'
import { subscribeToNewsletterRouter } from '~/server/api/routers/subscribeToNewsLetter/procedures/subscribeToNewsLetter'

/**
* This is the primary router for your server.
Expand All @@ -8,6 +10,8 @@ import { createCallerFactory, createTRPCRouter } from '~/server/api/trpc'
*/
export const appRouter = createTRPCRouter({
post: postRouter,
userInformation: userInformationRouter,
subscribeToNewsletter: subscribeToNewsletterRouter,
})

// export type definition of API
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { z } from 'zod'

import {
protectedProcedure,
} from '~/server/api/trpc'

export const userInformationRouter = protectedProcedure
.input(
z.object({
id: z.string(),
email: z.string().email().nullable(),
first_name: z.string().nullable(),
last_name: z.string().nullable(),
levels_of_study: z.string().nullable(),
major: z.string().nullable(),
date_of_birth: z.date().nullable(),
gender: z.string().nullable(),
phone_number: z.string().nullable(),
school: z.string().nullable(),
userId: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
const data = await ctx.db.userInformation.create({
data: {
id: input.id,
email: input.email,
first_name: input.first_name,
last_name: input.last_name,
levels_of_study: input.levels_of_study,
major: input.major,
date_of_birth: input.date_of_birth,
gender: input.gender,
phone_number: input.phone_number,
school: input.school,
userId: input.userId,
},
})
return data
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import process from 'node:process'
import { z } from 'zod'
import aws from 'aws-sdk'
import {
protectedProcedure,
} from '~/server/api/trpc'

aws.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: process.env.AWS_REGION,
})

const ses = new aws.SES({ apiVersion: '2010-12-01' })

export const subscribeToNewsletterRouter = protectedProcedure
.input(
z.object({
email: z.string().email(),
}),
)
.mutation(async ({ input }) => {
const { email } = input
const params = {
Destination: {
ToAddresses: [email],
},
Message: {
Body: {
Text: { Data: 'Thanks for subscribing to our newsletter!' },
},
Subject: {
Data: 'Welcome to CuHack Newsletter',
},
},
Source: process.env.EMAIL_SOURCE,
}

try {
await ses.sendEmail(params).promise()
return { success: true }
}
catch (error) {
console.error('Error sending email:', error)
throw new Error('Failed to send email. Please try again later.')
}
})
Loading