MOBILE-3200 database: Translate some async functions
parent
cb4eac9a17
commit
572e907ac1
|
@ -117,65 +117,51 @@ export class AddonModDataProvider {
|
||||||
* @param forceOffline Force editing entry in offline.
|
* @param forceOffline Force editing entry in offline.
|
||||||
* @return Promise resolved when the action is done.
|
* @return Promise resolved when the action is done.
|
||||||
*/
|
*/
|
||||||
addEntry(dataId: number, entryId: number, courseId: number, contents: AddonModDataSubfieldData[], groupId: number = 0,
|
async addEntry(dataId: number, entryId: number, courseId: number, contents: AddonModDataSubfieldData[], groupId: number = 0,
|
||||||
fields: any, siteId?: string, forceOffline: boolean = false): Promise<any> {
|
fields: any, siteId?: string, forceOffline: boolean = false): Promise<any> {
|
||||||
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
||||||
|
|
||||||
// Convenience function to store a data to be synchronized later.
|
// Convenience function to store a data to be synchronized later.
|
||||||
const storeOffline = (): Promise<any> => {
|
const storeOffline = async (): Promise<any> => {
|
||||||
return this.dataOffline.saveEntry(dataId, entryId, 'add', courseId, groupId, contents, undefined, siteId)
|
const entry = await this.dataOffline.saveEntry(dataId, entryId, 'add', courseId, groupId, contents, undefined, siteId);
|
||||||
.then((entry) => {
|
|
||||||
return {
|
return {
|
||||||
// Return provissional entry Id.
|
// Return provissional entry Id.
|
||||||
newentryid: entry,
|
newentryid: entry,
|
||||||
sent: false,
|
sent: false,
|
||||||
};
|
};
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Checks to store offline.
|
// Checks to store offline.
|
||||||
if (!this.appProvider.isOnline() || forceOffline) {
|
if (!this.appProvider.isOnline() || forceOffline) {
|
||||||
const notifications = this.checkFields(fields, contents);
|
const notifications = this.checkFields(fields, contents);
|
||||||
if (notifications) {
|
if (notifications) {
|
||||||
return Promise.resolve({
|
return { fieldnotifications: notifications };
|
||||||
fieldnotifications: notifications
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get other not synced actions.
|
// Remove unnecessary not synced actions.
|
||||||
return this.dataOffline.getEntryActions(dataId, entryId, siteId).then((entries) => {
|
await this.deleteEntryOfflineAction(dataId, entryId, 'add', siteId);
|
||||||
if (entries && entries.length) {
|
|
||||||
// Found. Delete add and edit actions first.
|
|
||||||
const proms = [];
|
|
||||||
entries.forEach((entry) => {
|
|
||||||
if (entry.action == 'add') {
|
|
||||||
proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return Promise.all(proms);
|
|
||||||
}
|
|
||||||
}).then(() => {
|
|
||||||
// App is offline, store the action.
|
// App is offline, store the action.
|
||||||
if (!this.appProvider.isOnline() || forceOffline) {
|
if (!this.appProvider.isOnline() || forceOffline) {
|
||||||
return storeOffline();
|
return storeOffline();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.addEntryOnline(dataId, contents, groupId, siteId).then((result) => {
|
try {
|
||||||
|
const result = await this.addEntryOnline(dataId, contents, groupId, siteId);
|
||||||
result.sent = true;
|
result.sent = true;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}).catch((error) => {
|
} catch (error) {
|
||||||
if (this.utils.isWebServiceError(error)) {
|
if (this.utils.isWebServiceError(error)) {
|
||||||
// The WebService has thrown an error, this means that responses cannot be submitted.
|
// The WebService has thrown an error, this means that responses cannot be submitted.
|
||||||
return Promise.reject(error);
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Couldn't connect to server, store in offline.
|
// Couldn't connect to server, store in offline.
|
||||||
return storeOffline();
|
return storeOffline();
|
||||||
});
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -212,48 +198,49 @@ export class AddonModDataProvider {
|
||||||
* @param siteId Site ID. If not defined, current site.
|
* @param siteId Site ID. If not defined, current site.
|
||||||
* @return Promise resolved when the action is done.
|
* @return Promise resolved when the action is done.
|
||||||
*/
|
*/
|
||||||
approveEntry(dataId: number, entryId: number, approve: boolean, courseId: number, siteId?: string): Promise<any> {
|
async approveEntry(dataId: number, entryId: number, approve: boolean, courseId: number, siteId?: string): Promise<any> {
|
||||||
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
||||||
|
|
||||||
// Convenience function to store a data to be synchronized later.
|
// Convenience function to store a data to be synchronized later.
|
||||||
const storeOffline = (): Promise<any> => {
|
const storeOffline = async (): Promise<any> => {
|
||||||
const action = approve ? 'approve' : 'disapprove';
|
const action = approve ? 'approve' : 'disapprove';
|
||||||
|
|
||||||
return this.dataOffline.saveEntry(dataId, entryId, action, courseId, undefined, undefined, undefined, siteId)
|
await this.dataOffline.saveEntry(dataId, entryId, action, courseId, undefined, undefined, undefined, siteId);
|
||||||
.then(() => {
|
|
||||||
return {
|
return {
|
||||||
sent: false,
|
sent: false,
|
||||||
};
|
};
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get if the opposite action is not synced.
|
// Get if the opposite action is not synced.
|
||||||
const oppositeAction = approve ? 'disapprove' : 'approve';
|
const oppositeAction = approve ? 'disapprove' : 'approve';
|
||||||
|
|
||||||
return this.dataOffline.getEntry(dataId, entryId, oppositeAction, siteId).then(() => {
|
const found = await this.deleteEntryOfflineAction(dataId, entryId, oppositeAction, siteId);
|
||||||
// Found. Just delete the action.
|
if (found) {
|
||||||
return this.dataOffline.deleteEntry(dataId, entryId, oppositeAction, siteId);
|
// Offline action has been found and deleted. Stop here.
|
||||||
}).catch(() => {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!this.appProvider.isOnline()) {
|
if (!this.appProvider.isOnline()) {
|
||||||
// App is offline, store the action.
|
// App is offline, store the action.
|
||||||
return storeOffline();
|
return storeOffline();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.approveEntryOnline(entryId, approve, siteId).then(() => {
|
try {
|
||||||
|
await this.approveEntryOnline(entryId, approve, siteId);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sent: true,
|
sent: true,
|
||||||
};
|
};
|
||||||
}).catch((error) => {
|
} catch (error) {
|
||||||
if (this.utils.isWebServiceError(error)) {
|
if (this.utils.isWebServiceError(error)) {
|
||||||
// The WebService has thrown an error, this means that responses cannot be submitted.
|
// The WebService has thrown an error, this means that responses cannot be submitted.
|
||||||
return Promise.reject(error);
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Couldn't connect to server, store in offline.
|
// Couldn't connect to server, store in offline.
|
||||||
return storeOffline();
|
return storeOffline();
|
||||||
});
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -317,38 +304,22 @@ export class AddonModDataProvider {
|
||||||
* @param siteId Site ID. If not defined, current site.
|
* @param siteId Site ID. If not defined, current site.
|
||||||
* @return Promise resolved when the action is done.
|
* @return Promise resolved when the action is done.
|
||||||
*/
|
*/
|
||||||
deleteEntry(dataId: number, entryId: number, courseId: number, siteId?: string): Promise<any> {
|
async deleteEntry(dataId: number, entryId: number, courseId: number, siteId?: string): Promise<any> {
|
||||||
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
||||||
|
|
||||||
// Convenience function to store a data to be synchronized later.
|
// Convenience function to store a data to be synchronized later.
|
||||||
const storeOffline = (): Promise<any> => {
|
const storeOffline = async (): Promise<any> => {
|
||||||
return this.dataOffline.saveEntry(dataId, entryId, 'delete', courseId, undefined, undefined, undefined, siteId)
|
await this.dataOffline.saveEntry(dataId, entryId, 'delete', courseId, undefined, undefined, undefined, siteId);
|
||||||
.then(() => {
|
|
||||||
return {
|
return {
|
||||||
sent: false,
|
sent: false,
|
||||||
};
|
};
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let justAdded = false;
|
|
||||||
|
|
||||||
// Check if the opposite action is not synced and just delete it.
|
// Check if the opposite action is not synced and just delete it.
|
||||||
return this.dataOffline.getEntryActions(dataId, entryId, siteId).then((entries) => {
|
const addedOffline = await this.deleteEntryOfflineAction(dataId, entryId, 'add', siteId);
|
||||||
if (entries && entries.length) {
|
if (addedOffline) {
|
||||||
// Found. Delete other actions first.
|
// Offline add action found and deleted. Stop here.
|
||||||
const proms = entries.map((entry) => {
|
|
||||||
if (entry.action == 'add') {
|
|
||||||
justAdded = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId);
|
|
||||||
});
|
|
||||||
|
|
||||||
return Promise.all(proms);
|
|
||||||
}
|
|
||||||
}).then(() => {
|
|
||||||
if (justAdded) {
|
|
||||||
// The field was added offline, delete and stop.
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -357,20 +328,21 @@ export class AddonModDataProvider {
|
||||||
return storeOffline();
|
return storeOffline();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.deleteEntryOnline(entryId, siteId).then(() => {
|
try {
|
||||||
|
await this.deleteEntryOnline(entryId, siteId);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sent: true,
|
sent: true,
|
||||||
};
|
};
|
||||||
}).catch((error) => {
|
} catch (error) {
|
||||||
if (this.utils.isWebServiceError(error)) {
|
if (this.utils.isWebServiceError(error)) {
|
||||||
// The WebService has thrown an error, this means that responses cannot be submitted.
|
// The WebService has thrown an error, this means that responses cannot be submitted.
|
||||||
return Promise.reject(error);
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Couldn't connect to server, store in offline.
|
// Couldn't connect to server, store in offline.
|
||||||
return storeOffline();
|
return storeOffline();
|
||||||
});
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -390,6 +362,29 @@ export class AddonModDataProvider {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete entry offline action.
|
||||||
|
*
|
||||||
|
* @param dataId Database ID.
|
||||||
|
* @param entryId Entry ID.
|
||||||
|
* @param action Action name to delete.
|
||||||
|
* @param siteId Site ID.
|
||||||
|
* @return Resolved with true if the action has been found and deleted.
|
||||||
|
*/
|
||||||
|
protected async deleteEntryOfflineAction(dataId: number, entryId: number, action: string, siteId: string): Promise<boolean> {
|
||||||
|
// Get other not not synced actions.
|
||||||
|
try {
|
||||||
|
await this.dataOffline.getEntry(dataId, entryId, action, siteId);
|
||||||
|
|
||||||
|
await this.dataOffline.deleteEntry(dataId, entryId, action, siteId);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
// Not found.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates an existing entry.
|
* Updates an existing entry.
|
||||||
*
|
*
|
||||||
|
@ -402,63 +397,49 @@ export class AddonModDataProvider {
|
||||||
* @param forceOffline Force editing entry in offline.
|
* @param forceOffline Force editing entry in offline.
|
||||||
* @return Promise resolved when the action is done.
|
* @return Promise resolved when the action is done.
|
||||||
*/
|
*/
|
||||||
editEntry(dataId: number, entryId: number, courseId: number, contents: AddonModDataSubfieldData[], fields: any, siteId?: string,
|
async editEntry(dataId: number, entryId: number, courseId: number, contents: AddonModDataSubfieldData[], fields: any,
|
||||||
forceOffline: boolean = false): Promise<any> {
|
siteId?: string, forceOffline: boolean = false): Promise<any> {
|
||||||
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
||||||
|
|
||||||
// Convenience function to store a data to be synchronized later.
|
// Convenience function to store a data to be synchronized later.
|
||||||
const storeOffline = (): Promise<any> => {
|
const storeOffline = async (): Promise<any> => {
|
||||||
return this.dataOffline.saveEntry(dataId, entryId, 'edit', courseId, undefined, contents, undefined, siteId)
|
await this.dataOffline.saveEntry(dataId, entryId, 'edit', courseId, undefined, contents, undefined, siteId);
|
||||||
.then(() => {
|
|
||||||
return {
|
return {
|
||||||
updated: true,
|
updated: true,
|
||||||
sent: false,
|
sent: false,
|
||||||
};
|
};
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!this.appProvider.isOnline() || forceOffline) {
|
if (!this.appProvider.isOnline() || forceOffline) {
|
||||||
const notifications = this.checkFields(fields, contents);
|
const notifications = this.checkFields(fields, contents);
|
||||||
if (notifications) {
|
if (notifications) {
|
||||||
return Promise.resolve({
|
return { fieldnotifications: notifications };
|
||||||
fieldnotifications: notifications
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get other not not synced actions.
|
// Remove unnecessary not synced actions.
|
||||||
return this.dataOffline.getEntryActions(dataId, entryId, siteId).then((entries) => {
|
await this.deleteEntryOfflineAction(dataId, entryId, 'edit', siteId);
|
||||||
if (entries && entries.length) {
|
|
||||||
// Found. Delete add and edit actions first.
|
|
||||||
const proms = [];
|
|
||||||
entries.forEach((entry) => {
|
|
||||||
if (entry.action == 'edit') {
|
|
||||||
proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return Promise.all(proms);
|
|
||||||
}
|
|
||||||
}).then(() => {
|
|
||||||
if (!this.appProvider.isOnline() || forceOffline) {
|
if (!this.appProvider.isOnline() || forceOffline) {
|
||||||
// App is offline, store the action.
|
// App is offline, store the action.
|
||||||
return storeOffline();
|
return storeOffline();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.editEntryOnline(entryId, contents, siteId).then((result) => {
|
try {
|
||||||
|
const result = await this.editEntryOnline(entryId, contents, siteId);
|
||||||
result.sent = true;
|
result.sent = true;
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}).catch((error) => {
|
} catch (error) {
|
||||||
if (this.utils.isWebServiceError(error)) {
|
if (this.utils.isWebServiceError(error)) {
|
||||||
// The WebService has thrown an error, this means that responses cannot be submitted.
|
// The WebService has thrown an error, this means that responses cannot be submitted.
|
||||||
return Promise.reject(error);
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Couldn't connect to server, store in offline.
|
// Couldn't connect to server, store in offline.
|
||||||
return storeOffline();
|
return storeOffline();
|
||||||
});
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue