not perfect - but done

This commit is contained in:
Kfir Dayan 2023-04-26 20:41:38 +03:00
parent 3e679c473a
commit 418b64ad9a
4 changed files with 51 additions and 14 deletions

View file

@ -1,5 +1,4 @@
import express from 'express';
const stam = 'fds'
import routes from './routes';
const app = express();

View file

@ -31,8 +31,8 @@
},
{
"id": 4,
"start_time": "2023-09-18 14:00:00",
"end_time": "2023-09-18 15:00:00",
"start_time": "2023-04-26 14:00:00",
"end_time": "2023-04-26 15:00:00",
"supported_postcodes": [
"W1H 1LJ",
"5555555",

View file

@ -1,17 +1,20 @@
import { Request, Response } from 'express';
import dateModule from 'date-and-time';
import { resolveAddress } from './geocoding';
import { Address, AvailableTimeslots } from './types';
import { randomUUID } from 'crypto';
import { getAvailableTimeSlots } from './services/timeslotsService';
import { getHolidays } from './services/holidaysService';
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.
const deliveriesCache = new Map();
// create a hashing for caching timeslots. this needs to be a caching db.
// create a hashing for caching timeslots.
const slotsInUse = new Map();
// orders by dates caching.
const ordersByDates = new Map();
export const resolveAddressHandler = (req: Request, res: Response) => {
if (!req.body.searchTerm) {
@ -66,24 +69,37 @@ export const deliveriesHandler = (req: Request, res: Response) => {
// 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 deliveryCreatedDate = dateModule.format(new Date(), dateFormat);
const date = deliveryCreatedDate.split(' ')[0];
const delivery = {
_id: deliveryId,
userId,
slotId,
deliveryCreatedDate: date
deliveryCreatedDate: deliveryCreatedDate
};
// INFO: This is a very simple implementation of caching.
// INFO: In my opinion, transaction should be used to avoid data inconsistency.
if (slotsInUse.has(slotId)) {
slotsInUse.get(slotId).push(deliveryId);
} else {
slotsInUse.set(slotId, [deliveryId]);
}
if (ordersByDates.has(date)) {
ordersByDates.get(date).push(deliveryId);
} else {
ordersByDates.set(date, [deliveryId]);
}
deliveriesCache.set(deliveryId, delivery);
console.log("ordersByDates", ordersByDates)
console.log("deliveriesCache", deliveriesCache)
console.log("slotsInUse", slotsInUse)
res.status(200).json(delivery);
};
@ -117,13 +133,33 @@ export const cancelDeliveryHandler = (req: Request, res: Response) => {
export const dailyDeliveriesHandler = (req: Request, res: Response) => {
// TODO: Implement daily deliveries functionality
// GET /deliveries/daily - retrieve all todays deliveries - by day by slotId.date
// get today's slots in slots
// GET /deliveries/daily - retrieve all todays deliveries - by today in ordersByDates
const date = dateModule.format(new Date(), dateFormat).split(' ')[0];
const todaysOrders = [];
if (ordersByDates.has(date)) {
ordersByDates.get(date).forEach((orderId) => {
todaysOrders.push(deliveriesCache.get(orderId));
});
}
res.status(200).json(todaysOrders);
};
export const weeklyDeliveriesHandler = (req: Request, res: Response) => {
// TODO: Implement weekly deliveries functionality
// GET /deliveries/weekly - retrieve all week deliveries - from today to 7 days later in ordersByDates
// this array will contain 7 elements - each element will be dates in the next 7 days
// ["today", "tomorrow" ... ]
const today = new Date();
const weeklyOrders = [];
for (let i = 0; i < 7; i++) {
const date = dateModule.format(new Date(today.setDate(today.getDate() + i)), dateFormat).split(' ')[0];
if (ordersByDates.has(date)) {
weeklyOrders.push(deliveriesCache.get(ordersByDates.get(date)));
}
}
res.status(200).json(weeklyOrders);
};

View file

@ -1,6 +1,8 @@
// INFO: this is a simple in-memory cache, for demo purposes only. this app will not scale!
// INFO: SQL database is required for continuing this application!
import app from './app';
const env = process.env.NODE_ENV || 'development';
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {