not perfect - but done
This commit is contained in:
parent
3e679c473a
commit
418b64ad9a
4 changed files with 51 additions and 14 deletions
|
@ -1,5 +1,4 @@
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
const stam = 'fds'
|
|
||||||
import routes from './routes';
|
import routes from './routes';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
|
@ -31,8 +31,8 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": 4,
|
"id": 4,
|
||||||
"start_time": "2023-09-18 14:00:00",
|
"start_time": "2023-04-26 14:00:00",
|
||||||
"end_time": "2023-09-18 15:00:00",
|
"end_time": "2023-04-26 15:00:00",
|
||||||
"supported_postcodes": [
|
"supported_postcodes": [
|
||||||
"W1H 1LJ",
|
"W1H 1LJ",
|
||||||
"5555555",
|
"5555555",
|
||||||
|
|
|
@ -1,17 +1,20 @@
|
||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
|
import dateModule from 'date-and-time';
|
||||||
|
|
||||||
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';
|
||||||
import { getAvailableTimeSlots } from './services/timeslotsService';
|
import { getAvailableTimeSlots } from './services/timeslotsService';
|
||||||
import { getHolidays } from './services/holidaysService';
|
import { getHolidays } from './services/holidaysService';
|
||||||
|
|
||||||
const dateModule = require('date-and-time')
|
|
||||||
const dateFormat = 'YYYY-MM-DD HH:MM:SS';
|
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();
|
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();
|
const slotsInUse = new Map();
|
||||||
|
// orders by dates caching.
|
||||||
|
const ordersByDates = new Map();
|
||||||
|
|
||||||
export const resolveAddressHandler = (req: Request, res: Response) => {
|
export const resolveAddressHandler = (req: Request, res: Response) => {
|
||||||
if (!req.body.searchTerm) {
|
if (!req.body.searchTerm) {
|
||||||
|
@ -66,24 +69,37 @@ export const deliveriesHandler = (req: Request, res: Response) => {
|
||||||
|
|
||||||
// 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
|
// the date needs to be in "2023-09-18 14:00:00" format
|
||||||
const now = new Date();
|
const deliveryCreatedDate = dateModule.format(new Date(), dateFormat);
|
||||||
const date = dateModule.format(now, dateFormat);
|
const date = deliveryCreatedDate.split(' ')[0];
|
||||||
|
|
||||||
const delivery = {
|
const delivery = {
|
||||||
_id: deliveryId,
|
_id: deliveryId,
|
||||||
userId,
|
userId,
|
||||||
slotId,
|
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)) {
|
if (slotsInUse.has(slotId)) {
|
||||||
slotsInUse.get(slotId).push(deliveryId);
|
slotsInUse.get(slotId).push(deliveryId);
|
||||||
} else {
|
} else {
|
||||||
slotsInUse.set(slotId, [deliveryId]);
|
slotsInUse.set(slotId, [deliveryId]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ordersByDates.has(date)) {
|
||||||
|
ordersByDates.get(date).push(deliveryId);
|
||||||
|
} else {
|
||||||
|
ordersByDates.set(date, [deliveryId]);
|
||||||
|
}
|
||||||
|
|
||||||
deliveriesCache.set(deliveryId, delivery);
|
deliveriesCache.set(deliveryId, delivery);
|
||||||
|
|
||||||
|
console.log("ordersByDates", ordersByDates)
|
||||||
console.log("deliveriesCache", deliveriesCache)
|
console.log("deliveriesCache", deliveriesCache)
|
||||||
console.log("slotsInUse", slotsInUse)
|
console.log("slotsInUse", slotsInUse)
|
||||||
|
|
||||||
res.status(200).json(delivery);
|
res.status(200).json(delivery);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -117,13 +133,33 @@ 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 today’s deliveries - by day by slotId.date
|
// GET /deliveries/daily - retrieve all today’s deliveries - by today in ordersByDates
|
||||||
// get today's slots in slots
|
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) => {
|
export const weeklyDeliveriesHandler = (req: Request, res: Response) => {
|
||||||
// TODO: Implement weekly deliveries functionality
|
// 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);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -148,7 +184,7 @@ async function filterOutHolidaysByCountryCode(address: Address, availableTimeSlo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function availableTimeSlots(address: Address): Promise<AvailableTimeslots []> {
|
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
|
||||||
|
|
|
@ -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';
|
import app from './app';
|
||||||
|
|
||||||
const env = process.env.NODE_ENV || 'development';
|
|
||||||
const PORT = process.env.PORT || 3000;
|
const PORT = process.env.PORT || 3000;
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
|
|
Loading…
Reference in a new issue