diff --git a/src/addons/calendar/services/calendar-sync.ts b/src/addons/calendar/services/calendar-sync.ts index 21845fba8..cc9cb00b0 100644 --- a/src/addons/calendar/services/calendar-sync.ts +++ b/src/addons/calendar/services/calendar-sync.ts @@ -27,7 +27,7 @@ import { import { AddonCalendarOffline } from './calendar-offline'; import { AddonCalendarHelper } from './calendar-helper'; import { makeSingleton, Translate } from '@singletons'; -import { CoreSync } from '@services/sync'; +import { CoreSync, CoreSyncResult } from '@services/sync'; import { CoreNetworkError } from '@classes/errors/network-error'; import moment from 'moment-timezone'; @@ -301,13 +301,11 @@ export class AddonCalendarSyncProvider extends CoreSyncBaseProvider; // Map offline ID with online ID for created events. deleted: number[]; toinvalidate: AddonCalendarSyncInvalidateEvent[]; - updated: boolean; source?: string; // Added on pages. moment?: moment.Moment; // Added on day page. }; diff --git a/src/addons/mod/assign/components/index/index.ts b/src/addons/mod/assign/components/index/index.ts index 5f4640723..411090e5c 100644 --- a/src/addons/mod/assign/components/index/index.ts +++ b/src/addons/mod/assign/components/index/index.ts @@ -14,6 +14,7 @@ import { Component, Optional, OnDestroy, OnInit, ViewChild } from '@angular/core'; import { Params } from '@angular/router'; +import { CoreError } from '@classes/errors/error'; import { CoreSite } from '@classes/site'; import { CoreCourseModuleMainActivityComponent } from '@features/course/classes/main-activity-component'; import { CoreCourseContentsPage } from '@features/course/pages/contents/contents'; @@ -314,11 +315,7 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo /** * @inheritdoc */ - protected hasSyncSucceed(result?: AddonModAssignSyncResult): boolean { - if (!result) { - return false; - } - + protected hasSyncSucceed(result: AddonModAssignSyncResult): boolean { if (result.updated) { this.submissionComponent?.invalidateAndRefresh(false); } @@ -384,9 +381,9 @@ export class AddonModAssignIndexComponent extends CoreCourseModuleMainActivityCo /** * @inheritdoc */ - protected async sync(): Promise { + protected async sync(): Promise { if (!this.assign) { - return; + throw new CoreError('Cannot sync without a assign.'); } return AddonModAssignSync.syncAssign(this.assign.id); diff --git a/src/addons/mod/assign/services/assign-sync.ts b/src/addons/mod/assign/services/assign-sync.ts index 3a04878b1..1c5ced9cf 100644 --- a/src/addons/mod/assign/services/assign-sync.ts +++ b/src/addons/mod/assign/services/assign-sync.ts @@ -32,7 +32,7 @@ import { AddonModAssignSubmissionsDBRecordFormatted, AddonModAssignSubmissionsGradingDBRecordFormatted, } from './assign-offline'; -import { CoreSync } from '@services/sync'; +import { CoreSync, CoreSyncResult } from '@services/sync'; import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreUtils } from '@services/utils/utils'; import { CoreNetwork } from '@services/network'; @@ -530,9 +530,7 @@ export const AddonModAssignSync = makeSingleton(AddonModAssignSyncProvider); /** * Data returned by a assign sync. */ -export type AddonModAssignSyncResult = { - warnings: string[]; // List of warnings. - updated: boolean; // Whether some data was sent to the server or offline data was updated. +export type AddonModAssignSyncResult = CoreSyncResult & { courseId?: number; // Course the assign belongs to (if known). gradesBlocked: number[]; // Whether some grade couldn't be synced because it was blocked. UserId fields of the blocked grade. }; diff --git a/src/addons/mod/choice/components/index/index.ts b/src/addons/mod/choice/components/index/index.ts index 91b25b58b..d4f7e039c 100644 --- a/src/addons/mod/choice/components/index/index.ts +++ b/src/addons/mod/choice/components/index/index.ts @@ -13,6 +13,7 @@ // limitations under the License. import { Component, Optional, OnInit } from '@angular/core'; +import { CoreError } from '@classes/errors/error'; import { CoreCourseModuleMainActivityComponent } from '@features/course/classes/main-activity-component'; import { CoreCourseContentsPage } from '@features/course/pages/contents/contents'; import { IonContent } from '@ionic/angular'; @@ -454,22 +455,14 @@ export class AddonModChoiceIndexComponent extends CoreCourseModuleMainActivityCo } /** - * Performs the sync of the activity. - * - * @returns Promise resolved when done. + * @inheritdoc */ protected sync(): Promise { - return AddonModChoiceSync.syncChoice(this.choice!.id, this.userId); - } + if (!this.choice) { + throw new CoreError('Cannot sync without a choice.'); + } - /** - * Checks if sync has succeed from result sync data. - * - * @param result Data returned on the sync function. - * @returns Whether it succeed or not. - */ - protected hasSyncSucceed(result: AddonModChoiceSyncResult): boolean { - return result.updated; + return AddonModChoiceSync.syncChoice(this.choice.id, this.userId); } } diff --git a/src/addons/mod/choice/services/choice-sync.ts b/src/addons/mod/choice/services/choice-sync.ts index d0b74a684..c98d270a5 100644 --- a/src/addons/mod/choice/services/choice-sync.ts +++ b/src/addons/mod/choice/services/choice-sync.ts @@ -15,6 +15,7 @@ import { Injectable } from '@angular/core'; import { CoreNetworkError } from '@classes/errors/network-error'; import { CoreCourseActivitySyncBaseProvider } from '@features/course/classes/activity-sync'; +import { CoreSyncResult } from '@services/sync'; import { CoreCourse } from '@features/course/services/course'; import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreNetwork } from '@services/network'; @@ -217,10 +218,7 @@ export const AddonModChoiceSync = makeSingleton(AddonModChoiceSyncProvider); /** * Data returned by a choice sync. */ -export type AddonModChoiceSyncResult = { - warnings: string[]; // List of warnings. - updated: boolean; // Whether some data was sent to the server or offline data was updated. -}; +export type AddonModChoiceSyncResult = CoreSyncResult; /** * Data passed to AUTO_SYNCED event. diff --git a/src/addons/mod/data/components/index/index.ts b/src/addons/mod/data/components/index/index.ts index 36948b327..2cdac62c5 100644 --- a/src/addons/mod/data/components/index/index.ts +++ b/src/addons/mod/data/components/index/index.ts @@ -519,24 +519,12 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp } /** - * Performs the sync of the activity. - * - * @returns Promise resolved when done. + * @inheritdoc */ protected sync(): Promise { return AddonModDataPrefetchHandler.sync(this.module, this.courseId); } - /** - * Checks if sync has succeed from result sync data. - * - * @param result Data returned on the sync function. - * @returns If suceed or not. - */ - protected hasSyncSucceed(result: AddonModDataSyncResult): boolean { - return result.updated; - } - /** * @inheritdoc */ diff --git a/src/addons/mod/data/services/data-sync.ts b/src/addons/mod/data/services/data-sync.ts index 5e76877bc..cd2e2ccea 100644 --- a/src/addons/mod/data/services/data-sync.ts +++ b/src/addons/mod/data/services/data-sync.ts @@ -24,7 +24,7 @@ import { CoreRatingSync } from '@features/rating/services/rating-sync'; import { CoreNetwork } from '@services/network'; import { CoreFileEntry } from '@services/file-helper'; import { CoreSites, CoreSitesReadingStrategy } from '@services/sites'; -import { CoreSync } from '@services/sync'; +import { CoreSync, CoreSyncResult } from '@services/sync'; import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; import { Translate, makeSingleton } from '@singletons'; @@ -477,10 +477,7 @@ export type AddonModDataSyncEntryResult = { /** * Data returned by a database sync. */ -export type AddonModDataSyncResult = { - warnings: string[]; // List of warnings. - updated: boolean; // Whether some data was sent to the server or offline data was updated. -}; +export type AddonModDataSyncResult = CoreSyncResult; export type AddonModDataAutoSyncData = { dataId: number; diff --git a/src/addons/mod/feedback/components/index/index.ts b/src/addons/mod/feedback/components/index/index.ts index c02c0fa53..2f877c17a 100644 --- a/src/addons/mod/feedback/components/index/index.ts +++ b/src/addons/mod/feedback/components/index/index.ts @@ -13,6 +13,7 @@ // limitations under the License. import { Component, Input, Optional, ViewChild, OnInit, OnDestroy } from '@angular/core'; +import { CoreError } from '@classes/errors/error'; import { CoreTabsComponent } from '@components/tabs/tabs'; import { CoreCourseModuleMainActivityComponent } from '@features/course/classes/main-activity-component'; import { CoreCourseContentsPage } from '@features/course/pages/contents/contents'; @@ -477,14 +478,11 @@ export class AddonModFeedbackIndexComponent extends CoreCourseModuleMainActivity * @inheritdoc */ protected sync(): Promise { - return AddonModFeedbackSync.syncFeedback(this.feedback!.id); - } + if (!this.feedback) { + throw new CoreError('Cannot sync without a feedback.'); + } - /** - * @inheritdoc - */ - protected hasSyncSucceed(result: AddonModFeedbackSyncResult): boolean { - return result.updated; + return AddonModFeedbackSync.syncFeedback(this.feedback.id); } /** diff --git a/src/addons/mod/feedback/services/feedback-sync.ts b/src/addons/mod/feedback/services/feedback-sync.ts index 5f84a7ceb..32f6791ef 100644 --- a/src/addons/mod/feedback/services/feedback-sync.ts +++ b/src/addons/mod/feedback/services/feedback-sync.ts @@ -20,7 +20,7 @@ import { CoreCourse, CoreCourseAnyModuleData } from '@features/course/services/c import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreNetwork } from '@services/network'; import { CoreSites, CoreSitesReadingStrategy } from '@services/sites'; -import { CoreSync } from '@services/sync'; +import { CoreSync, CoreSyncResult } from '@services/sync'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton, Translate } from '@singletons'; import { CoreEvents } from '@singletons/events'; @@ -292,10 +292,7 @@ export const AddonModFeedbackSync = makeSingleton(AddonModFeedbackSyncProvider); /** * Data returned by a feedback sync. */ -export type AddonModFeedbackSyncResult = { - warnings: string[]; // List of warnings. - updated: boolean; // Whether some data was sent to the server or offline data was updated. -}; +export type AddonModFeedbackSyncResult = CoreSyncResult; /** * Data passed to AUTO_SYNCED event. diff --git a/src/addons/mod/forum/components/index/index.ts b/src/addons/mod/forum/components/index/index.ts index 3dd733ad9..bb604e3f2 100644 --- a/src/addons/mod/forum/components/index/index.ts +++ b/src/addons/mod/forum/components/index/index.ts @@ -500,24 +500,12 @@ export class AddonModForumIndexComponent extends CoreCourseModuleMainActivityCom } /** - * Performs the sync of the activity. - * - * @returns Promise resolved when done. + * @inheritdoc */ protected sync(): Promise { return AddonModForumPrefetchHandler.sync(this.module, this.courseId); } - /** - * Checks if sync has succeed from result sync data. - * - * @param result Data returned on the sync function. - * @returns Whether it succeed or not. - */ - protected hasSyncSucceed(result: AddonModForumSyncResult): boolean { - return result.updated; - } - /** * Compares sync event data with current data to check if refresh content is needed. * diff --git a/src/addons/mod/forum/services/forum-sync.ts b/src/addons/mod/forum/services/forum-sync.ts index 37e88fc29..b2324c12d 100644 --- a/src/addons/mod/forum/services/forum-sync.ts +++ b/src/addons/mod/forum/services/forum-sync.ts @@ -21,7 +21,7 @@ import { CoreRatingSync } from '@features/rating/services/rating-sync'; import { CoreNetwork } from '@services/network'; import { CoreGroups } from '@services/groups'; import { CoreSites } from '@services/sites'; -import { CoreSync } from '@services/sync'; +import { CoreSync, CoreSyncResult } from '@services/sync'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton, Translate } from '@singletons'; import { CoreEvents } from '@singletons/events'; @@ -221,7 +221,7 @@ export class AddonModForumSyncProvider extends CoreCourseActivitySyncBaseProvide }; // Sync offline logs. - const syncDiscussions = async (): Promise<{ warnings: string[]; updated: boolean }> => { + const syncDiscussions = async (): Promise => { await CoreUtils.ignoreErrors( CoreCourseLogHelper.syncActivity(AddonModForumProvider.COMPONENT, forumId, siteId), ); @@ -643,10 +643,7 @@ export const AddonModForumSync = makeSingleton(AddonModForumSyncProvider); /** * Result of forum sync. */ -export type AddonModForumSyncResult = { - updated: boolean; - warnings: string[]; -}; +export type AddonModForumSyncResult = CoreSyncResult; /** * Data passed to AUTO_SYNCED event. diff --git a/src/addons/mod/forum/services/handlers/prefetch.ts b/src/addons/mod/forum/services/handlers/prefetch.ts index b03910690..29124b40e 100644 --- a/src/addons/mod/forum/services/handlers/prefetch.ts +++ b/src/addons/mod/forum/services/handlers/prefetch.ts @@ -22,7 +22,7 @@ import { CoreCourse, CoreCourseAnyModuleData, CoreCourseCommonModWSOptions } fro import { CoreUser } from '@features/user/services/user'; import { CoreGroups, CoreGroupsProvider } from '@services/groups'; import { CoreUtils } from '@services/utils/utils'; -import { AddonModForumSync } from '../forum-sync'; +import { AddonModForumSync, AddonModForumSyncResult } from '../forum-sync'; import { makeSingleton } from '@singletons'; import { CoreCourses } from '@features/courses/services/courses'; @@ -341,11 +341,3 @@ export class AddonModForumPrefetchHandlerService extends CoreCourseActivityPrefe } export const AddonModForumPrefetchHandler = makeSingleton(AddonModForumPrefetchHandlerService); - -/** - * Data returned by a forum sync. - */ -export type AddonModForumSyncResult = { - warnings: string[]; // List of warnings. - updated: boolean; // Whether some data was sent to the server or offline data was updated. -}; diff --git a/src/addons/mod/glossary/components/index/index.ts b/src/addons/mod/glossary/components/index/index.ts index 5b22a4cd4..6b0acca1c 100644 --- a/src/addons/mod/glossary/components/index/index.ts +++ b/src/addons/mod/glossary/components/index/index.ts @@ -215,24 +215,12 @@ export class AddonModGlossaryIndexComponent extends CoreCourseModuleMainActivity } /** - * Performs the sync of the activity. - * - * @returns Promise resolved when done. + * @inheritdoc */ protected sync(): Promise { return AddonModGlossaryPrefetchHandler.sync(this.module, this.courseId); } - /** - * Checks if sync has succeed from result sync data. - * - * @param result Data returned on the sync function. - * @returns Whether it succeed or not. - */ - protected hasSyncSucceed(result: AddonModGlossarySyncResult): boolean { - return result.updated; - } - /** * Compares sync event data with current data to check if refresh content is needed. * diff --git a/src/addons/mod/glossary/services/glossary-sync.ts b/src/addons/mod/glossary/services/glossary-sync.ts index 8db74bcc1..0fc65ad13 100644 --- a/src/addons/mod/glossary/services/glossary-sync.ts +++ b/src/addons/mod/glossary/services/glossary-sync.ts @@ -21,7 +21,7 @@ import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreRatingSync } from '@features/rating/services/rating-sync'; import { CoreNetwork } from '@services/network'; import { CoreSites } from '@services/sites'; -import { CoreSync } from '@services/sync'; +import { CoreSync, CoreSyncResult } from '@services/sync'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton, Translate } from '@singletons'; import { CoreEvents } from '@singletons/events'; @@ -344,10 +344,7 @@ export const AddonModGlossarySync = makeSingleton(AddonModGlossarySyncProvider); /** * Data returned by a glossary sync. */ -export type AddonModGlossarySyncResult = { - warnings: string[]; // List of warnings. - updated: boolean; // Whether some data was sent to the server or offline data was updated. -}; +export type AddonModGlossarySyncResult = CoreSyncResult; /** * Data passed to AUTO_SYNCED event. diff --git a/src/addons/mod/h5pactivity/services/h5pactivity-sync.ts b/src/addons/mod/h5pactivity/services/h5pactivity-sync.ts index 5eb33e384..7d9c3fb19 100644 --- a/src/addons/mod/h5pactivity/services/h5pactivity-sync.ts +++ b/src/addons/mod/h5pactivity/services/h5pactivity-sync.ts @@ -16,6 +16,7 @@ import { Injectable } from '@angular/core'; import { CoreNetworkError } from '@classes/errors/network-error'; import { CoreCourseActivitySyncBaseProvider } from '@features/course/classes/activity-sync'; +import { CoreSyncResult } from '@services/sync'; import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreXAPIOffline } from '@features/xapi/services/offline'; import { CoreXAPI } from '@features/xapi/services/xapi'; @@ -197,10 +198,7 @@ export const AddonModH5PActivitySync = makeSingleton(AddonModH5PActivitySyncProv /** * Sync result. */ -export type AddonModH5PActivitySyncResult = { - updated: boolean; - warnings: string[]; -}; +export type AddonModH5PActivitySyncResult = CoreSyncResult; /** * Data passed to AUTO_SYNC event. diff --git a/src/addons/mod/lesson/components/index/index.ts b/src/addons/mod/lesson/components/index/index.ts index 5495057e2..bd66d2b49 100644 --- a/src/addons/mod/lesson/components/index/index.ts +++ b/src/addons/mod/lesson/components/index/index.ts @@ -47,6 +47,7 @@ import { } from '../../services/lesson-sync'; import { AddonModLessonModuleHandlerService } from '../../services/handlers/module'; import { CoreTime } from '@singletons/time'; +import { CoreError } from '@classes/errors/error'; /** * Component that displays a lesson entry page. @@ -270,10 +271,7 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo } /** - * Checks if sync has succeed from result sync data. - * - * @param result Data returned on the sync function. - * @returns If suceed or not. + * @inheritdoc */ protected hasSyncSucceed(result: AddonModLessonSyncResult): boolean { if (result.updated || this.dataSent) { @@ -637,12 +635,14 @@ export class AddonModLessonIndexComponent extends CoreCourseModuleMainActivityCo } /** - * Performs the sync of the activity. - * - * @returns Promise resolved when done. + * @inheritdoc */ protected async sync(): Promise { - const result = await AddonModLessonSync.syncLesson(this.lesson!.id, true); + if (!this.lesson) { + throw new CoreError('Cannot sync without a lesson.'); + } + + const result = await AddonModLessonSync.syncLesson(this.lesson.id, true); if (!result.updated && this.dataSent && this.isPrefetched()) { // The user sent data to server, but not in the sync process. Check if we need to fetch data. diff --git a/src/addons/mod/lesson/services/lesson-sync.ts b/src/addons/mod/lesson/services/lesson-sync.ts index 1a4241178..adf4eb126 100644 --- a/src/addons/mod/lesson/services/lesson-sync.ts +++ b/src/addons/mod/lesson/services/lesson-sync.ts @@ -21,7 +21,7 @@ import { CoreCourse } from '@features/course/services/course'; import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreNetwork } from '@services/network'; import { CoreSites, CoreSitesReadingStrategy } from '@services/sites'; -import { CoreSync } from '@services/sync'; +import { CoreSync, CoreSyncResult } from '@services/sync'; import { CoreTimeUtils } from '@services/utils/time'; import { CoreUrlUtils } from '@services/utils/url'; import { CoreUtils } from '@services/utils/utils'; @@ -495,9 +495,7 @@ export const AddonModLessonSync = makeSingleton(AddonModLessonSyncProvider); /** * Data returned by a lesson sync. */ -export type AddonModLessonSyncResult = { - warnings: string[]; // List of warnings. - updated: boolean; // Whether some data was sent to the server or offline data was updated. +export type AddonModLessonSyncResult = CoreSyncResult & { courseId?: number; // Course the lesson belongs to (if known). }; diff --git a/src/addons/mod/quiz/components/index/index.ts b/src/addons/mod/quiz/components/index/index.ts index c4adff103..98b84d041 100644 --- a/src/addons/mod/quiz/components/index/index.ts +++ b/src/addons/mod/quiz/components/index/index.ts @@ -418,10 +418,7 @@ export class AddonModQuizIndexComponent extends CoreCourseModuleMainActivityComp } /** - * Checks if sync has succeed from result sync data. - * - * @param result Data returned on the sync function. - * @returns If suceed or not. + * @inheritdoc */ protected hasSyncSucceed(result: AddonModQuizSyncResult): boolean { if (result.attemptFinished) { @@ -553,9 +550,7 @@ export class AddonModQuizIndexComponent extends CoreCourseModuleMainActivityComp } /** - * Performs the sync of the activity. - * - * @returns Promise resolved when done. + * @inheritdoc */ protected async sync(): Promise { if (!this.candidateQuiz) { diff --git a/src/addons/mod/quiz/services/quiz-offline.ts b/src/addons/mod/quiz/services/quiz-offline.ts index 5009a68d7..ed477be09 100644 --- a/src/addons/mod/quiz/services/quiz-offline.ts +++ b/src/addons/mod/quiz/services/quiz-offline.ts @@ -13,7 +13,6 @@ // limitations under the License. import { Injectable } from '@angular/core'; -import { CoreError } from '@classes/errors/error'; import { CoreQuestionBehaviourDelegate, CoreQuestionQuestionWithAnswers } from '@features/question/services/behaviour-delegate'; import { CoreQuestionAnswerDBRecord } from '@features/question/services/database/question'; diff --git a/src/addons/mod/quiz/services/quiz-sync.ts b/src/addons/mod/quiz/services/quiz-sync.ts index 4b6a573ae..74871a22e 100644 --- a/src/addons/mod/quiz/services/quiz-sync.ts +++ b/src/addons/mod/quiz/services/quiz-sync.ts @@ -23,7 +23,7 @@ import { CoreQuestion, CoreQuestionQuestionParsed } from '@features/question/ser import { CoreQuestionDelegate } from '@features/question/services/question-delegate'; import { CoreNetwork } from '@services/network'; import { CoreSites, CoreSitesReadingStrategy } from '@services/sites'; -import { CoreSync } from '@services/sync'; +import { CoreSync, CoreSyncResult } from '@services/sync'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton, Translate } from '@singletons'; import { CoreEvents } from '@singletons/events'; @@ -482,10 +482,8 @@ export const AddonModQuizSync = makeSingleton(AddonModQuizSyncProvider); /** * Data returned by a quiz sync. */ -export type AddonModQuizSyncResult = { - warnings: string[]; // List of warnings. +export type AddonModQuizSyncResult = CoreSyncResult & { attemptFinished: boolean; // Whether an attempt was finished in the site due to the sync. - updated: boolean; }; /** diff --git a/src/addons/mod/scorm/components/index/index.ts b/src/addons/mod/scorm/components/index/index.ts index f3c0a7307..90e36e1ed 100644 --- a/src/addons/mod/scorm/components/index/index.ts +++ b/src/addons/mod/scorm/components/index/index.ts @@ -14,6 +14,7 @@ import { CoreConstants } from '@/core/constants'; import { Component, Input, OnInit, Optional } from '@angular/core'; +import { CoreError } from '@classes/errors/error'; import { CoreCourseModuleMainActivityComponent } from '@features/course/classes/main-activity-component'; import { CoreCourseContentsPage } from '@features/course/pages/contents/contents'; import { CoreCourse } from '@features/course/services/course'; @@ -364,10 +365,7 @@ export class AddonModScormIndexComponent extends CoreCourseModuleMainActivityCom } /** - * Checks if sync has succeed from result sync data. - * - * @param result Data returned on the sync function. - * @returns If suceed or not. + * @inheritdoc */ protected hasSyncSucceed(result: AddonModScormSyncResult): boolean { if (result.updated || this.dataSent) { @@ -377,7 +375,7 @@ export class AddonModScormIndexComponent extends CoreCourseModuleMainActivityCom this.dataSent = false; - return true; + return result.updated; } /** @@ -608,9 +606,9 @@ export class AddonModScormIndexComponent extends CoreCourseModuleMainActivityCom * @param retries Number of retries done. * @returns Promise resolved when done. */ - protected async sync(retries = 0): Promise { + protected async sync(retries = 0): Promise { if (!this.scorm) { - return; + throw new CoreError('Cannot sync without a scorm.'); } if (CoreSync.isBlocked(AddonModScormProvider.COMPONENT, this.scorm.id) && retries < 5) { diff --git a/src/addons/mod/scorm/services/scorm-sync.ts b/src/addons/mod/scorm/services/scorm-sync.ts index cfae16dbb..b55470e84 100644 --- a/src/addons/mod/scorm/services/scorm-sync.ts +++ b/src/addons/mod/scorm/services/scorm-sync.ts @@ -18,7 +18,7 @@ import { CoreCourseActivitySyncBaseProvider } from '@features/course/classes/act import { CoreCourse } from '@features/course/services/course'; import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreSites, CoreSitesReadingStrategy } from '@services/sites'; -import { CoreSync } from '@services/sync'; +import { CoreSync, CoreSyncResult } from '@services/sync'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton, Translate } from '@singletons'; import { CoreEvents } from '@singletons/events'; @@ -841,18 +841,14 @@ export const AddonModScormSync = makeSingleton(AddonModScormSyncProvider); /** * Data returned by a SCORM sync. */ -export type AddonModScormSyncResult = { - warnings: string[]; // List of warnings. +export type AddonModScormSyncResult = CoreSyncResult & { attemptFinished: boolean; // Whether an attempt was finished in the site due to the sync, - updated: boolean; // Whether some data was sent to the site. }; /** * Auto sync event data. */ -export type AddonModScormAutoSyncEventData = { +export type AddonModScormAutoSyncEventData = CoreSyncResult & { scormId: number; attemptFinished: boolean; - warnings: string[]; - updated: boolean; }; diff --git a/src/addons/mod/survey/components/index/index.ts b/src/addons/mod/survey/components/index/index.ts index 788ed1a0d..8fa968dda 100644 --- a/src/addons/mod/survey/components/index/index.ts +++ b/src/addons/mod/survey/components/index/index.ts @@ -13,6 +13,7 @@ // limitations under the License. import { Component, OnInit, Optional } from '@angular/core'; +import { CoreError } from '@classes/errors/error'; import { CoreIonLoadingElement } from '@classes/ion-loading'; import { CoreCourseModuleMainActivityComponent } from '@features/course/classes/main-activity-component'; import { CoreCourseContentsPage } from '@features/course/pages/contents/contents'; @@ -117,8 +118,8 @@ export class AddonModSurveyIndexComponent extends CoreCourseModuleMainActivityCo if (sync) { // Try to synchronize the survey. - const answersSent = await this.syncActivity(showErrors); - if (answersSent) { + const updated = await this.syncActivity(showErrors); + if (updated) { // Answers were sent, update the survey. this.survey = await AddonModSurvey.getSurvey(this.courseId, this.module.id); } @@ -236,26 +237,14 @@ export class AddonModSurveyIndexComponent extends CoreCourseModuleMainActivityCo } /** - * Performs the sync of the activity. - * - * @returns Promise resolved when done. + * @inheritdoc */ - protected async sync(): Promise { + protected async sync(): Promise { if (!this.survey) { - return; + throw new CoreError('Cannot sync without a survey.'); } return AddonModSurveySync.syncSurvey(this.survey.id, this.currentUserId); } - /** - * Checks if sync has succeed from result sync data. - * - * @param result Data returned on the sync function. - * @returns If suceed or not. - */ - protected hasSyncSucceed(result: AddonModSurveySyncResult): boolean { - return result.answersSent; - } - } diff --git a/src/addons/mod/survey/services/survey-sync.ts b/src/addons/mod/survey/services/survey-sync.ts index 455039f7b..6089e342a 100644 --- a/src/addons/mod/survey/services/survey-sync.ts +++ b/src/addons/mod/survey/services/survey-sync.ts @@ -15,6 +15,7 @@ import { Injectable } from '@angular/core'; import { CoreNetworkError } from '@classes/errors/network-error'; import { CoreCourseActivitySyncBaseProvider } from '@features/course/classes/activity-sync'; +import { CoreSyncResult } from '@services/sync'; import { CoreCourse } from '@features/course/services/course'; import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreNetwork } from '@services/network'; @@ -80,7 +81,7 @@ export class AddonModSurveySyncProvider extends CoreCourseActivitySyncBaseProvid ? this.syncSurvey(entry.surveyid, entry.userid, siteId) : this.syncSurveyIfNeeded(entry.surveyid, entry.userid, siteId)); - if (result && result.answersSent) { + if (result && result.updated) { // Sync successful, send event. CoreEvents.trigger(AddonModSurveySyncProvider.AUTO_SYNCED, { surveyId: entry.surveyid, @@ -150,7 +151,7 @@ export class AddonModSurveySyncProvider extends CoreCourseActivitySyncBaseProvid protected async performSyncSurvey(surveyId: number, userId: number, siteId: string): Promise { const result: AddonModSurveySyncResult = { warnings: [], - answersSent: false, + updated: false, }; // Sync offline logs. @@ -179,7 +180,7 @@ export class AddonModSurveySyncProvider extends CoreCourseActivitySyncBaseProvid try { await AddonModSurvey.submitAnswersOnline(surveyId, data.answers, siteId); - result.answersSent = true; + result.updated = true; // Answers sent, delete them. await AddonModSurveyOffline.deleteSurveyAnswers(surveyId, siteId, userId); @@ -190,7 +191,7 @@ export class AddonModSurveySyncProvider extends CoreCourseActivitySyncBaseProvid } // The WebService has thrown an error, this means that answers cannot be submitted. Delete them. - result.answersSent = true; + result.updated = true; await AddonModSurveyOffline.deleteSurveyAnswers(surveyId, siteId, userId); @@ -236,9 +237,7 @@ declare module '@singletons/events' { /** * Data returned by a assign sync. */ -export type AddonModSurveySyncResult = { - warnings: string[]; // List of warnings. - answersSent: boolean; // Whether some data was sent to the server or offline data was updated. +export type AddonModSurveySyncResult = CoreSyncResult & { courseId?: number; // Course the survey belongs to (if known). }; diff --git a/src/addons/mod/wiki/components/index/index.ts b/src/addons/mod/wiki/components/index/index.ts index 333b921d6..3b724896a 100644 --- a/src/addons/mod/wiki/components/index/index.ts +++ b/src/addons/mod/wiki/components/index/index.ts @@ -707,12 +707,9 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp } /** - * Checks if sync has succeed from result sync data. - * - * @param result Data returned on the sync function. - * @returns If suceed or not. + * @inheritdoc */ - protected hasSyncSucceed(result: AddonModWikiSyncWikiResult | undefined): boolean { + protected hasSyncSucceed(result: AddonModWikiSyncWikiResult): boolean { if (!result) { return false; } @@ -843,13 +840,11 @@ export class AddonModWikiIndexComponent extends CoreCourseModuleMainActivityComp } /** - * Performs the sync of the activity. - * - * @returns Promise resolved when done. + * @inheritdoc */ - protected async sync(): Promise { + protected async sync(): Promise { if (!this.wiki) { - return; + throw new CoreError('Cannot sync without a wiki.'); } return AddonModWikiSync.syncWiki(this.wiki.id, this.courseId, this.wiki.coursemodule); diff --git a/src/addons/mod/wiki/services/wiki-sync.ts b/src/addons/mod/wiki/services/wiki-sync.ts index 906c62365..0ba10b7f2 100644 --- a/src/addons/mod/wiki/services/wiki-sync.ts +++ b/src/addons/mod/wiki/services/wiki-sync.ts @@ -19,7 +19,7 @@ import { CoreCourseLogHelper } from '@features/course/services/log-helper'; import { CoreNetwork } from '@services/network'; import { CoreGroups } from '@services/groups'; import { CoreSites } from '@services/sites'; -import { CoreSync } from '@services/sync'; +import { CoreSync, CoreSyncResult } from '@services/sync'; import { CoreUtils } from '@services/utils/utils'; import { makeSingleton, Translate } from '@singletons'; import { CoreEvents } from '@singletons/events'; @@ -345,9 +345,7 @@ export const AddonModWikiSync = makeSingleton(AddonModWikiSyncProvider); /** * Data returned by a subwiki sync. */ -export type AddonModWikiSyncSubwikiResult = { - warnings: string[]; // List of warnings. - updated: boolean; // Whether data was updated in the site. +export type AddonModWikiSyncSubwikiResult = CoreSyncResult & { created: AddonModWikiCreatedPage[]; // List of created pages. discarded: AddonModWikiDiscardedPage[]; // List of discarded pages. }; @@ -355,9 +353,7 @@ export type AddonModWikiSyncSubwikiResult = { /** * Data returned by a wiki sync. */ -export type AddonModWikiSyncWikiResult = { - warnings: string[]; // List of warnings. - updated: boolean; // Whether data was updated in the site. +export type AddonModWikiSyncWikiResult = CoreSyncResult & { subwikis: { [subwikiId: number]: { // List of subwikis. created: AddonModWikiCreatedPage[]; diff --git a/src/addons/mod/workshop/components/index/index.ts b/src/addons/mod/workshop/components/index/index.ts index 70933bd6f..cfed3a329 100644 --- a/src/addons/mod/workshop/components/index/index.ts +++ b/src/addons/mod/workshop/components/index/index.ts @@ -528,9 +528,7 @@ export class AddonModWorkshopIndexComponent extends CoreCourseModuleMainActivity } /** - * Performs the sync of the activity. - * - * @returns Promise resolved when done. + * @inheritdoc */ protected sync(): Promise { if (!this.workshop) { @@ -540,16 +538,6 @@ export class AddonModWorkshopIndexComponent extends CoreCourseModuleMainActivity return AddonModWorkshopSync.syncWorkshop(this.workshop.id); } - /** - * Checks if sync has succeed from result sync data. - * - * @param result Data returned on the sync function. - * @returns If suceed or not. - */ - protected hasSyncSucceed(result: AddonModWorkshopSyncResult): boolean { - return result.updated; - } - /** * @inheritdoc */ diff --git a/src/addons/mod/workshop/services/workshop-sync.ts b/src/addons/mod/workshop/services/workshop-sync.ts index 695afa417..3e345020b 100644 --- a/src/addons/mod/workshop/services/workshop-sync.ts +++ b/src/addons/mod/workshop/services/workshop-sync.ts @@ -20,7 +20,7 @@ import { CoreFileUploaderStoreFilesResult } from '@features/fileuploader/service import { CoreNetwork } from '@services/network'; import { CoreFileEntry } from '@services/file-helper'; import { CoreSites } from '@services/sites'; -import { CoreSync } from '@services/sync'; +import { CoreSync, CoreSyncResult } from '@services/sync'; import { CoreTextUtils } from '@services/utils/text'; import { CoreUtils } from '@services/utils/utils'; import { Translate, makeSingleton } from '@singletons'; @@ -639,7 +639,4 @@ export type AddonModWorkshopAutoSyncData = { warnings: string[]; }; -export type AddonModWorkshopSyncResult = { - warnings: string[]; - updated: boolean; -}; +export type AddonModWorkshopSyncResult = CoreSyncResult; diff --git a/src/core/features/comments/services/comments-sync.ts b/src/core/features/comments/services/comments-sync.ts index 78a9d046a..1d37106cd 100644 --- a/src/core/features/comments/services/comments-sync.ts +++ b/src/core/features/comments/services/comments-sync.ts @@ -23,6 +23,7 @@ import { CoreNetwork } from '@services/network'; import { CoreUtils } from '@services/utils/utils'; import { CoreNetworkError } from '@classes/errors/network-error'; import { CoreCommentsDBRecord, CoreCommentsDeletedDBRecord } from './database/comments'; +import { CoreSyncResult } from '@services/sync'; /** * Service to sync omments. @@ -318,10 +319,7 @@ export class CoreCommentsSyncProvider extends CoreSyncBaseProvider { - return {}; + protected async sync(): Promise { + return { + updated: false, + warnings: [], + }; } /** - * Checks if sync has succeed from result sync data. + * Checks if sync has updated data on the server. * * @param result Data returned on the sync function. - * @returns If suceed or not. + * @returns If data has been updated or not. */ - // eslint-disable-next-line @typescript-eslint/no-unused-vars - protected hasSyncSucceed(result: unknown): boolean { - return true; + protected hasSyncSucceed(result: CoreSyncResult): boolean { + return result.updated; } /** * Tries to synchronize the activity. * * @param showErrors If show errors to the user of hide them. - * @returns Promise resolved with true if sync succeed, or false if failed. + * @returns Promise resolved with true if sync hast updated data to the server, false otherwise. */ protected async syncActivity(showErrors: boolean = false): Promise { try { - const result = <{warnings?: CoreWSExternalWarning[]}> await this.sync(); + const result = await this.sync(); - if (result?.warnings?.length) { + if (result.warnings.length) { CoreDomUtils.showErrorModal(result.warnings[0]); } diff --git a/src/core/services/sync.ts b/src/core/services/sync.ts index 70f990d4f..36ef93ac5 100644 --- a/src/core/services/sync.ts +++ b/src/core/services/sync.ts @@ -183,3 +183,8 @@ export class CoreSyncProvider { } export const CoreSync = makeSingleton(CoreSyncProvider); + +export type CoreSyncResult = { + warnings: string[]; // List of warnings. + updated: boolean; // Whether some data was sent to the server or offline data was updated. +};