|
|
|
@ -158,6 +158,46 @@ export class ApiService {
|
|
|
|
|
return `${this.baseEndpoint.endsWith('/') ? this.baseEndpoint.slice(0, -1) : this.baseEndpoint}${endpoint}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getDeviceToken(): Promise<string> {
|
|
|
|
|
return new Promise(async (res, rej) => {
|
|
|
|
|
const tokenKV = await this.db.getKeyValue('device_token');
|
|
|
|
|
if ( !tokenKV.data && this.isOffline ) {
|
|
|
|
|
return rej(new ResourceNotAvailableOfflineError());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( tokenKV.data ) {
|
|
|
|
|
const expDate = new Date(tokenKV.data.expiration_date);
|
|
|
|
|
if (expDate > new Date()) {
|
|
|
|
|
return res(tokenKV.data.token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.get(`/session/device-token`).subscribe({
|
|
|
|
|
next: async result => {
|
|
|
|
|
tokenKV.data = result.data;
|
|
|
|
|
await tokenKV.save();
|
|
|
|
|
res(result.data.token);
|
|
|
|
|
},
|
|
|
|
|
error: rej,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public resumeSession(): Promise<void> {
|
|
|
|
|
return new Promise(async (res, rej) => {
|
|
|
|
|
if ( !this.isOffline ) {
|
|
|
|
|
this.getDeviceToken().then(token => {
|
|
|
|
|
this.post(`/session/resume/${token}`).subscribe({
|
|
|
|
|
next: result => {
|
|
|
|
|
res();
|
|
|
|
|
},
|
|
|
|
|
error: rej,
|
|
|
|
|
});
|
|
|
|
|
}).catch(rej);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getMenuItems(): Promise<any[]> {
|
|
|
|
|
return new Promise(async (res, rej) => {
|
|
|
|
|
await this.db.createSchemata();
|
|
|
|
@ -193,6 +233,7 @@ export class ApiService {
|
|
|
|
|
public getSessionData(): Promise<any> {
|
|
|
|
|
return new Promise(async (res, rej) => {
|
|
|
|
|
const sessionKV = await this.db.getKeyValue('session_data');
|
|
|
|
|
const authenticatedUserKV = await this.db.getKeyValue('authenticated_user');
|
|
|
|
|
|
|
|
|
|
// If offline, just return the locally cached session data
|
|
|
|
|
if ( this.isOffline ) {
|
|
|
|
@ -205,6 +246,16 @@ export class ApiService {
|
|
|
|
|
|
|
|
|
|
// Otherwise, fetch the session data from the server and cache it locally
|
|
|
|
|
this.get('/session').subscribe(async result => {
|
|
|
|
|
// If the locally logged in user is not the one in the server session, purge everything and reset
|
|
|
|
|
if (
|
|
|
|
|
authenticatedUserKV.data
|
|
|
|
|
&& authenticatedUserKV.data?.user?.id
|
|
|
|
|
&& authenticatedUserKV.data.user.id !== result.data.user.id
|
|
|
|
|
) {
|
|
|
|
|
await this.cleanSession(result.data);
|
|
|
|
|
return res(result.data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sessionKV.data = result.data;
|
|
|
|
|
await sessionKV.save();
|
|
|
|
|
res(sessionKV.data);
|
|
|
|
@ -212,6 +263,22 @@ export class ApiService {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async cleanSession(data: any) {
|
|
|
|
|
await this.db.purge();
|
|
|
|
|
await new Promise((res, rej) => {
|
|
|
|
|
this.stat().subscribe({
|
|
|
|
|
next: res,
|
|
|
|
|
error: rej,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const sessionKV = await this.db.getKeyValue('session_data');
|
|
|
|
|
sessionKV.data = data;
|
|
|
|
|
await sessionKV.save();
|
|
|
|
|
|
|
|
|
|
await this.getDeviceToken();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public saveSessionData(data: any): Promise<void> {
|
|
|
|
|
return new Promise(async (res, rej) => {
|
|
|
|
|
// Update the local session data
|
|
|
|
|