This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cert): preliminary certificate generation, validation, distribut…
…ion (wip)
- Loading branch information
Showing
20 changed files
with
1,299 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Resend } from 'resend'; | ||
import { NextRequest } from 'next/server'; | ||
import { runs } from '@trigger.dev/sdk/v3'; | ||
import CertEmail from '@/emails/CertEmail'; | ||
|
||
/** | ||
* Email certificates to evaluation participants | ||
* | ||
* @param req PayloadCerts | ||
*/ | ||
export async function POST(req: NextRequest) { | ||
const { runId, activity, recipients } = await req.json(); | ||
|
||
// Validate required fields | ||
if (!runId || !activity || !recipients?.length) { | ||
return Response.json({ error: 'Missing required fields' }, { status: 400 }); | ||
} | ||
|
||
const run = await runs.retrieve(runId); | ||
if (!run?.isExecuting) { | ||
return Response.json( | ||
{ error: 'Invalid run ID or run not executing' }, | ||
{ status: 400 }, | ||
); | ||
} | ||
|
||
// send email to assigned faculties | ||
const resend = new Resend(process.env.RESEND_API); | ||
const errors = []; | ||
|
||
for (const recipient of recipients) { | ||
const { error } = await resend.emails.send({ | ||
from: 'Community Extension Services Office <noreply@mail.deuz.tech>', | ||
to: recipient.recipient_email, | ||
subject: 'Thank you for participating in the activity: ' + activity.title, | ||
react: CertEmail({ activity }), | ||
attachments: [ | ||
{ | ||
filename: `${recipient.recipient_name.replace(/[^a-z0-9]/gi, '_')}.pdf`, | ||
path: recipient.url as string, | ||
}, | ||
], | ||
headers: { | ||
'X-Entity-Ref-ID': runId, | ||
}, | ||
}); | ||
|
||
if (error) { | ||
errors.push({ | ||
recipient: recipient.recipient_email, | ||
error: error.message, | ||
}); | ||
} | ||
} | ||
|
||
if (errors.length > 0) { | ||
return Response.json( | ||
{ errors }, | ||
{ | ||
status: 400, | ||
}, | ||
); | ||
} | ||
|
||
return Response.json( | ||
{ message: 'Emails sent successfully' }, | ||
{ | ||
status: 200, | ||
}, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { Metadata } from 'next'; | ||
import { metadata as defaultMetadata } from '@/app/layout'; | ||
import { Box, Text, Title } from '@mantine/core'; | ||
import { cookies } from 'next/headers'; | ||
import { createServerClient } from '@/libs/supabase/server'; | ||
|
||
export const metadata: Metadata = { | ||
title: 'Certifications - ' + defaultMetadata.title, | ||
}; | ||
|
||
export default async function PublicCertsPage({ | ||
params, | ||
}: { | ||
params: Promise<{ id: string }>; | ||
}) { | ||
const cookieStore = cookies(); | ||
const supabase = await createServerClient(cookieStore); | ||
|
||
const { id } = await params; | ||
|
||
// get certs details | ||
const certsQuery = await supabase | ||
.from('certs') | ||
.select() | ||
.eq('hash', id) | ||
.limit(1) | ||
.single(); | ||
|
||
return ( | ||
<Box> | ||
<Title order={3}>Valid Certificate</Title> | ||
<br /> | ||
<Text fw="bold">{certsQuery.data?.recipient_name}</Text> | ||
<Text size="sm">{certsQuery.data?.recipient_email}</Text> | ||
</Box> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { ReactNode } from 'react'; | ||
import type { Metadata } from 'next'; | ||
import Image from 'next/image'; | ||
import { metadata as defaultMetadata } from '@/app/layout'; | ||
import { Container, Box, Group } from '@mantine/core'; | ||
import cesoLogo from '@/components/_assets/img/ceso-manila.webp'; | ||
|
||
export const metadata: Metadata = { | ||
title: defaultMetadata.title, | ||
description: defaultMetadata.description, | ||
}; | ||
|
||
export default async function Layout({ children }: { children: ReactNode }) { | ||
return ( | ||
<Container pb="lg" size="md"> | ||
<Group justify="center" my="xl"> | ||
<Image | ||
alt="Community Extensions Services Office of Technological Institute of the Philippines - Manila" | ||
className="rounded-md shadow-md" | ||
height={102} | ||
placeholder="blur" | ||
priority={false} | ||
src={cesoLogo} | ||
width={256} | ||
/> | ||
</Group> | ||
|
||
<Box | ||
bg="light-dark( | ||
var(--mantine-color-white), | ||
var(--mantine-color-dark-6) | ||
)" | ||
className="rounded-xl shadow-lg" | ||
my="lg" | ||
p="xl" | ||
> | ||
{children} | ||
</Box> | ||
</Container> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
'use client'; | ||
|
||
import { memo, useState, useEffect } from 'react'; | ||
import { useDebouncedValue } from '@mantine/hooks'; | ||
import { | ||
Autocomplete, | ||
type AutocompleteProps, | ||
Avatar, | ||
Group, | ||
Text, | ||
Loader, | ||
} from '@mantine/core'; | ||
import { notifications } from '@mantine/notifications'; | ||
import { getActivities } from '@/libs/supabase/api/activity'; | ||
import type { Tables } from '@/libs/supabase/_database'; | ||
import { formatDateRange } from 'little-date'; | ||
|
||
export const ActivityInput = memo((props: AutocompleteProps) => { | ||
const [query, setQuery] = useState(''); | ||
const [activityQuery] = useDebouncedValue(query, 200); | ||
const [data, setData] = useState<Tables<'activities_details_view'>[]>([]); | ||
const [loading, setLoading] = useState(false); | ||
|
||
// custom autocomplete item ui | ||
const renderAutocompleteOption: AutocompleteProps['renderOption'] = ({ | ||
option, | ||
}) => { | ||
const activity = data.find((activity) => activity.id === option.value); | ||
return ( | ||
<Group gap="sm"> | ||
<Avatar | ||
className="object-contain" | ||
color="initials" | ||
name={activity?.title as string} | ||
radius="md" | ||
size={90} | ||
src={activity?.image_url} | ||
/> | ||
<div> | ||
<Text fw="bold" size="sm"> | ||
{data.find((activity) => activity.id === option.value)?.title} | ||
</Text> | ||
<Text c="dimmed" size="xs"> | ||
{activity?.date_starting && | ||
formatDateRange( | ||
new Date(activity.date_starting), | ||
new Date(activity?.date_ending!), | ||
{ | ||
includeTime: true, | ||
}, | ||
)} | ||
</Text> | ||
</div> | ||
</Group> | ||
); | ||
}; | ||
|
||
useEffect(() => { | ||
const fetchSeries = async () => { | ||
setLoading(true); | ||
const response = await getActivities({ search: activityQuery }); | ||
|
||
if (response.data) { | ||
setData(response.data); | ||
} else { | ||
notifications.show({ | ||
title: 'Unable to fetch activity', | ||
message: response.message, | ||
color: 'red', | ||
withBorder: true, | ||
withCloseButton: true, | ||
autoClose: 5000, | ||
}); | ||
} | ||
|
||
setLoading(false); | ||
}; | ||
|
||
// practivities query on initial render | ||
if (activityQuery) { | ||
// noinspection JSIgnoredPromiseFromCall | ||
void fetchSeries(); | ||
} | ||
}, [activityQuery]); | ||
|
||
return ( | ||
<Autocomplete | ||
data={data.map((activity) => ({ | ||
value: activity.id!, | ||
label: activity.title, | ||
}))} | ||
label="Activity Title" | ||
limit={5} | ||
onChangeCapture={(e) => setQuery(e.currentTarget.value)} | ||
placeholder="Brigada Eskwela" | ||
renderOption={renderAutocompleteOption} | ||
rightSection={loading ? <Loader size="1rem" /> : null} | ||
{...props} | ||
/> | ||
); | ||
}); | ||
ActivityInput.displayName = 'ActivityInput'; |
Oops, something went wrong.