-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
665 lines (590 loc) · 19.1 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
import express from "express";
import path from "path";
import fs from "fs/promises";
import { fileURLToPath } from "url";
import { PDFDocument, StandardFonts } from "pdf-lib";
import { resourceLimits } from "worker_threads";
import bodyParser from "body-parser";
import { GoogleGenerativeAI } from "@google/generative-ai";
import dotenv from "dotenv";
import removeMd from "remove-markdown";
import http from 'http';
import WebSocket from 'ws';
import { WebSocketServer } from 'ws';
import farmers from './public/farmers.json' with { type: "json" };
import url from "url"
dotenv.config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true })); // To handle form data
app.use(bodyParser.json());
const server = http.createServer(app);
const wss = new WebSocketServer({ server });
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const DATA_FILE = path.join(__dirname, './public/farmers-details.json');
const BUYERS_FILE_PATH= path.join(__dirname, './public/buyers.json');
const DATA_FILE_DISPLAY=path.join(__dirname, './public/farmers.json');
app.use(express.static(path.join(__dirname, "public")));
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
const genAI = new GoogleGenerativeAI(process.env.GEM_API_KEY);
const WEBSITE_OVERVIEW = await fs.readFile("Kisansetu_Overview.md", "utf8");
const WEBSITE_FEATURES = await fs.readFile(
"Kisansetu_Important_Features.md",
"utf8"
);
const WEBSITE_FUNCTIONING = await fs.readFile(
"Kisansetu_How_It_Works.md",
"utf8"
);
const WEBSITE_PERSPECTIVE = await fs.readFile(
"Kisansetu_Buyer_Perspective.md",
"utf8"
);
app.post("/assistance", async (req, res) => {
const { message, language } = req.body;
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const chat = model.startChat({
history: [
{
role: "user",
parts: [
{
text: "Here's context about our website that i will be sharing in different parts:",
},
],
},
{
role: "model",
parts: [
{
text: "Okay, I'm ready to receive the context in different parts.",
},
],
},
{
role: "user",
parts: [
{
text: "okay so here is the first part which is about kisansetu overview",
},
],
},
{
role: "model",
parts: [
{ text: "Okay i am ready to receive the first part of context" },
],
},
{
role: "user",
parts: [{ text: WEBSITE_OVERVIEW }],
},
{
role: "model",
parts: [
{
text: "I have received the first part successfully now send the second part",
},
],
},
{
role: "user",
parts: [
{
text: "Okay this is the second part which is about website important features",
},
],
},
{
role: "user",
parts: [{ text: WEBSITE_FEATURES }],
},
{
role: "model",
parts: [
{
text: "Okay i have received the second part as well , now send me the third part",
},
],
},
{
role: "user",
parts: [
{
text: "Okay i am sharing the third part which is about how it works",
},
],
},
{
role: "user",
parts: [{ text: WEBSITE_FUNCTIONING }],
},
{
role: "model",
parts: [
{
text: "Okay i have received the third part as well , now send me the last part",
},
],
},
{
role: "user",
parts: [
{
text: "Okay i am sharing the last part which gives you a brief demon from a buyer's perspective",
},
],
},
{
role: "user",
parts: [
{
text: "Okay i am sharing the last part which gives you a brief demon from a buyer's perspective",
},
],
},
{
role: "user",
parts: [{ text: WEBSITE_PERSPECTIVE }],
},
{
role: "model",
parts: [
{
text: "Thank you for providing the context about KisanSetu in different parts. I understand it's a platform connecting farmers and buyers in India, facilitating digital contracts, direct communication, government verification, secure transactions, and access to various crops. How can I assist you with questions related to KisanSetu?",
},
],
},
],
});
const languageInstruction =
language === "hindi"
? "Please respond in Hindi."
: "Please respond in English.";
const result = await chat.sendMessage(message + "\n\n" + languageInstruction);
const response = await result.response;
let text = await response.text();
text = removeMd(text);
text = text.replace(/\*\*/g, "").trim();
// Improve formatting
text = text.replace(/(\d+\.)\s+/g, "\n$1 ");
text = text.replace(/([a-z])\s+([A-Z])/g, "$1\n$2");
text = text
.split("\n")
.map((line) => line.trim())
.join("\n"); // Trim each line
text = text.replace(/\n{3,}/g, "\n\n");
res.json({ text });
});
let uid;
app.post("/get-uid",(req,res)=>{
uid=req.body.user;
res.redirect('/')
})
app.get("/", async(req, res) => {
// const farmers=await getFarmersData();
// console.log(uid);
// if(uid) {
// const farmer=findFarmerById(uid,farmers);
// if(farmer){
// console.log("fameris here")
// res.render("home-page.ejs",{isFarmer:'true',farmerId:farmer.farmerId})
// }else{
// res.render("home-page.ejs",{isFarmer:'false',farmerId:null})
// }
// }
// else res.render("home-page.ejs",{isFarmer:'false',farmerId:null})
res.render("home-page.ejs")
});
app.get("/signup", (req, res) => {
res.sendFile(path.join(__dirname, "public", "signup.html"));
});
app.get("/dashboard", (req, res) => {
res.sendFile(path.join(__dirname, "public", "dashboard.html"));
});
app.get("/contract-form", (req, res) => {
res.sendFile(path.join(__dirname, "public", "contract_form.html"));
});
app.get('/secure/sendeth', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'afterButton.html'));
})
app.get("/register", (req, res) => {
res.sendFile(path.join(__dirname, "public", "form.html"));
});
app.post("/create-pdf", async (req, res) => {
const {
sellerName,
sellerAddress,
buyerName,
buyerAddress,
cropDescription,
quantity,
price,
totalAmount,
} = req.body;
try {
const pdfDoc = await PDFDocument.create();
const page = pdfDoc.addPage([1700, 2200]);
const font = await pdfDoc.embedFont(StandardFonts.TimesRoman);
const fontSize = 24;
const lineHeight = fontSize * 1.5;
const margin = 100;
const maxWidth = 1500;
const boldFont = await pdfDoc.embedFont(StandardFonts.HelveticaBold);
page.setFont(boldFont);
page.setFontSize(40);
page.drawText("KISANSETU", {
x: margin,
y: 2100,
size: 40,
underline: true,
});
const today = new Date();
const day = today.getDate();
const month = today.toLocaleString("default", { month: "long" });
const year = today.getFullYear();
const content = [
`Seller's Name: ${sellerName}`,
`Seller's Address: ${sellerAddress}`,
`Buyer's Name: ${buyerName}`,
`Buyer's Address: ${buyerAddress}`,
`This agreement is made and executed on this ${day} day of ${month}, ${year}, between the seller and the buyer`,
`for the sale of crops as per the terms and conditions laid down herein.`,
`The seller agrees to sell and the buyer agrees to purchase the crops described herein, in accordance with`,
`the provisions of the Indian Contract Act, 1872, and other applicable laws.`,
`The contract is legally valid and enforceable, as issued by the Government of India, and any disputes`,
`arising out of this contract shall be subject to the jurisdiction of the Indian courts.`,
`The seller guarantees that the crops being sold are free from any liens or encumbrances and are in good`,
`condition.`,
`The buyer acknowledges that they have inspected the crops and are satisfied with their quality before`,
`making the payment.`,
`This contract represents the entire agreement between the parties and supersedes all prior discussions,`,
`agreements, or understandings of any kind.`,
`The parties hereto have executed this agreement as of the day and year first above written.`,
`Crop Description: ${cropDescription}`,
`Quantity: ${quantity}`,
`Price: ${price}`,
`Total Amount: ${totalAmount}`,
`Signatures:`,
`Seller: ___________________________`,
`Buyer: ___________________________`,
`Date: ${day} ${month} ${year}`,
].join("\n\n");
const textLines = content.split("\n");
let yOffset = 1900;
page.setFont(font);
page.setFontSize(fontSize);
for (const line of textLines) {
page.drawText(line, { x: margin, y: yOffset, size: fontSize, maxWidth });
yOffset -= lineHeight;
}
const pdfBytes = await pdfDoc.save();
console.log("PDF generated successfully");
res.set({
"Content-Type": "application/pdf",
"Content-Disposition": "attachment; filename=farm-purchase-agreement.pdf",
});
res.send(Buffer.from(pdfBytes));
} catch (error) {
console.error("Error generating PDF:", error);
res.status(500).send("Failed to generate PDF");
}
});
async function getFarmersData() {
try {
const fileContent = await fs.readFile(DATA_FILE, 'utf-8');
return JSON.parse(fileContent);
} catch (err) {
console.error('Error reading or parsing data file:', err);
throw err;
}
}
// Function to write farmers data
async function writeFarmersData(data) {
try {
const content = JSON.stringify(data, null, 2);
await fs.writeFile(DATA_FILE, content, 'utf-8');
} catch (err) {
console.error('Error writing data file:', err);
throw err;
}
}
async function getFarmersDataForDisplay() {
try {
const fileContent = await fs.readFile(DATA_FILE_DISPLAY, 'utf-8');
return JSON.parse(fileContent);
} catch (err) {
console.error('Error reading or parsing data file:', err);
throw err;
}
}
// Function to write farmers data
async function writeFarmersDataForDisplay(data) {
try {
const content = JSON.stringify(data, null, 2);
await fs.writeFile(DATA_FILE_DISPLAY, content, 'utf-8');
} catch (err) {
console.error('Error writing data file:', err);
throw err;
}
}
// In-memory store for chat connections
const connections = {};
let chats = {};
// Route to render chat messages
async function getBuyersData() {
try {
const fileContent = await fs.readFile(BUYERS_FILE_PATH, 'utf-8');
return JSON.parse(fileContent);
} catch (err) {
console.error('Error reading or parsing data file:', err);
throw err;
}
}
// Function to write farmers data
async function writeBuyersData(data) {
try {
const content = JSON.stringify(data, null, 2);
await fs.writeFile(BUYERS_FILE_PATH, content, 'utf-8');
} catch (err) {
console.error('Error writing data file:', err);
throw err;
}
}
// In-memory store for chat connections
let farmerChats={}
//faemrs's buyer
app.get("/buyers/:id",async(req,res)=>{
const {id} = req.params;
const farmers=await getFarmersData();
const buyers=await getBuyersData();
console.log(buyers)
const currFarmer=findFarmerById(id,farmers);
const farmersBuyer=[];
for(let keys in currFarmer.messages){
console.log(keys)
const isBuyer=findBuyerById(keys,buyers)
if(isBuyer){
farmersBuyer.push(isBuyer)
}
}
console.log (farmersBuyer)
res.render("buyersList.ejs",{farmersBuyer,id})
})
// Route to render chat messages
// app.get("/chat/:id", async (req, res) => {
// const { id } = req.params;
// console.log(id);
// console.log(chats);
// const chatBetween = chats[id];
// const farmers=getFarmersData();
// console.log(farmers)
// const farmer = findFarmerById(parseInt(chatBetween), farmers);
// const buyers = await getBuyersData();
// const buyer = findBuyerById(parseInt(id), buyers);
// console.log(buyer);
// let messages = [];
// if(buyer.messages[chatBetween]) messages=farmer.messages[chatBetween]
// console.log(messages)
// res.render('message', {
// currUser: id,
// id: chatBetween,
// receiverUsername: farmer.name,
// messages
// });
// });
app.get("/chat/:id", async (req, res) => {
const { id } = req.params;
console.log(id);
console.log(chats);
const chatBetween = chats[id];
const farmers = await getFarmersData();
const farmer = findFarmerById(chatBetween, farmers);
const buyers=await getBuyersData();
const buyer=await findBuyerById(id,buyers)
console.log(buyer);
console.log(farmer);
let messages = [];
console.log("chatbetween",chatBetween)
if(buyer.messages[chatBetween]) messages=buyer.messages[chatBetween]
console.log(messages)
res.render('message', {
currUser: id,
id: chatBetween,
receiverUsername: farmer.name,
messages
});
});
// Route to handle chat updates
app.post("/chat/:id", async (req, res) => {
const { id } = req.params;
const data = req.body;
chats[data.userId] = data.farmerId;
console.log(data);
res.send(data);
});
app.get("/buyer/chat/:id", async (req, res) => {
const { id } = req.params;
const chatBetween = farmerChats[id];
console.log(chatBetween)
const farmers = await getFarmersData();
const buyers=await getBuyersData()
const farmer = findFarmerById(id, farmers);
const buyer=findBuyerById(chatBetween,buyers)
console.log(buyer);
let messages = [];
if(farmer.messages[chatBetween]) messages=farmer.messages[chatBetween]
console.log(messages)
res.render('buyermessage.ejs', {
currUser: id,
id: chatBetween,
receiverUsername: buyer.username,
messages
});
});
app.post("/buyer/chat/:id", async (req, res) => {
const { id } = req.params;
const data = req.body;
farmerChats[data.farmerId] = data.buyerId;
// console.log(farmerChats);
res.send(data);
});
app.post("/add-user",async(req,res)=>{
console.log(req.body);
if(req.body.occupation=='Buyer'){
const allBuyers=await getBuyersData();
const data={"id":req.body.uid,"username":req.body.name,messages:{}}
allBuyers.push(data)
await writeBuyersData(allBuyers)
}else if(req.body.occupation=='Farmer'){
const allFarmers=await getFarmersData();
const allFarmersDisplay=await getFarmersDataForDisplay();
const data={
"farmerId": req.body.uid,
"name": req.body.name,
"state": req.body.state,
"cropsGrown": req.body.cropsGrown,
"postalCode":req.body.postalCode,
"address":req.body.address,
"produceAmount": req.body.amountProduced,
"image": String(req.body.imageUrl),
"governmentVerified": req.body.governmentVerified,
messages:{}
}
const displayData={
"id": req.body.uid,
"name": req.body.name,
"state": req.body.state,
"cropsGrown": req.body.cropsGrown,
"produceAmount": req.body.amountProduced,
"image": String(req.body.imageUrl),
"governmentVerified": req.body.governmentVerified,}
allFarmers.push(data);
allFarmersDisplay.push(displayData);
await writeFarmersData(allFarmers)
await writeFarmersDataForDisplay(allFarmersDisplay);
}
})
// WebSocket connection handling
wss.on("connection", (ws, req) => {
const parsedUrl = url.parse(req.url);
const path = parsedUrl.pathname.substring(1);
if (connections[path]) {
connections[path].push(ws);
console.log(`${path} connected`);
} else {
connections[path] = [ws];
console.log(`${path} connected`);
}
console.log(`New connection on ${path}`);
ws.on("message", async (message) => {
console.log(`Received message on ${path}: ${message}`);
try {
const parsedMessage = JSON.parse(message);
console.log(parsedMessage);
const sender_id = parsedMessage.sender;
const receiver_id = parsedMessage.receiver;
// Update the farmers data
let farmers = await getFarmersData();
let buyers = await getBuyersData();
const receiver_farmer = findFarmerById(receiver_id, farmers);
const receiver_buyer = findBuyerById(receiver_id, buyers);
if (receiver_farmer) {
if (!receiver_farmer.messages) {
receiver_farmer.messages = {};
}
if (!receiver_farmer.messages[sender_id]) {
receiver_farmer.messages[sender_id] = [];
}
receiver_farmer.messages[sender_id].push({ from: parsedMessage.message });
const sender_buyer = findBuyerById(sender_id, buyers);
if (!sender_buyer.messages) {
sender_buyer.messages = {};
}
if (!sender_buyer.messages[receiver_id]) {
sender_buyer.messages[receiver_id] = [];
}
sender_buyer.messages[receiver_id].push({ to: parsedMessage.message });
await writeFarmersData(farmers);
await writeBuyersData(buyers);
console.log('Messages updated successfully.');
console.log('Updated Receiver:', receiver_farmer);
} else if (receiver_buyer) {
if (!receiver_buyer.messages) {
receiver_buyer.messages = {};
}
if (!receiver_buyer.messages[sender_id]) {
receiver_buyer.messages[sender_id] = [];
}
receiver_buyer.messages[sender_id].push({ from: parsedMessage.message });
const sender_farmer = findFarmerById(sender_id, farmers);
if (!sender_farmer.messages) {
sender_farmer.messages = {};
}
if (!sender_farmer.messages[receiver_id]) {
sender_farmer.messages[receiver_id] = [];
}
sender_farmer.messages[receiver_id].push({ to: parsedMessage.message });
await writeFarmersData(farmers);
await writeBuyersData(buyers);
console.log('Messages updated successfully.');
console.log('Updated Receiver:', receiver_buyer);
} else {
console.error("Error occurred");
return;
}
if (connections[parsedMessage.receiver]) {
connections[parsedMessage.receiver].forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({
sender: parsedMessage.sender,
receiver: parsedMessage.receiver,
message: parsedMessage.message
}));
}
});
} else {
console.log(`No clients connected to path ${parsedMessage.receiver}`);
}
} catch (error) {
console.log("Error parsing message:", error);
}
});
ws.on('close', () => {
connections[path] = connections[path].filter(client => client !== ws);
console.log(`Connection closed on ${path}`);
});
});
// Helper function to find a farmer by ID
function findFarmerById(id, farmers) {
return farmers.find(obj => obj.farmerId === id);
}
function findBuyerById(id, buyer) {
return buyer.find(obj => obj.id === id);
}
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});