Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit 6f4cd4f

Browse files
✨ Delete emails
1 parent 077b3f0 commit 6f4cd4f

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"scripts": {
99
"build": "touch .env && mkdir -p dist && cp .env dist/.env && tsc",
1010
"start": "concurrently 'yarn watch-build' 'yarn watch-prettier' 'yarn watch-server'",
11+
"prettier-all": "prettier '{src,examples}/**/*.{ts,json}' --write",
1112
"watch-build": "onchange '{src,examples}/**/*.ts' -- yarn build",
1213
"watch-prettier": "onchange '{src,examples}/**/*.{ts,json}' -- prettier --write {{changed}}",
1314
"watch-server": "cd dist && nodemon index.js"

src/crud/email.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ export const createEmail = async (email: Email, sendVerification = true) => {
1515
email.isVerified = false;
1616
email.createdAt = new Date();
1717
email.updatedAt = email.createdAt;
18-
const result = <InsertResult> await query(
19-
`INSERT INTO emails ${tableValues(email)}`,
20-
Object.values(email)
18+
const result = <InsertResult>(
19+
await query(
20+
`INSERT INTO emails ${tableValues(email)}`,
21+
Object.values(email)
22+
)
2123
);
2224
if (sendVerification) {
23-
await sendEmailVerification(result.insertId, email.email, await getUser(email.userId));
25+
await sendEmailVerification(
26+
result.insertId,
27+
email.email,
28+
await getUser(email.userId)
29+
);
2430
}
2531
return result;
2632
};

src/rest/auth.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ import { mail } from "../helpers/mail";
1111
import { verifyToken, loginToken, passwordResetToken } from "../helpers/jwt";
1212
import { KeyValue, Locals } from "../interfaces/general";
1313
import { createEvent } from "../crud/event";
14-
import { EventType, ErrorCode, MembershipRole, Templates } from "../interfaces/enum";
14+
import {
15+
EventType,
16+
ErrorCode,
17+
MembershipRole,
18+
Templates
19+
} from "../interfaces/enum";
1520
import { compare, hash } from "bcrypt";
1621
import { deleteSensitiveInfoUser } from "../helpers/utils";
1722
import { createMembership } from "../crud/membership";

src/rest/user.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
getUserOrganizationId,
1010
getUserMembershipObject
1111
} from "../crud/membership";
12-
import { createEmail } from "../crud/email";
12+
import { createEmail, deleteEmail, getEmail } from "../crud/email";
1313
import { Locals } from "../interfaces/general";
1414
import { createEvent } from "../crud/event";
1515

@@ -40,6 +40,25 @@ export const addEmailToUser = async (
4040
) => {
4141
// Add email validation
4242
await createEmail({ email, userId });
43-
await createEvent({ type: EventType.EMAIL_CREATED, data: { email } }, locals);
43+
await createEvent(
44+
{ userId, type: EventType.EMAIL_CREATED, data: { email } },
45+
locals
46+
);
47+
return;
48+
};
49+
50+
export const deleteEmailFromUser = async (
51+
emailId: number,
52+
userId: number,
53+
locals: Locals
54+
) => {
55+
const email = await getEmail(emailId);
56+
if (email.userId != userId)
57+
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
58+
await deleteEmail(emailId);
59+
await createEvent(
60+
{ userId, type: EventType.EMAIL_DELETED, data: { email: email.email } },
61+
locals
62+
);
4463
return;
4564
};

src/routes/emails.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Request, Response } from "express";
22
import { verifyEmail } from "../rest/auth";
33
import { ErrorCode } from "../interfaces/enum";
4-
import { addEmailToUser } from "../rest/user";
4+
import { addEmailToUser, deleteEmailFromUser } from "../rest/user";
55

66
export const routeEmailAdd = async (req: Request, res: Response) => {
77
const email = req.body.email;
@@ -10,6 +10,13 @@ export const routeEmailAdd = async (req: Request, res: Response) => {
1010
res.json({ success: true });
1111
};
1212

13+
export const routeEmailDelete = async (req: Request, res: Response) => {
14+
const emailId = req.params.id;
15+
if (!emailId) throw new Error(ErrorCode.MISSING_FIELD);
16+
await deleteEmailFromUser(emailId, res.locals.token.id, res.locals);
17+
res.json({ success: true });
18+
};
19+
1320
export const routeEmailVerify = async (req: Request, res: Response) => {
1421
await verifyEmail(req.body.token || req.params.token, res.locals);
1522
res.json({ success: true });

src/routes/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Application } from "express";
22
import asyncHandler from "express-async-handler";
33
import { routeUserMe, routeUserPut, routeUserId } from "./users";
4-
import { routeEmailVerify, routeEmailAdd } from "./emails";
4+
import { routeEmailVerify, routeEmailAdd, routeEmailDelete } from "./emails";
55
import { routeOrganizationCreate } from "./organizations";
66
import { authHandler } from "../helpers/middleware";
77
import {
@@ -43,6 +43,7 @@ const routesUser = (app: Application) => {
4343

4444
const routesEmail = (app: Application) => {
4545
app.put("/emails", authHandler, asyncHandler(routeEmailAdd));
46+
app.delete("/emails/:id", authHandler, asyncHandler(routeEmailDelete));
4647
app.post("/emails/verify", asyncHandler(routeEmailVerify));
4748
};
4849

0 commit comments

Comments
 (0)