Skip to content

Commit 90e4d3d

Browse files
authored
Merge pull request #54 from mathematicalthinking/development
Development (current deployment) update to main
2 parents 6bfda53 + f759c2f commit 90e4d3d

File tree

17 files changed

+245
-21
lines changed

17 files changed

+245
-21
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ typings/
6666
# compiled ts files
6767
dist
6868
# ethereal login info created on fly each test run
69-
ethereal_creds.json
69+
ethereal_creds.json
70+
*.zip
71+
.DS_Store

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mt-sso",
3-
"version": "0.0.0",
3+
"version": "1.1.4",
44
"private": true,
55
"scripts": {
66
"start": "node dist/bin/main",

src/constants/version.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const version = '1.1.4';

src/controllers/localSignup.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ export const createVmtUser = async (
105105
? requestBody.accountType
106106
: VmtAccountType.facilitator;
107107

108+
if (requestBodyType === 'google' && requestBody.username) {
109+
username = requestBody.username;
110+
}
111+
108112
let vmtUserBody = {
109113
firstName,
110114
lastName,
@@ -114,12 +118,23 @@ export const createVmtUser = async (
114118
accountType,
115119
isEmailConfirmed,
116120
rooms: requestBody.rooms,
121+
courses: requestBody.courses,
117122
};
118123

124+
// temp or imported user, preexisting data
119125
let wasFromTempUser =
120-
Array.isArray(requestBody.rooms) && requestBody.rooms.length > 0;
126+
(Array.isArray(requestBody.rooms) && requestBody.rooms.length > 0) ||
127+
(Array.isArray(requestBody.courses) && requestBody.courses.length > 0);
128+
// catch an imported proxy course user and confirm email, as it may not exist
129+
if (Array.isArray(requestBody.courses) && requestBody.courses.length > 0) {
130+
vmtUserBody = {
131+
...vmtUserBody,
132+
isEmailConfirmed: true,
133+
};
134+
}
121135

122136
let vmtUser;
137+
// For any user with prior data, find and update that VMT user with mt login data
123138
if (wasFromTempUser) {
124139
vmtUser = await VmtUser.findByIdAndUpdate(requestBody._id, vmtUserBody, {
125140
new: true,

src/controllers/oauth/google.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,57 @@ export const googleCallback = async (
126126

127127
let isNewUser = isNil(mtUser.encUserId);
128128

129+
if (!isNewUser) {
130+
// catch if pending user data was not added to existing VMT user
131+
// Matches for VMT user with same email and 'pending' accountType
132+
// env toggle to enable feature
133+
if (
134+
process.env.VMT_YES_TO_CONVT_PENDING &&
135+
process.env.VMT_YES_TO_CONVT_PENDING.toLowerCase() === 'yes'
136+
) {
137+
// find VMT user with data
138+
const vmtPendingData = await VmtUser.findOne({
139+
email: mtUser.email,
140+
accountType: 'pending',
141+
});
142+
// find duplicate VMT user to remove
143+
const vmtMTdata = await VmtUser.findOne({
144+
email: mtUser.email,
145+
ssoId: mtUser._id,
146+
});
147+
if (
148+
vmtPendingData &&
149+
vmtPendingData.accountType === 'pending' &&
150+
vmtMTdata
151+
) {
152+
// Swap linked Ids to link sso account with VMT account with data
153+
await VmtUser.findByIdAndUpdate(vmtPendingData._id, {
154+
ssoId: mtUser._id,
155+
accountType: 'facilitator',
156+
isEmailConfirmed: true,
157+
});
158+
mtUser.vmtUserId = vmtPendingData._id;
159+
await mtUser.save();
160+
161+
// invalidate or delete duplicate record
162+
await VmtUser.findByIdAndUpdate(vmtMTdata._id, {
163+
isTrashed: true,
164+
accountType: 'temp',
165+
email: '',
166+
});
167+
// await VmtUser.findByIdAndUpdate(vmtMTdata._id, { ssoId: '' });
168+
// await VmtUser.findByIdAndDelete(vmtMTdata._id);
169+
}
170+
}
171+
}
172+
129173
if (isNewUser) {
174+
// check to see if the email is pre-loaded to VMT with user-data
175+
const vmtUserData = await VmtUser.findOne({ email: mtUser.email });
176+
130177
[encUser, vmtUser] = await Promise.all([
131178
createEncUser(mtUser, {}, 'google'),
132-
createVmtUser(mtUser, {}, 'google'),
179+
createVmtUser(mtUser, vmtUserData || {}, 'google'),
133180
]);
134181

135182
if (encUser === null || vmtUser === null) {

src/db_migration/addEncUsers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ export const createVmtCounterparts = async function(): Promise<void> {
250250
encDb.close();
251251
} catch (err) {
252252
console.log('ERRR', err);
253-
throw new Error(err.message);
253+
const { message } = err as Error;
254+
throw new Error(message);
254255
}
255256
};

src/db_migration/addVmtUsers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ export const createEncCounterparts = async function(): Promise<void> {
206206
encDb.close();
207207
} catch (err) {
208208
console.log('ERRR', err);
209-
throw new Error(err.message);
209+
const { message } = err as Error;
210+
throw new Error(message);
210211
}
211212
};
212213

src/db_migration/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ export const connect = function connectToDb(
55
): Promise<mongoose.Connection> {
66
return new Promise(
77
(resolve, reject): void => {
8-
const db = mongoose.createConnection(url, { useNewUrlParser: true });
8+
const mongoOptions = {
9+
ssl: true,
10+
sslValidate: true,
11+
user: process.env.MT_DB_USER,
12+
pass: process.env.MT_DB_PASS,
13+
useNewUrlParser: true,
14+
};
15+
const db = mongoose.createConnection(url, mongoOptions);
916

1017
db.on(
1118
'error',

src/dbs/mt.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,44 @@
11
import mongoose from 'mongoose';
2+
import { version } from '../constants/version';
3+
const fs = require('fs');
24

35
export default (): void => {
46
let uri = process.env.MT_DB_URI;
57
if (typeof uri !== 'string') {
68
return;
79
}
8-
mongoose.connect(uri, { useNewUrlParser: true });
10+
let mongoOptions = {};
11+
if (process.env.NODE_ENV === 'production') {
12+
uri = process.env.MT_PROD_URI;
13+
mongoOptions = {
14+
ssl: true,
15+
sslValidate: true,
16+
user: process.env.MT_PROD_DB_USER,
17+
pass: process.env.MT_PROD_DB_PASS,
18+
sslKey: fs.readFileSync(process.env.MT_PROD_DB_SSL_KEY_DIR),
19+
sslCert: fs.readFileSync(process.env.MT_PROD_DB_SSL_CERT_DIR),
20+
authSource: process.env.MT_PROD_DB_AUTHDB,
21+
useNewUrlParser: true,
22+
};
23+
} else if (process.env.NODE_ENV === 'staging') {
24+
uri = process.env.MT_STAGE_URI;
25+
mongoOptions = {
26+
ssl: true,
27+
sslValidate: true,
28+
user: process.env.MT_STAGE_DB_USER,
29+
pass: process.env.MT_STAGE_DB_PASS,
30+
sslKey: fs.readFileSync(process.env.MT_STAGE_DB_SSL_KEY_DIR),
31+
sslCert: fs.readFileSync(process.env.MT_STAGE_DB_SSL_CERT_DIR),
32+
authSource: process.env.MT_STAGE_DB_AUTHDB,
33+
useNewUrlParser: true,
34+
};
35+
} else {
36+
mongoOptions = {
37+
useNewUrlParser: true,
38+
};
39+
}
40+
41+
mongoose.connect(uri, mongoOptions);
942

1043
const db = mongoose.connection;
1144

@@ -20,6 +53,7 @@ export default (): void => {
2053
'open',
2154
(): void => {
2255
console.log(`Successfully connected to ${uri}`);
56+
console.log(`MT-SSO version v.${version}`);
2357
},
2458
);
2559
};

0 commit comments

Comments
 (0)