-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from kentibs/feat/newletter
feat: add newsletter subscription endpoint
- Loading branch information
Showing
5 changed files
with
69 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-- CreateTable | ||
CREATE TABLE "_news_letter" ( | ||
"newsLetterId" TEXT NOT NULL, | ||
"email" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3), | ||
|
||
CONSTRAINT "_news_letter_pkey" PRIMARY KEY ("newsLetterId") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "_news_letter_email_key" ON "_news_letter"("email"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_news_letter_newsLetterId_idx" ON "_news_letter"("newsLetterId"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_news_letter_email_idx" ON "_news_letter"("email"); |
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,30 @@ | ||
import { Request, Response, NextFunction } from "express"; | ||
import { AppError } from "../utils/error"; | ||
import { asyncHandler } from "../utils/asyncHandler"; | ||
import { PrismaClient } from "@prisma/client"; | ||
|
||
const prisma = new PrismaClient(); | ||
const NewsLetter = prisma.newsLetter; | ||
|
||
export const subscribeToNewsLetter = asyncHandler( | ||
async (req: Request, res: Response, next: NextFunction) => { | ||
const email = req.body.email as string; | ||
|
||
if (!email) return next(new AppError("Please provide your email", 400)); | ||
|
||
const savedEmail = await NewsLetter.findFirst({ | ||
where: { email: { equals: email } }, | ||
}); | ||
|
||
if (savedEmail) { | ||
return next(new AppError("Provided email is already subscribed", 400)); | ||
} | ||
|
||
await NewsLetter.create({ data: { email: email } }); | ||
|
||
res.status(201).json({ | ||
status: "success", | ||
message: "Subscribed to SEG-MUK newsletter successfully", | ||
}); | ||
} | ||
); |
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,8 @@ | ||
import express from "express"; | ||
import { subscribeToNewsLetter } from "../../controllers/newsLetterController"; | ||
|
||
const router = express.Router(); | ||
|
||
router.post("/subscribe", subscribeToNewsLetter); | ||
|
||
export { router as newsLetterRoutes }; |