Skip to content

Commit

Permalink
Merge pull request #35 from Hello-Kitchen/34-optimisation-et-fix-de-l…
Browse files Browse the repository at this point in the history
…a-route-pour-la-route-pour-les-commandes-du-passe

fix: Route pour envoyer les commandes au passe
  • Loading branch information
JulesGresset authored Jul 5, 2024
2 parents 3e910ee + 8d95ece commit c43f297
Showing 1 changed file with 30 additions and 47 deletions.
77 changes: 30 additions & 47 deletions routes/api/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,36 @@ router.get('/', (req, res) => {
const db = client.db(DB_NAME);
const collection = db.collection(keys.ORDER_COLLECTION_NAME);

if (req.query.sort === "time") {
if (req.query.status) {
let readyOrders = [];

collection.find({}).toArray().then(orders => {
let promises = orders.map(order => {
return db.collection(keys.FOOD_ORDERED_COLLECTION_NAME).find({ id: { $in: order.food_ordered }, part: order.part }).toArray()
.then(foodOrdered => {
if (req.query.status == "pending" && foodOrdered.some(food => food.is_ready) && !foodOrdered.every(food => food.is_ready))
readyOrders.push(order);
else if (req.query.status == "ready" && foodOrdered.every(food => food.is_ready))
readyOrders.push(order);
})
.catch(err => {
res.status(500).send("Error reading foodOrdered from database: " + err);
throw err; // Re-throw error to ensure Promise.all fails
});
});

Promise.all(promises)
.then(() => {
res.json(readyOrders);
})
.catch(() => {
res.status(500).send("Error processing orders");
});
})
.catch(err => {
res.status(500).send("Error reading orders from database: " + err);
});
} else if (req.query.sort === "time") {
collection.find({}).sort({ date: 1 }).toArray().then(orders => {
res.json(orders);
}).catch(err => {
Expand Down Expand Up @@ -171,52 +200,6 @@ router.delete('/:id', (req, res) => {
});
});

router.get('/status/ready', async (req, res) => {
try {
await client.connect();
const db = client.db(DB_NAME);
const collection = db.collection(keys.ORDER_COLLECTION_NAME);

const orders = await collection.find({}).toArray();
const readyOrders = [];

for (let order of orders) {
const foodOrdered = await db.collection(keys.FOOD_ORDERED_COLLECTION_NAME).find({ id: { $in: order.food_ordered } }).toArray();
const allReady = foodOrdered.every(food => food.is_ready);

if (allReady) {
readyOrders.push(order);
}
}
res.status(200).json(readyOrders);
} catch (err) {
res.status(500).send("Error connecting to database: " + err);
}
});

router.get('/status/pending', async (req, res) => {
try {
await client.connect();
const db = client.db(DB_NAME);
const collection = db.collection(keys.ORDER_COLLECTION_NAME);

const orders = await collection.find({}).toArray();
const readyOrders = [];

for (let order of orders) {
const foodOrdered = await db.collection(keys.FOOD_ORDERED_COLLECTION_NAME).find({ id: { $in: order.food_ordered } }).toArray();
const allReady = foodOrdered.every(food => food.is_ready);

if (!allReady) {
readyOrders.push(order);
}
}
res.status(200).json(readyOrders);
} catch (err) {
res.status(500).send("Error connecting to database: " + err);
}
});

router.put('/next/:id', async (req, res) => {
try {
await client.connect();
Expand Down

0 comments on commit c43f297

Please sign in to comment.