Skip to content

Commit

Permalink
Fix env boolean comparisons, add esl unlink modal
Browse files Browse the repository at this point in the history
  • Loading branch information
megastary committed Oct 6, 2024
1 parent 8aa2952 commit b9da27a
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 28 deletions.
3 changes: 2 additions & 1 deletion functions/azure-passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ passport.use(
responseMode: process.env.CREDS_RESPONSE_MODE,
redirectUrl: process.env.CREDS_REDIRECT_URL,
allowHttpForRedirectUrl:
process.env.CREDS_ALLOW_HTTP_FOR_REDIRECT_URL === 'true' || false,
process.env.CREDS_ALLOW_HTTP_FOR_REDIRECT_URL.toLowerCase() ===
'true' || false,
clientSecret: process.env.CREDS_CLIENT_SECRET,
validateIssuer:
process.env.CREDS_VALIDATE_ISSUER.toLowerCase() === 'true' || false,
Expand Down
6 changes: 3 additions & 3 deletions functions/sendMail.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { createTransport } from 'nodemailer'
import hbs from 'nodemailer-express-handlebars'
import logger from './logger.js'

export function sendMail (mailto, template, context, image) {
export function sendMail(mailto, template, context, image) {
// In case mail is destined for system administrator or we run in development environment, send all e-mails to system address obtained from config.
if (
mailto === 'system@system' ||
(process.env.NODE_ENV === 'development' &&
process.env.MAIL_DEV_SYSTEM === 'true')
(process.env.NODE_ENV.toLowerCase() === 'development' &&
process.env.MAIL_DEV_SYSTEM.toLowerCase() === 'true')
) {
logger.warn(
`server.functions.sendmail__Sending e-mail [${context.subject}] to system administrator.`,
Expand Down
78 changes: 78 additions & 0 deletions public/javascripts/esl_mapping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*global bootstrap, $*/

const myModal = new bootstrap.Modal('#confirm-modal')

// Change settings when checkbox state changes
const checkboxes = document.getElementsByClassName('realtime-checkbox')

// eslint-disable-next-line no-unused-vars
const _checkboxesEvent = [...checkboxes].forEach((element) => {
element.addEventListener('change', async function () {
await fetch('/admin_users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
user: this.dataset.user,
name: this.dataset.property,
value: this.checked,
_csrf: document.getElementsByName('_csrf')[0].value
})
})
})
})

// Display confirmation dialog
// eslint-disable-next-line no-unused-vars
async function showUnlinkConfirm(id) {
const submit = document.getElementById('modal_confirm')
submit.dataset.submit_id = 'unlink_' + id
myModal.show()
}

// Submit form from modal to confirm deactivation
// eslint-disable-next-line no-unused-vars
function submitFromModal(ctx) {
document.getElementById(ctx.dataset.submit_id).submit()
}

// Initialize DataTables
// dom customizes header paging, search and export
// columndefs makes id unsortable, sortabledate hidden, date linked for filtering and displays currency next to price
// sort by date by default
document.addEventListener('DOMContentLoaded', function () {
$('#table-labels').DataTable({
dom:
"<'row'<'col-sm-12 col-md-4'l><'col-sm-12 col-md-4'f><'col-sm-12 col-md-4'B>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>",
buttons: [
{
extend: 'print',
exportOptions: {
columns: [0, 1, 5, 7]
}
},
'copyHtml5',
'excelHtml5',
'csvHtml5',
{
extend: 'pdfHtml5',
exportOptions: {
columns: [0, 1, 5, 7]
}
}
],
language: {
url: '/datatables/cs.json',
searchPlaceholder: 'Hledaný výraz'
},
lengthMenu: [
[10, 25, 50, -1],
[10, 25, 50, 'Vše']
],
order: [[0, 'asc']],
stateSave: false
})
})
31 changes: 19 additions & 12 deletions routes/esl_mapping.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Router } from 'express'
import moment from 'moment'
import csrf from 'csurf'
import { fetch, setGlobalDispatcher, Agent } from 'undici'
import Product from '../models/product.js'
Expand All @@ -8,6 +9,7 @@ import logger from '../functions/logger.js'
const router = Router()
const csrfProtection = csrf()
router.use(csrfProtection)
moment.locale('cs')

/* GET esl_mapping page. */
router.get('/', ensureAuthenticated, checkKiosk, function (req, res) {
Expand All @@ -24,10 +26,23 @@ router.get('/', ensureAuthenticated, checkKiosk, function (req, res) {
return
}

if (process.env.ESL_AIMS_ENABLED.toLowerCase() !== 'true') {
const alert = {
type: 'danger',
component: 'esl',
message: 'ESL cenovky nejsou v nastavení povolené.',
danger: 1
}
req.session.alert = alert
res.redirect('/')
return
}

setGlobalDispatcher(
new Agent({
connect: {
rejectUnauthorized: false
rejectUnauthorized:
process.env.ESL_AIMS_VERIFY_TLS.toLocaleLowerCase() === 'true'
}
})
)
Expand Down Expand Up @@ -62,24 +77,16 @@ router.get('/', ensureAuthenticated, checkKiosk, function (req, res) {
}
)
.then((response) => {
// const labels = response. {
// labelCode: response,
// type: ,
// networkStatus: ,
// updateStatus: ,
// temperature: ,
// requestDate: ,
// articleId: ,
// articleName:
// }
response.json().then((json) => {
const labels = json.labelList.map((item) => ({
labelCode: item.labelCode,
type: item.type,
networkStatus: item.networkStatus,
updateStatus: item.updateStatus,
temperature: item.temperature,
requestDate: item.requestDate,
requestDate: item.requestDate
? moment(item.requestDate).format('LLLL')
: null,
articleId: item.articleList[0]?.articleId,
articleName: item.articleList[0]?.articleName
}))
Expand Down
7 changes: 4 additions & 3 deletions tasks/continuous-products-aims-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ moment.locale('cs')
setGlobalDispatcher(
new Agent({
connect: {
rejectUnauthorized: false
rejectUnauthorized:
process.env.ESL_AIMS_VERIFY_TLS.toLocaleLowerCase() === 'true'
}
})
)
Expand All @@ -17,7 +18,7 @@ const scheduledTask = scheduleJob(
process.env.ESL_AIMS_INTEGRATION_CRON,
function () {
// This schedule can be disabled in the ENV
if (!process.env.ESL_AIMS_ENABLED) {
if (process.env.ESL_AIMS_ENABLED.toLowerCase() !== 'true') {
return
}

Expand Down Expand Up @@ -147,7 +148,7 @@ const scheduledTask = scheduleJob(
)
.then((msg) => {
logger.info(
`server.tasks.continuousproductsaimsintegration__Uploaded all_articles.length products to AIMS.`,
`server.tasks.continuousproductsaimsintegration__Uploaded ${all_articles.length} products to AIMS.`,
{
metadata: {
products_count: all_articles.length,
Expand Down
2 changes: 1 addition & 1 deletion tasks/daily-backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ rule.minute = process.env.TASKS_DAILY_BACKUP_MINUTE

const dailyBackup = scheduleJob(rule, function () {
// This schedule can be disabled in the ENV
if (!process.env.TASKS_DAILY_BACKUP_ENABLED) {
if (process.env.TASKS_DAILY_BACKUP_ENABLED.toLowerCase() !== 'true') {
return
}

Expand Down
4 changes: 3 additions & 1 deletion tasks/daily-paid-invoices-needs-approval.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ rule.minute = process.env.TASKS_DAILY_INCOMPLETE_INVOICE_MINUTE

const scheduledTask = scheduleJob(rule, function () {
// This schedule can be disabled in the ENV
if (!process.env.TASKS_DAILY_INCOMPLETE_INVOICE_ENABLED) {
if (
process.env.TASKS_DAILY_INCOMPLETE_INVOICE_ENABLED.toLowerCase() !== 'true'
) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion tasks/daily-purchase-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rule.minute = process.env.TASKS_DAILY_REPORT_SEND_MINUTE || 30

const scheduledTask = scheduleJob(rule, function () {
// This schedule can be disabled in the ENV
if (!process.env.TASKS_DAILY_REPORT_ENABLED) {
if (process.env.TASKS_DAILY_REPORT_ENABLED.toLowerCase() !== 'true') {
return
}

Expand Down
4 changes: 3 additions & 1 deletion tasks/daily-unpaid-invoices-notice.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ rule.minute = process.env.TASKS_DAILY_INCOMPLETE_INVOICE_MINUTE

const scheduledTask = scheduleJob(rule, function () {
// This schedule can be disabled in the ENV
if (!process.env.TASKS_DAILY_INCOMPLETE_INVOICE_ENABLED) {
if (
process.env.TASKS_DAILY_INCOMPLETE_INVOICE_ENABLED.toLowerCase() !== 'true'
) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion tasks/daily-user-phones.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rule.minute = process.env.TASKS_DAILY_USER_PHONES_MINUTE

const scheduledTask = scheduleJob(rule, async function () {
// This schedule can be disabled in the ENV
if (!process.env.TASKS_DAILY_USER_PHONES_ENABLED) {
if (process.env.TASKS_DAILY_USER_PHONES_ENABLED.toLowerCase() !== 'true') {
return
}

Expand Down
32 changes: 32 additions & 0 deletions views/partials/confirm_unlink_label.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class='modal' id='confirm-modal' tabindex='-1'>
<div class='modal-dialog modal-dialog-centered'>
<div class='modal-content'>
<div class='modal-header'>
<h5 class='modal-title'>Opravdu odstranit produkt z cenovky?</h5>
<button
type='button'
class='btn-close'
data-bs-dismiss='modal'
aria-label='Close'
></button>
</div>
<div class='modal-body mx-auto'>
<p>Cenovka nebude zobrazovat žádný produkt.</p>
</div>
<div class='modal-footer'>
<button
type='button'
class='btn btn-secondary'
data-bs-dismiss='modal'
>Zavřít</button>
<button
id='modal_confirm'
type='button'
class='btn btn-success'
data-bs-dismiss='modal'
onclick='submitFromModal(this)'
>Odstranit</button>
</div>
</div>
</div>
</div>
8 changes: 4 additions & 4 deletions views/shop/esl_mapping.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{> confirm_deactivate_modal }}
{{> confirm_unlink_label }}
<div class="row my-3">
<div class="col">
<div class="table-responsive">
Expand Down Expand Up @@ -44,10 +44,10 @@
{{ this.articleName }}
</td>
<td>
<form id="{{ this.id }}" class="needs-validation h-100" method="POST" action="/esl_mapping" novalidate>
<form id="unlink_{{ this._id }}" class="needs-validation h-100" method="POST" action="/esl_mapping" novalidate>
<div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
<div class="btn-group me-2 w-100" role="group" aria-label="First group">
<button type="button" class="btn btn-primary" onclick="void()"><i class="fa-solid fa-xmark"></i></button>
<button type="button" class="btn btn-danger" onclick="showUnlinkConfirm('{{ this._id }}')"><i class="fa-solid fa-xmark"></i></button>
<input type="hidden" name="action" value="storno">
<input type="submit" class="btn btn-success float-right w-100" value="Vybrat produkt">
</div>
Expand All @@ -66,4 +66,4 @@

{{> datatables }}

<script src="/javascripts/admin_users.js"></script>
<script src="/javascripts/esl_mapping.js"></script>

0 comments on commit b9da27a

Please sign in to comment.