MOBILE-4304 reminders: Update database usage
parent
869f08eee7
commit
4d4a506fe1
|
@ -159,6 +159,13 @@ export class CoreDatabaseTableProxy<
|
||||||
return this.target.insert(record);
|
return this.target.insert(record);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
|
syncInsert(record: Omit<DBRecord, RowIdColumn> & Partial<Pick<DBRecord, RowIdColumn>>): void {
|
||||||
|
this.target.syncInsert(record);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -23,6 +23,10 @@ import { CorePlatform } from '@services/platform';
|
||||||
import { CoreConstants } from '@/core/constants';
|
import { CoreConstants } from '@/core/constants';
|
||||||
import { CoreConfig } from '@services/config';
|
import { CoreConfig } from '@services/config';
|
||||||
import { CoreEvents } from '@singletons/events';
|
import { CoreEvents } from '@singletons/events';
|
||||||
|
import { lazyMap, LazyMap } from '@/core/utils/lazy-map';
|
||||||
|
import { CoreDatabaseTable } from '@classes/database/database-table';
|
||||||
|
import { asyncInstance, AsyncInstance } from '@/core/utils/async-instance';
|
||||||
|
import { CoreDatabaseCachingStrategy } from '@classes/database/database-table-proxy';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Units to set a reminder.
|
* Units to set a reminder.
|
||||||
|
@ -61,6 +65,20 @@ export class CoreRemindersService {
|
||||||
static readonly DEFAULT_NOTIFICATION_TIME_SETTING = 'CoreRemindersDefaultNotification';
|
static readonly DEFAULT_NOTIFICATION_TIME_SETTING = 'CoreRemindersDefaultNotification';
|
||||||
static readonly DEFAULT_NOTIFICATION_TIME_CHANGED = 'CoreRemindersDefaultNotificationChangedEvent';
|
static readonly DEFAULT_NOTIFICATION_TIME_CHANGED = 'CoreRemindersDefaultNotificationChangedEvent';
|
||||||
|
|
||||||
|
protected remindersTables: LazyMap<AsyncInstance<CoreDatabaseTable<CoreReminderDBRecord>>>;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.remindersTables = lazyMap(
|
||||||
|
siteId => asyncInstance(
|
||||||
|
() => CoreSites.getSiteTable(REMINDERS_TABLE, {
|
||||||
|
siteId,
|
||||||
|
config: { cachingStrategy: CoreDatabaseCachingStrategy.None },
|
||||||
|
onDestroy: () => delete this.remindersTables[siteId],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the service.
|
* Initialize the service.
|
||||||
*
|
*
|
||||||
|
@ -103,13 +121,13 @@ export class CoreRemindersService {
|
||||||
* @returns Resolved when done. Rejected on failure.
|
* @returns Resolved when done. Rejected on failure.
|
||||||
*/
|
*/
|
||||||
async addReminder(reminder: CoreReminderData, siteId?: string): Promise<void> {
|
async addReminder(reminder: CoreReminderData, siteId?: string): Promise<void> {
|
||||||
const site = await CoreSites.getSite(siteId);
|
siteId ??= CoreSites.getCurrentSiteId();
|
||||||
|
|
||||||
const reminderId = await site.getDb().insertRecord(REMINDERS_TABLE, reminder);
|
const reminderId = await this.remindersTables[siteId].insert(reminder);
|
||||||
|
|
||||||
const reminderRecord: CoreReminderDBRecord = Object.assign(reminder, { id: reminderId });
|
const reminderRecord: CoreReminderDBRecord = Object.assign(reminder, { id: reminderId });
|
||||||
|
|
||||||
await this.scheduleNotification(reminderRecord, site.getId());
|
await this.scheduleNotification(reminderRecord, siteId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -123,9 +141,9 @@ export class CoreRemindersService {
|
||||||
reminder: CoreReminderDBRecord,
|
reminder: CoreReminderDBRecord,
|
||||||
siteId?: string,
|
siteId?: string,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const site = await CoreSites.getSite(siteId);
|
siteId ??= CoreSites.getCurrentSiteId();
|
||||||
|
|
||||||
await site.getDb().updateRecords(REMINDERS_TABLE, reminder, { id: reminder.id });
|
await this.remindersTables[siteId].update(reminder, { id: reminder.id });
|
||||||
|
|
||||||
// Reschedule.
|
// Reschedule.
|
||||||
await this.scheduleNotification(reminder, siteId);
|
await this.scheduleNotification(reminder, siteId);
|
||||||
|
@ -162,9 +180,13 @@ export class CoreRemindersService {
|
||||||
* @returns Promise resolved when the reminder data is retrieved.
|
* @returns Promise resolved when the reminder data is retrieved.
|
||||||
*/
|
*/
|
||||||
async getAllReminders(siteId?: string): Promise<CoreReminderDBRecord[]> {
|
async getAllReminders(siteId?: string): Promise<CoreReminderDBRecord[]> {
|
||||||
const site = await CoreSites.getSite(siteId);
|
siteId ??= CoreSites.getCurrentSiteId();
|
||||||
|
|
||||||
return site.getDb().getRecords(REMINDERS_TABLE, undefined, 'time ASC');
|
return this.remindersTables[siteId].getMany(undefined, {
|
||||||
|
sorting: [
|
||||||
|
{ time: 'asc' },
|
||||||
|
],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -175,9 +197,13 @@ export class CoreRemindersService {
|
||||||
* @returns Promise resolved when the reminder data is retrieved.
|
* @returns Promise resolved when the reminder data is retrieved.
|
||||||
*/
|
*/
|
||||||
async getReminders(selector: CoreReminderSelector, siteId?: string): Promise<CoreReminderDBRecord[]> {
|
async getReminders(selector: CoreReminderSelector, siteId?: string): Promise<CoreReminderDBRecord[]> {
|
||||||
const site = await CoreSites.getSite(siteId);
|
siteId ??= CoreSites.getCurrentSiteId();
|
||||||
|
|
||||||
return site.getDb().getRecords(REMINDERS_TABLE, selector, 'time ASC');
|
return this.remindersTables[siteId].getMany(selector, {
|
||||||
|
sorting: [
|
||||||
|
{ time: 'asc' },
|
||||||
|
],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -187,13 +213,13 @@ export class CoreRemindersService {
|
||||||
* @returns Promise resolved when the reminder data is retrieved.
|
* @returns Promise resolved when the reminder data is retrieved.
|
||||||
*/
|
*/
|
||||||
protected async getRemindersWithDefaultTime(siteId?: string): Promise<CoreReminderDBRecord[]> {
|
protected async getRemindersWithDefaultTime(siteId?: string): Promise<CoreReminderDBRecord[]> {
|
||||||
const site = await CoreSites.getSite(siteId);
|
siteId ??= CoreSites.getCurrentSiteId();
|
||||||
|
|
||||||
return site.getDb().getRecords<CoreReminderDBRecord>(
|
return this.remindersTables[siteId].getMany({ timebefore: CoreRemindersService.DEFAULT_REMINDER_TIMEBEFORE }, {
|
||||||
REMINDERS_TABLE,
|
sorting: [
|
||||||
{ timebefore: CoreRemindersService.DEFAULT_REMINDER_TIMEBEFORE },
|
{ time: 'asc' },
|
||||||
'time ASC',
|
],
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -204,15 +230,15 @@ export class CoreRemindersService {
|
||||||
* @returns Promise resolved when the notification is updated.
|
* @returns Promise resolved when the notification is updated.
|
||||||
*/
|
*/
|
||||||
async removeReminder(id: number, siteId?: string): Promise<void> {
|
async removeReminder(id: number, siteId?: string): Promise<void> {
|
||||||
const site = await CoreSites.getSite(siteId);
|
siteId ??= CoreSites.getCurrentSiteId();
|
||||||
|
|
||||||
const reminder = await site.getDb().getRecord<CoreReminderDBRecord>(REMINDERS_TABLE, { id });
|
const reminder = await this.remindersTables[siteId].getOneByPrimaryKey({ id });
|
||||||
|
|
||||||
if (this.isEnabled()) {
|
if (this.isEnabled()) {
|
||||||
this.cancelReminder(id, reminder.component, site.getId());
|
this.cancelReminder(id, reminder.component, siteId);
|
||||||
}
|
}
|
||||||
|
|
||||||
await site.getDb().deleteRecords(REMINDERS_TABLE, { id });
|
await this.remindersTables[siteId].deleteByPrimaryKey({ id });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -223,8 +249,7 @@ export class CoreRemindersService {
|
||||||
* @returns Promise resolved when the notification is updated.
|
* @returns Promise resolved when the notification is updated.
|
||||||
*/
|
*/
|
||||||
async removeReminders(selector: CoreReminderSelector, siteId?: string): Promise<void> {
|
async removeReminders(selector: CoreReminderSelector, siteId?: string): Promise<void> {
|
||||||
const site = await CoreSites.getSite(siteId);
|
siteId ??= CoreSites.getCurrentSiteId();
|
||||||
siteId = site.getId();
|
|
||||||
|
|
||||||
if (this.isEnabled()) {
|
if (this.isEnabled()) {
|
||||||
const reminders = await this.getReminders(selector, siteId);
|
const reminders = await this.getReminders(selector, siteId);
|
||||||
|
@ -234,7 +259,7 @@ export class CoreRemindersService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await site.getDb().deleteRecords(REMINDERS_TABLE, selector);
|
await this.remindersTables[siteId].delete(selector);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue