From 31e87b081b79902d91e600c436816918e095bec0 Mon Sep 17 00:00:00 2001 From: Hiren F <146440442+hirenf14@users.noreply.github.com> Date: Mon, 11 Mar 2024 16:10:13 +0530 Subject: [PATCH] refactor: replaced moment with luxon (#233) --- apps/api/src/entities/user.entity.ts | 8 +++++-- apps/api/src/user/user.service.ts | 9 ++++---- package.json | 1 - yarn.lock | 34 ++++++++++++++++++++++++---- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/apps/api/src/entities/user.entity.ts b/apps/api/src/entities/user.entity.ts index e91d28c..306dac6 100644 --- a/apps/api/src/entities/user.entity.ts +++ b/apps/api/src/entities/user.entity.ts @@ -11,7 +11,7 @@ import { import { UserInterface } from '@isomera/interfaces' import bcrypt from 'bcryptjs' import { ConfirmCodeEntity } from './confirm-code.entity' -import moment from 'moment' +import { DateTime } from 'luxon' @Entity({ name: 'users' }) export class UserEntity implements UserInterface { @@ -76,6 +76,10 @@ export class UserEntity implements UserInterface { } public isValidResetCodeTime() { - return moment().isSameOrBefore(this.passwordResetExpiredTime) + const expiresAt = DateTime.fromFormat( + this.passwordResetExpiredTime, + 'yyyy-MM-dd HH:mm:ss' + ) + return DateTime.now() <= expiresAt } } diff --git a/apps/api/src/user/user.service.ts b/apps/api/src/user/user.service.ts index 629a0ee..e6e1bc6 100644 --- a/apps/api/src/user/user.service.ts +++ b/apps/api/src/user/user.service.ts @@ -5,7 +5,7 @@ import { Repository, FindOneOptions } from 'typeorm' import { UserUpdate } from './dto/user-update.dto' import { UserEntity } from '../entities/user.entity' import { ConfigService } from '@nestjs/config' -import moment from 'moment' +import { DateTime } from 'luxon' @Injectable() export class UserService { @@ -58,10 +58,9 @@ export class UserService { const resetPasswordPeriod = this.configService.get( 'RESET_PASSWORD_PERIOD' ) - const expiredTime = moment() - .clone() - .add(resetPasswordPeriod, 'minutes') - .format('YYYY-MM-DD HH:mm:ss') + const expiredTime = DateTime.now() + .plus({ minute: resetPasswordPeriod }) + .toFormat('yyyy-MM-dd HH:mm:ss') user.passwordResetCode = passwordResetCode user.passwordResetExpiredTime = expiredTime diff --git a/package.json b/package.json index dcf7af2..30173d0 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,6 @@ "formik": "^2.4.5", "helmet": "^7.1.0", "luxon": "^3.4.3", - "moment": "^2.29.4", "next": "14.0.0", "nodemailer": "^6.9.9", "otplib": "^12.0.1", diff --git a/yarn.lock b/yarn.lock index 73c51f0..fb8124d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13132,7 +13132,7 @@ mkdirp@^2.1.3, mkdirp@^2.1.6: resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-2.1.6.tgz#964fbcb12b2d8c5d6fbc62a963ac95a273e2cc19" integrity sha512-+hEnITedc8LAtIP9u3HJDFIdcLV2vXP33sqLLIzkv1Db1zO/1OxbvYf0Y1OC/S/Qo5dxHXepofhmxL02PsKe+A== -moment@^2.18.1, moment@^2.19.3, moment@^2.29.4: +moment@^2.18.1, moment@^2.19.3: version "2.30.1" resolved "https://registry.yarnpkg.com/moment/-/moment-2.30.1.tgz#f8c91c07b7a786e30c59926df530b4eac96974ae" integrity sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how== @@ -16365,7 +16365,16 @@ string-length@^5.0.1: char-regex "^2.0.0" strip-ansi "^7.0.1" -"string-width-cjs@npm:string-width@^4.2.0", string-width@4.2.3, "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3, string-width@^5.0.1, string-width@^5.1.2: +"string-width-cjs@npm:string-width@^4.2.0": + version "4.2.3" + resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" + integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== + dependencies: + emoji-regex "^8.0.0" + is-fullwidth-code-point "^3.0.0" + strip-ansi "^6.0.1" + +string-width@4.2.3, "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.2, string-width@^4.2.3, string-width@^5.0.1, string-width@^5.1.2: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -16430,7 +16439,7 @@ string_decoder@~1.1.1: dependencies: safe-buffer "~5.1.0" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: +"strip-ansi-cjs@npm:strip-ansi@^6.0.1": version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -16444,6 +16453,13 @@ strip-ansi@^5.0.0, strip-ansi@^5.2.0: dependencies: ansi-regex "^4.1.0" +strip-ansi@^6.0.0, strip-ansi@^6.0.1: + version "6.0.1" + resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" + integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== + dependencies: + ansi-regex "^5.0.1" + strip-ansi@^7.0.1: version "7.1.0" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45" @@ -18023,8 +18039,16 @@ wordwrap@^1.0.0: resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb" integrity sha512-gvVzJFlPycKc5dZN4yPkP8w7Dc37BtP1yczEneOb4uq34pXZcvrtRTmWV8W+Ume+XCxKgbjM+nevkyFPMybd4Q== -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@7.0.0, wrap-ansi@^7.0.0: - name wrap-ansi-cjs +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": + version "7.0.0" + resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" + integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== + dependencies: + ansi-styles "^4.0.0" + string-width "^4.1.0" + strip-ansi "^6.0.0" + +wrap-ansi@7.0.0, wrap-ansi@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==