Skip to content

Commit 8158773

Browse files
committed
Merge branch 'develop' of https://github.com/Real-Dev-Squad/website-backend into bugfix/issue957
2 parents b2c933e + 53b80cd commit 8158773

File tree

85 files changed

+5931
-328
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+5931
-328
lines changed

config/default.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ module.exports = {
8686
ttl: 60,
8787
},
8888

89+
cronJobHandler: {
90+
publicKey: "CRON_JOB_PUBLIC_KEY",
91+
},
92+
8993
integrations: {
9094
newrelic: {
9195
appName: "RDS_API_production",

config/test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,47 @@ module.exports = {
149149
ttl: 60,
150150
},
151151

152+
cronJobHandler: {
153+
privateKey:
154+
"-----BEGIN RSA PRIVATE KEY-----\n" +
155+
"MIIEowIBAAKCAQEAqjkUS3EGyuh64eITS/n5MX7G4z5MIv99DNqZezqCSRD/QIXO\n" +
156+
"1QtDrj/OKAB5a+4GAJSxIO8HgRFocdiKauxako3UagNSabiI+/H5zXjtnbPKwMBd\n" +
157+
"CoJ3r1+OzyMT4zL+SVlIMHkxYbRgMYJvNTyie/rIpjcpQhaBJxyBkaT2Imy9luGC\n" +
158+
"Rhd5wupx9+rhd8xOYu+hqSWfP4zIcEGCs86OXFFmNp6sqcQD1P15HkJtleodzcKg\n" +
159+
"msqo8RTk8t+urdWIdoLFAugwqFE9jor8UxApwT5xr8c84VDfbPgc10V2XTda4SXa\n" +
160+
"IRP7QY+9agGmZIYp0LWxUOhB37PNFDhUEw9dVwIDAQABAoIBAH9rJJ7oJz6B0WH+\n" +
161+
"WZV4s6jyDiySOGnGNzQE+fh9LoNFHtyMjOt6eBoaFtZorHs2+/U5WHGfm01o23bE\n" +
162+
"sbAh5hZn5kXI4MrUYG2/js7Yo3111OJ92+d/C9oRvJOe3Ucnp6L+GwR145oQbCUD\n" +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Fi130F3ozHV\n" +
178+
"o6x/R8X3QuyNeyZ8SI7eKJ+oo1jF97WgRWBiE66IHGziUZc4+gFAmHzdeZft0xtJ\n" +
179+
"AIvluPVA3HOHFj4US3LMxbxDsPr+gkTpkVGIfK0rk8Za3dN3mZJw\n" +
180+
"-----END RSA PRIVATE KEY-----",
181+
publicKey:
182+
"-----BEGIN PUBLIC KEY-----\n" +
183+
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqjkUS3EGyuh64eITS/n5\n" +
184+
"MX7G4z5MIv99DNqZezqCSRD/QIXO1QtDrj/OKAB5a+4GAJSxIO8HgRFocdiKauxa\n" +
185+
"ko3UagNSabiI+/H5zXjtnbPKwMBdCoJ3r1+OzyMT4zL+SVlIMHkxYbRgMYJvNTyi\n" +
186+
"e/rIpjcpQhaBJxyBkaT2Imy9luGCRhd5wupx9+rhd8xOYu+hqSWfP4zIcEGCs86O\n" +
187+
"XFFmNp6sqcQD1P15HkJtleodzcKgmsqo8RTk8t+urdWIdoLFAugwqFE9jor8UxAp\n" +
188+
"wT5xr8c84VDfbPgc10V2XTda4SXaIRP7QY+9agGmZIYp0LWxUOhB37PNFDhUEw9d\n" +
189+
"VwIDAQAB\n" +
190+
"-----END PUBLIC KEY-----",
191+
},
192+
152193
Event100ms: {
153194
APP_ACCESS_KEY: "EVENT_100MS_APP_ACCESS_KEY",
154195
APP_SECRET: "EVENT_100MS_APP_SECREt",

constants/bot.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const CLOUDFLARE_WORKER = "Cloudflare Worker";
22
const BAD_TOKEN = "BAD.JWT.TOKEN";
3+
const CRON_JOB_HANDLER = "Cron Job Handler";
34

4-
module.exports = { CLOUDFLARE_WORKER, BAD_TOKEN };
5+
module.exports = { CLOUDFLARE_WORKER, BAD_TOKEN, CRON_JOB_HANDLER };

constants/cacheKeys.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const ALL_TASKS = "cache:ALL-TASKS";
2+
3+
module.exports = { ALL_TASKS };

constants/errorMessages.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ module.exports = {
33
SOMETHING_WENT_WRONG: "Something went wrong. Please try again or contact admin",
44
ONLY_IMAGE_SUPPORTED: "Only image/jpeg, image/png supported",
55
ONLY_ONE_FILE_ALLOWED: "Only one file allowed",
6+
DATA_ADDED_SUCCESSFULLY: "User Device Info added successfully!",
7+
USER_DATA_ALREADY_PRESENT: "The authentication document has already been created",
8+
BAD_REQUEST: "BAD_REQUEST",
9+
INVALID_QUERY_PARAM: "Invalid Query Parameters Passed",
610
FILE_TOO_LARGE: (size) => `File too large, max accepted size is ${size} MB`,
711
};

constants/tasks.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ const TASK_STATUS_OLD = {
3030
OLD_COMPLETED: "completed",
3131
};
3232

33-
module.exports = { TASK_TYPE, TASK_STATUS, TASK_STATUS_OLD };
33+
const MAPPED_TASK_STATUS = {
34+
...TASK_STATUS,
35+
UNASSIGNED: "AVAILABLE",
36+
DONE: "COMPLETED",
37+
};
38+
39+
const TASK_SIZE = 5;
40+
41+
module.exports = { TASK_TYPE, TASK_STATUS, TASK_STATUS_OLD, MAPPED_TASK_STATUS, TASK_SIZE };

constants/userStatus.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ const userState = {
55
ONBOARDING: "ONBOARDING",
66
};
77

8-
module.exports = { userState };
8+
const CANCEL_OOO = "cancelOoo";
9+
10+
module.exports = { userState, CANCEL_OOO };

constants/users.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ const profileStatus = {
44
NOT_APPROVED: "NOT APPROVED",
55
};
66

7+
const USER_SENSITIVE_DATA = ["phone", "email", "chaincode", "tokens"];
8+
79
const USER_STATUS = {
810
OOO: "ooo",
911
IDLE: "idle",
1012
ACTIVE: "active",
13+
ONBOARDING: "onboarding",
1114
};
1215

1316
const ALLOWED_FILTER_PARAMS = {
@@ -16,4 +19,9 @@ const ALLOWED_FILTER_PARAMS = {
1619
ROLE: ["role"],
1720
};
1821

19-
module.exports = { profileStatus, USER_STATUS, ALLOWED_FILTER_PARAMS };
22+
module.exports = {
23+
profileStatus,
24+
USER_STATUS,
25+
ALLOWED_FILTER_PARAMS,
26+
USER_SENSITIVE_DATA,
27+
};

controllers/auth.js

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const passport = require("passport");
22
const users = require("../models/users");
3+
const QrCodeAuthModel = require("../models/qrCodeAuth");
34
const authService = require("../services/authService");
5+
const { SOMETHING_WENT_WRONG, DATA_ADDED_SUCCESSFULLY, BAD_REQUEST } = require("../constants/errorMessages");
46

57
/**
68
* Fetches the user info from GitHub and authenticates User
@@ -24,9 +26,6 @@ const githubAuth = (req, res, next) => {
2426
userData = {
2527
github_id: user.username,
2628
github_display_name: user.displayName,
27-
tokens: {
28-
githubAccessToken: accessToken,
29-
},
3029
github_user_id: user.id,
3130
};
3231

@@ -67,7 +66,81 @@ const signout = (req, res) => {
6766
});
6867
};
6968

69+
/**
70+
* Stores user-device data inside the DB for mobile auth
71+
*
72+
* @param req {Object} - Express request object
73+
* @param res {Object} - Express response object
74+
*/
75+
76+
const storeUserDeviceInfo = async (req, res) => {
77+
try {
78+
const userJson = {
79+
user_id: req.body.user_id,
80+
device_info: req.body.device_info,
81+
device_id: req.body.device_id,
82+
authorization_status: "NOT_INIT",
83+
};
84+
85+
const userInfo = await QrCodeAuthModel.storeUserDeviceInfo(userJson);
86+
87+
if (!userInfo) {
88+
return res.status(404).json({
89+
message: BAD_REQUEST,
90+
});
91+
}
92+
93+
return res.status(201).json({
94+
...userInfo,
95+
message: DATA_ADDED_SUCCESSFULLY,
96+
});
97+
} catch (err) {
98+
logger.error(`Error while storing user device info : ${err}`);
99+
return res.boom.badImplementation(SOMETHING_WENT_WRONG);
100+
}
101+
};
102+
103+
const updateAuthStatus = async (req, res) => {
104+
try {
105+
const userId = req.userData.id;
106+
const authStatus = req.params.authorization_status;
107+
const result = await QrCodeAuthModel.updateStatus(userId, authStatus);
108+
109+
if (!result.userExists) {
110+
return res.boom.notFound("Document not found!");
111+
}
112+
113+
return res.json({
114+
message: `Authentication document for user ${userId} updated successfully`,
115+
data: { ...result.data },
116+
});
117+
} catch (error) {
118+
logger.error(`Error while fetching user: ${error}`);
119+
return res.boom.badImplementation(SOMETHING_WENT_WRONG);
120+
}
121+
};
122+
123+
const fetchUserDeviceInfo = async (req, res) => {
124+
try {
125+
const deviceId = req.query.device_id;
126+
const userDeviceInfoData = await QrCodeAuthModel.retrieveUserDeviceInfo(deviceId);
127+
if (!userDeviceInfoData.userExists) {
128+
return res.boom.notFound(`User with id ${deviceId} does not exist.`);
129+
}
130+
return res.json({
131+
message: "Authentication document retrieved successfully.",
132+
data: { ...userDeviceInfoData.data },
133+
});
134+
} catch (error) {
135+
logger.error(`Error while fetching user: ${error}`);
136+
return res.boom.badImplementation(SOMETHING_WENT_WRONG);
137+
}
138+
};
139+
70140
module.exports = {
71141
githubAuth,
72142
signout,
143+
storeUserDeviceInfo,
144+
updateAuthStatus,
145+
fetchUserDeviceInfo,
73146
};

controllers/badges.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { ERROR_MESSAGES, SUCCESS_MESSAGES } = require("../constants/badges");
22
const { CONTROLLERS: CONTROLLERS_ERROR_MESSAGES } = ERROR_MESSAGES;
33
const { CONTROLLERS: CONTROLLERS_SUCCESS_MESSAGES } = SUCCESS_MESSAGES;
44
const badgeQuery = require("../models/badges");
5-
const { fetchUser } = require("../models/users");
5+
const dataAccess = require("../services/dataAccessLayer");
66
const imageService = require("../services/imageService");
77

88
/**
@@ -76,7 +76,7 @@ async function postBadge(req, res) {
7676
async function postUserBadges(req, res) {
7777
try {
7878
const { badgeIds, userId } = req.body;
79-
const { userExists } = await fetchUser({ userId });
79+
const { userExists } = await dataAccess.retrieveUsers({ id: userId });
8080
if (!userExists) {
8181
throw Error(ERROR_MESSAGES.MISC.USER_ID_DOES_NOT_EXIST);
8282
}

0 commit comments

Comments
 (0)