Skip to content
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

服用履歴の連番対応(after: 2025-04-01) #1291

Merged
merged 58 commits into from
Jan 16, 2025

Conversation

bannzai
Copy link
Owner

@bannzai bannzai commented Jan 14, 2025

Title

服用履歴の連番対応(after: 2025-04-01)


PR Type

Bug fix, Tests, Enhancement


Description

  • Refactored lastTakenPillNumber to lastTakenOrZeroPillNumber for better null safety and clarity.

  • Updated test cases to align with the new lastTakenOrZeroPillNumber logic.

  • Enhanced pill sheet group display logic to handle sequential and cyclic numbering modes.

  • Fixed nullable handling for afterLastTakenDate in RevertTakenPillValue.


Changes walkthrough 📝

Relevant files
Tests
5 files
pill_sheet_group_display_domain_test.dart
Updated test cases for sequential pill sheet numbering.   
+146/-146
pill_sheet_group_test.dart
Added tests for lastTakenPillNumberWithoutDate and sequential logic.
+408/-50
revert_take_pill_test.dart
Updated tests for reverting pill actions with null safety.
+17/-17 
pill_sheet_test.dart
Refactored tests for `lastTakenOrZeroPillNumber`.               
+29/-9   
change_pill_number_test.dart
Adjusted tests for null handling in pill number changes. 
+1/-1     
Enhancement
3 files
pill_sheet_group.codegen.dart
Added lastTakenPillSheetOrFirstPillSheet and refactored pill numbering
logic.
+129/-62
record_page_pill_sheet.dart
Updated UI logic to use `lastTakenOrZeroPillNumber`.         
+9/-9     
pill_sheet.dart
Updated historical pill sheet logic for `lastTakenOrZeroPillNumber`.
+6/-6     
Bug fix
1 files
pill_sheet_modified_history_value.codegen.freezed.dart
Fixed nullable handling for `afterLastTakenDate`.               
+17/-20 
Additional files
20 files
pill_sheet.codegen.dart +26/-3   
pill_sheet_modified_history.codegen.dart +5/-5     
pill_sheet_modified_history_value.codegen.dart +3/-3     
pill_sheet_modified_history_value.codegen.g.dart +2/-2     
pill_number.dart +3/-2     
taken_pill_action_o_list.dart +7/-4     
pill_sheet_modified_history_changed_pill_number_action.dart +17/-8   
pill_sheet_modified_history_revert_taken_pill_action.dart +19/-8   
pill_sheet_modified_history_taken_pill_action.dart +18/-13 
pill_sheet_modified_history_list.dart +6/-24   
cancel_button.dart +2/-2     
taken_button.dart +1/-1     
record_page_rest_duration_dialog.dart +2/-2     
change_pill_number.dart +7/-1     
revert_take_pill.dart +1/-1     
local_notification.dart +6/-9     
util_test.dart +8/-8     
initial_setting_state_notifier_test.dart +2/-2     
add_pill_sheet_group_test.dart +2/-2     
record_page_state_test.dart +1/-1     

💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@bannzai bannzai changed the title 服用履歴の連番対応(after: 2024-04-01) 服用履歴の連番対応(after: 2025-04-01) Jan 14, 2025
@bannzai
Copy link
Owner Author

bannzai commented Jan 14, 2025

ユーザーの手元のアプリのバージョンが古い場合にスキーマ事情により空の履歴が出る可能性がある。強制アップデートしたので3ヶ月は最低待つ

Copy link

Failed to generate code suggestions for PR

@bannzai bannzai marked this pull request as draft January 14, 2025 12:03
Fix
Fix
Fix
Fix
Fix
Fix
Fix
Fix
Fix
@bannzai bannzai requested a review from Copilot January 15, 2025 22:40

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 29 out of 29 changed files in this pull request and generated no comments.

@bannzai bannzai marked this pull request as ready for review January 15, 2025 22:43
@bannzai
Copy link
Owner Author

bannzai commented Jan 15, 2025

@CodiumAI-Agent /describe

@CodiumAI-Agent
Copy link

Title

服用履歴の連番対応(after: 2025-04-01)


PR Type

Bug fix, Tests, Enhancement


Description

  • Refactored lastTakenPillNumber to lastTakenOrZeroPillNumber for better null safety and clarity.

  • Updated test cases to align with the new lastTakenOrZeroPillNumber logic.

  • Enhanced pill sheet group display logic to handle sequential and cyclic numbering modes.

  • Fixed nullable handling for afterLastTakenDate in RevertTakenPillValue.


Changes walkthrough 📝

Relevant files
Tests
5 files
pill_sheet_group_display_domain_test.dart
Updated test cases for sequential pill sheet numbering.   
+146/-146
pill_sheet_group_test.dart
Added tests for lastTakenPillNumberWithoutDate and sequential logic.
+408/-50
revert_take_pill_test.dart
Updated tests for reverting pill actions with null safety.
+17/-17 
pill_sheet_test.dart
Refactored tests for `lastTakenOrZeroPillNumber`.               
+29/-9   
change_pill_number_test.dart
Adjusted tests for null handling in pill number changes. 
+1/-1     
Enhancement
3 files
pill_sheet_group.codegen.dart
Added lastTakenPillSheetOrFirstPillSheet and refactored pill numbering
logic.
+129/-62
record_page_pill_sheet.dart
Updated UI logic to use `lastTakenOrZeroPillNumber`.         
+9/-9     
pill_sheet.dart
Updated historical pill sheet logic for `lastTakenOrZeroPillNumber`.
+6/-6     
Bug fix
1 files
pill_sheet_modified_history_value.codegen.freezed.dart
Fixed nullable handling for `afterLastTakenDate`.               
+17/-20 
Additional files
20 files
pill_sheet.codegen.dart +26/-3   
pill_sheet_modified_history.codegen.dart +5/-5     
pill_sheet_modified_history_value.codegen.dart +3/-3     
pill_sheet_modified_history_value.codegen.g.dart +2/-2     
pill_number.dart +3/-2     
taken_pill_action_o_list.dart +7/-4     
pill_sheet_modified_history_changed_pill_number_action.dart +17/-8   
pill_sheet_modified_history_revert_taken_pill_action.dart +19/-8   
pill_sheet_modified_history_taken_pill_action.dart +18/-13 
pill_sheet_modified_history_list.dart +6/-24   
cancel_button.dart +2/-2     
taken_button.dart +1/-1     
record_page_rest_duration_dialog.dart +2/-2     
change_pill_number.dart +7/-1     
revert_take_pill.dart +1/-1     
local_notification.dart +6/-9     
util_test.dart +8/-8     
initial_setting_state_notifier_test.dart +2/-2     
add_pill_sheet_group_test.dart +2/-2     
record_page_state_test.dart +1/-1     

💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

@bannzai bannzai requested a review from Copilot January 15, 2025 22:50

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 9 out of 29 changed files in this pull request and generated no comments.

Files not reviewed (20)
  • lib/entity/pill_sheet.codegen.dart: Language not supported
  • lib/entity/pill_sheet_group.codegen.dart: Language not supported
  • lib/entity/pill_sheet_modified_history.codegen.dart: Language not supported
  • lib/entity/pill_sheet_modified_history_value.codegen.dart: Language not supported
  • lib/entity/pill_sheet_modified_history_value.codegen.freezed.dart: Language not supported
  • lib/entity/pill_sheet_modified_history_value.codegen.g.dart: Language not supported
  • lib/features/before_pill_sheet_group_history/component/pill_sheet.dart: Language not supported
  • lib/features/calendar/components/pill_sheet_modified_history/components/core/pill_number.dart: Language not supported
  • lib/features/calendar/components/pill_sheet_modified_history/components/core/taken_pill_action_o_list.dart: Language not supported
  • lib/features/calendar/components/pill_sheet_modified_history/components/rows/pill_sheet_modified_history_changed_pill_number_action.dart: Language not supported
  • lib/features/calendar/components/pill_sheet_modified_history/components/rows/pill_sheet_modified_history_revert_taken_pill_action.dart: Language not supported
  • lib/features/calendar/components/pill_sheet_modified_history/components/rows/pill_sheet_modified_history_taken_pill_action.dart: Language not supported
  • lib/features/calendar/components/pill_sheet_modified_history/pill_sheet_modified_history_list.dart: Language not supported
  • lib/features/record/components/button/cancel_button.dart: Language not supported
  • lib/features/record/components/button/taken_button.dart: Language not supported
  • lib/features/record/components/pill_sheet/components/record_page_rest_duration_dialog.dart: Language not supported
  • lib/features/record/components/pill_sheet/record_page_pill_sheet.dart: Language not supported
  • lib/provider/change_pill_number.dart: Language not supported
  • lib/provider/revert_take_pill.dart: Language not supported
  • lib/utils/local_notification.dart: Language not supported
@bannzai
Copy link
Owner Author

bannzai commented Jan 15, 2025

@CodiumAI-Agent /review

@CodiumAI-Agent
Copy link

CodiumAI-Agent commented Jan 15, 2025

PR Reviewer Guide 🔍

(Review updated until commit cc955ba)

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Possible Logic Issue

The lastTakenPillNumberWithoutDate logic in the tests should be carefully reviewed to ensure it correctly accounts for rest durations and sequential pill sheet numbering. Any discrepancies in the expected values could lead to incorrect behavior in production.

group("#lastTakenPillNumberWithoutDate", () {
  group("ピルシートが一つの場合", () {
    test("today: 2020-09-19, begin: 2020-09-14, end: 2020-09-18", () {
      final mockTodayRepository = MockTodayService();
      todayRepository = mockTodayRepository;
      when(mockTodayRepository.now()).thenReturn(DateTime.parse("2020-09-19"));

      const sheetType = PillSheetType.pillsheet_21;
      final pillSheet = PillSheet(
        id: firestoreIDGenerator(),
        beginingDate: DateTime.parse("2020-09-14"),
        lastTakenDate: DateTime.parse("2020-09-18"),
        createdAt: now(),
        typeInfo: PillSheetTypeInfo(
          dosingPeriod: sheetType.dosingPeriod,
          name: sheetType.fullName,
          totalCount: sheetType.totalCount,
          pillSheetTypeReferencePath: sheetType.rawPath,
        ),
      );
      // created at and id are anything value
      final pillSheetGroup = PillSheetGroup(
        pillSheetIDs: ["sheet_id"],
        pillSheets: [pillSheet],
        pillSheetAppearanceMode: PillSheetAppearanceMode.sequential,
        createdAt: now(),
      );
      expect(pillSheetGroup.lastTakenPillNumberWithoutDate, 5);
    });
    group("服用お休み期間を持つ場合", () {
      test("服用お休みが終わっていない場合", () {
        final mockTodayRepository = MockTodayService();
        todayRepository = mockTodayRepository;
        when(mockTodayRepository.now()).thenReturn(DateTime.parse("2020-09-28"));

        const sheetType = PillSheetType.pillsheet_21;
        final pillSheet = PillSheet(
          id: firestoreIDGenerator(),
          beginingDate: DateTime.parse("2020-09-01"),
          lastTakenDate: DateTime.parse("2020-09-28"),
          createdAt: now(),
          restDurations: [
            RestDuration(
              id: "rest_duration_id",
              beginDate: DateTime.parse("2020-09-22"),
              createdDate: DateTime.parse("2020-09-22"),
            )
          ],
          typeInfo: PillSheetTypeInfo(
            dosingPeriod: sheetType.dosingPeriod,
            name: sheetType.fullName,
            totalCount: sheetType.totalCount,
            pillSheetTypeReferencePath: sheetType.rawPath,
          ),
        );
        // created at and id are anything value
        final pillSheetGroup = PillSheetGroup(
          pillSheetIDs: ["sheet_id"],
          pillSheets: [pillSheet],
          pillSheetAppearanceMode: PillSheetAppearanceMode.sequential,
          createdAt: now(),
        );
        expect(pillSheetGroup.lastTakenPillNumberWithoutDate, 22);
      });

      test("服用お休みが終わっている場合", () {
        final mockTodayRepository = MockTodayService();
        todayRepository = mockTodayRepository;
        when(mockTodayRepository.now()).thenReturn(DateTime.parse("2020-09-28"));

        const sheetType = PillSheetType.pillsheet_21;
        final pillSheet = PillSheet(
          id: firestoreIDGenerator(),
          beginingDate: DateTime.parse("2020-09-01"),
          lastTakenDate: DateTime.parse("2020-09-28"),
          createdAt: now(),
          restDurations: [
            RestDuration(
              id: "rest_duration_id",
              beginDate: DateTime.parse("2020-09-22"),
              createdDate: DateTime.parse("2020-09-22"),
              endDate: DateTime.parse("2020-09-25"),
            )
          ],
          typeInfo: PillSheetTypeInfo(
            dosingPeriod: sheetType.dosingPeriod,
            name: sheetType.fullName,
            totalCount: sheetType.totalCount,
            pillSheetTypeReferencePath: sheetType.rawPath,
          ),
        );
        // created at and id are anything value
        final pillSheetGroup = PillSheetGroup(
          pillSheetIDs: ["sheet_id"],
          pillSheets: [pillSheet],
          pillSheetAppearanceMode: PillSheetAppearanceMode.sequential,
          createdAt: now(),
        );
        expect(pillSheetGroup.lastTakenPillNumberWithoutDate, 25);
      });
      group("複数の服用お休み期間を持つ場合", () {
        test("最後の服用お休みが終わっていない場合", () {
          final mockTodayRepository = MockTodayService();
          todayRepository = mockTodayRepository;
          when(mockTodayRepository.now()).thenReturn(DateTime.parse("2020-09-28"));

          const sheetType = PillSheetType.pillsheet_21;
          final pillSheet = PillSheet(
            id: firestoreIDGenerator(),
            beginingDate: DateTime.parse("2020-09-01"),
            lastTakenDate: DateTime.parse("2020-09-28"),
            createdAt: now(),
            restDurations: [
              RestDuration(
                id: "rest_duration_id",
                beginDate: DateTime.parse("2020-09-12"),
                createdDate: DateTime.parse("2020-09-12"),
                endDate: DateTime.parse("2020-09-15"),
              ),
              RestDuration(
                id: "rest_duration_id",
                beginDate: DateTime.parse("2020-09-22"),
                createdDate: DateTime.parse("2020-09-22"),
              )
            ],
            typeInfo: PillSheetTypeInfo(
              dosingPeriod: sheetType.dosingPeriod,
              name: sheetType.fullName,
              totalCount: sheetType.totalCount,
              pillSheetTypeReferencePath: sheetType.rawPath,
            ),
          );
          // created at and id are anything value
          final pillSheetGroup = PillSheetGroup(
            pillSheetIDs: ["sheet_id"],
            pillSheets: [pillSheet],
            pillSheetAppearanceMode: PillSheetAppearanceMode.sequential,
            createdAt: now(),
          );
          expect(pillSheetGroup.lastTakenPillNumberWithoutDate, 19);
        });
        test("最後の服用お休み期間が終わっている場合", () {
          final mockTodayRepository = MockTodayService();
          todayRepository = mockTodayRepository;
          when(mockTodayRepository.now()).thenReturn(DateTime.parse("2020-09-28"));

          const sheetType = PillSheetType.pillsheet_21;
          // 服用お休みを考慮しない場合は28日間
          final pillSheet = PillSheet(
            id: firestoreIDGenerator(),
            beginingDate: DateTime.parse("2020-09-01"),
            lastTakenDate: DateTime.parse("2020-09-28"),
            createdAt: now(),
            restDurations: [
              // 3日分
              RestDuration(
                id: "rest_duration_id",
                beginDate: DateTime.parse("2020-09-12"),
                createdDate: DateTime.parse("2020-09-12"),
                endDate: DateTime.parse("2020-09-15"),
              ),
              // 3日分
              RestDuration(
                id: "rest_duration_id",
                beginDate: DateTime.parse("2020-09-22"),
                createdDate: DateTime.parse("2020-09-22"),
                endDate: DateTime.parse("2020-09-25"),
              )
            ],
            typeInfo: PillSheetTypeInfo(
              dosingPeriod: sheetType.dosingPeriod,
              name: sheetType.fullName,
              totalCount: sheetType.totalCount,
              pillSheetTypeReferencePath: sheetType.rawPath,
            ),
          );
          // created at and id are anything value
          final pillSheetGroup = PillSheetGroup(
            pillSheetIDs: ["sheet_id"],
            pillSheets: [pillSheet],
            pillSheetAppearanceMode: PillSheetAppearanceMode.sequential,
            createdAt: now(),
          );
          expect(pillSheetGroup.lastTakenPillNumberWithoutDate, 22);
        });
      });
    });
  });
  group("has two pill sheets", () {
    test("it is plane pattern", () {
      final mockTodayRepository = MockTodayService();
      todayRepository = mockTodayRepository;
      when(mockTodayRepository.now()).thenReturn(DateTime.parse("2022-03-29"));

      const sheetType = PillSheetType.pillsheet_21;
      final pillSheet1 = PillSheet(
        id: firestoreIDGenerator(),
        beginingDate: DateTime.parse("2022-03-01"),
        lastTakenDate: DateTime.parse("2022-03-28"),
        createdAt: now(),
        groupIndex: 0,
        typeInfo: PillSheetTypeInfo(
          dosingPeriod: sheetType.dosingPeriod,
          name: sheetType.fullName,
          totalCount: sheetType.totalCount,
          pillSheetTypeReferencePath: sheetType.rawPath,
        ),
      );
      final pillSheet2 = PillSheet(
        id: firestoreIDGenerator(),
        beginingDate: DateTime.parse("2022-03-29"),
        lastTakenDate: null,
        groupIndex: 1,
        createdAt: now(),
        typeInfo: PillSheetTypeInfo(
          dosingPeriod: sheetType.dosingPeriod,
          name: sheetType.fullName,
          totalCount: sheetType.totalCount,
          pillSheetTypeReferencePath: sheetType.rawPath,
        ),
      );
      // created at and id are anything value
      final pillSheetGroup = PillSheetGroup(
        pillSheetIDs: ["sheet_id", "sheet_id2"],
        pillSheets: [pillSheet1, pillSheet2],
        pillSheetAppearanceMode: PillSheetAppearanceMode.sequential,
        createdAt: now(),
      );
      expect(pillSheetGroup.lastTakenPillNumberWithoutDate, 28);
    });
    test("with begin display number setting", () {
      final mockTodayRepository = MockTodayService();
      todayRepository = mockTodayRepository;
      when(mockTodayRepository.now()).thenReturn(DateTime.parse("2022-03-29"));

      const sheetType = PillSheetType.pillsheet_21;
      final pillSheet1 = PillSheet(
        id: firestoreIDGenerator(),
        beginingDate: DateTime.parse("2022-03-01"),
        lastTakenDate: DateTime.parse("2022-03-28"),
        createdAt: now(),
        groupIndex: 0,
        typeInfo: PillSheetTypeInfo(
          dosingPeriod: sheetType.dosingPeriod,
          name: sheetType.fullName,
          totalCount: sheetType.totalCount,
          pillSheetTypeReferencePath: sheetType.rawPath,
        ),
      );
      final pillSheet2 = PillSheet(
        id: firestoreIDGenerator(),
        beginingDate: DateTime.parse("2022-03-29"),
        lastTakenDate: null,
        createdAt: now(),
        groupIndex: 1,
        typeInfo: PillSheetTypeInfo(
          dosingPeriod: sheetType.dosingPeriod,
          name: sheetType.fullName,
          totalCount: sheetType.totalCount,
          pillSheetTypeReferencePath: sheetType.rawPath,
        ),
      );
      // created at and id are anything value
      final pillSheetGroup = PillSheetGroup(
        pillSheetIDs: ["sheet_id", "sheet_id2"],
        pillSheets: [pillSheet1, pillSheet2],
        createdAt: now(),
        displayNumberSetting: const PillSheetGroupDisplayNumberSetting(beginPillNumber: 2),
        pillSheetAppearanceMode: PillSheetAppearanceMode.sequential,
      );
      expect(pillSheetGroup.lastTakenPillNumberWithoutDate, 29);
    });
    test("with end display number setting", () {
      final mockTodayRepository = MockTodayService();
      todayRepository = mockTodayRepository;
      when(mockTodayRepository.now()).thenReturn(DateTime.parse("2022-03-29"));

      const sheetType = PillSheetType.pillsheet_21;
      final pillSheet1 = PillSheet(
        id: firestoreIDGenerator(),
        beginingDate: DateTime.parse("2022-03-01"),
        lastTakenDate: DateTime.parse("2022-03-28"),
        createdAt: now(),
        groupIndex: 0,
        typeInfo: PillSheetTypeInfo(
          dosingPeriod: sheetType.dosingPeriod,
          name: sheetType.fullName,
          totalCount: sheetType.totalCount,
          pillSheetTypeReferencePath: sheetType.rawPath,
        ),
      );
      final pillSheet2 = PillSheet(
        id: firestoreIDGenerator(),
        beginingDate: DateTime.parse("2022-03-29"),
        lastTakenDate: null,
        createdAt: now(),
        groupIndex: 1,
        typeInfo: PillSheetTypeInfo(
          dosingPeriod: sheetType.dosingPeriod,
          name: sheetType.fullName,
          totalCount: sheetType.totalCount,
          pillSheetTypeReferencePath: sheetType.rawPath,
        ),
      );
      // created at and id are anything value
      final pillSheetGroup = PillSheetGroup(
        pillSheetIDs: ["sheet_id", "sheet_id2"],
        pillSheets: [pillSheet1, pillSheet2],
        createdAt: now(),
        displayNumberSetting: const PillSheetGroupDisplayNumberSetting(endPillNumber: 28),
        pillSheetAppearanceMode: PillSheetAppearanceMode.sequential,
      );
      expect(pillSheetGroup.lastTakenPillNumberWithoutDate, 28);
    });
    test("with begin and end display number setting", () {
      final mockTodayRepository = MockTodayService();
      todayRepository = mockTodayRepository;
      when(mockTodayRepository.now()).thenReturn(DateTime.parse("2022-03-29"));

      const sheetType = PillSheetType.pillsheet_21;
      final pillSheet1 = PillSheet(
        id: firestoreIDGenerator(),
        beginingDate: DateTime.parse("2022-03-01"),
        lastTakenDate: DateTime.parse("2022-03-28"),
        createdAt: now(),
        groupIndex: 0,
        typeInfo: PillSheetTypeInfo(
          dosingPeriod: sheetType.dosingPeriod,
          name: sheetType.fullName,
          totalCount: sheetType.totalCount,
          pillSheetTypeReferencePath: sheetType.rawPath,
        ),
      );
      final pillSheet2 = PillSheet(
        id: firestoreIDGenerator(),
        beginingDate: DateTime.parse("2022-03-29"),
        lastTakenDate: null,
        createdAt: now(),
        groupIndex: 1,
        typeInfo: PillSheetTypeInfo(
          dosingPeriod: sheetType.dosingPeriod,
          name: sheetType.fullName,
          totalCount: sheetType.totalCount,
          pillSheetTypeReferencePath: sheetType.rawPath,
        ),
      );
      // created at and id are anything value
      final pillSheetGroup = PillSheetGroup(
        pillSheetIDs: ["sheet_id", "sheet_id2"],
        pillSheets: [pillSheet1, pillSheet2],
        createdAt: now(),
        displayNumberSetting: const PillSheetGroupDisplayNumberSetting(beginPillNumber: 2, endPillNumber: 28),
        pillSheetAppearanceMode: PillSheetAppearanceMode.sequential,
      );
      expect(pillSheetGroup.lastTakenPillNumberWithoutDate, 2);
    });
Nullable Handling

The handling of lastTakenDate being set to null in multiple test cases should be validated to ensure it aligns with the intended behavior of the application. This could impact the revert functionality.

      pillSheetIDs: ["sheet_id"],
      pillSheets: [
        pillSheet.copyWith(
          lastTakenDate: null,
        ),
      ],
      createdAt: now(),
    );
    final batchSetPillSheetGroup = MockBatchSetPillSheetGroup();
    when(batchSetPillSheetGroup(batch, updatedPillSheetGroup)).thenReturn(updatedPillSheetGroup);

    final history = PillSheetModifiedHistoryServiceActionFactory.createRevertTakenPillAction(
      pillSheetGroupID: "group_id",
      before: pillSheet,
      after: pillSheet.copyWith(
        lastTakenDate: yesterday.subtract(const Duration(days: 1)),
      ),
      beforePillSheetGroup: pillSheetGroup,
      afterPillSheetGroup: updatedPillSheetGroup,
    );
    final batchSetPillSheetModifiedHistory = MockBatchSetPillSheetModifiedHistory();
    when(batchSetPillSheetModifiedHistory(batch, history)).thenReturn(null);

    const setting = Setting(
      pillNumberForFromMenstruation: 22,
      durationMenstruation: 3,
      isOnReminder: true,
      timezoneDatabaseName: null,
      reminderTimes: [ReminderTime(hour: 21, minute: 20), ReminderTime(hour: 22, minute: 0)],
      pillSheetTypes: [
        PillSheetType.pillsheet_28_0,
      ],
    );
    final bachSetSetting = MockBatchSetSetting();
    when(bachSetSetting(batch, setting)).thenReturn(null);

    final revertTakePill = RevertTakePill(
        batchFactory: batchFactory,
        batchSetPillSheetModifiedHistory: batchSetPillSheetModifiedHistory,
        batchSetPillSheetGroup: batchSetPillSheetGroup);

    await revertTakePill.call(pillSheetGroup: pillSheetGroup, pageIndex: 0, targetRevertPillNumberIntoPillSheet: 1);
  });

  test("Revert today pill", () async {
    var mockTodayRepository = MockTodayService();
    final mockToday = DateTime.parse("2022-01-17");
    todayRepository = mockTodayRepository;
    when(mockTodayRepository.now()).thenReturn(mockToday);
    final yesterday = DateTime.parse("2022-01-16");

    final batchFactory = MockBatchFactory();
    final batch = MockWriteBatch();
    when(batchFactory.batch()).thenReturn(batch);

    final pillSheet = PillSheet(
      id: "sheet_id",
      typeInfo: PillSheetType.pillsheet_28_0.typeInfo,
      beginingDate: yesterday,
      groupIndex: 0,
      lastTakenDate: today(),
      createdAt: now(),
    );

    final pillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["sheet_id"],
      pillSheets: [
        pillSheet,
      ],
      createdAt: now(),
    );
    final updatedPillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["sheet_id"],
      pillSheets: [
        pillSheet.copyWith(lastTakenDate: yesterday),
      ],
      createdAt: now(),
    );
    final batchSetPillSheetGroup = MockBatchSetPillSheetGroup();
    when(batchSetPillSheetGroup(batch, updatedPillSheetGroup)).thenReturn(updatedPillSheetGroup);

    final history = PillSheetModifiedHistoryServiceActionFactory.createRevertTakenPillAction(
      pillSheetGroupID: "group_id",
      before: pillSheet,
      after: pillSheet.copyWith(
        lastTakenDate: yesterday,
      ),
      beforePillSheetGroup: pillSheetGroup,
      afterPillSheetGroup: updatedPillSheetGroup,
    );
    final batchSetPillSheetModifiedHistory = MockBatchSetPillSheetModifiedHistory();
    when(batchSetPillSheetModifiedHistory(batch, history)).thenReturn(null);

    const setting = Setting(
      pillNumberForFromMenstruation: 22,
      durationMenstruation: 3,
      isOnReminder: true,
      timezoneDatabaseName: null,
      reminderTimes: [ReminderTime(hour: 21, minute: 20), ReminderTime(hour: 22, minute: 0)],
      pillSheetTypes: [
        PillSheetType.pillsheet_28_0,
      ],
    );
    final bachSetSetting = MockBatchSetSetting();
    when(bachSetSetting(batch, setting)).thenReturn(null);

    final revertTakePill = RevertTakePill(
        batchFactory: batchFactory,
        batchSetPillSheetModifiedHistory: batchSetPillSheetModifiedHistory,
        batchSetPillSheetGroup: batchSetPillSheetGroup);

    await revertTakePill.call(pillSheetGroup: pillSheetGroup, pageIndex: 0, targetRevertPillNumberIntoPillSheet: 2);
  });

  test("revert with rest durations and removed rest duration", () async {
    var mockTodayRepository = MockTodayService();
    final mockToday = DateTime.parse("2022-01-17");
    todayRepository = mockTodayRepository;
    when(mockTodayRepository.now()).thenReturn(mockToday);
    when(mockTodayRepository.now()).thenReturn(mockToday);
    final beginDate = DateTime.parse("2022-01-06");

    final batchFactory = MockBatchFactory();
    final batch = MockWriteBatch();
    when(batchFactory.batch()).thenReturn(batch);

    final pillSheet = PillSheet(
      id: "sheet_id",
      typeInfo: PillSheetType.pillsheet_28_0.typeInfo,
      beginingDate: beginDate,
      groupIndex: 0,
      lastTakenDate: today(),
      createdAt: now(),
      restDurations: [
        RestDuration(
          id: "rest_duration_id",
          beginDate: mockToday.subtract(const Duration(days: 2)),
          createdDate: mockToday.subtract(const Duration(days: 2)),
          endDate: mockToday.subtract(const Duration(days: 1)),
        ),
      ],
    );

    final pillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["sheet_id"],
      pillSheets: [
        pillSheet,
      ],
      createdAt: now(),
    );
    final updatedPillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["sheet_id"],
      pillSheets: [
        pillSheet.copyWith(
          lastTakenDate: null,
          restDurations: [],
        ),
      ],
      createdAt: now(),
    );
    final batchSetPillSheetGroup = MockBatchSetPillSheetGroup();
    when(batchSetPillSheetGroup(batch, updatedPillSheetGroup)).thenReturn(updatedPillSheetGroup);

    final history = PillSheetModifiedHistoryServiceActionFactory.createRevertTakenPillAction(
      pillSheetGroupID: "group_id",
      before: pillSheet,
      after: pillSheet.copyWith(
        lastTakenDate: beginDate.subtract(const Duration(days: 1)),
        restDurations: [],
      ),
      beforePillSheetGroup: pillSheetGroup,
      afterPillSheetGroup: updatedPillSheetGroup,
    );
    final batchSetPillSheetModifiedHistory = MockBatchSetPillSheetModifiedHistory();
    when(batchSetPillSheetModifiedHistory(batch, history)).thenReturn(null);

    const setting = Setting(
      pillNumberForFromMenstruation: 22,
      durationMenstruation: 3,
      isOnReminder: true,
      timezoneDatabaseName: null,
      reminderTimes: [ReminderTime(hour: 21, minute: 20), ReminderTime(hour: 22, minute: 0)],
      pillSheetTypes: [
        PillSheetType.pillsheet_28_0,
      ],
    );
    final bachSetSetting = MockBatchSetSetting();
    when(bachSetSetting(batch, setting)).thenReturn(null);

    final revertTakePill = RevertTakePill(
        batchFactory: batchFactory,
        batchSetPillSheetModifiedHistory: batchSetPillSheetModifiedHistory,
        batchSetPillSheetGroup: batchSetPillSheetGroup);

    await revertTakePill.call(pillSheetGroup: pillSheetGroup, pageIndex: 0, targetRevertPillNumberIntoPillSheet: 1);
  });

  test("revert with rest durations but no removed rest duration", () async {
    var mockTodayRepository = MockTodayService();
    final mockToday = DateTime.parse("2022-01-17");
    todayRepository = mockTodayRepository;
    when(mockTodayRepository.now()).thenReturn(mockToday);
    when(mockTodayRepository.now()).thenReturn(mockToday);
    final beginDate = DateTime.parse("2022-01-06");
    final yesterday = DateTime.parse("2022-01-16");

    final batchFactory = MockBatchFactory();
    final batch = MockWriteBatch();
    when(batchFactory.batch()).thenReturn(batch);

    final pillSheet = PillSheet(
      id: "sheet_id",
      typeInfo: PillSheetType.pillsheet_28_0.typeInfo,
      beginingDate: beginDate,
      groupIndex: 0,
      lastTakenDate: today(),
      createdAt: now(),
      restDurations: [
        RestDuration(
          id: "rest_duration_id",
          beginDate: mockToday.subtract(const Duration(days: 8)),
          createdDate: mockToday.subtract(const Duration(days: 8)),
          endDate: mockToday.subtract(const Duration(days: 7)),
        ),
      ],
    );

    final pillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["sheet_id"],
      pillSheets: [
        pillSheet,
      ],
      createdAt: now(),
    );
    final updatedPillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["sheet_id"],
      pillSheets: [
        pillSheet.copyWith(
          lastTakenDate: yesterday,
        ),
      ],
      createdAt: now(),
    );
    final batchSetPillSheetGroup = MockBatchSetPillSheetGroup();
    when(batchSetPillSheetGroup(batch, updatedPillSheetGroup)).thenReturn(updatedPillSheetGroup);

    final history = PillSheetModifiedHistoryServiceActionFactory.createRevertTakenPillAction(
      pillSheetGroupID: "group_id",
      before: pillSheet,
      after: pillSheet.copyWith(lastTakenDate: yesterday),
      beforePillSheetGroup: pillSheetGroup,
      afterPillSheetGroup: updatedPillSheetGroup,
    );
    final batchSetPillSheetModifiedHistory = MockBatchSetPillSheetModifiedHistory();
    when(batchSetPillSheetModifiedHistory(batch, history)).thenReturn(null);

    const setting = Setting(
      pillNumberForFromMenstruation: 22,
      durationMenstruation: 3,
      isOnReminder: true,
      timezoneDatabaseName: null,
      reminderTimes: [ReminderTime(hour: 21, minute: 20), ReminderTime(hour: 22, minute: 0)],
      pillSheetTypes: [
        PillSheetType.pillsheet_28_0,
      ],
    );
    final bachSetSetting = MockBatchSetSetting();
    when(bachSetSetting(batch, setting)).thenReturn(null);

    final revertTakePill = RevertTakePill(
        batchFactory: batchFactory,
        batchSetPillSheetModifiedHistory: batchSetPillSheetModifiedHistory,
        batchSetPillSheetGroup: batchSetPillSheetGroup);

    await revertTakePill.call(pillSheetGroup: pillSheetGroup, pageIndex: 0, targetRevertPillNumberIntoPillSheet: 11);
  });
});

group("group has two pill sheet", () {
  test("call revert into actived pill sheet", () async {
    var mockTodayRepository = MockTodayService();
    final mockToday = DateTime.parse("2022-01-17");
    todayRepository = mockTodayRepository;
    when(mockTodayRepository.now()).thenReturn(mockToday);
    when(mockTodayRepository.now()).thenReturn(mockToday);
    final yesterday = DateTime.parse("2022-01-16");

    final batchFactory = MockBatchFactory();
    final batch = MockWriteBatch();
    when(batchFactory.batch()).thenReturn(batch);

    final pillSheet = PillSheet(
      id: "1",
      typeInfo: PillSheetType.pillsheet_28_0.typeInfo,
      beginingDate: mockToday.subtract(const Duration(days: 29)),
      groupIndex: 0,
      lastTakenDate: mockToday.subtract(const Duration(days: 2)),
      createdAt: now(),
    );

    // actived pill sheet
    final pillSheet2 = PillSheet(
      id: "2",
      typeInfo: PillSheetType.pillsheet_21.typeInfo,
      beginingDate: yesterday,
      lastTakenDate: mockToday,
      groupIndex: 1,
      createdAt: now(),
    );

    final pillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["1", "2"],
      pillSheets: [pillSheet, pillSheet2],
      createdAt: now(),
    );
    final updatedPillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["1", "2"],
      pillSheets: [
        pillSheet,
        pillSheet2.copyWith(
          lastTakenDate: yesterday,
        ),
      ],
      createdAt: now(),
    );
    final batchSetPillSheetGroup = MockBatchSetPillSheetGroup();
    when(batchSetPillSheetGroup(batch, updatedPillSheetGroup)).thenReturn(updatedPillSheetGroup);

    final history = PillSheetModifiedHistoryServiceActionFactory.createRevertTakenPillAction(
      pillSheetGroupID: "group_id",
      before: pillSheet2,
      after: pillSheet2.copyWith(
        lastTakenDate: yesterday,
      ),
      beforePillSheetGroup: pillSheetGroup,
      afterPillSheetGroup: updatedPillSheetGroup,
    );
    final batchSetPillSheetModifiedHistory = MockBatchSetPillSheetModifiedHistory();
    when(batchSetPillSheetModifiedHistory(batch, history)).thenReturn(null);

    const setting = Setting(
      pillNumberForFromMenstruation: 22,
      durationMenstruation: 3,
      isOnReminder: true,
      timezoneDatabaseName: null,
      reminderTimes: [ReminderTime(hour: 21, minute: 20), ReminderTime(hour: 22, minute: 0)],
      pillSheetTypes: [
        PillSheetType.pillsheet_28_0,
        PillSheetType.pillsheet_21,
      ],
    );
    final bachSetSetting = MockBatchSetSetting();
    when(bachSetSetting(batch, setting)).thenReturn(null);

    final revertTakePill = RevertTakePill(
        batchFactory: batchFactory,
        batchSetPillSheetModifiedHistory: batchSetPillSheetModifiedHistory,
        batchSetPillSheetGroup: batchSetPillSheetGroup);

    await revertTakePill.call(pillSheetGroup: pillSheetGroup, pageIndex: 1, targetRevertPillNumberIntoPillSheet: 2);
  });

  test("call revert from actived pill sheet to before pill sheet", () async {
    var mockTodayRepository = MockTodayService();
    final mockToday = DateTime.parse("2022-01-31");
    todayRepository = mockTodayRepository;
    when(mockTodayRepository.now()).thenReturn(mockToday);
    when(mockTodayRepository.now()).thenReturn(mockToday);
    final yesterday = DateTime.parse("2022-01-30");

    final batchFactory = MockBatchFactory();
    final batch = MockWriteBatch();
    when(batchFactory.batch()).thenReturn(batch);

    final pillSheet = PillSheet(
      id: "1",
      typeInfo: PillSheetType.pillsheet_28_0.typeInfo,
      beginingDate: mockToday.subtract(const Duration(days: 29)),
      groupIndex: 0,
      lastTakenDate: mockToday.subtract(const Duration(days: 2)),
      createdAt: now(),
    );

    // actived pill sheet
    final pillSheet2 = PillSheet(
      id: "2",
      typeInfo: PillSheetType.pillsheet_21.typeInfo,
      beginingDate: yesterday,
      lastTakenDate: mockToday,
      groupIndex: 1,
      createdAt: now(),
    );

    final pillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["1", "2"],
      pillSheets: [pillSheet, pillSheet2],
      createdAt: now(),
    );
    final updatedPillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["1", "2"],
      pillSheets: [
        pillSheet.copyWith(
          lastTakenDate: mockToday.subtract(const Duration(days: 4)),
        ),
        pillSheet2.copyWith(
          lastTakenDate: null,
        ),
      ],
      createdAt: now(),
    );
    final batchSetPillSheetGroup = MockBatchSetPillSheetGroup();
    when(batchSetPillSheetGroup(batch, updatedPillSheetGroup)).thenReturn(updatedPillSheetGroup);

    final history = PillSheetModifiedHistoryServiceActionFactory.createRevertTakenPillAction(
      pillSheetGroupID: "group_id",
      before: pillSheet2,
      after: pillSheet.copyWith(lastTakenDate: mockToday.subtract(const Duration(days: 4))),
      beforePillSheetGroup: pillSheetGroup,
      afterPillSheetGroup: updatedPillSheetGroup,
    );
    final batchSetPillSheetModifiedHistory = MockBatchSetPillSheetModifiedHistory();
    when(batchSetPillSheetModifiedHistory(batch, history)).thenReturn(null);

    const setting = Setting(
      pillNumberForFromMenstruation: 22,
      durationMenstruation: 3,
      isOnReminder: true,
      timezoneDatabaseName: null,
      reminderTimes: [ReminderTime(hour: 21, minute: 20), ReminderTime(hour: 22, minute: 0)],
      pillSheetTypes: [
        PillSheetType.pillsheet_28_0,
        PillSheetType.pillsheet_21,
      ],
    );
    final bachSetSetting = MockBatchSetSetting();
    when(bachSetSetting(batch, setting)).thenReturn(null);

    final revertTakePill = RevertTakePill(
        batchFactory: batchFactory,
        batchSetPillSheetModifiedHistory: batchSetPillSheetModifiedHistory,
        batchSetPillSheetGroup: batchSetPillSheetGroup);

    await revertTakePill.call(pillSheetGroup: pillSheetGroup, pageIndex: 0, targetRevertPillNumberIntoPillSheet: 27);
  });
  test("call revert with rest duration", () async {
    var mockTodayRepository = MockTodayService();
    final mockToday = DateTime.parse("2022-01-31");
    todayRepository = mockTodayRepository;
    when(mockTodayRepository.now()).thenReturn(mockToday);
    when(mockTodayRepository.now()).thenReturn(mockToday);
    final yesterday = DateTime.parse("2022-01-30");

    final batchFactory = MockBatchFactory();
    final batch = MockWriteBatch();
    when(batchFactory.batch()).thenReturn(batch);

    final pillSheet = PillSheet(
      id: "1",
      typeInfo: PillSheetType.pillsheet_28_0.typeInfo,
      beginingDate: mockToday.subtract(const Duration(days: 29)),
      groupIndex: 0,
      lastTakenDate: mockToday.subtract(const Duration(days: 2)),
      createdAt: now(),
    );

    // actived pill sheet
    final pillSheet2 = PillSheet(
      id: "2",
      typeInfo: PillSheetType.pillsheet_21.typeInfo,
      beginingDate: yesterday,
      lastTakenDate: mockToday,
      groupIndex: 1,
      restDurations: [
        RestDuration(id: "rest_duration_id", beginDate: yesterday, createdDate: yesterday, endDate: today()),
      ],
      createdAt: now(),
    );

    final pillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["1", "2"],
      pillSheets: [pillSheet, pillSheet2],
      createdAt: now(),
    );
    final updatedPillSheetGroup = PillSheetGroup(
      id: "group_id",
      pillSheetIDs: ["1", "2"],
      pillSheets: [
        pillSheet.copyWith(
          lastTakenDate: mockToday.subtract(const Duration(days: 4)),
        ),
        pillSheet2.copyWith(
          lastTakenDate: null,
          restDurations: [],
        )
      ],
      createdAt: now(),
    );
    final batchSetPillSheetGroup = MockBatchSetPillSheetGroup();
    when(batchSetPillSheetGroup(batch, updatedPillSheetGroup)).thenReturn(updatedPillSheetGroup);

    final history = PillSheetModifiedHistoryServiceActionFactory.createRevertTakenPillAction(
      pillSheetGroupID: "group_id",
      before: pillSheet2,
      after: pillSheet.copyWith(lastTakenDate: mockToday.subtract(const Duration(days: 4)), restDurations: []),
      beforePillSheetGroup: pillSheetGroup,
      afterPillSheetGroup: updatedPillSheetGroup,
Updated Pill Sheet Logic

The logic for updating lastTakenDate to null in the change_pill_number_test.dart file should be reviewed to ensure it does not introduce unintended side effects.

);
final updatedRight = right.copyWith(
  beginingDate: DateTime.parse("2022-05-01"),
  lastTakenDate: null,
);

Fix
This reverts commit 7fc2065.
This reverts commit f24fa4e.
@bannzai bannzai merged commit e8aa28c into main Jan 16, 2025
6 checks passed
@bannzai bannzai deleted the bugfix/view/pill_sheet_modified_history branch January 16, 2025 03:55
@CodiumAI-Agent
Copy link

Persistent review updated to latest commit cc955ba

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants