@@ -117,65 +117,51 @@ export class AddonModDataProvider {
117117 * @param forceOffline Force editing entry in offline.
118118 * @return Promise resolved when the action is done.
119119 */
120- addEntry ( dataId : number , entryId : number , courseId : number , contents : AddonModDataSubfieldData [ ] , groupId : number = 0 ,
120+ async addEntry ( dataId : number , entryId : number , courseId : number , contents : AddonModDataSubfieldData [ ] , groupId : number = 0 ,
121121 fields : any , siteId ?: string , forceOffline : boolean = false ) : Promise < any > {
122122 siteId = siteId || this . sitesProvider . getCurrentSiteId ( ) ;
123123
124124 // Convenience function to store a data to be synchronized later.
125- const storeOffline = ( ) : Promise < any > => {
126- return this . dataOffline . saveEntry ( dataId , entryId , 'add' , courseId , groupId , contents , undefined , siteId )
127- . then ( ( entry ) => {
128- return {
129- // Return provissional entry Id.
130- newentryid : entry ,
131- sent : false ,
132- } ;
133- } ) ;
125+ const storeOffline = async ( ) : Promise < any > => {
126+ const entry = await this . dataOffline . saveEntry ( dataId , entryId , 'add' , courseId , groupId , contents , undefined , siteId ) ;
127+
128+ return {
129+ // Return provissional entry Id.
130+ newentryid : entry ,
131+ sent : false ,
132+ } ;
134133 } ;
135134
136135 // Checks to store offline.
137136 if ( ! this . appProvider . isOnline ( ) || forceOffline ) {
138137 const notifications = this . checkFields ( fields , contents ) ;
139138 if ( notifications ) {
140- return Promise . resolve ( {
141- fieldnotifications : notifications
142- } ) ;
139+ return { fieldnotifications : notifications } ;
143140 }
144141 }
145142
146- // Get other not synced actions.
147- return this . dataOffline . getEntryActions ( dataId , entryId , siteId ) . then ( ( entries ) => {
148- if ( entries && entries . length ) {
149- // Found. Delete add and edit actions first.
150- const proms = [ ] ;
151- entries . forEach ( ( entry ) => {
152- if ( entry . action == 'add' ) {
153- proms . push ( this . dataOffline . deleteEntry ( dataId , entryId , entry . action , siteId ) ) ;
154- }
155- } ) ;
143+ // Remove unnecessary not synced actions.
144+ await this . deleteEntryOfflineAction ( dataId , entryId , 'add' , siteId ) ;
156145
157- return Promise . all ( proms ) ;
158- }
159- } ) . then ( ( ) => {
160- // App is offline, store the action.
161- if ( ! this . appProvider . isOnline ( ) || forceOffline ) {
162- return storeOffline ( ) ;
163- }
146+ // App is offline, store the action.
147+ if ( ! this . appProvider . isOnline ( ) || forceOffline ) {
148+ return storeOffline ( ) ;
149+ }
164150
165- return this . addEntryOnline ( dataId , contents , groupId , siteId ) . then ( ( result ) => {
166- result . sent = true ;
151+ try {
152+ const result = await this . addEntryOnline ( dataId , contents , groupId , siteId ) ;
153+ result . sent = true ;
167154
168- return result ;
169- } ) . catch ( ( error ) => {
170- if ( this . utils . isWebServiceError ( error ) ) {
171- // The WebService has thrown an error, this means that responses cannot be submitted.
172- return Promise . reject ( error ) ;
173- }
155+ return result ;
156+ } catch ( error ) {
157+ if ( this . utils . isWebServiceError ( error ) ) {
158+ // The WebService has thrown an error, this means that responses cannot be submitted.
159+ throw error ;
160+ }
174161
175- // Couldn't connect to server, store in offline.
176- return storeOffline ( ) ;
177- } ) ;
178- } ) ;
162+ // Couldn't connect to server, store in offline.
163+ return storeOffline ( ) ;
164+ }
179165 }
180166
181167 /**
@@ -212,48 +198,49 @@ export class AddonModDataProvider {
212198 * @param siteId Site ID. If not defined, current site.
213199 * @return Promise resolved when the action is done.
214200 */
215- approveEntry ( dataId : number , entryId : number , approve : boolean , courseId : number , siteId ?: string ) : Promise < any > {
201+ async approveEntry ( dataId : number , entryId : number , approve : boolean , courseId : number , siteId ?: string ) : Promise < any > {
216202 siteId = siteId || this . sitesProvider . getCurrentSiteId ( ) ;
217203
218204 // Convenience function to store a data to be synchronized later.
219- const storeOffline = ( ) : Promise < any > => {
205+ const storeOffline = async ( ) : Promise < any > => {
220206 const action = approve ? 'approve' : 'disapprove' ;
221207
222- return this . dataOffline . saveEntry ( dataId , entryId , action , courseId , undefined , undefined , undefined , siteId )
223- . then ( ( ) => {
224- return {
225- sent : false ,
226- } ;
227- } ) ;
208+ await this . dataOffline . saveEntry ( dataId , entryId , action , courseId , undefined , undefined , undefined , siteId ) ;
209+
210+ return {
211+ sent : false ,
212+ } ;
228213 } ;
229214
230215 // Get if the opposite action is not synced.
231216 const oppositeAction = approve ? 'disapprove' : 'approve' ;
232217
233- return this . dataOffline . getEntry ( dataId , entryId , oppositeAction , siteId ) . then ( ( ) => {
234- // Found. Just delete the action.
235- return this . dataOffline . deleteEntry ( dataId , entryId , oppositeAction , siteId ) ;
236- } ) . catch ( ( ) => {
218+ const found = await this . deleteEntryOfflineAction ( dataId , entryId , oppositeAction , siteId ) ;
219+ if ( found ) {
220+ // Offline action has been found and deleted. Stop here.
221+ return ;
222+ }
237223
238- if ( ! this . appProvider . isOnline ( ) ) {
239- // App is offline, store the action.
240- return storeOffline ( ) ;
241- }
224+ if ( ! this . appProvider . isOnline ( ) ) {
225+ // App is offline, store the action.
226+ return storeOffline ( ) ;
227+ }
242228
243- return this . approveEntryOnline ( entryId , approve , siteId ) . then ( ( ) => {
244- return {
245- sent : true ,
246- } ;
247- } ) . catch ( ( error ) => {
248- if ( this . utils . isWebServiceError ( error ) ) {
249- // The WebService has thrown an error, this means that responses cannot be submitted.
250- return Promise . reject ( error ) ;
251- }
229+ try {
230+ await this . approveEntryOnline ( entryId , approve , siteId ) ;
252231
253- // Couldn't connect to server, store in offline.
254- return storeOffline ( ) ;
255- } ) ;
256- } ) ;
232+ return {
233+ sent : true ,
234+ } ;
235+ } catch ( error ) {
236+ if ( this . utils . isWebServiceError ( error ) ) {
237+ // The WebService has thrown an error, this means that responses cannot be submitted.
238+ throw error ;
239+ }
240+
241+ // Couldn't connect to server, store in offline.
242+ return storeOffline ( ) ;
243+ }
257244 }
258245
259246 /**
@@ -317,60 +304,45 @@ export class AddonModDataProvider {
317304 * @param siteId Site ID. If not defined, current site.
318305 * @return Promise resolved when the action is done.
319306 */
320- deleteEntry ( dataId : number , entryId : number , courseId : number , siteId ?: string ) : Promise < any > {
307+ async deleteEntry ( dataId : number , entryId : number , courseId : number , siteId ?: string ) : Promise < any > {
321308 siteId = siteId || this . sitesProvider . getCurrentSiteId ( ) ;
322309
323310 // Convenience function to store a data to be synchronized later.
324- const storeOffline = ( ) : Promise < any > => {
325- return this . dataOffline . saveEntry ( dataId , entryId , 'delete' , courseId , undefined , undefined , undefined , siteId )
326- . then ( ( ) => {
327- return {
328- sent : false ,
329- } ;
330- } ) ;
331- } ;
311+ const storeOffline = async ( ) : Promise < any > => {
312+ await this . dataOffline . saveEntry ( dataId , entryId , 'delete' , courseId , undefined , undefined , undefined , siteId ) ;
332313
333- let justAdded = false ;
314+ return {
315+ sent : false ,
316+ } ;
317+ } ;
334318
335319 // Check if the opposite action is not synced and just delete it.
336- return this . dataOffline . getEntryActions ( dataId , entryId , siteId ) . then ( ( entries ) => {
337- if ( entries && entries . length ) {
338- // Found. Delete other actions first.
339- const proms = entries . map ( ( entry ) => {
340- if ( entry . action == 'add' ) {
341- justAdded = true ;
342- }
320+ const addedOffline = await this . deleteEntryOfflineAction ( dataId , entryId , 'add' , siteId ) ;
321+ if ( addedOffline ) {
322+ // Offline add action found and deleted. Stop here.
323+ return ;
324+ }
343325
344- return this . dataOffline . deleteEntry ( dataId , entryId , entry . action , siteId ) ;
345- } ) ;
326+ if ( ! this . appProvider . isOnline ( ) ) {
327+ // App is offline, store the action.
328+ return storeOffline ( ) ;
329+ }
346330
347- return Promise . all ( proms ) ;
348- }
349- } ) . then ( ( ) => {
350- if ( justAdded ) {
351- // The field was added offline, delete and stop.
352- return ;
353- }
331+ try {
332+ await this . deleteEntryOnline ( entryId , siteId ) ;
354333
355- if ( ! this . appProvider . isOnline ( ) ) {
356- // App is offline, store the action.
357- return storeOffline ( ) ;
334+ return {
335+ sent : true ,
336+ } ;
337+ } catch ( error ) {
338+ if ( this . utils . isWebServiceError ( error ) ) {
339+ // The WebService has thrown an error, this means that responses cannot be submitted.
340+ throw error ;
358341 }
359342
360- return this . deleteEntryOnline ( entryId , siteId ) . then ( ( ) => {
361- return {
362- sent : true ,
363- } ;
364- } ) . catch ( ( error ) => {
365- if ( this . utils . isWebServiceError ( error ) ) {
366- // The WebService has thrown an error, this means that responses cannot be submitted.
367- return Promise . reject ( error ) ;
368- }
369-
370- // Couldn't connect to server, store in offline.
371- return storeOffline ( ) ;
372- } ) ;
373- } ) ;
343+ // Couldn't connect to server, store in offline.
344+ return storeOffline ( ) ;
345+ }
374346 }
375347
376348 /**
@@ -390,6 +362,29 @@ export class AddonModDataProvider {
390362 } ) ;
391363 }
392364
365+ /**
366+ * Delete entry offline action.
367+ *
368+ * @param dataId Database ID.
369+ * @param entryId Entry ID.
370+ * @param action Action name to delete.
371+ * @param siteId Site ID.
372+ * @return Resolved with true if the action has been found and deleted.
373+ */
374+ protected async deleteEntryOfflineAction ( dataId : number , entryId : number , action : string , siteId : string ) : Promise < boolean > {
375+ // Get other not not synced actions.
376+ try {
377+ await this . dataOffline . getEntry ( dataId , entryId , action , siteId ) ;
378+
379+ await this . dataOffline . deleteEntry ( dataId , entryId , action , siteId ) ;
380+
381+ return true ;
382+ } catch ( error ) {
383+ // Not found.
384+ return false ;
385+ }
386+ }
387+
393388 /**
394389 * Updates an existing entry.
395390 *
@@ -402,64 +397,50 @@ export class AddonModDataProvider {
402397 * @param forceOffline Force editing entry in offline.
403398 * @return Promise resolved when the action is done.
404399 */
405- editEntry ( dataId : number , entryId : number , courseId : number , contents : AddonModDataSubfieldData [ ] , fields : any , siteId ?: string ,
406- forceOffline : boolean = false ) : Promise < any > {
400+ async editEntry ( dataId : number , entryId : number , courseId : number , contents : AddonModDataSubfieldData [ ] , fields : any ,
401+ siteId ?: string , forceOffline : boolean = false ) : Promise < any > {
407402 siteId = siteId || this . sitesProvider . getCurrentSiteId ( ) ;
408403
409404 // Convenience function to store a data to be synchronized later.
410- const storeOffline = ( ) : Promise < any > => {
411- return this . dataOffline . saveEntry ( dataId , entryId , 'edit' , courseId , undefined , contents , undefined , siteId )
412- . then ( ( ) => {
413- return {
414- updated : true ,
415- sent : false ,
416- } ;
417- } ) ;
405+ const storeOffline = async ( ) : Promise < any > => {
406+ await this . dataOffline . saveEntry ( dataId , entryId , 'edit' , courseId , undefined , contents , undefined , siteId ) ;
407+
408+ return {
409+ updated : true ,
410+ sent : false ,
411+ } ;
418412 } ;
419413
420414 if ( ! this . appProvider . isOnline ( ) || forceOffline ) {
421415 const notifications = this . checkFields ( fields , contents ) ;
422416 if ( notifications ) {
423- return Promise . resolve ( {
424- fieldnotifications : notifications
425- } ) ;
417+ return { fieldnotifications : notifications } ;
426418 }
427419 }
428420
429- // Get other not not synced actions.
430- return this . dataOffline . getEntryActions ( dataId , entryId , siteId ) . then ( ( entries ) => {
431- if ( entries && entries . length ) {
432- // Found. Delete add and edit actions first.
433- const proms = [ ] ;
434- entries . forEach ( ( entry ) => {
435- if ( entry . action == 'edit' ) {
436- proms . push ( this . dataOffline . deleteEntry ( dataId , entryId , entry . action , siteId ) ) ;
437- }
438- } ) ;
421+ // Remove unnecessary not synced actions.
422+ await this . deleteEntryOfflineAction ( dataId , entryId , 'edit' , siteId ) ;
439423
440- return Promise . all ( proms ) ;
441- }
442- } ) . then ( ( ) => {
443- if ( ! this . appProvider . isOnline ( ) || forceOffline ) {
444- // App is offline, store the action.
445- return storeOffline ( ) ;
446- }
424+ if ( ! this . appProvider . isOnline ( ) || forceOffline ) {
425+ // App is offline, store the action.
426+ return storeOffline ( ) ;
427+ }
447428
448- return this . editEntryOnline ( entryId , contents , siteId ) . then ( ( result ) => {
449- result . sent = true ;
429+ try {
430+ const result = await this . editEntryOnline ( entryId , contents , siteId ) ;
431+ result . sent = true ;
450432
451- return result ;
452- } ) . catch ( ( error ) => {
453- if ( this . utils . isWebServiceError ( error ) ) {
454- // The WebService has thrown an error, this means that responses cannot be submitted.
455- return Promise . reject ( error ) ;
456- }
433+ return result ;
434+ } catch ( error ) {
435+ if ( this . utils . isWebServiceError ( error ) ) {
436+ // The WebService has thrown an error, this means that responses cannot be submitted.
437+ throw error ;
438+ }
457439
458- // Couldn't connect to server, store in offline.
459- return storeOffline ( ) ;
460- } ) ;
461- } ) ;
462- }
440+ // Couldn't connect to server, store in offline.
441+ return storeOffline ( ) ;
442+ }
443+ }
463444
464445 /**
465446 * Updates an existing entry. It does not cache calls. It will fail if offline or cannot connect.
0 commit comments