Skip to content

Commit df8dab1

Browse files
committed
fix: make sure the cursus_user allows coalitions
1 parent c2bd790 commit df8dab1

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/routes/quiz.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ExpressIntraUser } from '../sync/oauth';
77
import { getAPIClient } from '../utils';
88
import { fetchSingle42ApiPage } from '../sync/base';
99
import { syncCoalitionUser } from '../sync/coalitions_users';
10+
import { CURSUS_ID } from '../env';
1011

1112
export interface QuizSessionQuestion {
1213
question: CodamCoalitionTestQuestion;
@@ -399,6 +400,25 @@ export const setupQuizRoutes = function(app: Express, prisma: PrismaClient): voi
399400
}
400401
}
401402
else {
403+
// Make sure the 42cursus allows for a coalition
404+
const cursus_users = await fetchSingle42ApiPage(api, `/cursus_users`, {
405+
'filter[user_id]': user.id.toString(),
406+
'filter[cursus_id]': CURSUS_ID.toString(),
407+
});
408+
if (cursus_users.length === 0) {
409+
console.error(`User ${user.login} is not enrolled in the 42cursus with ID ${CURSUS_ID}`);
410+
return res.status(412).send({ error: 'Failed to join coalition, try again later' });
411+
}
412+
if (cursus_users[0].has_coalition === false) {
413+
console.log(`Patching user ${user.login}'s cursus_user to allow for a coalition in the 42cursus...`);
414+
const response = await api.patch(`/cursus_users/${cursus_users[0].id}`, {
415+
cursus_user: {
416+
has_coalition: true,
417+
}
418+
});
419+
console.log(`${user.login}'s cursus_user patch response: ${response.status} ${response.statusText}`);
420+
}
421+
402422
console.log(`Creating a new IntraCoalitionUser for user ${user.login} in coalition ${coalitionId}`);
403423
const response = await api.post('/coalitions_users', {
404424
coalitions_user: {

0 commit comments

Comments
 (0)