Skip to content

Commit

Permalink
Create EmailService.ts (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
JorWo authored Mar 8, 2024
1 parent a88cde9 commit 32bf36e
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 12 deletions.
1 change: 1 addition & 0 deletions ui/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# =========================================================================
NEXT_PUBLIC_BASE_URL=http://localhost:8080/uhgroupings
NEXT_PUBLIC_API_BASE_URL=http://localhost:8081/uhgroupingsapi
NEXT_PUBLIC_API_2_1_BASE_URL=http://localhost:8081/uhgroupingsapi/api/groupings/v2.1

# =========================================================================
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/layout/footer/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Footer = () => (
<div className="grid sm:grid-cols-12 md:grid-cols-12">
<div className="mx-auto mb-6 col-span-6 md:col-span-5 lg:col-span-4">
<Image
src="uhgroupings/uh-logo-system.svg"
src="/uhgroupings/uh-logo-system.svg"
alt="UH System logo"
width={235}
height={235} />
Expand Down
4 changes: 2 additions & 2 deletions ui/src/components/layout/navbar/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Navbar = async () => {
<div className="flex justify-between">
<Link href="/" className="lg:inline hidden">
<Image
src="uhgroupings/uh-groupings-logo.svg"
src="/uhgroupings/uh-groupings-logo.svg"
alt="UH Groupings Logo"
width={256}
height={256} />
Expand All @@ -24,7 +24,7 @@ const Navbar = async () => {
<MobileNavbar currentUser={currentUser} />
<Link href="/">
<Image
src="uhgroupings/uh-groupings-logo-large.svg"
src="/uhgroupings/uh-groupings-logo-large.svg"
alt="UH Groupings Logo"
width={56}
height={56} />
Expand Down
29 changes: 29 additions & 0 deletions ui/src/services/EmailService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use server';

import { getCurrentUser } from '@/access/AuthenticationService';
import { ApiError } from '@/groupings/GroupingsApiResults';
import { postRequest } from './FetchService';

const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL as string;

export type Feedback = {
name?: string,
email: string,
type: string,
message: string,
exceptionMessage?: string
};

export type EmailResult = {
resultCode: string,
recipient: string,
from: string,
subject: string,
text: string
}

export const sendFeedback = async (feedback: Feedback): Promise<EmailResult & ApiError> => {
const currentUser = await getCurrentUser();
const endpoint = `${baseUrl}/email/send/feedback`;
return postRequest(endpoint, currentUser.uid, feedback);
}
8 changes: 4 additions & 4 deletions ui/src/services/FetchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const getRequest = async <T> (
export const postRequest = async <T> (
endpoint: string,
currentUserKey: string,
body?: string | string[],
body?: object | string | string[],
): Promise<T & ApiError> =>
await fetch(endpoint, {
method: 'POST',
Expand Down Expand Up @@ -130,7 +130,7 @@ export const postRequestRetry = async <T> (
export const putRequest = async <T> (
endpoint: string,
currentUserKey: string,
body?: string | string[]
body?: object | string | string[]
): Promise<T & ApiError> =>
await fetch(endpoint, {
method: 'PUT',
Expand Down Expand Up @@ -170,7 +170,7 @@ export const putRequestAsync = async <T> (
export const deleteRequest = async <T> (
endpoint: string,
currentUserKey: string,
body?: string | string[]
body?: object | string | string[]
): Promise<T & ApiError> =>
await fetch(endpoint, { method: 'DELETE', headers: { 'current_user': currentUserKey }, body: JSON.stringify(body) })
.then(res => res.json())
Expand All @@ -189,7 +189,7 @@ export const deleteRequest = async <T> (
export const deleteRequestAsync = async <T> (
endpoint: string,
currentUserKey: string,
body?: string | string[]
body?: object | string | string[]
): Promise<T & ApiError> =>
await fetch(endpoint, {
method: 'DELETE',
Expand Down
3 changes: 2 additions & 1 deletion ui/tests/components/layout/footer/Footer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ describe ('Footer', () => {
render(<Footer />);

expect(screen.getByRole('contentinfo')).toBeInTheDocument();
expect(screen.getByRole('img', { name: 'UH System logo' })).toBeInTheDocument();
expect(screen.getByRole('img', { name: 'UH System logo' }))
.toHaveAttribute('src', '/uhgroupings/uh-logo-system.svg');
expect(screen.getByRole('link', { name: 'equal opportunity/affirmative action institution' }))
.toHaveAttribute('href', 'https://www.hawaii.edu/offices/eeo/policies/?policy=antidisc');
expect(screen.getByRole('link', { name: 'Usage Policy' }))
Expand Down
8 changes: 4 additions & 4 deletions ui/tests/components/layout/navbar/Navbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Navbar', () => {

expect(screen.getByRole('navigation')).toBeInTheDocument();
expect(screen.getAllByRole('img', { name: 'UH Groupings Logo' })[0])
.toHaveAttribute('src', 'uhgroupings/uh-groupings-logo.svg');
.toHaveAttribute('src', '/uhgroupings/uh-groupings-logo.svg');
expect(screen.getAllByRole('link', { name: 'UH Groupings Logo' })[0]).toHaveAttribute('href', '/');
expect(screen.queryByRole('link', { name: 'Admin' })).not.toBeInTheDocument();
expect(screen.queryByRole('link', { name: 'Memberships' })).not.toBeInTheDocument();
Expand All @@ -43,7 +43,7 @@ describe('Navbar', () => {

expect(screen.getByRole('navigation')).toBeInTheDocument();
expect(screen.getAllByRole('img', { name: 'UH Groupings Logo' })[0])
.toHaveAttribute('src', 'uhgroupings/uh-groupings-logo.svg');
.toHaveAttribute('src', '/uhgroupings/uh-groupings-logo.svg');
expect(screen.getAllByRole('link', { name: 'UH Groupings Logo' })[0]).toHaveAttribute('href', '/');
expect(screen.queryByRole('link', { name: 'Admin' })).not.toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Memberships' })).toHaveAttribute('href', '/memberships');
Expand All @@ -60,7 +60,7 @@ describe('Navbar', () => {

expect(screen.getByRole('navigation')).toBeInTheDocument();
expect(screen.getAllByRole('img', { name: 'UH Groupings Logo' })[0])
.toHaveAttribute('src', 'uhgroupings/uh-groupings-logo.svg');
.toHaveAttribute('src', '/uhgroupings/uh-groupings-logo.svg');
expect(screen.getAllByRole('link', { name: 'UH Groupings Logo' })[0]).toHaveAttribute('href', '/');
expect(screen.queryByRole('link', { name: 'Admin' })).not.toBeInTheDocument();
expect(screen.getByRole('link', { name: 'Memberships' })).toHaveAttribute('href', '/memberships');
Expand All @@ -77,7 +77,7 @@ describe('Navbar', () => {

expect(screen.getByRole('navigation')).toBeInTheDocument();
expect(screen.getAllByRole('img', { name: 'UH Groupings Logo' })[0])
.toHaveAttribute('src', 'uhgroupings/uh-groupings-logo.svg');
.toHaveAttribute('src', '/uhgroupings/uh-groupings-logo.svg');
expect(screen.getAllByRole('link', { name: 'UH Groupings Logo' })[0]).toHaveAttribute('href', '/');
expect(screen.getByRole('link', { name: 'Admin' })).toHaveAttribute('href', '/admin');
expect(screen.getByRole('link', { name: 'Memberships' })).toHaveAttribute('href', '/memberships');
Expand Down
38 changes: 38 additions & 0 deletions ui/tests/services/EmailService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import User from '@/access/User';
import { Feedback, sendFeedback } from '@/services/EmailService';
import * as AuthenticationService from '@/access/AuthenticationService';

const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL as string;
const testUser: User = JSON.parse(process.env.TEST_USER_A as string);

jest.mock('@/access/AuthenticationService');

describe('EmailService', () => {

const currentUser = testUser;
const headers = { 'current_user': currentUser.uid };

beforeAll(() => {
jest.spyOn(AuthenticationService, 'getCurrentUser').mockResolvedValue(testUser);
});

describe('sendFeedback', () => {

const feedback: Feedback = {
name: 'name',
email: 'email',
type: 'type',
message: 'message'
};

it('should make a POST request at the correct endpoint', async () => {
await sendFeedback(feedback);
expect(fetch).toHaveBeenCalledWith(`${baseUrl}/email/send/feedback`, {
body: JSON.stringify(feedback),
headers,
method: 'POST'
});
});

});
});

0 comments on commit 32bf36e

Please sign in to comment.