Skip to content

Commit

Permalink
Refactor openapi.yaml attributes to use consistent
Browse files Browse the repository at this point in the history
format and fix endpoint summary

The openapi.yaml file was modified to refactor the attributes for
various endpoints in order to use a consistent format. In particular,
the format for UUID-like attributes was changed to match a specific
pattern and have a fixed length of 24 characters. Additionally, the
summary of the "/models" endpoint was updated to clarify its purpose.
These changes improve the consistency and clarity of the API
documentation. No issues were referenced in this commit.
  • Loading branch information
karol-preiskorn committed Jan 4, 2024
1 parent 69cadb5 commit 92c63ea
Show file tree
Hide file tree
Showing 15 changed files with 2,996 additions and 1,407 deletions.
657 changes: 429 additions & 228 deletions api/openapi.yaml

Large diffs are not rendered by default.

Binary file removed assets/3d-inventory.zip
Binary file not shown.
Binary file removed assets/3d-inventory_client_20231121204713.zip
Binary file not shown.
3 changes: 1 addition & 2 deletions index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import logger from "./utils/logger.mjs"
import swaggerUi from "swagger-ui-express"
import * as OpenApiValidator from "express-openapi-validator"
import fs from "fs"
import path from "path"
import YAML from "yaml"

logger.info(
Expand Down Expand Up @@ -94,7 +93,7 @@ try {
app.use(OpenApiValidator.middleware({
apiSpec: yamlFilename,
validateRequests: true,
validateResponses: false
validateResponses: true
}))
} catch (error) {
logger.error(`[openApiValidator] ${error}`)
Expand Down
2 changes: 1 addition & 1 deletion loadEnvironment.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* File: /loadEnviroment.mjs
* Description:
* Used by:
Expand Down
3,291 changes: 2,221 additions & 1,070 deletions package-lock.json

Large diffs are not rendered by default.

46 changes: 7 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "3d-inventory-mongo-api",
"version": "0.0.24",
"version": "0.0.25",
"description": "Mongo API for 3d-inventory project",
"keywords": [
"3d-inventory",
Expand All @@ -25,7 +25,7 @@
"debug": "npm run build && npm run watch-debug",
"format": "prettier --single-quote --check .",
"kill-port-upsage": "kill -9 `lsof -nP -iTCP:8080 -sTCP:LISTEN | sed -n 2p | awk '{print $2}'`",
"lint": "echo EsLint - `date +%R` | /usr/bin/figlet && eslint \"*.{js}\" --quiet --fix",
"lint": "echo EsLint - `date +%R` | /usr/bin/figlet && eslint \"*.mjs\" --quiet --fix",
"lint:docs": "typedoc --out doc --tsconfig tsconfig.json src/** test/**",
"lint:js": "eslint --ignore-path .gitignore --config .eslintrc src/**/*.js",
"maven:latest": "cd api && mvn versions:use-latest-versions",
Expand Down Expand Up @@ -54,21 +54,18 @@
}
},
"lint-staged": {
"*.js": [
"prettier --single-quote --write",
"*.mjs": [
"prettier --double-quote --write",
"eslint --fix"
]
},
"dependencies": {
"@apollo/client": "^3.8.8",
"@babel/core": "^7.23.7",
"async": "3.2.5",
"babel-jest": "^29.7.0",
"bcryptjs": "^2.4.3",
"body-parser": "1.20.2",
"bson": "^6.2.0",
"chai-http": "^4.4.0",
"compression": "1.7.4",
"connect": "^3.7.0",
"connect-mongo": "^5.1.0",
"cookie-session": "^2.0.0",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
Expand All @@ -86,13 +83,13 @@
"graphql": "^16.8.1",
"husky": "^8.0.3",
"jade-bootstrap": "^1.0.14",
"js-yaml": "^4.1.0",
"json-schema-to-openapi-schema": "^0.4.0",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
"lusca": "1.7.0",
"moment": "^2.30.1",
"mongodb": "^6.3.0",
"npm-check": "^6.0.1",
"npm-check-updates": "^16.14.12",
"oas-normalize": "^11.0.1",
"oas3-tools": "^2.2.3",
Expand All @@ -116,41 +113,12 @@
"devDependencies": {
"@faker-js/faker": "^8.3.1",
"@shelf/jest-mongodb": "^4.2.0",
"@types/async": "3.2.24",
"@types/bcrypt-nodejs": "0.0.31",
"@types/body-parser": "1.19.5",
"@types/chai": "4.3.11",
"@types/compression": "1.7.5",
"@types/cors": "^2.8.17",
"@types/errorhandler": "1.5.3",
"@types/eslint": "8.56.0",
"@types/express": "^4.17.21",
"@types/express-flash": "0.0.5",
"@types/express-session": "1.17.10",
"@types/figlet": "^1.5.8",
"@types/jest": "^29.5.11",
"@types/lodash": "^4.14.202",
"@types/lusca": "1.7.4",
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.5",
"@types/passport": "1.0.16",
"@types/passport-facebook": "3.0.3",
"@types/passport-local": "1.0.38",
"@types/pug": "2.0.10",
"@types/request": "2.48.12",
"@types/request-promise": "4.1.51",
"@types/shelljs": "0.8.15",
"@types/supertest": "6.0.2",
"@types/swagger-ui-express": "^4.1.6",
"chai": "^5.0.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "9.1.0",
"eslint-config-standard": "17.1.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-node": "11.1.0",
"jest": "^29.7.0",
"nodemon": "^3.0.2",
"shelljs": "0.8.5",
"supertest": "6.3.3",
"typedoc": "^0.25.4"
},
Expand Down
14 changes: 13 additions & 1 deletion routers/devices.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ router.get("/:id", async (req, res) => {
else res.send(result).status(200)
})

// Get a single post
router.get("/model/:id", async (req, res) => {
const collection = await db.collection("devices")
const query = { modelId: ObjectId(req.params.id) }
const result = await collection.findOne(query)
if (!result) res.send("Not found").status(404)
else res.send(result).status(200)
})


// Create
router.post("/", async (req, res) => {
const collection = await db.collection("devices")
Expand Down Expand Up @@ -57,13 +67,15 @@ router.delete("/:id", async (req, res) => {
res.send(result).status(200)
})

router.delete("/all", async (req, res) => {
// delete all devices
router.delete("/", async (req, res) => {
const query = { }
const collection = db.collection("devices")
const result = await collection.deleteMany(query)
res.send(result).status(200)
})

// delete all devices with specific :id model
router.delete("/model/:id", async (req, res) => {
const query = { modelId: ObjectId(req.params.id) }
const collection = db.collection("devices")
Expand Down
68 changes: 68 additions & 0 deletions routers/models.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* File: /routers/models.mjs
* Description:
*
* Date By Comments
* ---------- ----- ------------------------------
* 2023-12-29 C2RLO Initial
**/

import express from "express"
import db from "../db/conn.mjs"
import { ObjectId } from "mongodb"

const router = express.Router()

// Get all
router.get("/", async (req, res) => {
const collection = await db.collection("models")
const results = await collection.find({}).limit(50).toArray()
res.send(results).status(200)
})

// Get a single post
router.get("/:id", async (req, res) => {
const collection = await db.collection("models")
const query = { _id: ObjectId(req.params.id) }
const result = await collection.findOne(query)
if (!result) res.send("Not found").status(404)
else res.send(result).status(200)
})

// Create
router.post("/", async (req, res) => {
const collection = await db.collection("models")
const newDocument = req.body
// newDocument.date = new Date()
const results = await collection.insertOne(newDocument)
res.send(results).status(204)
})

// Update the device's :id position
router.patch("/position/:id", async (req, res) => {
const query = { _id: ObjectId(req.params.id) }
const updates = {
$push: { position: req.body }
}
const collection = await db.collection("models")
const result = await collection.updateOne(query, updates)
res.send(result).status(200)
})

// Delete an entry
router.delete("/:id", async (req, res) => {
const query = { _id: ObjectId(req.params.id) }
const collection = db.collection("models")
const result = await collection.deleteOne(query)
res.send(result).status(200)
})

// Delete all
router.delete("/", async (req, res) => {
const query = { }
const collection = db.collection("models")
const result = await collection.deleteMany(query)
res.send(result).status(200)
})

export default router
89 changes: 55 additions & 34 deletions tests/devices.test.mjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,65 @@
/**
* File: /tests/device.test.ts
* Description: create device in mongo DB
* /api/devices
* https://jestjs.io/docs/bypassing-module-mocks
* File: /tests/devices.mjs
* Description:
*
* Date By Comments
* ---------- ----- ------------------------------
* 2023-12-26 C2RLO Initial
**/
* 2024-01-03 C2RLO Initial
*/

import express from "express"
import request from "supertest"
import assert from "assert"
import { faker } from "@faker-js/faker"
import "../loadEnvironment.mjs"
import db from "../db/conn.mjs"
const faker = require("@faker-js/faker")
const request = require("supertest")
const express = require("express")

const deviceType = [
{ name: "Bridge", description: "" },
{ name: "CoolAir", description: "" },
{ name: "Copier", description: "" },
{ name: "Desktop", description: "" },
{ name: "Firewall", description: "" },
{ name: "Getaway", description: "" },
{ name: "Hubs", description: "" },
{ name: "Load Balancer", description: "" },
{ name: "Modem", description: "" },
{ name: "Multiplexer", description: "" },
{ name: "PDU System", description: "" },
{ name: "Power", description: "" },
{ name: "Printer", description: "" },
{ name: "Probe", description: "" },
{ name: "Repeaters", description: "" },
{ name: "Router", description: "" },
{ name: "Security Device", description: "" },
{ name: "Server", description: "" },
{ name: "Switch", description: "" },
{ name: "Telephone", description: "" },
{ name: "Terminal", description: "" },
{ name: "Traffic shaper", description: "" },
{ name: "Transceiver", description: "" },
{ name: "UPS System", description: "" },
{ name: "Workstations", description: "" },
]

const deviceCategory = [
{
name: "Connectivity",
description: "Data centers often have multiple fiber connections to the internet provided by multiple carriers.",
},
{
name: "Facility",
description:
"Data center buildings may be specifically designed as a data center. For example, the height of ceilings will match requirements for racks and overhead systems. In some cases, a data center occupies a floor of an existing building.",
},
{
name: "Site",
description:
"A data center requires a site with connections to grids, networks and physical <a href=\"https://simplicable.com/new/infrastructure\">infrastructure</a> such as roads. Proximity to markets, customers, employees and services also play a role in selecting an appropriate site. Locating data centers in cold climates can reduce cooling costs.",
},
]

const app = express()

describe("GET /devices", () => {
it("Should create Device by API", async () => {
describe("get /devices", () => {
it("should get one device", async () => {
return new Promise((resolve, reject) => {
try {
request(app)
Expand All @@ -36,24 +76,5 @@ describe("GET /devices", () => {
resolve(error).expect(error.statusCode).not.toBe(200)
}
})
}, 10000)
})

describe("Create Device", () => {
it("should insert a Device doc into collection devices", async () => {
const device = db.collection("devices")
const mockDevice = {
"name": faker.commerce.product() + " " + faker.color.human() + "-" + faker.animal.type(),
"modelId": faker.string.uuid(),
"position": {
x: faker.number.int(100),
y: faker.number.int(100),
h: faker.number.int(100),
},
}

await device.insertOne(mockDevice)
const insertedDevice = await device.findOne(mockDevice)
expect(insertedDevice).toEqual(mockDevice)
})
}, 8000)
})
1 change: 1 addition & 0 deletions tests/mongo.test.mjs → tests/models.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import { faker } from "@faker-js/faker"
import "../loadEnvironment.mjs"
import db from "../db/conn.mjs"
import { ObjectId } from "mongodb"

describe("Test Mongo Atlas DB connection and schema", () => {
it("should insert a User doc into collection", async () => {
Expand Down
Loading

0 comments on commit 92c63ea

Please sign in to comment.