@@ -35,3 +35,97 @@ export const updateCourseName = async (code, newName) => {
3535 throw error ;
3636 }
3737} ;
38+
39+ // Create a new course
40+ export const createCourse = async ( code , name ) => {
41+ try {
42+ const response = await fetch ( `${ API_BASE_URL } api/course/create/${ code } ` , {
43+ method : "POST" ,
44+ headers : {
45+ "Content-Type" : "application/json" ,
46+ Authorization : "Bearer admin-coursehub-cc23-golang" ,
47+ } ,
48+ body : JSON . stringify ( { name } ) ,
49+ credentials : "include" ,
50+ } ) ;
51+ return await response . json ( ) ;
52+ } catch ( error ) {
53+ console . error ( "Error creating course:" , error ) ;
54+ throw error ;
55+ }
56+ } ;
57+
58+ // Bulk sync courses from CSV data
59+ export const bulkSyncCourses = async ( courses , analysis , onProgress = null ) => {
60+ const results = {
61+ created : [ ] ,
62+ updated : [ ] ,
63+ skipped : [ ] ,
64+ errors : [ ] ,
65+ } ;
66+
67+ // Only process courses that need changes
68+ const coursesToProcess = [ ] ;
69+
70+ // Add missing courses (need to be created)
71+ if ( analysis . missingCourses && analysis . missingCourses . length > 0 ) {
72+ coursesToProcess . push ( ...analysis . missingCourses ) ;
73+ }
74+
75+ // Add courses with name mismatches (need to be updated)
76+ if ( analysis . nameConflicts && analysis . nameConflicts . length > 0 ) {
77+ coursesToProcess . push (
78+ ...analysis . nameConflicts . map ( ( conflict ) => ( {
79+ code : conflict . code ,
80+ name : conflict . csvName ,
81+ } ) )
82+ ) ;
83+ }
84+
85+ const total = coursesToProcess . length ;
86+ let processed = 0 ;
87+
88+ if ( total === 0 ) {
89+ // No courses need processing
90+ if ( onProgress ) {
91+ onProgress ( 0 , 0 ) ;
92+ }
93+ return results ;
94+ }
95+
96+ for ( const course of coursesToProcess ) {
97+ try {
98+ // Check if this is a missing course (needs creation) or existing course (needs update)
99+ const isMissingCourse =
100+ analysis . missingCourses &&
101+ analysis . missingCourses . some ( ( c ) => c . code === course . code ) ;
102+
103+ if ( isMissingCourse ) {
104+ // Create new course
105+ const createResponse = await createCourse ( course . code , course . name ) ;
106+ if ( createResponse . message === "Course created successfully" ) {
107+ results . created . push ( course ) ;
108+ } else {
109+ results . errors . push ( { course, error : "Failed to create course" } ) ;
110+ }
111+ } else {
112+ // Update existing course name
113+ const updateResponse = await updateCourseName ( course . code , course . name ) ;
114+ if ( updateResponse && updateResponse . code ) {
115+ results . updated . push ( course ) ;
116+ } else {
117+ results . errors . push ( { course, error : "Failed to update course name" } ) ;
118+ }
119+ }
120+ } catch ( error ) {
121+ results . errors . push ( { course, error : error . message } ) ;
122+ }
123+
124+ processed ++ ;
125+ if ( onProgress ) {
126+ onProgress ( processed , total ) ;
127+ }
128+ }
129+
130+ return results ;
131+ } ;
0 commit comments