Skip to content

Commit 906f30a

Browse files
committed
cron job to expire admin notes
1 parent c442cc4 commit 906f30a

File tree

12 files changed

+2040
-1240
lines changed

12 files changed

+2040
-1240
lines changed

backend/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ COPY ./prisma ./prisma
1212
# libcurl3 is required for mongodb-memory-server, which is used for testing
1313
# RUN apt-get update && apt-get install -y libcurl3
1414

15+
RUN yarn cache clean
1516
RUN yarn install
1617

1718
COPY . ./

backend/crons/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import cron from "node-cron";
2+
import expireAdminNotes from "./scripts/expireAdminNotes";
3+
4+
cron.schedule("0 0 * * * *", async () => {
5+
const res = await expireAdminNotes();
6+
if (res) {
7+
console.log("Deleted expired notes");
8+
}
9+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import prisma from "../../prisma";
2+
3+
async function expireAdminNotes(): Promise<boolean> {
4+
const limit = new Date(Date.now() - 48 * 60 * 60 * 1000);
5+
try {
6+
await prisma.note.deleteMany({
7+
where: {
8+
date: {
9+
lt: limit.toISOString(),
10+
},
11+
},
12+
});
13+
return true;
14+
} catch (err) {
15+
console.log(err);
16+
return false;
17+
}
18+
}
19+
20+
export default expireAdminNotes;

backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@types/lodash": "^4.14.168",
6060
"@types/mongoose": "^5.10.3",
6161
"@types/node": "^14.14.31",
62+
"@types/node-cron": "^3.0.11",
6263
"@types/node-fetch": "^2.5.8",
6364
"@types/nodemailer": "^6.4.1",
6465
"@types/pg": "^7.14.10",
@@ -73,6 +74,7 @@
7374
"eslint-plugin-prettier": "^3.3.1",
7475
"jest": "^27.0.4",
7576
"mongodb-memory-server": "^6.9.6",
77+
"node-cron": "^3.0.3",
7678
"nodemon": "^2.0.7",
7779
"prettier": "^2.2.1",
7880
"prisma": "5.11.0",

backend/server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import express from "express";
55
import { ApolloServer } from "apollo-server-express";
66
import schema from "./graphql";
77

8+
require("./crons/index.ts");
9+
810
const CORS_ALLOW_LIST = [
911
"http://localhost:3000",
1012
"https://uw-blueprint-starter-code.firebaseapp.com",

backend/yarn.lock

Lines changed: 1808 additions & 1234 deletions
Large diffs are not rendered by default.

frontend/public/favicon.ico

174 KB
Binary file not shown.

frontend/src/components/pages/home/NoteSection.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ const NoteSection = () => {
4343
await createNote({
4444
variables: {
4545
message: newNote,
46-
date: formatted,
46+
date: now.toISOString(),
47+
formattedDate: formatted,
4748
},
4849
});
4950
window.location.reload();
@@ -158,7 +159,7 @@ const NoteSection = () => {
158159
alignItems="flex-end"
159160
>
160161
<Flex fontSize="sm" color="gray.500">
161-
{note.date}
162+
{note.formattedDate}
162163
</Flex>
163164
<Flex
164165
onClick={() => dismissNote(note.noteId)}

frontend/src/gql/mutations.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ export const UPDATE_PARTICIPANT_BY_ID = gql`
3535
`;
3636

3737
export const CREATE_NOTE = gql`
38-
mutation createNote($message: String!, $date: String!) {
39-
createNote(message: $message, date: $date)
38+
mutation createNote(
39+
$message: String!
40+
$date: String!
41+
$formattedDate: String!
42+
) {
43+
createNote(message: $message, date: $date, formattedDate: $formattedDate)
4044
}
4145
`;
4246

frontend/src/gql/queries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const GET_NOTES = gql`
4545
getNotes {
4646
noteId
4747
message
48-
date
48+
formattedDate
4949
}
5050
}
5151
`;

0 commit comments

Comments
 (0)