From 3e679c473aeaf62cb4215c0981592930c0782a9f Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Wed, 26 Apr 2023 20:04:31 +0300 Subject: [PATCH] add back id & install formatter module + using it --- package-lock.json | 6 ++++++ package.json | 1 + src/handlers.ts | 20 ++++++++++++++------ src/services/timeslotsService.ts | 1 + src/types/index.ts | 2 +- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 592c53b..d93aa19 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@types/express": "^4.17.17", "axios": "^1.3.6", "body-parser": "^1.20.2", + "date-and-time": "^3.0.0", "dotenv": "^16.0.3", "express": "^4.18.2", "knex": "^2.4.2", @@ -444,6 +445,11 @@ "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", "dev": true }, + "node_modules/date-and-time": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/date-and-time/-/date-and-time-3.0.0.tgz", + "integrity": "sha512-uuzXp/mvv6jEMLiP5QzERSQPzHqYnv9i8NZ8BS5kYeB2sakv74EewQiCS4Ahxwq3In+9fYZhGztuDHRVzIOkFQ==" + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", diff --git a/package.json b/package.json index 531253f..9b7c40b 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "@types/express": "^4.17.17", "axios": "^1.3.6", "body-parser": "^1.20.2", + "date-and-time": "^3.0.0", "dotenv": "^16.0.3", "express": "^4.18.2", "knex": "^2.4.2", diff --git a/src/handlers.ts b/src/handlers.ts index f1a66a5..3d86572 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -2,11 +2,11 @@ import { Request, Response } from 'express'; import { resolveAddress } from './geocoding'; import { Address, AvailableTimeslots } from './types'; import { randomUUID } from 'crypto'; -// DOME import { getAvailableTimeSlots } from './services/timeslotsService'; import { getHolidays } from './services/holidaysService'; -const env = require('dotenv'); +const dateModule = require('date-and-time') +const dateFormat = 'YYYY-MM-DD HH:MM:SS'; // create a hashing for caching delivery slots. this needs to be a caching db. const deliveriesCache = new Map(); @@ -51,7 +51,7 @@ export const deliveriesHandler = (req: Request, res: Response) => { return; } - // Idea: check if user has already booked a delivery + // Idea: check if user has already booked a delivery - is this needed? // if (deliveriesCache.has(userId) && deliveriesCache.get(userId).slotId === slotId) { // res.status(400).json({ error: 'User has already booked a delivery' }); // return; @@ -65,11 +65,15 @@ export const deliveriesHandler = (req: Request, res: Response) => { const deliveryId = randomUUID(); // Idea: create new delivery for user + // the date needs to be in "2023-09-18 14:00:00" format + const now = new Date(); + const date = dateModule.format(now, dateFormat); + const delivery = { _id: deliveryId, userId, slotId, - deliveryCreatedDate: new Date() + deliveryCreatedDate: date }; if (slotsInUse.has(slotId)) { slotsInUse.get(slotId).push(deliveryId); @@ -113,6 +117,9 @@ export const cancelDeliveryHandler = (req: Request, res: Response) => { export const dailyDeliveriesHandler = (req: Request, res: Response) => { // TODO: Implement daily deliveries functionality + // GET /deliveries/daily - retrieve all today’s deliveries - by day by slotId.date + // get today's slots in slots + }; export const weeklyDeliveriesHandler = (req: Request, res: Response) => { @@ -141,10 +148,11 @@ async function filterOutHolidaysByCountryCode(address: Address, availableTimeSlo } -async function availableTimeSlots(address: Address) { +async function availableTimeSlots(address: Address): Promise { const availableTimeSlot = []; const timeslots = await getAvailableTimeSlots(); // check by postcode if any available timeslots + // the time needs to be in "2023-09-18 14:00:00" format for (const timeslot of timeslots.courier_available_timeslots) { if (timeslot.supported_postcodes.includes(address.postcode)) { availableTimeSlot.push({ @@ -155,4 +163,4 @@ async function availableTimeSlots(address: Address) { } } return availableTimeSlot; -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/services/timeslotsService.ts b/src/services/timeslotsService.ts index 68e2213..20cea7d 100644 --- a/src/services/timeslotsService.ts +++ b/src/services/timeslotsService.ts @@ -1,4 +1,5 @@ // for demo resolving the promise immediately with mock data +// INFO: assuming this will be replaced with an API call, Will add id to the mock data import timeSlots from '../data/timeslots.json'; export function getAvailableTimeSlots() { diff --git a/src/types/index.ts b/src/types/index.ts index 443b813..0c12fa8 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -8,7 +8,7 @@ export interface Address { } export interface AvailableTimeslots { - id: number; + id: string; start_time: string; end_time: string; } \ No newline at end of file