Skip to content

Commit 23ee662

Browse files
chore(apps/olat-api): catch errors
1 parent d0436c5 commit 23ee662

1 file changed

Lines changed: 52 additions & 32 deletions

File tree

apps/olat-api/src/index.ts

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { validate as validateUUID } from 'uuid'
99

1010
const app: express.Express = express()
1111
const prisma = new PrismaClient()
12-
const PORT = process.env.PORT || 3020
12+
const PORT = process.env.PORT || 3000
1313

1414
const API_NAME = 'olat-api'
1515
const API_KEY = process.env.OLAT_API_KEY
@@ -159,15 +159,15 @@ app.get('/api/configuration/courses', (req: Request, res: Response) => {
159159
}
160160

161161
res.set('Content-Type', 'application/json')
162-
res.status(StatusCode.SUCCESS).json({
162+
return res.status(StatusCode.SUCCESS).json({
163163
courses: courses,
164164
timestamp: new Date().toISOString(),
165165
api: API_NAME,
166166
})
167167
})
168168
.catch((error) => {
169169
console.error('Error fetching courses:', error)
170-
res
170+
return res
171171
.status(StatusCode.INTERNAL_SERVER_ERROR)
172172
.json({ error: 'Internal server error' })
173173
})
@@ -193,20 +193,27 @@ app.get('/api/configuration/activityTypes', (req: Request, res: Response) => {
193193
.json({ error: 'Invalid request headers' })
194194
}
195195

196-
getActivityTypes().then((activityTypes) => {
197-
if (activityTypes === null) {
198-
return res
199-
.status(StatusCode.NOT_FOUND)
200-
.json({ error: 'Course not found' })
201-
}
196+
getActivityTypes()
197+
.then((activityTypes) => {
198+
if (activityTypes === null) {
199+
return res
200+
.status(StatusCode.NOT_FOUND)
201+
.json({ error: 'Course not found' })
202+
}
202203

203-
res.set('Content-Type', 'application/json')
204-
res.status(StatusCode.SUCCESS).json({
205-
activityTypes: activityTypes,
206-
timestamp: new Date().toISOString(),
207-
api: API_NAME,
204+
res.set('Content-Type', 'application/json')
205+
return res.status(StatusCode.SUCCESS).json({
206+
activityTypes: activityTypes,
207+
timestamp: new Date().toISOString(),
208+
api: API_NAME,
209+
})
210+
})
211+
.catch((error) => {
212+
console.error('Error fetching activity types:', error)
213+
return res
214+
.status(StatusCode.INTERNAL_SERVER_ERROR)
215+
.json({ error: 'Internal server error' })
208216
})
209-
})
210217
})
211218

212219
async function getCourseActivityTypes(courseId: string): Promise<any[] | null> {
@@ -310,20 +317,27 @@ app.get(
310317
.json({ error: 'Invalid courseID' })
311318
}
312319

313-
getCourseActivityTypes(courseID).then((activityTypes) => {
314-
if (activityTypes === null) {
315-
return res
316-
.status(StatusCode.NOT_FOUND)
317-
.json({ error: 'Course not found' })
318-
}
320+
getCourseActivityTypes(courseID)
321+
.then((activityTypes) => {
322+
if (activityTypes === null) {
323+
return res
324+
.status(StatusCode.NOT_FOUND)
325+
.json({ error: 'Course not found' })
326+
}
319327

320-
res.set('Content-Type', 'application/json')
321-
res.status(StatusCode.SUCCESS).json({
322-
activityTypes: activityTypes,
323-
timestamp: new Date().toISOString(),
324-
api: API_NAME,
328+
res.set('Content-Type', 'application/json')
329+
return res.status(StatusCode.SUCCESS).json({
330+
activityTypes: activityTypes,
331+
timestamp: new Date().toISOString(),
332+
api: API_NAME,
333+
})
334+
})
335+
.catch((error) => {
336+
console.error('Error fetching activity types for course:', error)
337+
return res
338+
.status(StatusCode.INTERNAL_SERVER_ERROR)
339+
.json({ error: 'Internal server error' })
325340
})
326-
})
327341
}
328342
)
329343

@@ -345,7 +359,8 @@ async function getActivities(
345359
const activityDetails = (
346360
course.liveQuizzes ??
347361
course.practiceQuizzes ??
348-
course.microLearnings
362+
course.microLearnings ??
363+
[]
349364
) // NOTE: modify if required
350365
.map((activity) => ({
351366
id: activity.id,
@@ -380,8 +395,8 @@ app.get(
380395
activityKeysSubselection.indexOf(activityTypeKey) !== -1 &&
381396
validateUUID(courseID)
382397
) {
383-
getActivities(courseID, activityTypeKey as ActivityTypeSubselection).then(
384-
(activityTypes) => {
398+
getActivities(courseID, activityTypeKey as ActivityTypeSubselection)
399+
.then((activityTypes) => {
385400
if (activityTypes === null) {
386401
return res
387402
.status(StatusCode.NOT_FOUND)
@@ -394,8 +409,13 @@ app.get(
394409
timestamp: new Date().toISOString(),
395410
api: API_NAME,
396411
})
397-
}
398-
)
412+
})
413+
.catch((error) => {
414+
console.error('Error fetching activity type for course:', error)
415+
return res
416+
.status(StatusCode.INTERNAL_SERVER_ERROR)
417+
.json({ error: 'Internal server error' })
418+
})
399419
} else if (activityKeysGeneral.indexOf(activityTypeKey) !== -1) {
400420
return res.status(StatusCode.SUCCESS).json({
401421
activityTypes: [],

0 commit comments

Comments
 (0)