Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sachintha #100

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions backend/controllers/Admin/AcustomerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export const getAllUsers = async (req, res) => {
}
}



// Fetch single user by ID
export const getUserById = async (req, res) => {
try {
Expand All @@ -26,7 +24,6 @@ export const getUserById = async (req, res) => {
}
}


// Update user by ID
export const updateUserById = async (req, res) => {
const {
Expand Down Expand Up @@ -107,4 +104,4 @@ export const getUserCount = async (req, res) => {
} catch (error) {
res.status(500).json({ error: 'Error fetching user count' })
}
}
}
3 changes: 1 addition & 2 deletions backend/controllers/Admin/Afarmer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import Farmer from '../../models/farmerModel'
import mongoose from 'mongoose'
export const getFarmersCount = async (req, res) => {
Expand All @@ -17,4 +16,4 @@ export const getAllFarmers = async (req, res) => {
} catch (error) {
res.status(500).json({ message: error.message })
}
}
}
87 changes: 78 additions & 9 deletions backend/controllers/DLDeliveryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,42 @@ export const sendEmailToDriver = async (driver, delivery) => {

// Set up the email options
const mailOptions = {
from: process.env.EMAIL_USER,
from: `'FarmCart 🌱' <${process.env.EMAIL_USER}`,
to: driver.email,
subject: `Order Assignment: Order ${delivery.oID} Assigned to You`,
html: `
<h2>Hi ${driver.fullName},</h2>
<p>We are excited to inform you that you have been assigned to deliver the following order:</p>
<h3>Order Details</h3>
<ul>
<div style="font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px; text-align: center;">
<div style="max-width: 600px; margin: 0 auto; background-color: #fff; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 10px rgba(0,0,0,0.1); border: 2px solid #38A169;">

<h2 style="font-size: 24px; font-weight: bold; margin-bottom: 20px; color: #2D3748;">Hi ${driver.fullName},</h2>
<p style="font-size: 16px; line-height: 1.6; color: #4A5568;">
We are excited to inform you that you have been assigned to deliver the following order:
</p>

<h3 style="font-size: 20px; font-weight: bold; color: #2D3748; margin-top: 20px; text-align: left;">Order Details:</h3>
<ul style="text-align: left; font-size: 16px; line-height: 1.8; color: #4A5568; list-style: none; padding: 0;">
<li><strong>Order ID:</strong> ${delivery.oID}</li>
<li><strong>Tracking ID:</strong> ${delivery.trackingID}</li>
<li><strong>Shop Name:</strong> ${delivery.shopName}</li>
<li><strong>Pickup Address:</strong> ${delivery.pickupAddress}</li>
<li><strong>Customer Name:</strong> ${delivery.customerName}</li>
<li><strong>Drop-Off Address:</strong> ${delivery.dropOffAddress}</li>
</ul>
<p>For more details, please log in to the delivery portal or contact support if needed.</p>

<p style="font-size: 16px; line-height: 1.6; color: #4A5568; margin-top: 20px;">
For more details, please log in to the delivery portal or contact support if needed.
</p>

<br/>
<p>Thank you for being part of our delivery team!</p>
<p>Best Regards,</p>
<p><strong>Your Company Name</strong></p>
<p style="font-size: 16px; line-height: 1.6; color: #4A5568;">
Thank you for being part of our delivery team!
</p>

<p style="font-size: 16px; line-height: 1.6; color: #2D3748; font-weight: bold; margin-top: 20px;">Best Regards,</p>
<p style="font-size: 16px; color: #4A5568;">The FarmCart Team 🌱</p>

</div>
</div>
`,
}

Expand Down Expand Up @@ -293,3 +309,56 @@ export const getDeliveriesByDriver = async (req, res) => {
})
}
}

//
// deleteing the duplicated ones
// Function to clean up duplicate deliveries by orderID
export const cleanUpDuplicateDeliveries = async () => {
try {
// Find all deliveries grouped by orderID and check for duplicates
const duplicates = await DLDelivery.aggregate([
{
$group: {
_id: '$orderID', // Group by orderID
count: { $sum: 1 }, // Count occurrences of each orderID
docs: { $push: '$$ROOT' }, // Push all documents with the same orderID
},
},
{
$match: {
count: { $gt: 1 }, // Filter groups that have more than 1 occurrence
},
},
])

// Loop through each duplicate and keep the first created, delete the rest
for (const duplicate of duplicates) {
const sortedDocs = duplicate.docs.sort(
(a, b) =>
new Date(a.assignDateTime) - new Date(b.assignDateTime)
) // Sort by creation date (assignDateTime)

const [firstDoc, ...duplicatesToDelete] = sortedDocs

// Keep the first document and delete the rest
for (const doc of duplicatesToDelete) {
await DLDelivery.findByIdAndDelete(doc._id)
console.log(
`Deleted duplicate delivery with orderID: ${doc.orderID}`
)
}

console.log(
`Kept delivery for orderID: ${firstDoc.orderID}, removed ${duplicatesToDelete.length} duplicates`
)
}

if (duplicates.length === 0) {
/*
// console.log('No duplicate deliveries found.')
*/
}
} catch (error) {
console.error('Error cleaning up duplicate deliveries:', error)
}
}
54 changes: 40 additions & 14 deletions backend/controllers/DLEmailController.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,46 @@ export const sendApprovalEmail = asyncHandler(async (req, res) => {
to: driver.email,
subject: 'Farmcart: Approval Confirmation',
html: `
<p>Dear ${driver.fullName},</p>
<p>Congratulations! Your driver registration has been approved. Please click the button below to log in and complete your registration:</p>
<a href="${process.env.SITE_URL}${loginUrl}" style="
background-color: #4CAF50;
color: white;
padding: 10px 20px;
text-align: center;
text-decoration: none;
display: inline-block;
border-radius: 5px;">
Click Here to Log In
</a>
<p>Alternatively, you can enter your ID to log in for the first time.</p>
<p>Regards,<br/>FarmCart Team</p>
<div style="font-family: Arial, sans-serif; background-color: #f4f4f4; padding: 20px;">
<div style="max-width: 600px; margin: 0 auto; background-color: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border: 2px solid #4CAF50;">
<h2 style="text-align: center; color: #2E7D32; font-size: 24px; font-weight: bold;">FarmCart: Approval Confirmation</h2>

<p style="font-size: 16px; line-height: 1.6; color: #333;">
Dear <strong>${driver.fullName}</strong>,
</p>

<p style="font-size: 16px; line-height: 1.6; color: #333;">
Congratulations! Your driver registration has been approved.
</p>

<p style="font-size: 16px; line-height: 1.6; color: #333; font-weight: bold; color: #2E7D32;">
You can log in to your account by clicking the button below to complete your registration:
</p>

<div style="text-align: center; margin: 20px 0;">
<a href="${process.env.SITE_URL}${loginUrl}" style="
background-color: #4CAF50;
color: white;
padding: 12px 24px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
font-weight: bold;
border-radius: 5px;">
Click Here to Log In
</a>
</div>

<p style="font-size: 16px; line-height: 1.6; color: #333;">
Alternatively, you can enter your ID to log in for the first time.
</p>

<p style="font-size: 16px; line-height: 1.6; color: #333;">
Regards,<br/>FarmCart Team
</p>
</div>
</div>
`,
}

Expand Down
12 changes: 6 additions & 6 deletions backend/controllers/DLOcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ const assignReadyOrders = async () => {
orderID: order._id.toString(),
})
if (existingDelivery) {
console.log(
`Order with ID ${order._id} already exists in DLDelivery.`
)
// console.log(
// `Order with ID ${order._id} already exists in DLDelivery.`
// )
continue // Skip this order if it already exists in DLDelivery
}

Expand Down Expand Up @@ -183,7 +183,7 @@ const assignReadyOrders = async () => {
/*console.log(`Order with ID ${order._id} has been successfully assigned to dOrder with new orderID ${newDOrder.orderID}.`)*/

// After saving the new dOrder, update the order status to "Assigning" in the Order model
order.orderStatus = ' .Ready. '
order.orderStatus = ' Ready. '
await order.save() // Save the updated order
// console.log(`Order with ID ${order._id} has been marked as "Assigning" in the Order model.`)
}
Expand Down Expand Up @@ -234,11 +234,11 @@ const syncDeliveryAndOrderStatus = async () => {
// Function to repeatedly check for ready orders every 5 seconds
const startOrderAssignment = () => {
/* console.log('Starting periodic check for ready orders...')*/
setInterval(assignReadyOrders, 5000) // Run the check every 5 seconds
setInterval(assignReadyOrders, 1000) // Run the check every 5 seconds
}

const startSyncDeliveryOrderStatus = () => {
setInterval(syncDeliveryAndOrderStatus, 5000) // Run every 5 seconds
setInterval(syncDeliveryAndOrderStatus, 1000) // Run every 5 seconds
}

export { startSyncDeliveryOrderStatus }
Expand Down
31 changes: 31 additions & 0 deletions backend/models/Blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// models/Blog.js

import mongoose from 'mongoose'

const blogSchema = new mongoose.Schema({
title: {
type: String,
required: true,
minlength: 5,
maxlength: 100,
},
content: {
type: String,
required: true,
minlength: 5,
},
author: {
type: String,
required: true,
},
image: {
type: String,
},
createdAt: {
type: Date,
default: Date.now,
},
})

const Blog = mongoose.model('Blog', blogSchema)
export default Blog
28 changes: 28 additions & 0 deletions backend/models/Comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// models/Comment.js

import mongoose from 'mongoose'

const { Schema } = mongoose

const commentSchema = new Schema({
blogId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Blog',
required: true,
},
name: {
type: String,
required: true,
},
comment: {
type: String,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
})

const Comment = mongoose.model('Comment', commentSchema)
export default Comment
8 changes: 7 additions & 1 deletion backend/models/DLDeliveryModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ const DLDeliverySchema = new mongoose.Schema({
orderID: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Order',
required: [true, 'Order ID is required'],
unique: [true, 'Order ID is unique'],
},

oID: { type: String },
oID: {
type: String,
required: [true, 'Order ID is required'],
unique: [true, 'Order ID is unique'],
},

driverID: {
type: mongoose.Schema.Types.ObjectId,
Expand Down
6 changes: 5 additions & 1 deletion backend/models/DLOModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const dorderSchema = new mongoose.Schema(
{
/* generate and store the oID:{type: String,}, */

oID: { type: String },
oID: {
type: String,
required: [true, 'Order ID required'], // Ensure each order has a unique ID
unique: [true, 'Order already exists'],
},

orderID: {
type: String, // You can change this to any type or ObjectId if needed
Expand Down
9 changes: 9 additions & 0 deletions backend/models/OrderModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ const orderSchema = new mongoose.Schema(
totalPrice: { type: Number, required: true },
orderStatus: {
type: String,
enum: [
'Pending',
'Rejected',
'Ready',
' Ready. ',
'Picked Up',
'On The Way',
'Delivered',
],
default: 'Pending',
},
deliveryDate: {
Expand Down
33 changes: 33 additions & 0 deletions backend/models/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// speechGenerator.js

// Create a new speech synthesis utterance object
const speech = new SpeechSynthesisUtterance()
let voices = []

// Select the voice selection dropdown
const voiceSelect = document.querySelector('select')

// Populate the voice list when voices are available
window.speechSynthesis.onvoiceschanged = () => {
voices = window.speechSynthesis.getVoices()
speech.voice = voices[0] // Default to the first available voice

// Populate the select dropdown with available voices
voices.forEach((voice, i) => {
const option = new Option(voice.name, i)
voiceSelect.options[i] = option
})
}

// Event listener for the voice select dropdown
voiceSelect.addEventListener('change', () => {
speech.voice = voices[voiceSelect.value]
})

// Event listener for the button click to start speech synthesis
document.querySelector('button').addEventListener('click', () => {
speech.text = document.querySelector('textarea').value
window.speechSynthesis.speak(speech)
})

export { speech, voices, voiceSelect }
7 changes: 2 additions & 5 deletions backend/routes/Admin/AfarmerRoute.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import express from 'express'
import {
getFarmersCount,
getAllFarmers,
} from '../../controllers/Admin/Afarmer'
import { getFarmersCount, getAllFarmers } from '../../controllers/Admin/Afarmer'

const router = express.Router()

router.get('/', getAllFarmers)
router.post('/count', getFarmersCount)

export default router
export default router
Loading
Loading