@@ -4,6 +4,7 @@ import { Solution } from "../models/Solution.js";
44import { Category } from '../models/Category.js' ;
55import { ensureCategoriesExist } from "../utils/categoryHelper.js" ;
66import auditService from "../services/auditService.js" ;
7+ import { getISTDateBounds , istToUTC } from "../utils/timezone.js" ;
78
89export const getUsers = async ( req , res ) => {
910 try {
@@ -98,33 +99,26 @@ export const addChallenge = async (req, res) => {
9899 solution,
99100 } = req . body ;
100101
101- // Convert createdAt to midnight IST (18:30 UTC of previous day)
102102 let scheduleDate ;
103103 if ( createdAt ) {
104- const date = new Date ( createdAt ) ;
105- // Set to 18:30:00 UTC of the previous day (00:00 IST of selected day)
106- date . setUTCHours ( 18 , 30 , 0 , 0 ) ;
107- scheduleDate = date ;
104+ const istDate = new Date ( createdAt ) ;
105+ istDate . setHours ( 0 , 0 , 0 , 0 ) ;
106+ scheduleDate = istToUTC ( istDate ) ;
108107 } else {
109- // If no date provided, use next day midnight IST
110- const now = new Date ( ) ;
111- const tomorrow = new Date ( now ) ;
112- tomorrow . setDate ( tomorrow . getDate ( ) + 1 ) ;
113- tomorrow . setUTCHours ( 18 , 30 , 0 , 0 ) ;
114- scheduleDate = tomorrow ;
108+ const tomorrowIST = new Date ( ) ;
109+ tomorrowIST . setDate ( tomorrowIST . getDate ( ) + 1 ) ;
110+ tomorrowIST . setHours ( 0 , 0 , 0 , 0 ) ;
111+ scheduleDate = istToUTC ( tomorrowIST ) ;
115112 }
116113
117- // Validate required fields
118114 if ( ! title || ! description || ! category || ! difficulty || ! points || ! problemLink || ! platform ) {
119115 return res . status ( 400 ) . json ( { success : false , message : "All fields are required" } ) ;
120116 }
121117
122- // Ensure category is an array of strings
123118 if ( ! Array . isArray ( category ) ) {
124119 return res . status ( 400 ) . json ( { success : false , message : "Category must be an array of strings" } ) ;
125120 }
126121
127- // Ensure categories exist in database (create if they don't)
128122 await ensureCategoriesExist ( category ) ;
129123
130124 const newChallenge = new Challenge ( {
@@ -142,7 +136,6 @@ export const addChallenge = async (req, res) => {
142136
143137 let savedSolution = null ;
144138 if ( solution ) {
145- // Create solution with all fields, including explanation
146139 const newSolution = new Solution ( {
147140 explanation : solution . explanation || "No explanation provided" ,
148141 cpp : solution . cpp || "" ,
@@ -188,13 +181,11 @@ export const updateChallenge = async (req, res) => {
188181 solution,
189182 } = req . body ;
190183
191- // Convert createdAt to midnight IST if provided
192184 let scheduleDate ;
193185 if ( createdAt ) {
194- const date = new Date ( createdAt ) ;
195- // Set to 18:30:00 UTC of the previous day (00:00 IST of selected day)
196- date . setUTCHours ( 18 , 30 , 0 , 0 ) ;
197- scheduleDate = date ;
186+ const istDate = new Date ( createdAt ) ;
187+ istDate . setHours ( 0 , 0 , 0 , 0 ) ;
188+ scheduleDate = istToUTC ( istDate ) ;
198189 }
199190 if ( ! challengeId ) {
200191 return res . status ( 400 ) . json ( { success : false , message : "Challenge ID is required" } ) ;
@@ -203,11 +194,10 @@ export const updateChallenge = async (req, res) => {
203194 if ( ! challenge ) {
204195 return res . status ( 404 ) . json ( { success : false , message : "Challenge not found" } ) ;
205196 }
206- // Update challenge fields if provided
197+
207198 if ( title ) challenge . title = title ;
208199 if ( description ) challenge . description = description ;
209200 if ( category ) {
210- // Ensure new categories exist (create if they don't)
211201 await ensureCategoriesExist ( category ) ;
212202 challenge . category = category ;
213203 }
@@ -268,26 +258,12 @@ export const updateChallenge = async (req, res) => {
268258// Example: GET /admin/getchallenge
269259export const getTodayChallenge = async ( req , res ) => {
270260 try {
271- // Get current time in UTC
272- const now = new Date ( ) ;
273-
274- // Calculate today's and tomorrow's midnight in IST (18:30 UTC of previous day)
275- const todayIST = new Date ( now ) ;
276- todayIST . setUTCHours ( 18 , 30 , 0 , 0 ) ;
277- if ( now < todayIST ) {
278- // If current time is before today's IST midnight, adjust to previous day
279- todayIST . setDate ( todayIST . getDate ( ) - 1 ) ;
280- }
281-
282- const tomorrowIST = new Date ( todayIST ) ;
283- tomorrowIST . setDate ( tomorrowIST . getDate ( ) + 1 ) ;
261+ const { start : todayUTC , end : tomorrowUTC } = getISTDateBounds ( ) ;
284262
285- // Find today's challenge using createdAt
286- // The dates are already in UTC since we set them using setUTCHours
287263 const challenge = await Challenge . findOne ( {
288264 createdAt : {
289- $gte : todayIST ,
290- $lt : tomorrowIST
265+ $gte : todayUTC ,
266+ $lt : tomorrowUTC
291267 }
292268 } ) . sort ( { createdAt : - 1 } ) ;
293269
@@ -298,7 +274,6 @@ export const getTodayChallenge = async (req, res) => {
298274 } ) ;
299275 }
300276
301- // Manually fetch related solution (since challenge schema has no `solution` field)
302277 const solution = await Solution . findOne ( { challenge : challenge . _id } ) ;
303278
304279 return res . status ( 200 ) . json ( {
0 commit comments