add back id & install formatter module + using it

This commit is contained in:
Kfir Dayan 2023-04-26 20:04:31 +03:00
parent 9e437999f7
commit 3e679c473a
5 changed files with 23 additions and 7 deletions

6
package-lock.json generated
View file

@ -13,6 +13,7 @@
"@types/express": "^4.17.17", "@types/express": "^4.17.17",
"axios": "^1.3.6", "axios": "^1.3.6",
"body-parser": "^1.20.2", "body-parser": "^1.20.2",
"date-and-time": "^3.0.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",
"knex": "^2.4.2", "knex": "^2.4.2",
@ -444,6 +445,11 @@
"integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==",
"dev": true "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": { "node_modules/debug": {
"version": "2.6.9", "version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",

View file

@ -15,6 +15,7 @@
"@types/express": "^4.17.17", "@types/express": "^4.17.17",
"axios": "^1.3.6", "axios": "^1.3.6",
"body-parser": "^1.20.2", "body-parser": "^1.20.2",
"date-and-time": "^3.0.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",
"knex": "^2.4.2", "knex": "^2.4.2",

View file

@ -2,11 +2,11 @@ import { Request, Response } from 'express';
import { resolveAddress } from './geocoding'; import { resolveAddress } from './geocoding';
import { Address, AvailableTimeslots } from './types'; import { Address, AvailableTimeslots } from './types';
import { randomUUID } from 'crypto'; import { randomUUID } from 'crypto';
// DOME
import { getAvailableTimeSlots } from './services/timeslotsService'; import { getAvailableTimeSlots } from './services/timeslotsService';
import { getHolidays } from './services/holidaysService'; 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. // create a hashing for caching delivery slots. this needs to be a caching db.
const deliveriesCache = new Map(); const deliveriesCache = new Map();
@ -51,7 +51,7 @@ export const deliveriesHandler = (req: Request, res: Response) => {
return; 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) { // if (deliveriesCache.has(userId) && deliveriesCache.get(userId).slotId === slotId) {
// res.status(400).json({ error: 'User has already booked a delivery' }); // res.status(400).json({ error: 'User has already booked a delivery' });
// return; // return;
@ -65,11 +65,15 @@ export const deliveriesHandler = (req: Request, res: Response) => {
const deliveryId = randomUUID(); const deliveryId = randomUUID();
// Idea: create new delivery for user // 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 = { const delivery = {
_id: deliveryId, _id: deliveryId,
userId, userId,
slotId, slotId,
deliveryCreatedDate: new Date() deliveryCreatedDate: date
}; };
if (slotsInUse.has(slotId)) { if (slotsInUse.has(slotId)) {
slotsInUse.get(slotId).push(deliveryId); slotsInUse.get(slotId).push(deliveryId);
@ -113,6 +117,9 @@ export const cancelDeliveryHandler = (req: Request, res: Response) => {
export const dailyDeliveriesHandler = (req: Request, res: Response) => { export const dailyDeliveriesHandler = (req: Request, res: Response) => {
// TODO: Implement daily deliveries functionality // TODO: Implement daily deliveries functionality
// GET /deliveries/daily - retrieve all todays deliveries - by day by slotId.date
// get today's slots in slots
}; };
export const weeklyDeliveriesHandler = (req: Request, res: Response) => { 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<AvailableTimeslots []> {
const availableTimeSlot = []; const availableTimeSlot = [];
const timeslots = await getAvailableTimeSlots(); const timeslots = await getAvailableTimeSlots();
// check by postcode if any available timeslots // 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) { for (const timeslot of timeslots.courier_available_timeslots) {
if (timeslot.supported_postcodes.includes(address.postcode)) { if (timeslot.supported_postcodes.includes(address.postcode)) {
availableTimeSlot.push({ availableTimeSlot.push({
@ -155,4 +163,4 @@ async function availableTimeSlots(address: Address) {
} }
} }
return availableTimeSlot; return availableTimeSlot;
} }

View file

@ -1,4 +1,5 @@
// for demo resolving the promise immediately with mock data // 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'; import timeSlots from '../data/timeslots.json';
export function getAvailableTimeSlots() { export function getAvailableTimeSlots() {

View file

@ -8,7 +8,7 @@ export interface Address {
} }
export interface AvailableTimeslots { export interface AvailableTimeslots {
id: number; id: string;
start_time: string; start_time: string;
end_time: string; end_time: string;
} }