Skip to content

Commit e10f69d

Browse files
committed
feat: birthday service
1 parent c559cf4 commit e10f69d

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

backend/src/app.ts

+3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import { currencySetter } from './middlewares/currencySetter.middleware';
88
import mongoose from 'mongoose';
99
import fileupload from 'express-fileupload';
1010
import StripeService from './services/stripe.service';
11+
import BirthdayService from './services/birthday.service';
1112

1213
const app = express();
1314
const mongoConnectionString = process.env.MONGO_URI || '';
15+
1416
StripeService.getInstance();
17+
BirthdayService.getInstance();
1518

1619
app.use(cors()); // Enable CORS
1720
app.use(fileupload({ useTempFiles: true })); // Enable file upload

backend/src/services/birthday.service.ts

+37-9
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,43 @@ import { PromoCode } from '../database/models/promoCode.model';
22
import { User } from '../database/models/user.model';
33
import { PromoCodeType } from '../types/PromoCode.types';
44
import emailService from './email/email.service';
5-
import cron from 'node-cron';
65

7-
class BirthdayService {
6+
export default class BirthdayService {
7+
private static instance: BirthdayService;
8+
89
constructor() {
9-
// Schedule the task to run every 2 minutes
10-
cron.schedule('*/2 * * * *', this.sendBirthdayPromoCode.bind(this));
10+
this.init();
11+
}
12+
13+
private async init(): Promise<void> {
14+
await this.sendBirthdayPromoCode();
15+
this.scheduleBirthdayCheck();
16+
}
17+
18+
public static getInstance(): BirthdayService {
19+
if (!BirthdayService.instance) {
20+
BirthdayService.instance = new BirthdayService();
21+
}
22+
return BirthdayService.instance;
1123
}
1224

1325
async sendBirthdayPromoCode() {
1426
try {
1527
const today = new Date();
1628
const todayMonthDay = `${today.getMonth() + 1}-${today.getDate()}`;
1729

18-
// Find users with birthdays today
19-
const users = await User.find().where({
20-
birthday: { $regex: `-${todayMonthDay}$` },
30+
// Find all users
31+
const users = await User.find();
32+
33+
// Filter users with birthdays today
34+
const birthdayUsers = users.filter((user) => {
35+
if (!user.dob) return false;
36+
const dob = new Date(user.dob);
37+
const dobMonthDay = `${dob.getMonth() + 1}-${dob.getDate()}`;
38+
return dobMonthDay === todayMonthDay;
2139
});
2240

23-
for (const user of users) {
41+
for (const user of birthdayUsers) {
2442
if (!user.email || !user.name) {
2543
throw new Error('User email not found');
2644
}
@@ -29,7 +47,6 @@ class BirthdayService {
2947
// Check if the promo code already exists
3048
const existingPromoCode = await PromoCode.findOne({
3149
code: promoCodeString,
32-
created_by: user._id,
3350
});
3451

3552
if (!existingPromoCode) {
@@ -52,6 +69,17 @@ class BirthdayService {
5269
console.log('Error sending birthday promo code:', error);
5370
}
5471
}
72+
73+
private scheduleBirthdayCheck(): void {
74+
setTimeout(async () => {
75+
try {
76+
await this.sendBirthdayPromoCode();
77+
} catch (error: any) {
78+
console.log('Error refreshing access token:', error.message);
79+
}
80+
this.scheduleBirthdayCheck(); // Schedule the next refresh
81+
}, 150000); // 2.5 minutes
82+
}
5583
}
5684

5785
export const birthdayService = new BirthdayService();

0 commit comments

Comments
 (0)