Skip to content

Commit

Permalink
fix desktop for new regional endpoints (#1227)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebrehault authored Dec 22, 2023
1 parent eb5afcf commit 0874a68
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion apps/desktop/src/app/account/account.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ul>
<li
*ngFor="let account of accounts | async"
(click)="selectAccount(account.slug)">
(click)="selectAccount(account.slug, account.id)">
{{ account.title }}
<pa-icon
class="account-arrow"
Expand Down
6 changes: 3 additions & 3 deletions apps/desktop/src/app/account/account.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RouterTestingModule } from '@angular/router/testing';
import { SDKService } from '@flaps/core';
import { of } from 'rxjs';
import { SyncService } from '@nuclia/sync';
import { MockModule} from 'ng-mocks';
import { MockModule } from 'ng-mocks';
import { PaIconModule } from '@guillotinaweb/pastanaga-angular';

import { SelectAccountComponent } from './account.component';
Expand All @@ -22,7 +22,7 @@ describe('SelectAccountComponent', () => {
{
provide: SDKService,
useValue: {
nuclia: { db: { getAccounts: () => of([{ slug: 'account1', title: 'Account 1' }]) } },
nuclia: { db: { getAccounts: () => of([{ slug: 'account1', id: '123', title: 'Account 1' }]) } },
},
},
],
Expand All @@ -39,6 +39,6 @@ describe('SelectAccountComponent', () => {
it('should select account', () => {
const element = fixture.debugElement.nativeElement.querySelector('li');
element.click();
expect(sync.selectAccount).toHaveBeenCalledWith('account1');
expect(sync.selectAccount).toHaveBeenCalledWith('account1', '123');
});
});
14 changes: 9 additions & 5 deletions apps/desktop/src/app/account/account.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Router } from '@angular/router';
import { ACCOUNT_KEY, SyncService } from '@nuclia/sync';
import { SyncService } from '@nuclia/sync';
import { SDKService } from '@flaps/core';
import { catchError, of, tap } from 'rxjs';

Expand All @@ -23,15 +23,19 @@ export class SelectAccountComponent {
}),
);

constructor(private sdk: SDKService, private sync: SyncService, private router: Router) {}
constructor(
private sdk: SDKService,
private sync: SyncService,
private router: Router,
) {}

selectAccount(account: string) {
this.sync.selectAccount(account);
selectAccount(account: string, accountId: string) {
this.sync.selectAccount(account, accountId);
this.router.navigate(['/']);
}

logout() {
localStorage.removeItem(ACCOUNT_KEY);
this.sync.cleanUpAccount();
this.sdk.nuclia.auth.logout();
this.router.navigate(['/']);
}
Expand Down
7 changes: 4 additions & 3 deletions apps/desktop/src/app/base-layout/base-layout.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ChangeDetectionStrategy, ChangeDetectorRef, Component } from '@angular/
import { Router } from '@angular/router';
import { SDKService } from '@flaps/core';
import { catchError, filter, of, switchMap, take } from 'rxjs';
import { ACCOUNT_KEY, SyncService } from '@nuclia/sync';
import { SyncService } from '@nuclia/sync';

@Component({
selector: 'nde-base-layout',
Expand All @@ -24,12 +24,13 @@ export class BaseLayoutComponent {
filter((yes) => yes),
take(1),
switchMap(() => {
const accountSlug = this.sync.getAccountSlug();
const accountId = this.sync.getAccountId();
return !accountId
? of(false)
: this.sdk.nuclia.db.getKnowledgeBoxes(accountId).pipe(
: this.sdk.nuclia.db.getKnowledgeBoxes(accountSlug, accountId).pipe(
catchError(() => {
localStorage.removeItem(ACCOUNT_KEY);
this.sync.cleanUpAccount();
this.sdk.nuclia.auth.logout();
this.router.navigate(['/']);
return of(false);
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-core/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export interface IDb {
getAccount(): Observable<Account>;
getAccount(account?: string): Observable<Account>;
getStandaloneKbs(): Observable<IStandaloneKb[]>;
getKnowledgeBoxes(accountSlug: string): Observable<IKnowledgeBoxItem[]>;
getKnowledgeBoxes(accountSlug: string, accountId?: string): Observable<IKnowledgeBoxItem[]>;
getKnowledgeBoxesForZone(accountId: string, zone: string): Observable<IKnowledgeBoxItem[]>;
getKnowledgeBox(): Observable<WritableKnowledgeBox>;
getKnowledgeBox(account: string, knowledgeBoxId: string): Observable<WritableKnowledgeBox>;
Expand Down
2 changes: 1 addition & 1 deletion libs/sync/src/lib/main-layout/main-layout.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class MainLayoutComponent implements OnDestroy {
if (this.config.staticConf.client === 'dashboard') {
// Automatically select current account (In dashboard there's no account selection page like in Desktop app)
this.sdk.currentAccount.pipe(take(1)).subscribe((account) => {
this.sync.selectAccount(account.slug);
this.sync.selectAccount(account.slug, account.id);
});

// Check if it's a redirect from OAuth server
Expand Down
7 changes: 5 additions & 2 deletions libs/sync/src/lib/sync/destinations/nuclia-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Field,
IDestinationConnector,
} from '../models';
import { ACCOUNT_KEY } from '../sync.service';
import { ACCOUNT_ID_KEY, ACCOUNT_KEY } from '../sync.service';

export const NucliaCloudKB: DestinationConnectorDefinition = {
id: 'nucliacloud',
Expand Down Expand Up @@ -59,7 +59,10 @@ class NucliaCloudKBImpl implements IDestinationConnector {
})),
),
)
: this.nuclia.db.getKnowledgeBoxes(localStorage.getItem(ACCOUNT_KEY) || '');
: this.nuclia.db.getKnowledgeBoxes(
localStorage.getItem(ACCOUNT_KEY) || '',
localStorage.getItem(ACCOUNT_ID_KEY) || '',
);
return request.pipe(
map((kbs) => ({
id: 'kb',
Expand Down
4 changes: 2 additions & 2 deletions libs/sync/src/lib/sync/new-sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ export class NewSyncService {
return this.sdk.currentAccount.pipe(
take(1),
switchMap((account) =>
this.sdk.nuclia.db.getKnowledgeBoxes(account.slug).pipe(
this.sdk.nuclia.db.getKnowledgeBoxes(account.slug, account.id).pipe(
map((kbs) => kbs.find((kb) => kb.id === kbId)),
switchMap((kb) => this.sdk.nuclia.db.getKnowledgeBox(account.slug, kb?.slug || '')),
switchMap((kb) => this.sdk.nuclia.db.getKnowledgeBox(account.slug, kb?.id || '')),
),
),
);
Expand Down
26 changes: 20 additions & 6 deletions libs/sync/src/lib/sync/sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { ConfluenceConnector } from './sources/confluence';
import { OAuthConnector } from './sources/oauth';

export const ACCOUNT_KEY = 'NUCLIA_ACCOUNT';
export const ACCOUNT_ID_KEY = 'NUCLIA_ID_ACCOUNT';
export const LOCAL_SYNC_SERVER = 'http://localhost:5001';
export const SYNC_SERVER_KEY = 'NUCLIA_SYNC_SERVER';

Expand Down Expand Up @@ -284,12 +285,17 @@ export class SyncService {
return source && !(source.connectorId === 'sitemap' || (source.connectorId === 'folder' && source.permanentSync));
}

getAccountId(): string {
getAccountSlug(): string {
return localStorage.getItem(ACCOUNT_KEY) || '';
}

selectAccount(account: string) {
getAccountId(): string {
return localStorage.getItem(ACCOUNT_ID_KEY) || '';
}

selectAccount(account: string, accountId: string) {
localStorage.setItem(ACCOUNT_KEY, account);
localStorage.setItem(ACCOUNT_ID_KEY, accountId);
this.setAccount();
}

Expand All @@ -300,7 +306,10 @@ export class SyncService {
take(1),
switchMap(() =>
this.sdk.nuclia.db.getAccount(this.getAccountId()).pipe(
tap((account) => (this.sdk.nuclia.options.accountType = account.type)),
tap((account) => {
this.sdk.nuclia.options.accountType = account.type;
this.sdk.nuclia.options.account = account.slug;
}),
switchMap((account) => this.sdk.nuclia.rest.getZoneSlug(account.zone)),
tap((zone) => (this.sdk.nuclia.options.zone = zone)),
),
Expand Down Expand Up @@ -341,9 +350,9 @@ export class SyncService {
return this.sdk.currentAccount.pipe(
take(1),
switchMap((account) =>
this.sdk.nuclia.db.getKnowledgeBoxes(account.slug).pipe(
this.sdk.nuclia.db.getKnowledgeBoxes(account.slug, account.id).pipe(
map((kbs) => kbs.find((kb) => kb.id === kbId)),
switchMap((kb) => this.sdk.nuclia.db.getKnowledgeBox(account.slug, kb?.slug || '')),
switchMap((kb) => this.sdk.nuclia.db.getKnowledgeBox(account.slug, kb?.id || '')),
),
),
);
Expand Down Expand Up @@ -448,8 +457,13 @@ export class SyncService {
return this.http.delete<void>(`${this._syncServer.getValue()}/logs`);
}

logout() {
cleanUpAccount() {
localStorage.removeItem(ACCOUNT_KEY);
localStorage.removeItem(ACCOUNT_ID_KEY);
}

logout() {
this.cleanUpAccount();
this.sdk.nuclia.auth.logout();
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nuclia",
"version": "2.7.6",
"version": "2.7.7",
"license": "MIT",
"author": "Nuclia.cloud",
"description": "Nuclia frontend apps and libs",
Expand Down

0 comments on commit 0874a68

Please sign in to comment.