-
Notifications
You must be signed in to change notification settings - Fork 6
Implement CalendarService #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f3b87e9
feat: implement CalendarService for fetching NTUT academic calendar e…
kevinlee-06 6cf79c2
test: add reference tests for CalendarService and calModeApp JSON
kevinlee-06 f65a132
docs: update last updated date and add CalendarService to services list
kevinlee-06 c45fb28
fix: run dart format
kevinlee-06 d229d16
fix: comment out debug code
kevinlee-06 8723d27
Update test/services/calendar_service_test.dart
kevinlee-06 064c0ba
Update test/services/calendar_service_test.dart
kevinlee-06 8989283
Update lib/services/calendar_service.dart
kevinlee-06 33a637e
Merge branch 'main' into NTUT-Calendar-Service
kevinlee-06 0b873f1
refactor: consolidate academic calendar event fetching into PortalSer…
kevinlee-06 25e3c20
refactor: move calendar tests into portal_service_test.dart and delet…
Copilot 1f5423f
test: normalize portal_service_test group names to match method-name …
rileychh 48e7618
Merge pull request #192 from NTUT-NPC/copilot/sub-pr-154
rileychh a48dfcb
refactor: filter out weekend markers from getCalendar and remove isHo…
rileychh 2f2e0a7
refactor: use semantic names and proper types in CalendarEventDto
rileychh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kevinlee-06 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:intl/intl.dart'; | ||
| import 'package:riverpod/riverpod.dart'; | ||
| import 'package:tattoo/services/portal_service.dart'; | ||
| import 'package:tattoo/utils/http.dart'; | ||
|
|
||
| /// Represents a calendar event from the NTUT Portal. | ||
| /// | ||
| /// Events come in two flavors: | ||
| /// - **Named events** with [id], [calTitle], [ownerName], [creatorName] | ||
| /// (e.g., exam periods, registration deadlines) | ||
| /// - **Holiday markers** with [isHoliday] = "1" and an empty title | ||
| /// (weekends and national holidays) | ||
| typedef CalendarEventDto = ({ | ||
| /// Event ID (absent for holiday markers). | ||
| int? id, | ||
|
|
||
| /// Event start time (epoch milliseconds). | ||
| int? calStart, | ||
|
|
||
| /// Event end time (epoch milliseconds). | ||
| int? calEnd, | ||
|
|
||
| /// Whether this is an all-day event ("1" = yes). | ||
| String? allDay, | ||
|
|
||
| /// Event title / description. | ||
| String? calTitle, | ||
|
|
||
| /// Event location. | ||
| String? calPlace, | ||
|
|
||
| /// Event content / details. | ||
| String? calContent, | ||
|
|
||
| /// Owner name (e.g., "學校行事曆"). | ||
| String? ownerName, | ||
|
|
||
| /// Creator name (e.g., "教務處"). | ||
| String? creatorName, | ||
|
|
||
| /// Whether this is a holiday ("1" = yes). | ||
| String? isHoliday, | ||
| }); | ||
|
|
||
| /// Provides the singleton [CalendarService] instance. | ||
| final calendarServiceProvider = Provider<CalendarService>( | ||
| (ref) => CalendarService(ref.read(portalServiceProvider)), | ||
| ); | ||
|
|
||
| /// Service for fetching NTUT academic calendar events. | ||
| /// | ||
| /// Uses the `calModeApp.do` JSON API on the NTUT Portal host. | ||
| /// Requires an active portal session (shared cookie jar from [PortalService.login]). | ||
| class CalendarService { | ||
| final Dio _dio; | ||
|
|
||
| CalendarService(PortalService portalService) : _dio = portalService.portalDio; | ||
|
|
||
| /// Fetches academic calendar events within a date range. | ||
| /// | ||
| /// Returns a list of calendar events (e.g., holidays, exam periods, | ||
| /// registration deadlines) between [startDate] and [endDate] inclusive. | ||
| /// | ||
| /// Requires an active portal session (call [PortalService.login] first). | ||
| Future<List<CalendarEventDto>> getCalendar( | ||
| DateTime startDate, | ||
| DateTime endDate, | ||
| ) async { | ||
| final formatter = DateFormat('yyyy/MM/dd'); | ||
| final response = await _dio.get( | ||
| 'calModeApp.do', | ||
| queryParameters: { | ||
| 'startDate': formatter.format(startDate), | ||
| 'endDate': formatter.format(endDate), | ||
| }, | ||
| ); | ||
|
|
||
| final List<dynamic> events = jsonDecode(response.data); | ||
| return events.map<CalendarEventDto>((e) { | ||
| String? normalizeEmpty(String? value) => | ||
| value?.isNotEmpty == true ? value : null; | ||
|
|
||
kevinlee-06 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return ( | ||
| id: e['id'] as int?, | ||
| calStart: e['calStart'] as int?, | ||
| calEnd: e['calEnd'] as int?, | ||
| allDay: normalizeEmpty(e['allDay'] as String?), | ||
| calTitle: normalizeEmpty(e['calTitle'] as String?), | ||
| calPlace: normalizeEmpty(e['calPlace'] as String?), | ||
| calContent: normalizeEmpty(e['calContent'] as String?), | ||
| ownerName: normalizeEmpty(e['ownerName'] as String?), | ||
| creatorName: normalizeEmpty(e['creatorName'] as String?), | ||
| isHoliday: normalizeEmpty(e['isHoliday'] as String?), | ||
| ); | ||
| }).toList(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kevinlee-06 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:tattoo/services/calendar_service.dart'; | ||
| import 'package:tattoo/services/portal_service.dart'; | ||
|
|
||
| import '../test_helpers.dart'; | ||
|
|
||
| /// Reference test to inspect raw JSON from calModeApp.do. | ||
| /// | ||
| /// Run with: | ||
| /// ```bash | ||
| /// flutter test --dart-define-from-file=test/test_config.json -r expanded test/services/cal_debug_test.dart | ||
| /// ``` | ||
| /// | ||
| /// Example response (2025/09 semester): | ||
| /// ```json | ||
| /// [ | ||
| /// { | ||
| /// "id": 60564, | ||
| /// "calStart": 1755619200000, | ||
| /// "calEnd": 1756818000000, | ||
| /// "calTitle": "新生網路預選(日間部 17:00 截止;進修部 21:00 截止)", | ||
| /// "calPlace": "", | ||
| /// "calContent": "", | ||
| /// "calColor": "#DDDDDD;#000000", | ||
| /// "ownerId": "1540521049552", | ||
| /// "ownerName": "學校行事曆", | ||
| /// "creatorId": "ntutoaa", | ||
| /// "creatorName": "教務處", | ||
| /// "modifyDate": 1751339649000, | ||
| /// "hasBeenDeleted": 0, | ||
| /// "calAlertList": [], | ||
| /// "calInviteeList": [] | ||
| /// }, | ||
| /// { | ||
| /// "calStart": 1758297600000, | ||
| /// "calEnd": 1758384000000, | ||
| /// "allDay": "1", | ||
| /// "calTitle": "", | ||
| /// "ownerId": "holiday_system", | ||
| /// "isHoliday": "1", | ||
| /// "calAlertList": [], | ||
| /// "calInviteeList": [] | ||
| /// } | ||
| /// ] | ||
| /// ``` | ||
| void main() { | ||
| test('print raw calModeApp.do JSON', () async { | ||
| TestCredentials.validate(); | ||
| final portalService = PortalService(); | ||
| await portalService.login( | ||
| TestCredentials.username, | ||
| TestCredentials.password, | ||
| ); | ||
|
|
||
| final events = await CalendarService(portalService).getCalendar( | ||
| DateTime(2025, 1, 1), | ||
| DateTime(2026, 12, 31), | ||
| ); | ||
kevinlee-06 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| print('=== ${events.length} events ==='); | ||
| for (final e in events) { | ||
| final start = e.calStart != null | ||
| ? DateTime.fromMillisecondsSinceEpoch(e.calStart!) | ||
| : null; | ||
| final end = e.calEnd != null | ||
| ? DateTime.fromMillisecondsSinceEpoch(e.calEnd!) | ||
| : null; | ||
| final holiday = e.isHoliday == '1' ? ' [holiday]' : ''; | ||
| print(' $start ~ $end: ${e.calTitle ?? "(no title)"}$holiday'); | ||
| } | ||
| }); | ||
kevinlee-06 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:tattoo/services/calendar_service.dart'; | ||
| import 'package:tattoo/services/portal_service.dart'; | ||
|
|
||
| import '../test_helpers.dart'; | ||
|
|
||
| void main() { | ||
| group('CalendarService Tests', () { | ||
| late PortalService portalService; | ||
| late CalendarService calendarService; | ||
|
|
||
| setUpAll(() { | ||
| TestCredentials.validate(); | ||
| }); | ||
|
|
||
| setUp(() async { | ||
| portalService = PortalService(); | ||
| calendarService = CalendarService(portalService); | ||
| await portalService.login( | ||
| TestCredentials.username, | ||
| TestCredentials.password, | ||
| ); | ||
kevinlee-06 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| test('should return calendar events for a semester date range', () async { | ||
| final events = await calendarService.getCalendar( | ||
| DateTime(2025, 1, 1), | ||
| DateTime(2025, 6, 30), | ||
| ); | ||
|
|
||
| expect(events, isNotEmpty, reason: 'Semester should have events'); | ||
|
|
||
| // Verify structure of first event (skip holidays with empty titles) | ||
| final event = events.firstWhere((e) => e.calTitle != null); | ||
kevinlee-06 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| expect(event.calTitle, isNotNull, reason: 'Event should have a title'); | ||
| expect( | ||
| event.calStart, | ||
| isNotNull, | ||
| reason: 'Event should have a start time', | ||
| ); | ||
| }); | ||
|
|
||
| test('should return empty list for a date range with no events', () async { | ||
| // A single day far in the past unlikely to have events | ||
| final events = await calendarService.getCalendar( | ||
| DateTime(2000, 1, 1), | ||
| DateTime(2000, 1, 2), | ||
| ); | ||
|
|
||
| // May still contain weekend/holiday markers, but should be a valid list | ||
| expect(events, isA<List>()); | ||
| }); | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.