Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change logic for Sitemap CronJob #1912

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Changes from 1 commit
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
71 changes: 68 additions & 3 deletions src/services/cronJobs/generateSitemapOnFrontend.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* This cron job is responsible for generating sitemap on frontend.
*
* It sends a request to frontend to generate sitemap.
* It sends a request to frontend to generate sitemap with all projects, users and qfRounds.
*
* It is scheduled to run every Sunday at 00:00.
*
Expand All @@ -11,11 +11,16 @@ import { schedule } from 'node-cron';
import axios from 'axios';
import config from '../../config';
import { logger } from '../../utils/logger';
import { Project, ProjStatus } from '../../entities/project';
import { User } from '../../entities/user';
import { QfRound } from '../../entities/qfRound';

// Every Sunday at 00:00
const cronJobTime =
(config.get('GENERATE_SITEMAP_CRONJOB_EXPRESSION') as string) || '0 0 * * 0';

const FRONTEND_URL = process.env.FRONTEND_URL || '';
kkatusic marked this conversation as resolved.
Show resolved Hide resolved

export const runGenerateSitemapOnFrontend = () => {
logger.debug(
'runGenerateSitemapOnFrontend() has been called, cronJobTime:',
Expand All @@ -24,9 +29,29 @@ export const runGenerateSitemapOnFrontend = () => {

schedule(cronJobTime, async () => {
logger.debug('runGenerateSitemapOnFrontend() job has started');
logger.debug('FRONTEND_URL:', process.env.FRONTEND_URL);
try {
const response = await axios.get(
`https://${process.env.FRONTEND_URL}/api/generate-sitemap`,
const projects = await fetchProjects();
const users = await fetchUsers();
const qfRounds = await fetchQFRounds();

if (!process.env.FRONTEND_URL) {
logger.error(
'FRONTEND_URL is not defined in the environment variables',
);
}
kkatusic marked this conversation as resolved.
Show resolved Hide resolved

const frontendUrl = FRONTEND_URL.startsWith('http')
? FRONTEND_URL.trim()
: `https://${FRONTEND_URL.trim()}`;

const response = await axios.post(
`${frontendUrl}/api/generate-sitemap`,
{
projects,
users,
qfRounds,
},
{
headers: {
Authorization: `Bearer ${process.env.SITEMAP_CRON_SECRET}`,
Expand All @@ -40,3 +65,43 @@ export const runGenerateSitemapOnFrontend = () => {
logger.debug('runGenerateSitemapOnFrontend() job has finished');
});
};

const fetchProjects = async () => {
try {
const projects = await Project.createQueryBuilder('project')
.select(['project.title', 'project.slug', 'project.descriptionSummary'])
.where('project.statusId= :statusId', { statusId: ProjStatus.active })
.getMany();

return projects;
} catch (error) {
logger.error('Error fetching projects:', error.message);
return [];
}
};

const fetchUsers = async () => {
try {
const users = await User.createQueryBuilder('user')
.select(['user.firstName', 'user.lastName', 'user.walletAddress'])
.getMany();
kkatusic marked this conversation as resolved.
Show resolved Hide resolved

return users;
} catch (error) {
logger.error('Error fetching users:', error.message);
return [];
}
};

const fetchQFRounds = async () => {
try {
const qfRounds = await QfRound.createQueryBuilder('qf_round')
.select(['qf_round.slug', 'qf_round.name', 'qf_round.description'])
.getMany();

return qfRounds;
} catch (error) {
logger.error('Error fetching qfRounds:', error.message);
return [];
}
};
Loading