Skip to content

Commit

Permalink
Merge pull request #166 from prona-p4-learning-platform/mongodb-bump
Browse files Browse the repository at this point in the history
Mongodb bump
  • Loading branch information
srieger1 authored Jan 30, 2024
2 parents ae878a4 + 5e0b8c2 commit a7647c6
Show file tree
Hide file tree
Showing 5 changed files with 8,381 additions and 6,407 deletions.
20 changes: 10 additions & 10 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,43 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@shelf/jest-mongodb": "^1.3.4",
"@types/bcrypt": "^5.0.0",
"@shelf/jest-mongodb": "^4.2.0",
"@types/bcrypt": "^5.0.2",
"@types/bson": "^4.0.5",
"@types/connect-history-api-fallback": "^1.3.3",
"@types/cors": "^2.8.13",
"@types/dockerode": "^3.3.15",
"@types/express": "^4.17.17",
"@types/jest": "^29.4.0",
"@types/jest": "^29.5.11",
"@types/jsonwebtoken": "^9.0.2",
"@types/mongodb": "^3.6.20",
"@types/mongodb": "^4.0.7",
"@types/netmask": "^2.0.2",
"@types/node": "^18.14.5",
"@types/ssh2": "^0.5.52",
"@types/supertest": "^2.0.10",
"@types/supertest": "^6.0.2",
"@types/ws": "^8.5.4",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"eslint": "^8.35.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-prettier": "^4.2.1",
"express": "^4.18.2",
"jest": "^29.4.3",
"jest": "^29.7.0",
"prettier": "^2.8.4",
"supertest": "^6.3.3",
"ts-jest": "^29.0.5",
"supertest": "^6.3.4",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
},
"dependencies": {
"axios": "^1.6.0",
"bcrypt": "^5.1.0",
"bcrypt": "^5.1.1",
"celebrate": "^15.0.1",
"connect-history-api-fallback": "^2.0.0",
"cors": "^2.8.5",
"dockerode": "^3.3.4",
"js-base64": "^3.7.6",
"jsonwebtoken": "^9.0.0",
"mongodb": "^3.7.3",
"mongodb": "^6.3.0",
"netmask": "^2.0.2",
"node-pty": "^0.10.1",
"npm": "^10.2.1",
Expand Down
39 changes: 27 additions & 12 deletions backend/src/database/MongoDBPersister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,24 @@ import {

const saltRounds = 10;

interface EnvironmentEntry {
environment: string;
description: string;
instance: string;
}

interface UserEntry {
_id?: string;
username: string;
password?: string;
passwordHash?: string;
groupNumber: number;
assignmentListFilter?: string;
environments: EnvironmentEntry[];
}

interface SubmissionEntry {
_id?: string;
username: string;
groupNumber: number;
environment: string;
Expand All @@ -29,9 +46,7 @@ export default class MongoDBPersister implements Persister {

private async getClient(): Promise<MongoClient> {
if (!this.connectPromise) {
this.connectPromise = MongoClient.connect(this.connectURL, {
useUnifiedTopology: true,
});
this.connectPromise = MongoClient.connect(this.connectURL);
}
if (!this.mongoClient) {
const client = await this.connectPromise;
Expand All @@ -42,7 +57,7 @@ export default class MongoDBPersister implements Persister {

async GetUserAccount(username: string): Promise<UserAccount> {
const client = await this.getClient();
return client.db().collection("users").findOne({ username });
return client.db().collection<UserEntry>("users").findOne({ username });
}

async ChangeUserPassword(
Expand All @@ -53,7 +68,7 @@ export default class MongoDBPersister implements Persister {
const client = await this.getClient();
return client
.db()
.collection("users")
.collection<UserEntry>("users")
.findOneAndUpdate(
{ username },
{ $set: { passwordHash }, $unset: { password: "" } }
Expand All @@ -65,7 +80,7 @@ export default class MongoDBPersister implements Persister {
const client = await this.getClient();
return client
.db()
.collection("users")
.collection<UserEntry>("users")
.findOne({ username }, { projection: { environments: 1 } })
.then((result) =>
result && result.environments ? result.environments : []
Expand All @@ -81,7 +96,7 @@ export default class MongoDBPersister implements Persister {
const client = await this.getClient();
return client
.db()
.collection("users")
.collection<UserEntry>("users")
.findOneAndUpdate(
{ username, "environments.environment": { $ne: environment } },
{ $push: { environments: { environment, description, instance } } },
Expand All @@ -99,7 +114,7 @@ export default class MongoDBPersister implements Persister {
const client = await this.getClient();
return client
.db()
.collection("users")
.collection<UserEntry>("users")
.findOneAndUpdate(
{ username, "environments.environment": { $eq: environment } },
{ $pull: { environments: { environment } } },
Expand Down Expand Up @@ -137,7 +152,7 @@ export default class MongoDBPersister implements Persister {
// delete all previous submissions of this environment for the current user
client
.db()
.collection("submissions")
.collection<SubmissionEntry>("submissions")
.deleteMany({
username: username,
environment: environment,
Expand All @@ -152,7 +167,7 @@ export default class MongoDBPersister implements Persister {
// delete all previous submissions of this environment for the current group
client
.db()
.collection("submissions")
.collection<SubmissionEntry>("submissions")
.deleteMany({
groupNumber: groupNumber,
environment: environment,
Expand All @@ -167,7 +182,7 @@ export default class MongoDBPersister implements Persister {

return client
.db()
.collection("submissions")
.collection<SubmissionEntry>("submissions")
.insertOne({
username: username,
groupNumber: groupNumber,
Expand Down Expand Up @@ -197,7 +212,7 @@ export default class MongoDBPersister implements Persister {
// retrieve all previous submissions the current user or group
client
.db()
.collection("submissions")
.collection<SubmissionEntry>("submissions")
.find({
$or: [{ username: username }, { groupNumber: groupNumber }],
})
Expand Down
5 changes: 1 addition & 4 deletions backend/test/integration/MongoDBPersister.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ let instance: MongoDBPersister = null;
let connection: MongoClient = null;

beforeAll(async () => {
connection = await MongoClient.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
connection = await MongoClient.connect(process.env.MONGO_URL);
instance = new MongoDBPersister(process.env.MONGO_URL);
try {
await connection.db().dropCollection("users");
Expand Down
5 changes: 1 addition & 4 deletions backend/test/integration/User.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ let connection: MongoClient = null;
let instance: MongoDBPersister = null;

beforeAll(async () => {
connection = await MongoClient.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
connection = await MongoClient.connect(process.env.MONGO_URL);
try {
await connection.db().dropCollection("users");
} catch (err) {}
Expand Down
Loading

0 comments on commit a7647c6

Please sign in to comment.