feat: Add and test the finance and task api routes
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import Finance from "../models/financeModel.js";
|
||||
import Farm from "../models/farmModel.js";
|
||||
const Finance = require("../Models/finance.model.js");
|
||||
const Farm = require("../Models/farm.model.js");
|
||||
|
||||
// Create finance record for a farm
|
||||
export const createFinance = async (req, res) => {
|
||||
const createFinance = async (req, res) => {
|
||||
try {
|
||||
const { farm } = req.body;
|
||||
|
||||
@@ -31,8 +31,9 @@ export const createFinance = async (req, res) => {
|
||||
};
|
||||
|
||||
// Get finance details by farm ID
|
||||
export const getFinanceByFarm = async (req, res) => {
|
||||
const getFinanceByFarm = async (req, res) => {
|
||||
try {
|
||||
console.log("My farm id is : ", req.params.farmId);
|
||||
const finance = await Finance.findOne({ farm: req.params.farmId });
|
||||
|
||||
if (!finance)
|
||||
@@ -45,7 +46,7 @@ export const getFinanceByFarm = async (req, res) => {
|
||||
};
|
||||
|
||||
// Add a transaction (expense/revenue)
|
||||
export const addTransaction = async (req, res) => {
|
||||
const addTransaction = async (req, res) => {
|
||||
try {
|
||||
const { type, amount, description } = req.body;
|
||||
|
||||
@@ -70,7 +71,7 @@ export const addTransaction = async (req, res) => {
|
||||
};
|
||||
|
||||
// Delete a transaction
|
||||
export const deleteTransaction = async (req, res) => {
|
||||
const deleteTransaction = async (req, res) => {
|
||||
try {
|
||||
const finance = await Finance.findById(req.params.financeId);
|
||||
if (!finance)
|
||||
@@ -97,7 +98,7 @@ export const deleteTransaction = async (req, res) => {
|
||||
};
|
||||
|
||||
// Get all transactions for a farm's finance
|
||||
export const getTransactions = async (req, res) => {
|
||||
const getTransactions = async (req, res) => {
|
||||
try {
|
||||
const finance = await Finance.findById(req.params.financeId);
|
||||
if (!finance)
|
||||
@@ -110,7 +111,7 @@ export const getTransactions = async (req, res) => {
|
||||
};
|
||||
|
||||
// Get total expenses and revenue
|
||||
export const getFinancialSummary = async (req, res) => {
|
||||
const getFinancialSummary = async (req, res) => {
|
||||
try {
|
||||
const finance = await Finance.findById(req.params.financeId);
|
||||
if (!finance)
|
||||
@@ -124,3 +125,12 @@ export const getFinancialSummary = async (req, res) => {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createFinance,
|
||||
getFinanceByFarm,
|
||||
addTransaction,
|
||||
deleteTransaction,
|
||||
getTransactions,
|
||||
getFinancialSummary,
|
||||
};
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import Task from "../models/taskModel.js";
|
||||
import Farm from "../models/farmModel.js";
|
||||
import Crop from "../models/cropModel.js";
|
||||
const Task = require("../Models/task.model.js");
|
||||
const Farm = require("../Models/farm.model.js");
|
||||
const Crop = require("../Models/crop.model.js");
|
||||
|
||||
// Create a new task
|
||||
export const createTask = async (req, res) => {
|
||||
const createTask = async (req, res) => {
|
||||
try {
|
||||
const { farm, crop, taskType, description, assignedDate, status } =
|
||||
req.body;
|
||||
@@ -37,7 +37,7 @@ export const createTask = async (req, res) => {
|
||||
};
|
||||
|
||||
// Get all tasks for a specific farm
|
||||
export const getTasksByFarm = async (req, res) => {
|
||||
const getTasksByFarm = async (req, res) => {
|
||||
try {
|
||||
const tasks = await Task.find({ farm: req.params.farmId }).populate("crop");
|
||||
|
||||
@@ -48,7 +48,7 @@ export const getTasksByFarm = async (req, res) => {
|
||||
};
|
||||
|
||||
// Get a single task by ID
|
||||
export const getTaskById = async (req, res) => {
|
||||
const getTaskById = async (req, res) => {
|
||||
try {
|
||||
const task = await Task.findById(req.params.taskId).populate("farm crop");
|
||||
|
||||
@@ -61,7 +61,7 @@ export const getTaskById = async (req, res) => {
|
||||
};
|
||||
|
||||
// Update task details
|
||||
export const updateTask = async (req, res) => {
|
||||
const updateTask = async (req, res) => {
|
||||
try {
|
||||
const updatedTask = await Task.findByIdAndUpdate(
|
||||
req.params.taskId,
|
||||
@@ -79,7 +79,7 @@ export const updateTask = async (req, res) => {
|
||||
};
|
||||
|
||||
// Delete a task
|
||||
export const deleteTask = async (req, res) => {
|
||||
const deleteTask = async (req, res) => {
|
||||
try {
|
||||
const task = await Task.findById(req.params.taskId);
|
||||
|
||||
@@ -94,7 +94,7 @@ export const deleteTask = async (req, res) => {
|
||||
};
|
||||
|
||||
// Update task status (Pending → Completed)
|
||||
export const updateTaskStatus = async (req, res) => {
|
||||
const updateTaskStatus = async (req, res) => {
|
||||
try {
|
||||
const { status } = req.body;
|
||||
|
||||
@@ -109,3 +109,12 @@ export const updateTaskStatus = async (req, res) => {
|
||||
res.status(500).json({ message: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createTask,
|
||||
getTasksByFarm,
|
||||
getTaskById,
|
||||
updateTask,
|
||||
deleteTask,
|
||||
updateTaskStatus,
|
||||
};
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
import express from "express";
|
||||
import {
|
||||
createFinanceRecord,
|
||||
getFinanceRecords,
|
||||
getFinanceById,
|
||||
updateFinance,
|
||||
deleteFinance,
|
||||
const express = require("express");
|
||||
const {
|
||||
addTransaction,
|
||||
} from "../controllers/financeController.js";
|
||||
import { checkAuthenticated } from "../Middlewares/authentication.js";
|
||||
createFinance,
|
||||
getFinanceByFarm,
|
||||
deleteTransaction,
|
||||
getTransactions,
|
||||
getFinancialSummary,
|
||||
} = require("../Controllers/finance.controller.js");
|
||||
const { checkAuthenticated } = require("../Middlewares/authentication.js");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Routes for finance management
|
||||
router.post("/", checkAuthenticated, createFinanceRecord); // Create a new finance record
|
||||
router.get("/", checkAuthenticated, getFinanceRecords); // Get all finance records
|
||||
router.get("/:financeId", checkAuthenticated, getFinanceById); // Get a finance record by ID
|
||||
router.put("/:financeId", checkAuthenticated, updateFinance); // Update finance record
|
||||
router.delete("/:financeId", checkAuthenticated, deleteFinance); // Delete a finance record
|
||||
router.post("/", checkAuthenticated, createFinance); // Create a new finance record
|
||||
router.get("/:farmId", checkAuthenticated, getFinanceByFarm); // Get all finance records
|
||||
router.get("/transactions/:financeId", checkAuthenticated, getTransactions); // Get a finance record by ID
|
||||
router.get("/summary/:financeId", checkAuthenticated, getFinancialSummary); //
|
||||
router.delete("/:financeId", checkAuthenticated, deleteTransaction); // Delete a finance record
|
||||
|
||||
// Add transactions (Expense/Revenue) to a finance record
|
||||
router.post("/:financeId/transaction", checkAuthenticated, addTransaction);
|
||||
|
||||
export default router;
|
||||
module.exports = router;
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import express from "express";
|
||||
import {
|
||||
const { checkAuthenticated } = require("../Middlewares/authentication.js");
|
||||
|
||||
const express = require("express");
|
||||
const {
|
||||
createTask,
|
||||
getTasksByFarm,
|
||||
getTaskById,
|
||||
updateTask,
|
||||
deleteTask,
|
||||
updateTaskStatus,
|
||||
} from "../controllers/taskController.js";
|
||||
const { checkAuthenticated } = require("../Middlewares/authentication.js");
|
||||
} = require("../Controllers/task.controller.js");
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -19,6 +20,6 @@ router.put("/:taskId", checkAuthenticated, updateTask); // Update task details
|
||||
router.delete("/:taskId", checkAuthenticated, deleteTask); // Delete a task
|
||||
|
||||
// Update task status (Pending → Completed)
|
||||
router.patch("/:taskId/status", protect, updateTaskStatus);
|
||||
router.patch("/:taskId/status", checkAuthenticated, updateTaskStatus);
|
||||
|
||||
export default router;
|
||||
module.exports = router;
|
||||
|
||||
+7
-3
@@ -1,12 +1,12 @@
|
||||
const express = require("express");
|
||||
const cors = require("cors");
|
||||
const cookieParser = require("cookie-parser");
|
||||
const { Server } = require("socket.io");
|
||||
const { createServer } = require("http");
|
||||
|
||||
const userRoute = require("./Routes/user.routes.js");
|
||||
const farmRoute = require("./Routes/farm.routes.js");
|
||||
const cropRoute = require("./Routes/crop.routes.js");
|
||||
|
||||
const financeRoute = require("./Routes/finance.routes.js");
|
||||
const taskRoute = require("./Routes/task.routes.js");
|
||||
const { checkAuthenticated } = require("./Middlewares/authentication.js");
|
||||
const dotenv = require("dotenv");
|
||||
|
||||
@@ -38,4 +38,8 @@ app.use("/api/v1/farm", farmRoute);
|
||||
|
||||
app.use("/api/v1/crop", cropRoute);
|
||||
|
||||
app.use("/api/v1/finance", financeRoute);
|
||||
|
||||
app.use("/api/v1/task", taskRoute);
|
||||
|
||||
module.exports = app;
|
||||
|
||||
Generated
+2625
File diff suppressed because it is too large
Load Diff
@@ -10,12 +10,15 @@
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"@huggingface/transformers": "^3.3.3",
|
||||
"@xenova/transformers": "^2.17.2",
|
||||
"bcrypt": "^5.1.1",
|
||||
"cloudinary": "^2.5.1",
|
||||
"cookie-parser": "^1.4.6",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.19.2",
|
||||
"jimp": "^1.6.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"mongoose": "^8.6.1",
|
||||
"multer": "^1.4.5-lts.1",
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { pipeline } from "@xenova/transformers";
|
||||
import Jimp from "jimp";
|
||||
|
||||
async function main() {
|
||||
const modelName = "Xenova/distilbert-base-uncased-finetuned-sst-2-english"; // Example model
|
||||
|
||||
// Load the model
|
||||
const classifier = await pipeline("image-classification", modelName);
|
||||
|
||||
// Load the image
|
||||
let image;
|
||||
try {
|
||||
image = await Jimp.read("/home/karan/Downloads/tomato.png");
|
||||
image.rgba(true);
|
||||
} catch (error) {
|
||||
console.error("Error: Unable to open image. Check the file type or path.");
|
||||
console.error(error);
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert image to buffer
|
||||
const buffer = await image.getBufferAsync(Jimp.MIME_PNG);
|
||||
|
||||
// Classify the image
|
||||
const result = await classifier(buffer);
|
||||
|
||||
console.log("Predicted class:", result);
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
|
||||
Reference in New Issue
Block a user