-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42ebafe
commit 6507f1c
Showing
13 changed files
with
378 additions
and
121 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
prisma/migrations/20241205125506_create_reminder/migration.sql
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,20 @@ | ||
-- CreateTable | ||
CREATE TABLE "Reminder" ( | ||
"id" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"remindAt" TIMESTAMP(3) NOT NULL, | ||
"createdById" TEXT NOT NULL, | ||
"postId" TEXT, | ||
"messageId" TEXT, | ||
|
||
CONSTRAINT "Reminder_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Reminder" ADD CONSTRAINT "Reminder_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Reminder" ADD CONSTRAINT "Reminder_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Reminder" ADD CONSTRAINT "Reminder_messageId_fkey" FOREIGN KEY ("messageId") REFERENCES "Message"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
5 changes: 5 additions & 0 deletions
5
prisma/migrations/20241205150736_update_reminder/migration.sql
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,5 @@ | ||
-- AlterTable | ||
ALTER TABLE "Reminder" ADD COLUMN "channelId" TEXT; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Reminder" ADD CONSTRAINT "Reminder_channelId_fkey" FOREIGN KEY ("channelId") REFERENCES "Channel"("id") ON DELETE SET NULL ON UPDATE CASCADE; |
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,12 @@ | ||
import { USER_REMINDER_ADD, USER_REMINDER_REMOVE } from '../common/ClientEventNames'; | ||
import { getIO } from '../socket/socket'; | ||
import { PartialReminder } from '../services/Reminder'; | ||
|
||
export const emitReminderAdd = (userId: string, reminder: PartialReminder) => { | ||
const io = getIO(); | ||
io.in(userId).emit(USER_REMINDER_ADD, reminder); | ||
}; | ||
export const emitReminderRemove = (userId: string, reminderId: string) => { | ||
const io = getIO(); | ||
io.in(userId).emit(USER_REMINDER_REMOVE, { id: reminderId }); | ||
}; |
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,10 @@ | ||
import { Router } from 'express'; | ||
import { remindersAddRoute } from './remindersAddRoute'; | ||
import { remindersDeleteRoute } from './remindersDeleteRoute'; | ||
|
||
const RemindersRouter = Router(); | ||
|
||
remindersAddRoute(RemindersRouter); | ||
remindersDeleteRoute(RemindersRouter); | ||
|
||
export { RemindersRouter }; |
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,52 @@ | ||
import { Request, Response, Router } from 'express'; | ||
|
||
import { authenticate } from '../../middleware/authenticate'; | ||
import { rateLimit } from '../../middleware/rateLimit'; | ||
import { body } from 'express-validator'; | ||
import { customExpressValidatorResult } from '../../common/errorHandler'; | ||
import { addReminder } from '../../services/Reminder'; | ||
|
||
export function remindersAddRoute(Router: Router) { | ||
Router.post( | ||
'/reminders', | ||
authenticate(), | ||
body('timestamp').not().isEmpty().withMessage('timestamp is required.').isNumeric().isLength({ min: 1, max: 255 }).withMessage('Invalid timestamp.'), | ||
|
||
body('postId').optional(true).isString().withMessage('postId must be a string').isLength({ min: 1, max: 255 }).withMessage('postId must be between 1 and 255 characters long'), | ||
body('messageId').optional(true).isString().withMessage('messageId must be a string').isLength({ min: 1, max: 255 }).withMessage('messageId must be between 1 and 255 characters long'), | ||
|
||
rateLimit({ | ||
name: 'reminders_add', | ||
restrictMS: 60000, | ||
requests: 5, | ||
}), | ||
route | ||
); | ||
} | ||
|
||
interface Body { | ||
timestamp: number; | ||
postId?: string; | ||
messageId?: string; | ||
} | ||
async function route(req: Request, res: Response) { | ||
const body = req.body as Body; | ||
|
||
const validateError = customExpressValidatorResult(req); | ||
|
||
if (validateError) { | ||
return res.status(400).json(validateError); | ||
} | ||
|
||
if (!body.postId && !body.messageId) { | ||
return res.status(400).json('postId or messageId is required.'); | ||
} | ||
|
||
const [reminder, error] = await addReminder({ userId: req.userCache.id, timestamp: body.timestamp, messageId: body.messageId, postId: body.postId }); | ||
|
||
if (error) { | ||
return res.status(400).json(error); | ||
} | ||
|
||
return res.status(200).json(reminder); | ||
} |
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,31 @@ | ||
import { Request, Response, Router } from 'express'; | ||
|
||
import { authenticate } from '../../middleware/authenticate'; | ||
import { rateLimit } from '../../middleware/rateLimit'; | ||
import { deleteReminder } from '../../services/Reminder'; | ||
|
||
export function remindersDeleteRoute(Router: Router) { | ||
Router.delete( | ||
'/reminders/:id', | ||
authenticate(), | ||
|
||
rateLimit({ | ||
name: 'reminders_delete', | ||
restrictMS: 60000, | ||
requests: 20, | ||
}), | ||
route | ||
); | ||
} | ||
|
||
async function route(req: Request, res: Response) { | ||
const id = req.params.id as string; | ||
|
||
const [reminder, error] = await deleteReminder(id, req.userCache.id); | ||
|
||
if (error) { | ||
return res.status(400).json(error); | ||
} | ||
|
||
return res.status(200).json(reminder); | ||
} |
Oops, something went wrong.