Skip to content

Commit

Permalink
Dates handling using moment.js
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinarau committed Mar 7, 2024
1 parent c8b601f commit b8a789f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pages/api/external-samples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down Expand Up @@ -47,7 +47,7 @@ const handler: NextApiHandler = async (req, res) => {
samples = await prismaClient.sample.findMany({
where: {
takenAt: {
gte: atStartOfHoursAgoISO(3),
gte: atStartOfHoursAgo(3),
},
},
select: {
Expand Down
2 changes: 2 additions & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
8 changes: 4 additions & 4 deletions server/notifier/appNotifierService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: [
Expand All @@ -74,15 +74,15 @@ async function findUsersToNotify(coords: DeviceCoordinates) {
usersNotified.push(user.id);
}
});
const today = nowWithTimezone();
const now = moment().format();
await prismaClient.notifyOrder.updateMany({
where: {
id: {
in: usersNotified,
},
},
data: {
lastNotifiedAt: today,
lastNotifiedAt: now,
},
});
}
4 changes: 4 additions & 0 deletions utils/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit b8a789f

Please sign in to comment.