From b8a789fc1a48769d131ce8617198d9d3bffe05fb Mon Sep 17 00:00:00 2001 From: valentinarau Date: Thu, 7 Mar 2024 13:24:02 -0300 Subject: [PATCH] Dates handling using moment.js --- pages/api/external-samples/index.ts | 4 ++-- pages/index.tsx | 2 ++ server/notifier/appNotifierService.ts | 8 ++++---- utils/dates.ts | 4 ++++ 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pages/api/external-samples/index.ts b/pages/api/external-samples/index.ts index fe8d4c0..5d60d85 100644 --- a/pages/api/external-samples/index.ts +++ b/pages/api/external-samples/index.ts @@ -3,7 +3,7 @@ import availableMethodsHandler from "../../../utils/availableMethodsHandler"; import { prismaClient } from "../../../server/prisma/client"; import { createConfirmationTokenAWSSchema } from "../../../model/confirmationTokenAWS"; import { ZodError, z } from "zod"; -import { atStartOfHoursAgoISO } from "../../../utils/dates"; +import { atStartOfHoursAgo, atStartOfHoursAgoISO } from '../../../utils/dates'; const handler: NextApiHandler = async (req, res) => { if (!availableMethodsHandler(req, res, ['POST', 'GET'])) { @@ -47,7 +47,7 @@ const handler: NextApiHandler = async (req, res) => { samples = await prismaClient.sample.findMany({ where: { takenAt: { - gte: atStartOfHoursAgoISO(3), + gte: atStartOfHoursAgo(3), }, }, select: { diff --git a/pages/index.tsx b/pages/index.tsx index bcdf01c..619dc39 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -66,6 +66,8 @@ const getMapPosition = (coords: Coordinates | undefined): MapPosition => { }; const parseBoat = (deviceSample: any): Boat => { + console.log(moment(deviceSample.takenAt, 'YYYY-MM-DD HH:mm')) + return { id: deviceSample.deviceId, coordinates: { lat: deviceSample.latitude, lng: deviceSample.longitude }, diff --git a/server/notifier/appNotifierService.ts b/server/notifier/appNotifierService.ts index 82e2f1d..e7ca730 100644 --- a/server/notifier/appNotifierService.ts +++ b/server/notifier/appNotifierService.ts @@ -3,8 +3,8 @@ import { DeviceCoordinates } from './deviceCoordinates'; import { prismaClient } from '../prisma/client'; import { computeDistanceBetween } from 'spherical-geometry-js'; import { MIN_DISTANCE } from './notifierConfig'; -import { nowWithTimezone, todayAtStartOfDayWithTimezone } from '../../utils/dates'; import { sendWhatsappNotification } from './whatsappService'; +import moment from 'moment'; const http = axios.create({ baseURL: process.env.NOTIFIER_URL, @@ -48,7 +48,7 @@ async function authenticate() { } async function findUsersToNotify(coords: DeviceCoordinates) { - const firstHourToday = todayAtStartOfDayWithTimezone(); + const firstHourToday = moment().startOf('day').format(); const users = await prismaClient.notifyOrder.findMany({ where: { OR: [ @@ -74,7 +74,7 @@ async function findUsersToNotify(coords: DeviceCoordinates) { usersNotified.push(user.id); } }); - const today = nowWithTimezone(); + const now = moment().format(); await prismaClient.notifyOrder.updateMany({ where: { id: { @@ -82,7 +82,7 @@ async function findUsersToNotify(coords: DeviceCoordinates) { }, }, data: { - lastNotifiedAt: today, + lastNotifiedAt: now, }, }); } \ No newline at end of file diff --git a/utils/dates.ts b/utils/dates.ts index 49749e9..409d99e 100644 --- a/utils/dates.ts +++ b/utils/dates.ts @@ -22,4 +22,8 @@ export function todayAtStartOfDayWithTimezone() { export function atStartOfHoursAgoISO(hours: number) { return moment().startOf('hour').subtract(hours, 'hour').toISOString(); +} + +export function atStartOfHoursAgo(hours: number) { + return moment().startOf('hour').subtract(hours, 'hour').format(); } \ No newline at end of file