Skip to content

Commit

Permalink
create mcda endpoint and function
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep committed Jul 11, 2023
1 parent 8e8a71b commit 6876237
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
23 changes: 23 additions & 0 deletions lib/mcda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const pl = require("nodejs-polars");


const mcda = function(body){
if(body === undefined || !body instanceof Object) return {error: "body must be an array of ids"}
if(!(body instanceof Array)) return {error: "body must be an array of ids"}

let df = pl.readCSV("public/technologies.csv")

wrong_ids = body.filter(e => !df['id'].toArray().includes(e))
if (wrong_ids.length > 0) return {error: "some ids are not valid"}

df = df.filter(pl.col("id").isIn(body))

// TODO: Començar a calcular scores


return(df.toRecords())
}

module.exports = {
mcda
}
17 changes: 17 additions & 0 deletions routes/treatment.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ router.post('/find-nbs-multiple', function(req, res){
}
})

router.post('/mcda', function (req, res) {
try {
let result = findNBS.findNBS(req.body)

if (result.error) {
res.statusMessage = result.error
res.status(400)
res.send(result.error)
} else {
res.send(result)
}
}
catch (e){
res.send(e)
}
})

module.exports = router;


3 changes: 1 addition & 2 deletions tests/test-find-nbs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const findNBS = require("../lib/find-nbs").findNBS
const expect = require("chai").expect
const should = require("chai").should()
const jstat = require("jstat")


Expand All @@ -12,7 +11,7 @@ describe("Test /find-nbs", () => {
describe('findNBS returns an array of technologies', () => {
let result = findNBS({})
it("result is an array", () => {
result.should.be.an('array')
expect(result).to.be.an('array')
});
it('technologies have id', () => {
expect(result[1]).to.have.any.keys("id")
Expand Down
25 changes: 25 additions & 0 deletions tests/test-mcda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const expect = require("chai").expect
const mcda = require("../lib/mcda").mcda

describe('Test /mcda', () => {
describe("Raise errors on data sanitation", () => {
it('Returns error if no body', () => {
expect(mcda()).to.have.key('error')
});
it('Returns error if body is not an array', () => {
expect(mcda({a: 'dsvsddf', b: 'aasdf'})).to.have.key('error')
expect(mcda('dsvsddf')).to.have.key('error')
expect(mcda(123)).to.have.key('error')
});
it('Returns error if some ids are wrong', () => {
result = mcda(['a', 'WW', 'French_CW'])
expect(result).to.have.key("error")
})
});
describe("Returns an array of technologies", () => {
let result = mcda(['WW', 'French_CW', 'HSSF_CW'])
it('result is an array', () => {
expect(result).to.be.an('array')
})
})
})

0 comments on commit 6876237

Please sign in to comment.