Skip to content

Commit

Permalink
fix: prevent score creation for different seasons than the current
Browse files Browse the repository at this point in the history
  • Loading branch information
FreekBes committed Jan 28, 2025
1 parent 1d2d5b0 commit f1aa109
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/handlers/points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,22 @@ export const createScore = async function(prisma: PrismaClient, type: CodamCoali
console.warn(`User ${user.login} is a test account, skipping score creation...`);
return null;
}
const blocAtScoreCreation = await getBlocAtDate(prisma, scoreDate);
const currentBloc = await getBlocAtDate(prisma, new Date());
if (blocAtScoreCreation && currentBloc && blocAtScoreCreation.id !== currentBloc.id) { // Check if the score creation date is in the current bloc if a current bloc and previous bloc exist (if not, just assign the score - it won't belong to any season but it can be shifted using the admin panel)
console.warn(`Score creation date ${scoreDate} is not in the current bloc ${currentBloc.id} (${currentBloc.begin_at} - ${currentBloc.end_at}) but in bloc ${blocAtScoreCreation.id} (${blocAtScoreCreation.begin_at} - ${blocAtScoreCreation.end_at}), skipping score creation...`);
return null;
}
if (!blocAtScoreCreation) { // Check if there is a season ongoing at the score creation date
console.warn(`No bloc found for score creation date ${scoreDate}. The score will be created, but will not belong to any season. It should be shifted later to the correct season using the admin panel.`);
}
if (!user.coalition_users || user.coalition_users.length === 0) { // Check if user has a coalition
console.warn(`User ${userId} does not have a coalition, skipping score creation...`);
return null;
}
const coalitionUser = user.coalition_users[0];

console.log(`Creating score for user ${userId} in coalition ${coalitionUser.coalition_id} with ${points} points for reason "${reason}" (connected to Intra object ${typeIntraId} for fixed type ${(type ? type.type : "null")})...`);
console.log(`Creating score for user ${userId} in coalition ${coalitionUser.coalition_id} with ${points} points for reason "${reason}" (connected to Intra object ${typeIntraId} for fixed type ${(type ? type.type : "null")}), at score creation date ${scoreDate}...`);
const score = await prisma.codamCoalitionScore.create({
data: {
amount: points,
Expand Down
13 changes: 11 additions & 2 deletions src/routes/admin/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import { CoalitionScore, getCoalitionScore, getScoresPerType } from '../../utils
export const setupAdminDashboardRoutes = function(app: Express, prisma: PrismaClient): void {
app.get('/admin', async (req, res) => {
// Get current bloc deadline
const blocDeadline = await prisma.intraBlocDeadline.findFirst({
const currentBlocDeadline = await prisma.intraBlocDeadline.findFirst({
orderBy: {
end_at: 'desc',
},
});

// Get previous bloc deadlines
const blocDeadlines = await prisma.intraBlocDeadline.findMany({
orderBy: {
end_at: 'desc',
},
take: 10,
});

// Get coalitions
const coalitions = await prisma.codamCoalition.findMany({
select: {
Expand Down Expand Up @@ -41,7 +49,8 @@ export const setupAdminDashboardRoutes = function(app: Express, prisma: PrismaCl
}

return res.render('admin/dashboard.njk', {
blocDeadline,
currentBlocDeadline,
blocDeadlines,
coalitions,
coalitionScores,
coalitionScoresPerFixedType,
Expand Down
21 changes: 19 additions & 2 deletions templates/admin/dashboard.njk
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<h5 class="card-title mb-0">Season deadlines</h5>
</div>
<div class="card-body">
<p>Start: <span id="season-start" data-date="{{ blocDeadline.begin_at | timestamp }}">{{ blocDeadline.begin_at | timeAgo }}</span></p>
<p>End: <span id="season-end" data-date="{{ blocDeadline.end_at | timestamp }}">{{ blocDeadline.end_at | timeFromNow }}</span></p>
<p>Start: <span id="season-start" data-date="{{ currentBlocDeadline.begin_at | timestamp }}">{{ currentBlocDeadline.begin_at | timeAgo }}</span></p>
<p>End: <span id="season-end" data-date="{{ currentBlocDeadline.end_at | timestamp }}">{{ currentBlocDeadline.end_at | timeFromNow }}</span></p>
</div>
</div>
</div>
Expand Down Expand Up @@ -99,5 +99,22 @@
</div>
{% endfor %}
</div>

<div class="row ms-0 me-0 mb-4">
<div class="col-md-6">
<div class="card h-100">
<div class="card-header">
<h5 class="card-title mb-0">Previous 10 seasons</h5>
</div>
<div class="card-body">
<ul>
{% for blocDeadline in blocDeadlines %}
<li>{{ blocDeadline.begin_at | dateInput }} - {{ blocDeadline.end_at | dateInput }}</li>
{% endfor %}
</ul>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

0 comments on commit f1aa109

Please sign in to comment.