Skip to content

Commit

Permalink
SonarQube
Browse files Browse the repository at this point in the history
  • Loading branch information
oscrx committed Aug 6, 2023
1 parent 988a628 commit 5d7a354
Show file tree
Hide file tree
Showing 20 changed files with 101 additions and 86 deletions.
2 changes: 1 addition & 1 deletion beer-import/services/processData.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const processData = (data, stores) => {
if (!newStores[data[obj].store]) {
newStores[data[obj].store] = data[obj].store
}
if (storeMap && storeMap.get(data[obj].store)) {
if (storeMap?.get(data[obj].store)) {
data[obj].rawStore = data[obj].store
data[obj].store = storeMap.get(data[obj].store)
}
Expand Down
7 changes: 2 additions & 5 deletions pils-frontend/build/check-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ if (shell.which('npm')) {
module.exports = function () {
const warnings = []

for (let i = 0; i < versionRequirements.length; i++) {
const mod = versionRequirements[i]

for (const mod of versionRequirements) {
if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
warnings.push(mod.name + ': ' +
chalk.red(mod.currentVersion) + ' should be ' +
Expand All @@ -43,8 +41,7 @@ module.exports = function () {
console.log(chalk.yellow('To use this template, you must update following to modules:'))
console.log()

for (let i = 0; i < warnings.length; i++) {
const warning = warnings[i]
for (const warning of warnings) {
console.log(' ' + warning)
}

Expand Down
2 changes: 1 addition & 1 deletion pils-frontend/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ exports.createNotifierCallback = () => {
if (severity !== 'error') return

const error = errors[0]
const filename = error.file && error.file.split('!').pop()
const filename = error.file?.split('!').pop()

notifier.notify({
title: packageConfig.name,
Expand Down
1 change: 0 additions & 1 deletion pils-frontend/build/webpack.dev.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const config = require('../config')
const merge = require('webpack-merge')
const path = require('path')
const baseWebpackConfig = require('./webpack.base.conf')
// const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const portfinder = require('portfinder')
Expand Down
1 change: 0 additions & 1 deletion pils-frontend/build/webpack.prod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const webpack = require('webpack')
const config = require('../config')
const merge = require('webpack-merge')
const baseWebpackConfig = require('./webpack.base.conf')
// const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
Expand Down
2 changes: 1 addition & 1 deletion pils-frontend/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
Expand Down
16 changes: 8 additions & 8 deletions pils-frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
<div class="has-text-centered">
<nav class="navbar" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<!-- <a class="navbar-item" href="#/">
<img src="https://bulma.io/images/bulma-logo.png" width="112" height="28">
</a> -->

<a role="button" class="navbar-burger burger" v-if="isAuthenticated" @click="burger = !burger" aria-label="menu" aria-expanded="false" data-target="navbar" v-bind:class="{'is-active': burger}">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
Expand Down Expand Up @@ -81,19 +77,23 @@
</template>

<script>
// addToHomescreen();
import Api from '@/services/Api'
export default {
name: 'App',
data() {
const isAuthenticated = !!localStorage.getItem('isAuthenticated')
const isAdmin = !!localStorage.getItem('isAdmin')
const userId = isAuthenticated ? localStorage.getItem('userId') : null
return {
burger: false,
isAuthenticated: localStorage.getItem('isAuthenticated') ? true : false,
isAdmin: localStorage.getItem('isAdmin') ? true : false,
userId: localStorage.getItem('isAuthenticated')
isAuthenticated,
isAdmin,
userId,
}
},
}
methods: {
Logout() {
Api().delete(`/api/v1/users/logout`)
Expand Down
26 changes: 12 additions & 14 deletions pils-frontend/src/components/Account.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<h3 class="title has-text-grey">Scan this QR code</h3>
<a :href='otp.uri'>
<div class="container" id="qrcode">
<img id="preview" src="/favicon.ico">
<img id="preview" src="/favicon.ico" alt="Beer emoji">
<img :src='otp.QRdata' :alt='otp.uri'>
</div>
</a>
Expand Down Expand Up @@ -113,20 +113,18 @@
this.Account()
},
computed: {
isDisabled:function() {
pwned(this.newUser.password).then(isPwned => {
this.state.pwned = isPwned
})
async isDisabled() {
// Check if the password is pwned
this.state.pwned = await pwned(this.newUser.password);
if (this.newUser.password !== this.verifyPassword) {
this.state.notEqual = true
} else {
this.state.notEqual = false
}
const stuffToEdit = (this.newUser.password || this.newUser.username || this.newUser.otp) ? true : false
return (this.newUser.oldPassword && stuffToEdit && !this.state.pwned && !this.state.notEqual) ? false : true
// Check if the password is not equal to the verified password
this.state.notEqual = this.newUser.password !== this.verifyPassword;
// Check if there is anything to edit (password, username, or otp)
const stuffToEdit = this.newUser.password || this.newUser.username || this.newUser.otp;
// Determine if the form should be disabled
return !(this.newUser.oldPassword && stuffToEdit && !this.state.pwned && !this.state.notEqual);
}
},
methods: {
Expand Down
63 changes: 35 additions & 28 deletions pils-frontend/src/components/Discounts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
</nav>
<progress v-if="discounts.length === 0" class="progress is-small"></progress>
<table class='table container'>
<caption>Table of beer discounts</caption>
<thead>
<tr><!-- first row -->
<th><input class='input' v-model='search' type='text' placeholder='Search' autofocus></th>
Expand Down Expand Up @@ -97,20 +98,8 @@ export default {
mixins: [Vue2Filters.mixin],
data () {
return {
discounts: [],
discountAverage: [],
literAverage: [],
percentageAverage: [],
onlineCounter: 0,
search: '',
online: false,
zero: true,
store: '',
stores: [],
volume: '',
volumes: [],
sort:'literPrice',
sortDir: 1,
discounts: [],
headers: {
brand: 'Brand',
alcoholPercentage: '%',
Expand All @@ -122,6 +111,18 @@ export default {
discountPercentage: '%',
volume: 'Volume'
}
literAverage: [],
online: false,
onlineCounter: 0,
percentageAverage: [],
search: '',
sort:'literPrice',
sortDir: 1,
store: '',
stores: [],
volume: '',
volumes: [],
zero: true,
}
},
created () {
Expand All @@ -130,22 +131,28 @@ export default {
methods: {
async getPils () {
const response = await Api().get('/api/v1/discounts')
for (let i = 0; i < response.data.length; i++) {
for (const item of response.data) {
// take data to main object for sorting
response.data[i].discount = ((response.data[i].pricing.oldPrice - response.data[i].pricing.newPrice) / 100).toFixed(2)
response.data[i].discountPercentage = (100 - (response.data[i].pricing.newPrice * 100 / response.data[i].pricing.oldPrice)).toPrecision(2)
// response.data[i].literPrice = response.data[i].pricing.literPrice
response.data[i].literPrice = response.data[i].pricing.newPrice / response.data[i].liter * 10 // Temporary
response.data[i].newPrice = response.data[i].pricing.newPrice
response.data[i].oldPrice = response.data[i].pricing.oldPrice
item.discount = ((item.pricing.oldPrice - item.pricing.newPrice) / 100).toFixed(2)
item.discountPercentage = (100 - (item.pricing.newPrice * 100 / item.pricing.oldPrice)).toPrecision(2)
item.literPrice = item.pricing.newPrice / item.liter * 10
item.newPrice = item.pricing.newPrice
item.oldPrice = item.pricing.oldPrice
this.discountAverage.push(response.data[i].discount)
this.percentageAverage.push(response.data[i].discountPercentage)
this.literAverage.push(response.data[i].literPrice)
if (response.data[i].uri){ this.onlineCounter++ }
if(this.stores.indexOf(response.data[i].store) === -1) {this.stores.push(response.data[i].store)}
if(this.volumes.indexOf(response.data[i].volume) === -1) {this.volumes.push(response.data[i].volume)}
this.discountAverage.push(item.discount)
this.percentageAverage.push(item.discountPercentage)
this.literAverage.push(item.literPrice)
if (item.uri) {
this.onlineCounter++
}
if (this.stores.indexOf(item.store) === -1) {
this.stores.push(item.store)
}
if (this.volumes.indexOf(item.volume) === -1) {
this.volumes.push(item.volume)
}
}
this.discounts = response.data
this.volumes.sort()
},
Expand All @@ -157,8 +164,8 @@ export default {
},
average:function(inputArray){
let result = 0
for (let i = 0; i < inputArray.length; i++) {
result += Number(inputArray[i]) // x += y same as x = x + y
for (const item of inputArray) {
result += Number(item)
}
return result / inputArray.length
}
Expand Down
32 changes: 23 additions & 9 deletions pils-frontend/src/components/Logging.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{{message}}
</div>
<table class='table container'>
<caption>Table of logs</caption>
<thead>
<tr> <!-- first row -->
<th><a class="button is-danger" @click='state.deleteMsg = !state.deleteMsg'>Delete all logs</a></th>
Expand Down Expand Up @@ -107,23 +108,36 @@ export default {
}
},
methods: {
async getLogs () {
async getLogs() {
try {
const response = await Api().get('/api/v1/logging')
this.logs = response.data
for (let i = 0; i < response.data.length; i++){
if (this.contexts.indexOf(response.data[i].context) === -1) {
this.contexts.push(response.data[i].context)
// Create separate arrays for contexts and types if they don't exist already
this.contexts = this.contexts || []
this.types = this.types || []
for (const log of response.data) {
if (!this.contexts.includes(log.context)) {
this.contexts.push(log.context)
}
if (this.types.indexOf(response.data[i].type) === -1) {
this.types.push(response.data[i].type)
if (!this.types.includes(log.type)) {
this.types.push(log.type)
}
}
},
async deleteLogs() {
const response = await Api().delete(`/api/v1/logging`)
} catch (error) {
console.error('Error fetching logs:', error)
}
},
async deleteLogs() {
try {
const response = await Api().delete('/api/v1/logging')
this.message = response.data
this.logs = []
} catch (error) {
console.error('Error deleting logs:', error)
}
}
}
}
</script>
5 changes: 1 addition & 4 deletions pils-frontend/src/components/NotFound.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<template>
<!-- <figure class="image is-16by9">
<img :src="`//media.disquscdn.com/errors/img/${Math.ceil( Math.random() * 8 )}.gif`">
</figure> -->
<figure class="image is-16by9">
<img :src="`//media.disquscdn.com/errors/img/${3}.gif`">
<img :src="`//media.disquscdn.com/errors/img/${3}.gif`" alt='404 page, people slapping threir forehead gif.'>
</figure>
</template>
13 changes: 8 additions & 5 deletions pils-frontend/src/components/Register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@
}
},
computed: {
isDisabled:function() {
pwned(this.$data.password).then(isPwned => {
this.$data.isPwned = isPwned
})
return (this.$data.username && this.$data.password && !this.$data.isPwned) ? false : true
async isPwnedPassword(password) {
const isPwned = await pwned(password);
return isPwned;
},
async isDisabled() {
const { username, password } = this.$data;
const isPwned = await this.isPwnedPassword(password);
return !(username && password && !isPwned);
}
},
methods: {
Expand Down
2 changes: 1 addition & 1 deletion pils-frontend/src/components/Storemapping.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
type="button" :disabled="isEmpty">Save</span>
</div>
<table class='container table'>
<caption>Table of store names</caption>
<thead>
<th>Old</th>
<th>New</th>
Expand All @@ -17,7 +18,6 @@
<tr v-for='(newName, oldName) in stores'>
<th><input class="input" type="text" v-model="oldName" disabled></th>
<th><input class="input" type="text" :placeholder="newName" v-model="newStores[oldName]"></th>
<!-- <span class="delete" @click='Delete(oldName)'></span> -->
</tr>
<tr>
<th></th>
Expand Down
1 change: 1 addition & 0 deletions pils-frontend/src/components/Users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{{error}}
</div>
<table class='container table'>
<caption>Table of users</caption>
<thead>
<th>Username</th>
<th>Admin</th>
Expand Down
2 changes: 1 addition & 1 deletion pils-frontend/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ new Vue({
template: '<App/>'
})

var Vue2FiltersConfig = {
const Vue2FiltersConfig = {
capitalize: {
onlyFirstLetter: true
},
Expand Down
4 changes: 2 additions & 2 deletions pils/api/v1/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ router.post('/login', function (req, res) {
if (error || !user) {
writeLog(`Failed login attempt for user: ${req.body.username}`, 'Warning', context, req.realIp)
res.status(401).send('Incorrect username or password')
} else if (user.otp && user.otp.status && !req.body.token) {
} else if (user.otp?.status && !req.body.token) {
writeLog(`User ${user.username}: ${user._id} requires a 2fa token.`, 'Info', context, req.realIp)
return res.json({ otp: true })
} else {
if (user.otp && user.otp.status && !otp.check(req.body.token, user.otp.secret)) {
if (user.otp?.status && !otp.check(req.body.token, user.otp.secret)) {
return res.status(401).send('The 2FA code is only valid for 30 seconds, try again.')
}
writeLog(`User ${user.username}: ${user._id} has logged in.`, 'Info', context, req.realIp)
Expand Down
2 changes: 1 addition & 1 deletion pils/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const UserSchema = new mongoose.Schema({
})

UserSchema.statics.authenticate = function (username, password, callback) {
User.findOne({ username: username })
User.findOne({ username })
.exec(function (err, user) {
if (err) {
return callback(err)
Expand Down
2 changes: 1 addition & 1 deletion pils/services/isAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const devmode = process.env.DEVMODE || false

const isAdmin = (req, res, next) => {
if (!devmode) {
if (req.session && req.session.userId) {
if (req.session?.userId) {
if (!req.session.admin) {
res.status(403).send('You are no admin!')
} else {
Expand Down

0 comments on commit 5d7a354

Please sign in to comment.