Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gor3a committed Feb 8, 2023
0 parents commit 9319b74
Show file tree
Hide file tree
Showing 79 changed files with 82,799 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WEB3_PROVIDER_URL=""
NETWORK_ID=""
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.idea
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Vehicle Registration
<p align="center" color="red">Vehicle registration system to replace recorder deeds</p>

## Installation

Use the package manager NPM, so you need to install [Nodejs](https://nodejs.org/en/download/)

```bash
npm install
```

## Usage

```bash
npm run start
```

## Contributing

Pull requests are welcome. For major changes, please open an issue first
to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License

[MIT](https://choosealicense.com/licenses/mit/)
81 changes: 81 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const expressValidator = require("express-validator")
const session = require("express-session")
const expressLayout = require('express-ejs-layouts')
const flash = require('express-flash')

require('dotenv').config()

// Routes
const userRoutes = require("./routes/users");
const homeRoutes = require("./routes/home");
const vehiclesRoutes = require("./routes/vehicles");
const installUsers = require("./helpers/installUsers");
const installManufactures = require("./helpers/installManufactures");
const installVehicles = require("./helpers/installVehicles");
const licensesRoutes = require("./routes/licenses");
const manufacturersRoutes = require("./routes/manufacturers");
const banksRoutes = require("./routes/banks");
const installBanks = require("./helpers/installBanks");
const roles = require("./utilities/roles");
const installLicenses = require("./helpers/installLicenses");

const app = express();

app.use(flash())
// Global middleware
app.use((req, res, next) => {
res.locals.session = req.session
res.locals.user = req.user
next()
})
// view engine setup
app.use(expressLayout)
app.set("views", path.join(__dirname, "resources/views"));
app.set("view engine", "ejs");

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(expressValidator())
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

const oneDay = 1000 * 60 * 60 * 24;
app.use(session({
secret: "thisismysecrctekeyfhrgfgrfrty84fwir767",
saveUninitialized: true,
cookie: {maxAge: oneDay},
resave: false
}));

// init
installUsers()
installManufactures()
installVehicles()
installBanks()
installLicenses()

app.use(function (req, res, next) {
res.locals.currentUser = req.session.user;
res.locals.currentUserRole = roles(req.session.user?.user_role);
res.locals.message = req.flash();
next()
})

homeRoutes(app)
userRoutes(app)
vehiclesRoutes(app)
licensesRoutes(app)
manufacturersRoutes(app)
banksRoutes(app)

app.use((req, res) => {
res.status(404).render('404')
})

module.exports = app;
28 changes: 28 additions & 0 deletions app/controllers/BanksController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const mainContract = require("../../utilities/contract");

function BanksController() {
return {
async allBanks(req, res, next) {

const contract = await mainContract()
let users = []
let banks = await contract.methods.get_banks().call(async function (err, result) {
return result
})

for (const i in banks) {
const user = await contract.methods.get_user(banks[i].user_id).call((err, result) => {
return result
})
banks[i].user = user
users.push(user)
}

res.render("banks/list", {
banks, users
})
}
}
}

module.exports = BanksController
5 changes: 5 additions & 0 deletions app/controllers/HomeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function HomeController() {

}

module.exports = HomeController
105 changes: 105 additions & 0 deletions app/controllers/LicensesController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const mainContract = require("../../utilities/contract");
const {accounts} = require("../../utilities/accounts");

function LicensesController() {
return {
async allLicenses(req, res) {

const contract = await mainContract()

const licenses = await contract.methods.get_licenses().call((err, result) => {
return result
})

const vehicles = await contract.methods.get_vehicles().call((err, result) => {
return result
})

function timeConverter(UNIX_timestamp) {
let a = new Date(parseInt(UNIX_timestamp));
let months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
let year = a.getFullYear();
let month = a.getMonth();
let date = a.getDate();
return year + '/' + month + '/' + date;
}

const currentUserRole = res.locals.currentUserRole

if (currentUserRole.toString() === 'admin') {
return res.render('licenses/list', {
licenses: licenses,
vehicles: vehicles,
timeConverter,
currentTime: Date.now(),
})
} else {
const user_id = res.locals.currentUser?.user_id
let new_licenses = []
let new_licenses_id = []
for (const i in licenses) {
if (parseInt(licenses[i].user_id) === parseInt(user_id)) {
new_licenses.push(licenses[i])
new_licenses_id.push(i)
}
}

let new_vehicles = []

for (const license of new_licenses) {
new_vehicles.push(vehicles[license.car_id])
}

return res.render('licenses/list', {
licenses: new_licenses,
vehicles: new_vehicles,
timeConverter,
currentTime: Date.now(),
new_licenses_id
})
}
},

async add(req, res) {
const contract = await mainContract()

const vehicles = await contract.methods.get_vehicles().call((err, result) => {
return result
})

const user_id = res.locals.currentUser?.user_id
let new_vehicles = []
for (const vehicle of vehicles) {
if (parseInt(vehicle.user_id) === parseInt(user_id)) new_vehicles.push(vehicle)
}

res.render('licenses/add', {
vehicles: new_vehicles
})
},

async renewal(req, res) {
const license_id = req.params.id
const expire_at = req.params.expire_at
const allAccounts = await accounts()

const contract = await mainContract()

const aYearFromNow = new Date(parseInt(expire_at));
aYearFromNow.setFullYear(new Date().getFullYear() + 5);

const new_expire_at = aYearFromNow.getTime().toString()

await contract.methods.renewal_license(license_id, new_expire_at).send({
from: allAccounts[0],
gas: 200000000
})

req.flash('success', "Renewal Successfully!");

res.redirect("/licenses")
}
}
}

module.exports = LicensesController
27 changes: 27 additions & 0 deletions app/controllers/ManufacturersController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const mainContract = require("../../utilities/contract");

function ManufacturersController() {
return {
async allManufacturers(req, res, next) {
const contract = await mainContract()
let users = []
let manufactures = await contract.methods.get_manufactures().call(async function (err, result) {
return result
})

for (const i in manufactures) {
const user = await contract.methods.get_user(manufactures[i].user_id).call((err, result) => {
return result
})
manufactures[i].user = user
users.push(user)
}

res.render("manufacturers/list", {
manufactures, users
})
}
}
}

module.exports = ManufacturersController
Loading

0 comments on commit 9319b74

Please sign in to comment.