Skip to content
Merged
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
16 changes: 16 additions & 0 deletions .github/workflows/maintainance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Daily Maintenance

on:
schedule:
- cron: "0 8 * * *"

jobs:
maintenance:
runs-on: ubuntu-latest

steps:
- name: Call maintenance endpoint
run: |
curl -X POST \
https://subly-backend-iuej.onrender.com/api/jobs/maintenance \
-H "x-cron-secret: ${{ secrets.CRON_SECRET }}"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
2 changes: 2 additions & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { currencyRouters } from "./src/routes/rate.route.js";
import { historyRouter } from "./src/routes/history.route.js";
import cors from "cors";
import { notificationRouter } from "./src/routes/notification.route.js";
import { jobsMaintainanceRouter } from "./src/routes/maintainance.route.js";



Expand Down Expand Up @@ -67,6 +68,7 @@ app.use(cookieParser());

app.use("/api/auth", authLimiter, authRouter);
app.use("/api/me", requireAuth, meRouter);
app.use("/api/jobs", jobsMaintainanceRouter);
app.use("/api/rate", currencyRouters);
app.use("/api/refresh", apiLimiter, refreshRouter);
app.use("/api/subscription", requireAuth, subscriptionRouter);
Expand Down
1 change: 1 addition & 0 deletions server/src/config/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export const env = {
CLIENT_URL: process.env.CLIENT_URL,
CORS_ORIGINS: process.env.CORS_ORIGINS,
RESEND_API_KEY: process.env.RESEND_API_KEY,
CRON_SECRET: process.env.CRON_SECRET,
};
21 changes: 21 additions & 0 deletions server/src/controllers/maintainance.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { sendExpired } from "../jobs/sendExpired.js";
import { sendReminder } from "../jobs/sendReminder.js";
import { syncRate } from "../jobs/syncRate.js";


export async function jobsMaintainance(req, res, next) {
try {
const results = await Promise.allSettled([
syncRate(),
sendReminder(),
sendExpired()
]);

return res.json({
message: "Maintainance completed",
results
})
} catch (err) {
next(err);
}
}
28 changes: 13 additions & 15 deletions server/src/jobs/sendExpired.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { prisma } from "../libs/prisma.js";

async function sendExpired() {
const now = new Date()
export async function sendExpired() {
const now = new Date();

const result = await prisma.subscription.updateMany({
where: {
status: 'ACTIVE',
nextBillingDate: {
lt: now
}
},
data: {
status:'EXPIRED'
}
})


const result = await prisma.subscription.updateMany({
where: {
status: "ACTIVE",
nextBillingDate: {
lt: now,
},
},
data: {
status: "EXPIRED",
},
});
}

sendExpired().catch(console.error).finally(async () => {
Expand Down
15 changes: 4 additions & 11 deletions server/src/jobs/sendReminder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { prisma } from "../libs/prisma.js";
import { sendReminderEmail } from "../services/email.service.js";

async function sendReminder() {
export async function sendReminder() {
const users = await prisma.user.findMany({
where: {
emailNofiticationEnabled: true,
Expand All @@ -23,8 +23,6 @@ async function sendReminder() {
const dueSubscription = user.subscriptions.filter(
(sub) => !sub.reminderDaysBefore && sub.nextBillingDate <= reminderDate,
);



if (!dueSubscription.length) continue;

Expand All @@ -34,16 +32,11 @@ async function sendReminder() {

await prisma.notification.create({
data: {
userId: user.id,
title: "Subscriptions about to be renewed",
message: `${dueSubscription.length} of your subscription is about to expire`,
userId: user.id,
title: "Subscriptions about to be renewed",
message: `${dueSubscription.length} of your subscription is about to expire`,
},
});
}
}

sendReminder()
.catch(console.error)
.finally(async () => {
await prisma.$disconnect()
})
35 changes: 16 additions & 19 deletions server/src/jobs/syncRate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,22 @@ import { prisma } from "../libs/prisma.js";

import axios from 'axios';

async function syncRate() {
const response = await axios.get("https://api.exchangerate-api.com/v4/latest/EUR");
export async function syncRate() {
const response = await axios.get(
"https://api.exchangerate-api.com/v4/latest/EUR",
);

await prisma.rates.upsert({

where: {
baseCurrency: "EUR",
},
update: {
rates: response.data.rates,
},
create: {
baseCurrency: "EUR",
rates: response.data.rates,
},

})
await prisma.rates.upsert({
where: {
baseCurrency: "EUR",
},
update: {
rates: response.data.rates,
},
create: {
baseCurrency: "EUR",
rates: response.data.rates,
},
});
}

syncRate().catch(console.error).finally(async () => {
await prisma.$disconnect()
})
18 changes: 18 additions & 0 deletions server/src/middlewares/verifyCronSecret.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { env } from "../config/env";
export function verifyCronSecret(req, res, next) {
const secret = req.headers["x-cron-secret"];

if (!secret) {
return res.status(401).json({
message: "Missing cron secret",
});
}

if (secret !== env.CRON_SECRET) {
return res.status(401).json({
message: "Invalid cron secret",
});
}

next();
}
8 changes: 8 additions & 0 deletions server/src/routes/maintainance.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import express from 'express'
import { jobsMaintainance } from '../controllers/maintainance.controller.js';


export const jobsMaintainanceRouter = express.Router();

jobsMaintainanceRouter.post('/maintainance', jobsMaintainance)