From 973040f40976b0f1d3b32d6599ce6d618c15c828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Tue, 23 Nov 2021 16:09:15 +0000 Subject: [PATCH] feat: remove all instances of moment usage --- lib/admin/server.js | 10 +- lib/customers.js | 14 +-- lib/logs.js | 8 +- new-lamassu-admin/package-lock.json | 5 - new-lamassu-admin/package.json | 1 - .../src/components/LogsDownloaderPopper.js | 19 ++-- .../components/date-range-picker/Calendar.js | 91 ++++++++++--------- .../date-range-picker/DateRangePicker.js | 23 +++-- .../src/pages/Accounting/Accounting.js | 30 ++++-- .../components/wrappers/HourOfDayWrapper.js | 6 +- .../Analytics/graphs/HourOfDayBarGraph.js | 12 +-- .../Analytics/graphs/OverTimeDotGraph.js | 11 ++- .../src/pages/Customers/CustomerData.js | 17 ++-- .../src/pages/Customers/CustomersList.js | 5 +- .../pages/Customers/components/IdDataCard.js | 9 +- .../Customers/components/TransactionsList.js | 4 +- .../Graphs/RefScatterplot.js | 18 ++-- .../SystemPerformance/SystemPerformance.js | 31 +++---- new-lamassu-admin/src/pages/Funding.js | 10 +- .../Machines/MachineComponents/Details.js | 2 +- .../Machines/MachineComponents/Overview.js | 5 +- .../Transactions/Transactions.js | 19 +++- .../src/pages/Maintenance/CashboxHistory.js | 21 ++++- .../src/pages/Maintenance/MachineStatus.js | 7 +- new-lamassu-admin/src/pages/ServerLogs.js | 2 +- .../SessionManagement/SessionManagement.js | 36 ++++++-- .../src/pages/Transactions/DetailsCard.js | 15 ++- new-lamassu-admin/src/utils/timezones.js | 13 ++- package.json | 1 - 29 files changed, 252 insertions(+), 193 deletions(-) diff --git a/lib/admin/server.js b/lib/admin/server.js index 0aa93706..f31cf4fd 100644 --- a/lib/admin/server.js +++ b/lib/admin/server.js @@ -1,11 +1,11 @@ const _ = require('lodash/fp') -const moment = require('moment') const ticker = require('../ticker') const settingsLoader = require('./settings-loader') const db = require('../db') const machineLoader = require('../machine-loader') +const { intervalToDuration, secondsToMilliseconds, formatDuration } = require('date-fns') const CONSIDERED_UP_SECS = 30 @@ -37,8 +37,8 @@ function machinesLastPing () { if (downRows.length === 1) { const row = downRows[0] - const age = moment.duration(row.age, 'seconds') - return `${row.name} down for ${age.humanize()}` + const age = intervalToDuration({ start: 0, end: secondsToMilliseconds(row.age) }) + return `${row.name} down for ${formatDuration(age)}` } return 'Multiple machines down' @@ -54,9 +54,9 @@ function status () { return Promise.all([checkWasConfigured(), db.oneOrNone(sql, ['ping']), machinesLastPing()]) .then(([wasConfigured, statusRow, machineStatus]) => { - const age = statusRow && moment.duration(statusRow.age, 'seconds') + const age = statusRow && intervalToDuration({ start: 0, end: secondsToMilliseconds(statusRow.age) }) const up = statusRow ? statusRow.age < CONSIDERED_UP_SECS : false - const lastPing = statusRow && age.humanize() + const lastPing = statusRow && formatDuration(age) return settingsLoader.loadLatest() .catch(() => null) diff --git a/lib/customers.js b/lib/customers.js index c7aa00e3..478f6f7c 100644 --- a/lib/customers.js +++ b/lib/customers.js @@ -6,7 +6,6 @@ const makeDir = require('make-dir') const path = require('path') const fs = require('fs') const util = require('util') -const moment = require('moment') const db = require('./db') const BN = require('./bn') @@ -24,6 +23,7 @@ const operatorDataDir = _.get('operatorDataDir', options) const sms = require('./sms') const settingsLoader = require('./new-settings-loader') const logger = require('./logger') +const { sub, differenceInHours } = require('date-fns') const TX_PASSTHROUGH_ERROR_CODES = ['operatorCancel'] @@ -215,14 +215,14 @@ function getDailyVolumeMinusCurrentTxQueries (id, txId) { } function getHoursTillLimitClear (cashInDate, cashOutDate) { - let startDate = moment() - startDate = startDate.subtract(1, 'days') + let startDate = new Date() + startDate = sub(startDate, { days: 1 }) - const cashInMoment = moment(cashInDate || startDate) - const cashOutMoment = moment(cashOutDate || startDate) + const cashInMoment = new Date(cashInDate || startDate) + const cashOutMoment = new Date(cashOutDate || startDate) - const cashInDuration = moment.duration(cashInMoment.diff(startDate)).asHours() - const cashOutDuration = moment.duration(cashOutMoment.diff(startDate)).asHours() + const cashInDuration = differenceInHours(startDate, cashInMoment) + const cashOutDuration = differenceInHours(startDate, cashOutMoment) return _.ceil(_.max([cashInDuration, cashOutDuration, 0])) } diff --git a/lib/logs.js b/lib/logs.js index 44f7c793..6b6ea61f 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -113,15 +113,17 @@ function simpleGetMachineLogs (deviceId, from = new Date(0).toISOString(), until } function logDateFormat (timezone, logs, fields) { - return _.map(log => { const values = _.map( field => - format(utcToZonedTime(zonedTimeToUtc(log[field], process.env.TZ), timezone), 'yyyy-MM-ddTHH:mm:ss.SSS'), + { + if (_.isNil(log[field])) return null + const date = utcToZonedTime(log[field], timezone) + return `${format(date, 'yyyy-MM-dd')}T${format(date, 'HH:mm:ss.SSS')}` + }, fields ) const fieldsToOverride = _.zipObject(fields, values) - return { ...log, ...fieldsToOverride diff --git a/new-lamassu-admin/package-lock.json b/new-lamassu-admin/package-lock.json index ebbb2730..161c8a4e 100644 --- a/new-lamassu-admin/package-lock.json +++ b/new-lamassu-admin/package-lock.json @@ -18326,11 +18326,6 @@ "minimist": "^1.2.5" } }, - "moment": { - "version": "2.24.0", - "resolved": "https://registry.npmjs.org/moment/-/moment-2.24.0.tgz", - "integrity": "sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg==" - }, "move-concurrently": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/move-concurrently/-/move-concurrently-1.0.1.tgz", diff --git a/new-lamassu-admin/package.json b/new-lamassu-admin/package.json index b807b5c8..bb59ee3d 100644 --- a/new-lamassu-admin/package.json +++ b/new-lamassu-admin/package.json @@ -31,7 +31,6 @@ "lamassu-coins": "git+https://github.com/lamassu/lamassu-coins.git", "libphonenumber-js": "^1.7.50", "match-sorter": "^4.2.0", - "moment": "2.24.0", "pretty-ms": "^2.1.0", "qrcode.react": "0.9.3", "ramda": "^0.26.1", diff --git a/new-lamassu-admin/src/components/LogsDownloaderPopper.js b/new-lamassu-admin/src/components/LogsDownloaderPopper.js index 9d64082f..7ff6a16c 100644 --- a/new-lamassu-admin/src/components/LogsDownloaderPopper.js +++ b/new-lamassu-admin/src/components/LogsDownloaderPopper.js @@ -1,8 +1,8 @@ import { useLazyQuery } from '@apollo/react-hooks' import { makeStyles, ClickAwayListener } from '@material-ui/core' import classnames from 'classnames' +import { format, isSameDay } from 'date-fns' import FileSaver from 'file-saver' -import moment from 'moment' import * as R from 'ramda' import React, { useState, useCallback } from 'react' @@ -65,12 +65,13 @@ const DateContainer = ({ date, children, ...props }) => { {date && ( <>
-
{date.format('D')}
+
{format(date, 'd')}
- {`${date.format( + {`${format( + date, 'MMM' - )} ${date.format('YYYY')}`} - {date.format('dddd')} + )} ${format(date, 'yyyy')}`} + {format(date, 'EEEE')}
@@ -186,8 +187,8 @@ const LogsDownloaderPopover = ({ } if (!range || !range.from) return - if (range.from && !range.until) range.until = moment() - if (moment(range.from).isSame(range.until, 'day')) range.until = moment() + if (range.from && !range.until) range.until = new Date() + if (isSameDay(range.from, range.until)) range.until = new Date() if (selectedRadio === RANGE) { fetchLogs({ @@ -203,7 +204,7 @@ const LogsDownloaderPopover = ({ const createLogsFile = (logs, range) => { const formatDateFile = date => { - return formatDate(date, timezone, 'YYYY-MM-DD_HH-mm') + return formatDate(date, timezone, 'yyyy-MM-dd_HH-mm') } const blob = new window.Blob([logs], { @@ -277,7 +278,7 @@ const LogsDownloaderPopover = ({ )} diff --git a/new-lamassu-admin/src/components/date-range-picker/Calendar.js b/new-lamassu-admin/src/components/date-range-picker/Calendar.js index f93df5dd..7e0661cb 100644 --- a/new-lamassu-admin/src/components/date-range-picker/Calendar.js +++ b/new-lamassu-admin/src/components/date-range-picker/Calendar.js @@ -1,5 +1,18 @@ import { makeStyles } from '@material-ui/core/styles' -import moment from 'moment' +import { + add, + differenceInMonths, + format, + getDay, + getDaysInMonth, + isAfter, + isSameDay, + isSameMonth, + lastDayOfMonth, + startOfMonth, + startOfWeek, + sub +} from 'date-fns' import * as R from 'ramda' import React, { useState } from 'react' @@ -72,49 +85,36 @@ const styles = { const useStyles = makeStyles(styles) const Calendar = ({ minDate, maxDate, handleSelect, ...props }) => { - const [currentDisplayedMonth, setCurrentDisplayedMonth] = useState(moment()) + const [currentDisplayedMonth, setCurrentDisplayedMonth] = useState(new Date()) const classes = useStyles() - const weekdays = moment.weekdaysMin().map(day => day.slice(0, 1)) - const monthLength = month => - Number.parseInt( - moment(month) - .endOf('month') - .format('D') - ) + const weekdays = Array.from(Array(7)).map((_, i) => + format(add(startOfWeek(new Date()), { days: i }), 'EEEEE') + ) + + const monthLength = month => getDaysInMonth(month) const monthdays = month => { - const lastMonth = moment(month).subtract(1, 'month') - const lastMonthRange = R.range( - 0, - moment(month) - .startOf('month') - .weekday() - ).reverse() + const lastMonth = sub(month, { months: 1 }) + const lastMonthRange = R.range(0, getDay(startOfMonth(month))).reverse() const lastMonthDays = R.map(i => - moment(lastMonth) - .endOf('month') - .subtract(i, 'days') + sub(lastDayOfMonth(lastMonth), { days: i }) )(lastMonthRange) const thisMonthRange = R.range(0, monthLength(month)) - const thisMonthDays = R.map(i => - moment(month) - .startOf('month') - .add(i, 'days') - )(thisMonthRange) + const thisMonthDays = R.map(i => add(startOfMonth(month), { days: i }))( + thisMonthRange + ) - const nextMonth = moment(month).add(1, 'month') + const nextMonth = add(month, { months: 1 }) const nextMonthRange = R.range( 0, 42 - lastMonthDays.length - thisMonthDays.length ) - const nextMonthDays = R.map(i => - moment(nextMonth) - .startOf('month') - .add(i, 'days') - )(nextMonthRange) + const nextMonthDays = R.map(i => add(startOfMonth(nextMonth), { days: i }))( + nextMonthRange + ) return R.concat(R.concat(lastMonthDays, thisMonthDays), nextMonthDays) } @@ -122,22 +122,24 @@ const Calendar = ({ minDate, maxDate, handleSelect, ...props }) => { const getRow = (month, row) => monthdays(month).slice(row * 7 - 7, row * 7) const handleNavPrev = currentMonth => { - const prevMonth = moment(currentMonth).subtract(1, 'month') + const prevMonth = sub(currentMonth, { months: 1 }) if (!minDate) setCurrentDisplayedMonth(prevMonth) else { setCurrentDisplayedMonth( - prevMonth.isSameOrAfter(minDate, 'month') + isSameMonth(prevMonth, minDate) || + differenceInMonths(prevMonth, minDate) > 0 ? prevMonth : currentDisplayedMonth ) } } const handleNavNext = currentMonth => { - const nextMonth = moment(currentMonth).add(1, 'month') + const nextMonth = add(currentMonth, { months: 1 }) if (!maxDate) setCurrentDisplayedMonth(nextMonth) else { setCurrentDisplayedMonth( - nextMonth.isSameOrBefore(maxDate, 'month') + isSameMonth(nextMonth, maxDate) || + differenceInMonths(maxDate, nextMonth) > 0 ? nextMonth : currentDisplayedMonth ) @@ -153,9 +155,10 @@ const Calendar = ({ minDate, maxDate, handleSelect, ...props }) => { - {`${currentDisplayedMonth.format( - 'MMMM' - )} ${currentDisplayedMonth.format('YYYY')}`} + {`${format(currentDisplayedMonth, 'MMMM')} ${format( + currentDisplayedMonth, + 'yyyy' + )}`}