Skip to content

Commit 5ddfa21

Browse files
authored
feat: converted moment to dayjs (#405)
1 parent bb24abf commit 5ddfa21

File tree

12 files changed

+402
-410
lines changed

12 files changed

+402
-410
lines changed

template/app/controllers/web/auth.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const Boom = require('@hapi/boom');
22
const _ = require('lodash');
33
const cryptoRandomString = require('crypto-random-string');
44
const isSANB = require('is-string-and-not-blank');
5-
const moment = require('moment');
5+
const dayjs = require('dayjs-with-plugins');
66
const qrcode = require('qrcode');
77
const sanitizeHtml = require('sanitize-html');
88
const validator = require('validator');
@@ -151,9 +151,9 @@ async function login(ctx, next) {
151151
}
152152

153153
let greeting = 'Good morning';
154-
if (moment().format('HH') >= 12 && moment().format('HH') <= 17)
154+
if (dayjs().format('HH') >= 12 && dayjs().format('HH') <= 17)
155155
greeting = 'Good afternoon';
156-
else if (moment().format('HH') >= 17) greeting = 'Good evening';
156+
else if (dayjs().format('HH') >= 17) greeting = 'Good evening';
157157

158158
if (user) {
159159
await ctx.login(user);
@@ -365,19 +365,19 @@ async function forgotPassword(ctx) {
365365
if (
366366
user[config.userFields.resetTokenExpiresAt] &&
367367
user[config.userFields.resetToken] &&
368-
moment(user[config.userFields.resetTokenExpiresAt]).isAfter(
369-
moment().subtract(30, 'minutes')
368+
dayjs(user[config.userFields.resetTokenExpiresAt]).isAfter(
369+
dayjs().subtract(30, 'minutes')
370370
)
371371
)
372372
throw Boom.badRequest(
373373
ctx.translateError(
374374
'PASSWORD_RESET_LIMIT',
375-
moment(user[config.userFields.resetTokenExpiresAt]).fromNow()
375+
dayjs(user[config.userFields.resetTokenExpiresAt]).fromNow()
376376
)
377377
);
378378

379379
// set the reset token and expiry
380-
user[config.userFields.resetTokenExpiresAt] = moment()
380+
user[config.userFields.resetTokenExpiresAt] = dayjs()
381381
.add(30, 'minutes')
382382
.toDate();
383383
user[config.userFields.resetToken] = cryptoRandomString({ length: 32 });

template/app/controllers/web/support.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const sanitize = require('sanitize-html');
2-
const moment = require('moment');
2+
const dayjs = require('dayjs');
33
const isSANB = require('is-string-and-not-blank');
44
const Boom = require('@hapi/boom');
55
const _ = require('lodash');
@@ -50,7 +50,7 @@ async function support(ctx) {
5050
}
5151
],
5252
created_at: {
53-
$gte: moment()
53+
$gte: dayjs()
5454
.subtract(1, 'day')
5555
.toDate()
5656
}

template/app/models/user.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const _ = require('lodash');
33
const captainHook = require('captain-hook');
44
const cryptoRandomString = require('crypto-random-string');
55
const isSANB = require('is-string-and-not-blank');
6-
const moment = require('moment');
6+
const dayjs = require('dayjs');
7+
const updateLocale = require('dayjs/plugin/updateLocale');
78
const mongoose = require('mongoose');
89
const mongooseCommonPlugin = require('mongoose-common-plugin');
910
const mongooseOmitCommonFields = require('mongoose-omit-common-fields');
@@ -44,7 +45,7 @@ const omitExtraFields = [
4445
];
4546

4647
// set relative threshold for messages
47-
moment.relativeTimeThreshold('ss', 5);
48+
dayjs.extend(updateLocale, { thresholds: [{ l: 'ss', r: 5 }] });
4849

4950
const User = new mongoose.Schema({
5051
// group permissions
@@ -259,7 +260,7 @@ User.methods.sendVerificationEmail = async function(ctx) {
259260
phrase: config.i18n.phrases.EMAIL_VERIFICATION_INTERVAL,
260261
locale: this[config.lastLocaleField]
261262
},
262-
moment
263+
dayjs
263264
.duration(config.verificationPinEmailIntervalMs - diff)
264265
.locale(this[config.lastLocaleField])
265266
.humanize()

template/app/views/admin/index.pug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ block body
77
.col
88
include ../_breadcrumbs
99
p.lead
10-
if (moment().format('HH') >= 12 && moment().format('HH') <= 17)
10+
if (dayjs().format('HH') >= 12 && dayjs().format('HH') <= 17)
1111
= t('Good afternoon')
12-
else if (moment().format('HH') >= 17)
12+
else if (dayjs().format('HH') >= 17)
1313
= t('Good evening')
1414
else
1515
= t('Good morning')

template/app/views/admin/users/index.pug

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ block body
3232
td.align-middle= user[config.passport.fields.familyName]
3333
td.align-middle: a(href=`mailto:${user.email}`, target='_blank')= user.email
3434
td.align-middle= titleize(user.group)
35-
td.align-middle= moment(user.created_at).format('M/D/YY h:mm A')
36-
td.align-middle= moment(user.updated_at).format('M/D/YY h:mm A')
37-
td.align-middle= moment(user.last_login_at).format('M/D/YY h:mm A')
35+
td.align-middle= dayjs(user.created_at).format('M/D/YY h:mm A')
36+
td.align-middle= dayjs(user.updated_at).format('M/D/YY h:mm A')
37+
td.align-middle= dayjs(user.last_login_at).format('M/D/YY h:mm A')
3838
td.align-middle: code= user[config.storeIPAddress.ip]
3939
td.align-middle: code= user[config.lastLocaleField]
4040
if boolean(process.env.AUTH_OTP_ENABLED)

template/app/views/dashboard/index.pug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ block body
77
.col
88
include ../_breadcrumbs
99
p.lead
10-
if (moment().format('HH') >= 12 && moment().format('HH') <= 17)
10+
if (dayjs().format('HH') >= 12 && dayjs().format('HH') <= 17)
1111
= t('Good afternoon')
12-
else if (moment().format('HH') >= 17)
12+
else if (dayjs().format('HH') >= 17)
1313
= t('Good evening')
1414
else
1515
= t('Good morning')

template/config/utilities.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const hljs = require('highlight.js');
66
const humanize = require('humanize-string');
77
const isBot = require('isbot');
88
const isSANB = require('is-string-and-not-blank');
9-
const moment = require('moment');
9+
const dayjs = require('dayjs');
1010
const numeral = require('numeral');
1111
const pluralize = require('pluralize');
1212
const reservedEmailAddressesList = require('reserved-email-addresses-list');
@@ -33,7 +33,7 @@ module.exports = {
3333
isBot,
3434
isSANB,
3535
json,
36-
moment,
36+
dayjs,
3737
numeral,
3838
pluralize,
3939
reservedEmailAddressesList,

template/emails/inquiry/html.pug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ block content
1313
= t('Thank you for submitting a support request on our website. We will get back to you soon!')
1414
else
1515
= t('Below is a copy of your request submitted on ')
16-
= moment(inquiry.created_at).format('MM/DD/YY')
16+
= dayjs(inquiry.created_at).format('MM/DD/YY')
1717
= t(' at ')
18-
= moment(inquiry.created_at).format('h:mm A')
18+
= dayjs(inquiry.created_at).format('h:mm A')
1919
= '.'
2020
hr
2121
p: pre: code= inquiry.message

template/emails/reset-password/html.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ block content
1010
.card-body
1111
.card-text
1212
p= `${user[config.passport.fields.givenName] ? user[config.passport.fields.givenName] : t('Hello')},`
13-
p= t(`Click the button below within ${moment(user[config.userFields.resetTokenExpiresAt]).fromNow(true)} to continue.`)
13+
p= t(`Click the button below within ${dayjs(user[config.userFields.resetTokenExpiresAt]).fromNow(true)} to continue.`)
1414
a.btn.btn-lg.btn-block.btn-success(href=link, role="button")= t('Change your password')
1515
.card-footer: small.text-muted= t('If you did not submit this request, then please reply to let us know.')

template/emails/verify/html.pug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ block content
1111
.card-text
1212
p= t('Use this verification pin:')
1313
pre: code.display-3= pin
14-
p= t(`This pin expires within ${moment(expiresAt).fromNow(true)}.`)
14+
p= t(`This pin expires within ${dayjs(expiresAt).fromNow(true)}.`)
1515
a.btn.btn-lg.btn-block.btn-success(href=link, role="button")= t('Verify now')
1616
.card-footer: small.text-muted= t('If you did not submit this request, then please reply to let us know.')

0 commit comments

Comments
 (0)