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

Add Email Unsubscribe Functionality #192

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions backend/src/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as cors from 'cors';
import * as helmet from 'helmet';
import { handler as healthcheck } from './healthcheck';
import * as auth from './auth';
import * as emailUnsubscribe from './email-unsubscribe';
import * as cpes from './cpes';
import * as cves from './cves';
import * as domains from './domains';
Expand Down Expand Up @@ -111,6 +112,8 @@ app.use(cookieParser());
app.get('/', handlerToExpress(healthcheck));
app.post('/auth/login', handlerToExpress(auth.login));
app.post('/auth/callback', handlerToExpress(auth.callback));
app.post('/email-unsubscribe', handlerToExpress(emailUnsubscribe.unsubscribe));
app.post('/email-resubscribe', handlerToExpress(emailUnsubscribe.resubscribe));
app.post('/users/register', handlerToExpress(users.register));
app.post('/readysetcyber/register', handlerToExpress(users.RSCRegister));

Expand Down
54 changes: 54 additions & 0 deletions backend/src/api/email-unsubscribe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { IsString } from 'class-validator';
import { validateBody, wrapHandler, NotFound } from './helpers';
import { connectToDatabase, EmailBlocklist } from '../models';

class Email {
@IsString()
email: string;
}

/**
* @swagger
*
* /email-unsubscribe:
* post:
* description: Add user to the email blocklist.
*/
export const unsubscribe = wrapHandler(async (event) => {
console.log(event);
const body = await validateBody(Email, event.body);

await connectToDatabase();

body.email = body.email.toLowerCase();
const email = await EmailBlocklist.create({
...body
});
await EmailBlocklist.save(email);

return {
statusCode: 200,
body: 'Email has been added to blocklist'
};
});

export const resubscribe = wrapHandler(async (event) => {
const body = await validateBody(Email, event.body);
await connectToDatabase();

body.email = body.email.toLowerCase();

const email = await EmailBlocklist.findOne({
email: body.email
});
if (email) {
const result = await EmailBlocklist.delete({
email: body.email
});
return {
statusCode: 200,
body: JSON.stringify(result)
};
}
return NotFound;
});
2 changes: 2 additions & 0 deletions backend/src/models/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Cpe,
Cve,
Domain,
EmailBlocklist,
Organization,
OrganizationTag,
Question,
Expand Down Expand Up @@ -121,6 +122,7 @@ const connectDb = async (logging?: boolean) => {
Cpe,
Cve,
Domain,
EmailBlocklist,
Organization,
OrganizationTag,
Question,
Expand Down
21 changes: 21 additions & 0 deletions backend/src/models/email-blocklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
Entity,
Column,
Unique,
PrimaryGeneratedColumn,
BaseEntity,
CreateDateColumn
} from 'typeorm';

@Entity()
@Unique(['email'])
export class EmailBlocklist extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column()
email: string;

@CreateDateColumn()
createdAt: Date;
}
1 change: 1 addition & 0 deletions backend/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './connection';
export * from './cpe';
export * from './cve';
export * from './domain';
export * from './email-blocklist';
export * from './organization';
export * from './organization-tag';
export * from './question';
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
Feeds,
Domains,
Reports,
RegionUsers
RegionUsers,
EmailUnsubscribe
} from 'pages';
import { Layout, RouteGuard } from 'components';
import './styles.scss';
Expand Down Expand Up @@ -131,6 +132,12 @@ const App: React.FC = () => (
/>
<Route exact path="/terms" component={TermsOfUse} />

<Route
exact
path="/unsubscribe"
component={EmailUnsubscribe}
/>

<RouteGuard
exact
path="/inventory"
Expand Down
90 changes: 90 additions & 0 deletions frontend/src/pages/EmailUnsubscribe/EmailUnsubscribe.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React, { useState } from 'react';
import { useHistory } from 'react-router-dom';
import { useAuthContext } from 'context';
import { Button, TextInput, Label } from '@trussworks/react-uswds';

interface FormData {
email: string;
}
interface Errors extends Partial<FormData> {
global?: string;
}

export const EmailUnsubscribe: React.FC = () => {
const history = useHistory();
const [errors, setErrors] = useState<Errors>({});
const { apiPost } = useAuthContext();
const [value, setValue] = useState<FormData>({
email: ''
});

const onTextChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
setValue({
...value,
[e.target.name]: e.target.value
});
};

const onSubmit: React.FormEventHandler<HTMLFormElement> = async (e) => {
e.preventDefault();
try {
if (!value) throw Error('Email must be provided.');
await apiPost(`/email-unsubscribe`, {
body: value
});

history.push('/unsubscribe', {
message: 'You have been successfully unsubscribed from our emails.'
});
} catch (e: any) {
setErrors({
global: e.message ?? e.toString()
});
}
};

const handleResubscribe = async () => {
try {
if (!value) throw Error('Email must be provided.');
await apiPost(`/email-resubscribe`, {
body: value
});

history.push('/unsubscribe', {
message: 'You have been successfully resubscribed to our emails.'
});
} catch (e: any) {
setErrors({
global: e.message ?? e.toString()
});
}
};

return (
<form onSubmit={onSubmit}>
<h1>Unsubscribe from our emails.</h1>
<p>Youllll no longer receive emails from us.</p>
<Label htmlFor="email">Email</Label>
<TextInput
required
id="email"
name="email"
type="text"
value={value.email}
onChange={onTextChange}
/>
<p></p>
<div className="width-full display-flex flex-justify-start">
{errors.global && <p className="text-error">{errors.global}</p>}
</div>
<Button type="submit">Unsubscribe</Button>
<p>Changed your mind?</p>
<div className="width-full display-flex flex-justify-start">
{errors.global && <p className="text-error">{errors.global}</p>}
</div>
<Button type="button" onClick={handleResubscribe}>
Resubscribe
</Button>
</form>
);
};
1 change: 1 addition & 0 deletions frontend/src/pages/EmailUnsubscribe/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './EmailUnsubscribe';
1 change: 1 addition & 0 deletions frontend/src/pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './Domains';
export * from './Domain';
export * from './Vulnerability';
export * from './TermsOfUse';
export * from './EmailUnsubscribe';
export * from './Search';
export * from './LoginGovCallback';
export { default as AdminTools } from './AdminTools';
Expand Down
Loading