Skip to content

Commit c9aa63c

Browse files
Merge pull request #126 from sliit-foss/development
Spectator role addition and performance enhancements
2 parents f15a6f8 + b47fe69 commit c9aa63c

File tree

24 files changed

+135
-85
lines changed

24 files changed

+135
-85
lines changed

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import context from 'express-http-context';
33
import rateLimit from 'express-rate-limit';
44
import httpLogger from '@sliit-foss/http-logger';
55
import { moduleLogger } from '@sliit-foss/module-logger';
6-
import compression from 'compression';
7-
import cors from 'cors';
8-
import crypto from 'crypto';
9-
import helmet from 'helmet';
6+
import { default as compression } from 'compression';
7+
import { default as cors } from 'cors';
8+
import { default as crypto } from 'crypto';
9+
import { default as helmet } from 'helmet';
1010
import { omit, pick } from 'lodash';
1111
import { default as connectDB } from '@/database';
1212
import { errorHandler, queryMapper, responseInterceptor } from '@/middleware';
@@ -79,8 +79,6 @@ global.__basedir = __dirname;
7979

8080
const port = process.env.PORT || 3000;
8181

82-
app.listen(port, (err) => {
83-
if (!err) {
84-
logger.info(`Bashaway server successfully started on port ${port}`);
85-
}
82+
app.listen(port, () => {
83+
logger.info(`Bashaway server successfully started on port ${port}`);
8684
});

src/constants/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './question';
2+
export * from './user';

src/constants/question.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const DIFFICULTY = {
2+
EASY: 'EASY',
3+
MEDIUM: 'MEDIUM',
4+
HARD: 'HARD',
5+
EXTREME: 'EXTREME'
6+
};

src/constants/user.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const ROLE = {
2+
ADMIN: 'ADMIN',
3+
SPECTATOR: 'SPECTATOR',
4+
GROUP: 'GROUP'
5+
};
6+
7+
export const GENDER = {
8+
MALE: 'M',
9+
FEMALE: 'F',
10+
OTHER: 'O',
11+
NOT_SPECIFIED: '-'
12+
};
13+
14+
export const MEAL_PREFERENCE = {
15+
VEG: 'VEG',
16+
NON_VEG: 'NON_VEG'
17+
};

src/controllers/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs';
22
import handlebars from 'handlebars';
33
import { default as createError } from 'http-errors';
4-
import path from 'path';
4+
import { default as path } from 'path';
55
import { getRegistrationDeadline } from '@/repository/settings';
66
import { blacklistToken } from '@/repository/token';
77
import { getOneUser } from '@/repository/user';

src/helpers/question.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { ROLE } from '@/constants';
12
import { getDistinctSubmissions } from '@/repository/submission';
23

34
export const attachSubmissionAttributesToQuestion = async (question, user) => {
45
const submissions = await getDistinctSubmissions(question._id);
56
question.total_submissions = submissions.length;
6-
if (user.role === 'GROUP') question.submitted = submissions.some((s) => s.user.toString() === user._id.toString());
7+
if (user.role === ROLE.GROUP) question.submitted = submissions.some((s) => s.user.toString() === user._id.toString());
78
return question;
89
};

src/middleware/auth.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export const protect = asyncHandler(async (req) => {
2323
req.user = user;
2424
});
2525

26-
export const adminProtect = asyncHandler((req) => {
27-
if (req.headers['x-api-key'] === process.env.API_ACCESS_KEY) return;
28-
if (req.user.role !== 'ADMIN') throw new createError(403, 'You are not permitted to access this resource');
29-
});
26+
export const roleProtect = (roles = []) => {
27+
return asyncHandler((req) => {
28+
if (req.headers['x-api-key'] === process.env.API_ACCESS_KEY) return;
29+
if (!roles.includes(req.user?.role)) throw new createError(403, 'You are not permitted to access this resource');
30+
});
31+
};

src/models/question.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import mongoose from 'mongoose';
22
import mongoosePaginate from 'mongoose-paginate-v2';
3+
import { DIFFICULTY } from '@/constants';
34

45
const QuestionSchema = new mongoose.Schema(
56
{
@@ -14,8 +15,9 @@ const QuestionSchema = new mongoose.Schema(
1415
},
1516
difficulty: {
1617
type: String,
17-
enum: ['EASY', 'MEDIUM', 'HARD', 'EXTREME'],
18-
required: true
18+
enum: Object.values(DIFFICULTY),
19+
required: true,
20+
index: true
1921
},
2022
constraints: [
2123
{
@@ -28,12 +30,14 @@ const QuestionSchema = new mongoose.Schema(
2830
},
2931
enabled: {
3032
type: Boolean,
31-
default: true
33+
default: true,
34+
index: true
3235
},
3336
creator: {
3437
type: mongoose.Schema.Types.ObjectId,
3538
ref: 'User',
36-
required: true
39+
required: true,
40+
index: true
3741
},
3842
creator_lock: {
3943
type: Boolean,

src/models/submission.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ const SubmissionSchema = mongoose.Schema(
66
user: {
77
type: mongoose.Schema.Types.ObjectId,
88
ref: 'User',
9-
required: true
9+
required: true,
10+
index: true
1011
},
1112
question: {
1213
type: mongoose.Schema.Types.ObjectId,
1314
ref: 'Question',
14-
required: true
15+
required: true,
16+
index: true
1517
},
1618
link: {
1719
type: String,

0 commit comments

Comments
 (0)