1- /*
2- 1. Since no categories are assigned in the db yet the category search cannot be performed
3- 2. Also commented the the res.user since validation is not yet started we cannot use that
4- 3. Since no category id is present i added random category id
5- */
61import Category from "../models/categories.js" ;
72import Item from "../models/items.js" ;
8- import User from "../models/users.js"
3+ import User from "../models/users.js" ;
94
105export const donateItem = async ( req , res ) => {
116 try {
127 console . log ( "req.body:" , req . body ) ;
138 console . log ( "req.files:" , req . files ) ;
149
15- if ( ! req . user ) {
16- return res . status ( 401 ) . json ( {
17- error : "Unauthorized" ,
18- message : "User not authenticated" ,
19- } ) ;
20- }
21-
10+ if ( ! req . user ) {
11+ return res . status ( 401 ) . json ( {
12+ error : "Unauthorized" ,
13+ message : "User not authenticated" ,
14+ } ) ;
15+ }
2216
2317 const {
2418 itemTitle,
@@ -34,15 +28,10 @@ export const donateItem = async (req, res) => {
3428 contactMethods,
3529 } = req . body ;
3630
37-
38-
39- //increment points after a donation
4031 await User . findByIdAndUpdate ( req . user . _id , {
41- $inc : { points : 10 }
42- } ) ;
43-
32+ $inc : { points : 10 } ,
33+ } ) ;
4434
45- // Validate required fields
4635 if ( ! itemTitle || ! itemTitle . trim ( ) ) {
4736 return res . status ( 400 ) . json ( {
4837 error : "Validation Error" ,
@@ -64,41 +53,17 @@ export const donateItem = async (req, res) => {
6453 } ) ;
6554 }
6655
67-
68- // Find category document
6956 const categoryDoc = await Category . findOne ( {
70- name : category . trim ( ) . replace ( / \r ? \n / g, " " ) ,
71- } ) ;
57+ name : category . trim ( ) . replace ( / \r ? \n / g, " " ) ,
58+ } ) ;
7259
7360 if ( ! categoryDoc ) {
74- return res . status ( 400 ) . json ( {
75- error : "Invalid Category" ,
76- message : "The selected category does not exist" ,
77- } ) ;
78- }
79-
80- //Find Donor
81-
82-
83-
84- // // Validate subcategory if provided
85- // if (
86- // subcategory &&
87- // categoryDoc.subcategories &&
88- // categoryDoc.subcategories.length > 0
89- // ) {
90- // const validSubcategory = categoryDoc.subcategories.includes(
91- // subcategory.trim()
92- // );
93- // if (!validSubcategory) {
94- // return res.status(400).json({
95- // error: "Invalid Subcategory",
96- // message: "The selected subcategory is not valid for this category",
97- // });
98- // }
99- // }
100-
101- // Parse and validate price if it's a paid donation
61+ return res . status ( 400 ) . json ( {
62+ error : "Invalid Category" ,
63+ message : "The selected category does not exist" ,
64+ } ) ;
65+ }
66+
10267 let finalPrice = 0 ;
10368 if ( isPaid === "yes" || isPaid === true ) {
10469 finalPrice = parseFloat ( price ) ;
@@ -110,7 +75,6 @@ export const donateItem = async (req, res) => {
11075 }
11176 }
11277
113- // Handle contact methods - ensure it's always an array
11478 let contactMethodsArray = [ ] ;
11579 if ( contactMethods ) {
11680 if ( Array . isArray ( contactMethods ) ) {
@@ -120,38 +84,32 @@ export const donateItem = async (req, res) => {
12084 }
12185 }
12286
87+ let finalDate = null ;
88+ if ( availableUntil ) {
89+ finalDate = new Date ( availableUntil ) ;
90+ const today = new Date ( ) ;
91+ today . setHours ( 0 , 0 , 0 , 0 ) ;
12392
124- //validating date
125- // Validate availableUntil
126- let finalDate = null ;
127- if ( availableUntil ) {
128- finalDate = new Date ( availableUntil ) ;
129- const today = new Date ( ) ;
130- today . setHours ( 0 , 0 , 0 , 0 ) ; // normalize to midnight
131-
132- if ( isNaN ( finalDate . getTime ( ) ) ) {
133- return res . status ( 400 ) . json ( {
134- error : "Validation Error" ,
135- message : "Invalid date format for availableUntil"
136- } ) ;
137- }
138-
139- if ( finalDate < today ) {
140- return res . status ( 400 ) . json ( {
141- error : "Validation Error" ,
142- message : "Available until date cannot be in the past"
143- } ) ;
144- }
145- }
93+ if ( isNaN ( finalDate . getTime ( ) ) ) {
94+ return res . status ( 400 ) . json ( {
95+ error : "Validation Error" ,
96+ message : "Invalid date format for availableUntil" ,
97+ } ) ;
98+ }
14699
100+ if ( finalDate < today ) {
101+ return res . status ( 400 ) . json ( {
102+ error : "Validation Error" ,
103+ message : "Available until date cannot be in the past" ,
104+ } ) ;
105+ }
106+ }
147107
148- // Handle image URLs // currently storing file paths, need to be changes to Urls when w store it in cloud
149108 const imageURLs =
150109 req . files && req . files . length > 0
151110 ? req . files . map ( ( f ) => `/uploads/${ f . filename } ` )
152111 : [ ] ;
153112
154- // Create the item
155113 const newItem = await Item . create ( {
156114 name : itemTitle . trim ( ) ,
157115 description : description ? description . trim ( ) : "" ,
@@ -181,7 +139,6 @@ if (availableUntil) {
181139 } catch ( err ) {
182140 console . error ( "Donation error:" , err ) ;
183141
184- // Handle mongoose validation errors
185142 if ( err . name === "ValidationError" ) {
186143 const messages = Object . values ( err . errors ) . map ( ( e ) => e . message ) ;
187144 return res . status ( 400 ) . json ( {
@@ -190,7 +147,6 @@ if (availableUntil) {
190147 } ) ;
191148 }
192149
193- // Handle duplicate key errors
194150 if ( err . code === 11000 ) {
195151 return res . status ( 400 ) . json ( {
196152 error : "Duplicate Error" ,
0 commit comments