-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: link software assets to hardware assets (1-many)
- Loading branch information
1 parent
438764d
commit 855dca5
Showing
24 changed files
with
434 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { Request, Response } from "express"; | ||
import { wrapper } from ".."; | ||
import { APIResponse } from "../../../src/interfaces/APIRequests"; | ||
import { Collection } from "mongodb"; | ||
|
||
const express = require('express'); | ||
const mongo = require('mongodb'); | ||
const router = express.Router(); | ||
|
||
const DATABASE = "asset-links"; | ||
|
||
// @ROUTE: GET api/asset-links/ | ||
// @DESCRIPTION: Used for getting ALL Asset Links. | ||
router.get('/', async (_: Request, res: Response) => { | ||
await wrapper(async (db: any) => { | ||
const collection: Collection = db.collection(DATABASE); | ||
const resp = await collection.find({}).toArray(); | ||
return res.send(resp); | ||
}); | ||
}); | ||
|
||
// @ROUTE: POST api/asset-links/ | ||
// @DESCRIPTION: Used for creating an Asset Link. | ||
router.post('/', async (req: Request, res: Response) => { | ||
await wrapper(async (db: any) => { | ||
console.log('request got at PSOT asset-links/') | ||
const { hardware_id, software_id, date, created_by }: APIResponse.CreateAssetLink = req.body as APIResponse.CreateAssetLink; | ||
const collection: Collection = db.collection(DATABASE); | ||
|
||
const HardwareID = new mongo.ObjectId(hardware_id); | ||
const SoftwareID = new mongo.ObjectId(software_id) | ||
|
||
const isExisting = await collection.find({ | ||
hardware_id: HardwareID, | ||
software_id: SoftwareID | ||
}).toArray() | ||
|
||
if (isExisting.length > 0) { | ||
return res.send({ status: false }) | ||
} | ||
|
||
const _date = new Date(date); | ||
const resp = await collection.insertOne({ | ||
hardware_id: HardwareID, | ||
software_id: SoftwareID, | ||
date: _date, | ||
created_by | ||
}) | ||
|
||
if (resp.acknowledged) return res.send(resp); | ||
return res.send({ status: false }) | ||
}); | ||
}); | ||
|
||
// @ROUTE: GET api/asset-links/hardware/:id | ||
// @DESCRIPTION: Used for getting ALL Asset Links for a specific Hardware Asset. | ||
router.get('/hardware/:id', async (req: Request, res: Response) => { | ||
await wrapper(async (db: any) => { | ||
const collection: Collection = db.collection(DATABASE); | ||
const id = req.params.id; | ||
|
||
const resp = await collection.find({ hardware_id: new mongo.ObjectId(id) }).toArray(); | ||
|
||
let resp_arr = []; | ||
for (const [i, link] of resp.entries()) { | ||
const subresp = await db.collection('software').findOne({ _id: link.software_id }) ?? undefined; | ||
resp_arr.push({ | ||
software: subresp, | ||
link: resp[i] | ||
}); | ||
} | ||
return res.json(resp_arr.filter(x => !!x)); | ||
}); | ||
}); | ||
// @ROUTE: DELETE api/asset-links/hardware/:id | ||
// @DESCRIPTION: Used for getting ALL Asset Links for a specific Hardware Asset. | ||
router.delete('/hardware/:hwid/:swid', async (req: Request, res: Response) => { | ||
await wrapper(async (db: any) => { | ||
console.log('api response recieved!!!!', req.params.link_id) | ||
const collection: Collection = db.collection(DATABASE); | ||
const hwid = req.params.hwid; | ||
const swid = req.params.swid; | ||
|
||
const resp = await collection.deleteMany({ | ||
hardware_id: new mongo.ObjectId(hwid), | ||
software_id: new mongo.ObjectId(swid) | ||
}); | ||
console.log(resp); | ||
return res.json(resp); | ||
}); | ||
}); | ||
|
||
// @ROUTE: GET api/asset-links/hardware/:id | ||
// @DESCRIPTION: Used for getting ALL Asset Links for a specific Software Asset. | ||
router.get('/software/:id', async (req: Request, res: Response) => { | ||
await wrapper(async (db: any) => { | ||
const collection: Collection = db.collection(DATABASE); | ||
const id = req.params.id; | ||
|
||
const resp = await collection.find({ software_id: new mongo.ObjectId(id) }).toArray(); | ||
return res.send(resp); | ||
}); | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
import { ObjectId } from "mongodb"; | ||
import { Asset } from "../Asset"; | ||
import { IParentHardware, ISoftwareAsset } from "../../../interfaces/Asset"; | ||
import { ISoftwareAsset } from "../../../interfaces/Asset"; | ||
|
||
export class SoftwareAsset extends Asset implements ISoftwareAsset { | ||
public _id: ObjectId; | ||
public name: string; | ||
public version: string; | ||
public manufacturer: string; | ||
public parent_hardware: IParentHardware | undefined; | ||
public risk_level: string; | ||
public created_at: string; | ||
public last_edit_at: string; | ||
|
||
constructor(props: SoftwareAsset) { | ||
super(props); | ||
this._id = props._id; | ||
this.name = props.name; | ||
this.manufacturer = props.manufacturer; | ||
this.version = props.version; | ||
this.parent_hardware = (props.parent_hardware) ? { | ||
id: props.parent_hardware.id, | ||
date: props.parent_hardware.date | ||
} : undefined; | ||
this.risk_level = props.risk_level; | ||
this.created_at = props.created_at; | ||
this.last_edit_at = props.last_edit_at; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.