Skip to content

Commit

Permalink
chore: move away from esm package and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
harbassan committed Oct 1, 2024
1 parent 7ec1f6f commit 13218e2
Show file tree
Hide file tree
Showing 17 changed files with 1,666 additions and 2,413 deletions.
7 changes: 7 additions & 0 deletions backend/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
env: {
test: {
plugins: ["@babel/plugin-transform-modules-commonjs"],
},
},
};
12 changes: 0 additions & 12 deletions backend/babel.config.js

This file was deleted.

6 changes: 6 additions & 0 deletions backend/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
transform: {
"^.+\\.[t|j]sx?$": "babel-jest",
},
testEnvironment: "node",
};
5 changes: 0 additions & 5 deletions backend/jest.config.js

This file was deleted.

22 changes: 11 additions & 11 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "./src/index.js",
"type": "module",
"scripts": {
"start": "node -r esm ./src/index.js",
"dev": "nodemon -r esm ./src/index.js",
"start": "node ./src/index.js",
"dev": "nodemon ./src/index.js",
"server": "npm run start",
"test": "jest",
"lint": "eslint --ext .js,.jsx .",
Expand All @@ -15,29 +15,29 @@
"prettify": "prettier --write src"
},
"dependencies": {
"axios": "^0.21.1",
"axios": "^1.7.7",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"esm": "^3.2.25",
"express": "~4.16.1",
"express": "^4.21.0",
"firebase": "^9.1.1",
"firebase-admin": "^9.11.1",
"mongodb-memory-server": "^7.3.6",
"mongoose": "^5.12.3",
"mongodb-memory-server": "^10.0.1",
"mongoose": "^8.7.0",
"nodemon": "^3.0.1"
},
"devDependencies": {
"@babel/core": "^7.13.14",
"@babel/core": "^7.25.2",
"@babel/plugin-transform-modules-commonjs": "^7.24.8",
"@babel/preset-env": "^7.13.12",
"@types/jest": "^26.0.22",
"babel-jest": "^26.6.3",
"@types/jest": "^29.5.13",
"babel-jest": "^29.7.0",
"eslint": "^7.23.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.3.4",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.0",
"jest": "^29.7.0",
"prettier": "^2.2.1"
}
}
11 changes: 7 additions & 4 deletions backend/src/db/daos/scenarioDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,16 @@ const updateRoleList = async (scenarioId, updatedRoleList) => {
/**
* Deletes a scenario from the database
* @param {String} scenarioId MongoDB ID of scenario
* @returns {Boolean} True if successfully deleted, False if error
* @returns {Promise<Boolean>} True if successfully deleted, False if error
*/
const deleteScenario = async (scenarioId) => {
try {
const scenario = await Scenario.findById(scenarioId);
await scenario.remove();
return true;
const res = await Scenario.findOneAndDelete({ _id: scenarioId });
if (res !== null) {
await Scene.deleteMany({ _id: { $in: res.scenes } });
return true;
}
return false;
} catch (e) {
return false;
}
Expand Down
15 changes: 5 additions & 10 deletions backend/src/db/daos/sceneDao.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,19 @@ const updateScene = async (sceneId, updatedScene) => {
* Deletes a scene from the database, and removes it from its parent scenario
* @param {String} scenarioId MongoDB ID of scenario
* @param {String} sceneId MongoDB ID of scene
* @returns {Boolean} True if successfully deleted, False if error
* @returns {Promise<Boolean>} true if scene was deleted, false otherwise
*/
const deleteScene = async (scenarioId, sceneId) => {
const scenarioRes = await Scenario.updateOne(
const scenarioRes = await Scenario.findOneAndUpdate(
{ _id: scenarioId },
{ $pull: { scenes: sceneId } }
);
if (scenarioRes.n !== 1) {
if (!scenarioRes) {
return false;
}

try {
const scene = await Scene.findById(sceneId);
await scene.remove();
return true;
} catch (e) {
return false;
}
const res = await Scene.findOneAndDelete({ _id: sceneId });
return res !== null;
};

/**
Expand Down
5 changes: 1 addition & 4 deletions backend/src/db/db-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,5 @@ const DEFAULT_CONNECTION_STRING = `mongodb+srv://${process.env.MONGODB_USER}:${p
export default function connectToDatabase(
connectionString = DEFAULT_CONNECTION_STRING
) {
return mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
return mongoose.connect(connectionString);
}
1 change: 0 additions & 1 deletion backend/src/db/models/image.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import mongoose from "mongoose";

const { Schema } = mongoose;
mongoose.set("useFindAndModify", false);

const imageSchema = new Schema({
url: {
Expand Down
1 change: 0 additions & 1 deletion backend/src/db/models/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import mongoose from "mongoose";
import { tryDeleteFile } from "../../firebase/storage.js";

const { Schema } = mongoose;
mongoose.set("useFindAndModify", false);

const sceneSchema = new Schema({
name: {
Expand Down
5 changes: 1 addition & 4 deletions backend/src/middleware/__tests__/scenarioAuth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ describe("Scenario Auth Middleware tests", () => {
mongoServer = await MongoMemoryServer.create();
const uri = mongoServer.getUri();

await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
await mongoose.connect(uri);
});

beforeEach(async () => {
Expand Down
9 changes: 1 addition & 8 deletions backend/src/routes/api/__tests__/imageApi.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-underscore-dangle */
import { MongoMemoryServer } from "mongodb-memory-server";
import express from "express";
import mongoose from "mongoose";
Expand All @@ -23,10 +19,7 @@ describe("Image API tests", () => {
mongoServer = await MongoMemoryServer.create();
const uri = mongoServer.getUri();

await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
await mongoose.connect(uri);

const app = express();
app.use(express.json());
Expand Down
8 changes: 2 additions & 6 deletions backend/src/routes/api/__tests__/scenarioApi.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-underscore-dangle */
import { MongoMemoryServer } from "mongodb-memory-server";
import express from "express";
import mongoose from "mongoose";
Expand Down Expand Up @@ -67,10 +66,7 @@ describe("Scenario API tests", () => {
mongoServer = await MongoMemoryServer.create();
const uri = mongoServer.getUri();

await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
await mongoose.connect(uri);

const app = express();
app.use(express.json());
Expand Down Expand Up @@ -151,7 +147,7 @@ describe("Scenario API tests", () => {
`http://localhost:${port}/api/scenario/${scenario2._id}/`,
authHeaders("user1")
);
expect(response.status).toBe(HTTP_NO_CONTENT);
expect(response.status).toBe(HTTP_OK);

// check scenario has been removed
const dbScenario2 = await Scenario.findById(scenario2._id);
Expand Down
8 changes: 2 additions & 6 deletions backend/src/routes/api/__tests__/sceneApi.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-underscore-dangle */
import { MongoMemoryServer } from "mongodb-memory-server";
import express from "express";
import mongoose from "mongoose";
Expand Down Expand Up @@ -78,10 +77,7 @@ describe("Scene API tests", () => {
mongoServer = await MongoMemoryServer.create();
const uri = mongoServer.getUri();

await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
await mongoose.connect(uri);

const app = express();
app.use(express.json());
Expand Down Expand Up @@ -249,7 +245,7 @@ describe("Scene API tests", () => {
`http://localhost:${port}/api/scenario/${scenario2._id}/scene/${scene1._id}/`,
authHeaders("user1")
);
expect(response.status).toBe(HTTP_NO_CONTENT);
expect(response.status).toBe(HTTP_OK);

// check scene has been removed
const dbScene1 = await Scene.findById(scene1._id);
Expand Down
3 changes: 1 addition & 2 deletions backend/src/routes/api/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import scene from "./scene.js";
const router = Router();

const HTTP_OK = 200;
const HTTP_NO_CONTENT = 204;
const HTTP_NOT_FOUND = 404;

router.use("/:scenarioId/scene", scene);
Expand Down Expand Up @@ -69,7 +68,7 @@ router.put("/:scenarioId", async (req, res) => {
router.delete("/:scenarioId", async (req, res) => {
const deleted = await deleteScenario(req.params.scenarioId);
if (deleted) {
res.sendStatus(HTTP_NO_CONTENT);
res.sendStatus(HTTP_OK);
} else {
res.sendStatus(HTTP_NOT_FOUND);
}
Expand Down
3 changes: 1 addition & 2 deletions backend/src/routes/api/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ router.put("/:sceneId", async (req, res) => {
// Delete a scene
router.delete("/:sceneId", async (req, res) => {
const deleted = await deleteScene(req.params.scenarioId, req.params.sceneId);

if (deleted) {
res.sendStatus(HTTP_NO_CONTENT);
res.sendStatus(HTTP_OK);
} else {
res.sendStatus(HTTP_NOT_FOUND);
}
Expand Down
Loading

0 comments on commit 13218e2

Please sign in to comment.