@@ -2,6 +2,7 @@ import Assignment from "../models/Assignment.js";
22import Submission from "../models/Submission.js" ;
33import Course from "../models/Course.js" ;
44import { emitToCourse , emitToUser } from "../services/socketService.js" ;
5+ import { pushNotification } from "../services/notificationService.js" ;
56
67// ─── POST /api/courses/:courseId/assignments ──────────────────────────────────
78export async function createAssignment ( req , res ) {
@@ -29,6 +30,12 @@ export async function createAssignment(req, res) {
2930
3031 const formatted = formatAssignment ( assignment ) ;
3132 emitToCourse ( courseId , "assignment:new" , formatted ) ;
33+
34+ // Notify every enrolled student
35+ course . enrolledStudents . forEach ( ( studentId ) => {
36+ pushNotification ( studentId . toString ( ) , `📝 New assignment: "${ title } "` , "course" ) ;
37+ } ) ;
38+
3239 res . status ( 201 ) . json ( formatted ) ;
3340 } catch ( err ) {
3441 console . error ( "createAssignment error:" , err ) ;
@@ -144,14 +151,19 @@ export async function submitAssignment(req, res) {
144151 { upsert : true , new : true }
145152 ) ;
146153
147- // Notify teacher in real-time
154+ // Notify teacher in real-time (socket) + persist notification
148155 emitToUser ( course . teacher . toString ( ) , "assignment:submitted" , {
149156 assignmentId : id ,
150157 assignmentTitle : assignment . title ,
151158 studentId,
152159 courseId : assignment . course . toString ( ) ,
153160 submittedAt : now ,
154161 } ) ;
162+ pushNotification (
163+ course . teacher . toString ( ) ,
164+ `📤 A student submitted assignment: "${ assignment . title } "` ,
165+ "course"
166+ ) ;
155167
156168 res . status ( 201 ) . json ( formatSubmission ( submission ) ) ;
157169 } catch ( err ) {
@@ -218,7 +230,7 @@ export async function gradeSubmission(req, res) {
218230 submission . status = "graded" ;
219231 await submission . save ( ) ;
220232
221- // Notify the student of their grade
233+ // Notify the student of their grade (socket) + persist notification
222234 emitToUser ( submission . student . toString ( ) , "assignment:graded" , {
223235 assignmentId : submission . assignment . _id . toString ( ) ,
224236 assignmentTitle : submission . assignment . title ,
@@ -227,6 +239,11 @@ export async function gradeSubmission(req, res) {
227239 feedback : submission . feedback ,
228240 maxScore : submission . assignment . maxScore ,
229241 } ) ;
242+ pushNotification (
243+ submission . student . toString ( ) ,
244+ `✅ Your assignment "${ submission . assignment . title } " was graded: ${ submission . score } /${ submission . assignment . maxScore } ` ,
245+ "course"
246+ ) ;
230247
231248 res . json ( formatSubmission ( submission ) ) ;
232249 } catch ( err ) {
0 commit comments