Node.js: calcolare il valore dell'IVA

In Node.js possiamo calcolare il valore dell'IVA con ExpressJS.

Node.js di Gabriele Romanato

In Node.js possiamo calcolare il valore dell'IVA con ExpressJS.

Creiamo la nostra classe di utility:


'use strict';

class Vat {
  constructor(value) {
    this.value = ((value / 100) + 1);
  }
  add(amount) {
    return (amount * this.value);
  }
  remove(amount) {
    return (amount / this.value);
  }
  amount(total) {
    let diff = (total / this.value);
    return (total - diff);
  }
}

module.exports = Vat;

Quindi creiamo la nostra route:


'use strict';

const express = require('express');
const app = express();
const Vat = require('./classes/vat');
const actions = ['add','remove','amount'];

app.post('/calculate', (req, res) => {
    let action = req.body.action;
    let value = parseInt(req.body.value, 10);
    let amount = Number(req.body.amount);

    if(actions.indexOf(action) !== -1) {
        let vatInstance = new Vat(value);
        let output = vatInstance[action](amount);
        res.json({vat: output});
    } else {
      res.sendStatus(403);
    }

});

Commenti