Skip to content

Commit

Permalink
Crud the dashboards and their previews by id
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedHamouGisaia committed Jan 16, 2024
1 parent 6f5f012 commit 25a7a41
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 102 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"angular-oauth2-oidc-jwks": "~14.0.1",
"arlas-iam-api": "24.1.0-rc.0",
"arlas-permissions-api": "~24.0.4",
"arlas-persistence-api": "~24.0.4",
"arlas-persistence-api": "24.0.5-SNAPSHOT-1",
"arlas-tagger-api": "~24.1.0",
"arlas-web-components": "~24.3.2",
"arlas-web-contributors": "~24.3.0",
Expand Down
2 changes: 1 addition & 1 deletion projects/arlas-toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"angular-oauth2-oidc-jwks": "~14.0.1",
"arlas-iam-api": "24.1.0-rc.0",
"arlas-permissions-api": "~24.0.4",
"arlas-persistence-api": "~24.0.4",
"arlas-persistence-api": "24.0.5-SNAPSHOT-1",
"arlas-tagger-api": "~24.1.0",
"arlas-web-components": "~24.3.2",
"arlas-web-contributors": "~24.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import { Component, Inject } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { PersistenceService } from '../../../services/persistence/persistence.service';
import { ConfigAction, ConfigActionEnum } from '../../../tools/utils';
import { Config, ConfigAction, ConfigActionEnum } from '../../../tools/utils';
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
import { ArlasConfigService } from '../../../services/startup/startup.service';
import { DataWithLinks, Exists } from 'arlas-persistence-api';
import { Observable, catchError, map, of } from 'rxjs';

@Component({
selector: 'arlas-action-modal',
Expand All @@ -38,81 +41,58 @@ export class ActionModalComponent {
public constructor(
@Inject(MAT_DIALOG_DATA) data: ConfigAction,
private dialogRef: MatDialogRef<ActionModalComponent>,
private persistenceService: PersistenceService
private persistenceService: PersistenceService,
private configurationService: ArlasConfigService
) {
this.action = data;
}

public duplicate(newName: string, configId: string) {
this.persistenceService.duplicate('config.json', configId, newName)
.subscribe({
next: () => {
this.duplicatePreview(configId, newName);
this.errorMessage = '';
this.dialogRef.close();
},
error: (error) => this.raiseError(error)
});
public duplicate(newName: string, config: Config) {
const arlasConfig = this.configurationService.parse(config.value);
if (!!arlasConfig) {
const previewId = this.configurationService.getPreview(arlasConfig);
if (previewId) {
this.duplicatePreviewThenConfig$(previewId, arlasConfig, config.name, newName).subscribe();
} else {
this.duplicateConfig$(config.id, newName).subscribe();
}
} else {
/** */ console.error('Error duplicating the config: the config is not valid');
}
}


private duplicatePreview(configId: string, newConfigName: string): void {
this.persistenceService.get(configId).subscribe({
next: (currentConfig) => {
const previewName = currentConfig.doc_key.concat('_preview');
const newPreviewName = (!!newConfigName ? newConfigName : 'Copy of ' + currentConfig.doc_key).concat('_preview');
this.persistenceService.existByZoneKey('preview', previewName).subscribe(
exist => {
if (exist.exists) {
this.persistenceService.getByZoneKey('preview', previewName).subscribe({
next: (data) => {
let previewReaders = [];
let previewWriters = [];
if (currentConfig.doc_readers) {
previewReaders = currentConfig.doc_readers.map(reader => reader.replace('config.json', 'preview'));
}
if (currentConfig.doc_readers) {
previewWriters = currentConfig.doc_writers.map(writer => writer.replace('config.json', 'preview'));
}
this.persistenceService.create(
'preview',
newPreviewName,
data.doc_value,
previewReaders,
previewWriters
).subscribe({
error: (error) => this.raiseError(error)
});
}
});
}
});
},
error: (error) => this.raiseError(error)

});
private duplicateConfig$(configId: string, newConfigName: string) {
return this.persistenceService.duplicate('config.json', configId, newConfigName)
.pipe(
catchError(() => /** todo */ of()));

}

private duplicatePreviewThenConfig$(previewId: string, arlasConfig: any, oldConfigName: string, newConfigName: string) {
const newArlasConfig = this.configurationService.updatePreview(arlasConfig, previewId);
const stringifiedNewArlasConfig = JSON.stringify(newArlasConfig);
return this.duplicatePreview$(previewId, newConfigName)
.pipe(
map((p: DataWithLinks) => this.persistenceService.duplicateValue('config.json', stringifiedNewArlasConfig, oldConfigName, newArlasConfig))
).pipe(
catchError(() => /** todo */ of()));

}

private duplicatePreview$(previewId: string, newConfigName: string): Observable<any> {
const newPreviewName = newConfigName.concat('_preview');
return this.persistenceService.get(previewId)
.pipe(map((p: DataWithLinks) => this.persistenceService.create('preview', newPreviewName, p.doc_value)))
.pipe(catchError(() => /** todo*/ of()));
}

public rename(newName: string, configId: string) {
this.persistenceService.get(configId).subscribe(
currentConfig => {
const key = currentConfig.doc_key;
['i18n', 'tour'].forEach(zone => ['fr', 'en'].forEach(lg => this.renameLinkedData(zone, key, newName, lg)));
// ['i18n', 'tour'].forEach(zone => ['fr', 'en'].forEach(lg => this.renameLinkedData(zone, key, newName, lg)));
this.persistenceService.rename(configId, newName).subscribe({
next: () => {
this.errorMessage = '';
this.dialogRef.close();
let previewReaders = [];
let previewWriters = [];
if (currentConfig.doc_readers) {
previewReaders = currentConfig.doc_readers.map(reader => reader.replace('config.json', 'preview'));
}
if (currentConfig.doc_readers) {
previewWriters = currentConfig.doc_writers.map(writer => writer.replace('config.json', 'preview'));
}
this.persistenceService.updatePreview(newName.concat('_preview'), previewReaders, previewWriters);
},
error: error => this.raiseError(error)
});
});
Expand Down Expand Up @@ -156,17 +136,6 @@ export class ActionModalComponent {
this.errorMessage = marker('An error occurred, please try later');
}
}

private renameLinkedData(zone: string, key: string, newName: string, lg: string) {
this.persistenceService.existByZoneKey(zone, key.concat('_').concat(lg)).subscribe(
exist => {
if (exist.exists) {
this.persistenceService.getByZoneKey(zone, key.concat('_').concat(lg))
.subscribe(i => this.persistenceService.rename(i.id, newName.concat('_').concat(lg)).subscribe(d => { }));
}
}
);
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { Observable } from 'rxjs/internal/Observable';
import { from } from 'rxjs/internal/observable/from';
import { Configuration, PersistApi, DataResource, DataWithLinks, Exists } from 'arlas-persistence-api';
import { ArlasSettingsService } from '../settings/arlas.settings.service';
import { map, mergeMap } from 'rxjs/operators';
import { catchError, map, mergeMap } from 'rxjs/operators';
import { GET_OPTIONS } from '../../tools/utils';
import { of } from 'rxjs';


@Injectable({
Expand Down Expand Up @@ -38,23 +39,27 @@ export class PersistenceService {
}

public create(zone: string, name: string, value: string, readers?: string[], writers?: string[]): Observable<DataWithLinks> {
return from(this.persistenceApi.create(zone, name, value, readers, writers, false, this.options));
return from(this.persistenceApi.create(zone, name, value, readers, writers, false, this.options)).pipe(catchError(() => /** todo*/ of()));;

}
public get(id: string): Observable<DataWithLinks> {
return from(this.persistenceApi.getById(id, false, this.options));
}

public getByZoneKey(zone: string, key: string): Observable<DataWithLinks> {
return from(this.persistenceApi.getByKey(zone, key, false, this.options));
}

public exist(id: string): Observable<Exists> {
public exists(id: string): Observable<Exists> {
return from(this.persistenceApi.existsById(id, false, this.options));
}
public existByZoneKey(zone: string, key: string): Observable<Exists> {
return from(this.persistenceApi.existsByKey(zone, key, false, this.options));
}

// public getByZoneKey(zone: string, key: string): Observable<DataWithLinks> {
// return from(this.persistenceApi.getByKey(zone, key, false, this.options));
// }

// public exist(id: string): Observable<Exists> {
// return from(this.persistenceApi.existsById(id, false, this.options));
// }
// public existByZoneKey(zone: string, key: string): Observable<Exists> {
// return from(this.persistenceApi.existsByKey(zone, key, false, this.options));
// }

public list(zone: string, size: number, page: number, order: string): Observable<DataResource> {
return from(this.persistenceApi.list(zone, size, page, order, false, this.options));
Expand All @@ -72,6 +77,10 @@ export class PersistenceService {
);
}

public duplicateValue(zone: string, value: string, oldName: string, newName?: string): Observable<DataWithLinks> {
return this.create(zone, newName ? newName : 'Copy of ' + oldName, value);
}

public rename(id: string, newName: string): Observable<DataWithLinks> {
return this.get(id).pipe(
map(data => this.update(id, data.doc_value, new Date(data.last_update_date).getTime(), newName, data.doc_readers, data.doc_writers)),
Expand All @@ -88,13 +97,13 @@ export class PersistenceService {
}

/** updates the preview's name, readers and writers */
public updatePreview(previewName: string, readers: string[], writers: string[]) {
this.existByZoneKey('preview', previewName).subscribe(
public updatePreview(previewId: string, readers: string[], writers: string[]) {
this.exists(previewId).subscribe(
exist => {
if (exist.exists) {
this.getByZoneKey('preview', previewName).subscribe({
this.get(previewId).subscribe({
next: (data) => {
this.update(data.id, data.doc_value, new Date(data.last_update_date).getTime(), previewName,
this.update(data.id, data.doc_value, new Date(data.last_update_date).getTime(), data.doc_key,
readers, writers);
}
});
Expand All @@ -103,15 +112,13 @@ export class PersistenceService {
}

/** deletes the preview by its name */
public deletePreview(previewName: string) {
this.existByZoneKey('preview', previewName).subscribe(
public deletePreview(previewId: string) {
this.exists(previewId).subscribe(
exist => {
if (exist.exists) {
this.getByZoneKey('preview', previewName).subscribe({
next: (data) => {
this.delete(data.id);
}
});
this.delete(previewId).pipe(
catchError(() => /** todo */ of())
).subscribe();
}
});
}
Expand Down
23 changes: 23 additions & 0 deletions projects/arlas-toolkit/src/lib/services/startup/startup.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,32 @@ import { DashboardError } from '../../tools/errors/dashboard-error';
export class ArlasConfigService extends ConfigService {
public errorsQueue = new Array<Error>();
public appName = '';

public constructor() {
super();
}

/** Returns the preview identifier stored in the config.json */
public getPreviewId(): string | undefined {
return this.getValue('resources.previewId');
}


public parse(stringifiedConfig: string) {
return JSON.parse(stringifiedConfig);
}

public getPreview(config: any) {
return config?.resources?.previewId;
}

public updatePreview(config: any, previewId: string) {
if (!config.resources) {
config.resources = {};
}
config.resources.previewId = previewId;
return config;
}
}

@Injectable()
Expand Down

0 comments on commit 25a7a41

Please sign in to comment.