Skip to content

Commit

Permalink
Feat: add options param in persistence function
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbet committed Jan 23, 2024
1 parent 82d3b6d commit d0976cd
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class ActionModalComponent {
map((p: DataWithLinks) => {
const newArlasConfig = this.configurationService.updatePreview(arlasConfig, p.id);
const stringifiedNewArlasConfig = JSON.stringify(newArlasConfig);
this.persistenceService.duplicateValue('config.json', stringifiedNewArlasConfig, oldConfigName, newArlasConfig);
this.persistenceService.duplicateValue('config.json', stringifiedNewArlasConfig, oldConfigName, newArlasConfig,
this.getOptionsSetOrg(p.doc_organization, this.persistenceService.options));
})
).pipe(
catchError(() => /** todo */ of()));
Expand All @@ -84,18 +85,21 @@ export class ActionModalComponent {

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)))
return this.persistenceService.get(previewId, this.getOptionsWithoutOrg())
.pipe(map((p: DataWithLinks) => {
this.persistenceService.create('preview', newPreviewName, p.doc_value, [], [],
this.getOptionsSetOrg(p.doc_organization, this.persistenceService.options));
}))
.pipe(catchError(() => /** todo*/ of()));
}

public rename(newName: string, configId: string) {
this.persistenceService.get(configId).subscribe(
this.persistenceService.get(configId, this.getOptionsWithoutOrg()).subscribe(
currentConfig => {
const key = currentConfig.doc_key;
// NO NEED TO RENAME IT
// ['i18n', 'tour'].forEach(zone => ['fr', 'en'].forEach(lg => this.renameLinkedData(zone, key, newName, lg)));
this.persistenceService.rename(configId, newName).subscribe({
this.persistenceService.rename(configId, newName, this.getOptionsWithoutOrg()).subscribe({
error: error => this.raiseError(error)
});
});
Expand Down Expand Up @@ -139,6 +143,26 @@ export class ActionModalComponent {
this.errorMessage = marker('An error occurred, please try later');
}
}

private getOptionsWithoutOrg(): any {
const options = Object.assign({}, this.persistenceService.options);
// No need to have arlas-org-filer headers to delete or get by id
if (!!options && !!options['headers']) {
if (!!options['headers']['arlas-org-filter']) {
delete options['headers']['arlas-org-filter'];
}
}
return options;
}

private getOptionsSetOrg(options: any, org: string) {
const newOptions = Object.assign({}, options);
// No need to have arlas-org-filer headers to delete or get by id
if (!!newOptions && !!newOptions['headers']) {
newOptions['headers']['arlas-org-filter'] = org;;
}
return newOptions;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class ConfigMenuComponent implements OnInit {
if (action.url && action.config && action.config.id && action.configIdParam) {

let url = action.url.concat('/?' + action.configIdParam + '=').concat(action.config.id);
if (!!action.config.org) {
if (!!action.config.org && action.config.org!== 'no_organisation' ) {
url = url.concat('&org=' + action.config.org);
}
this.openUrl(url);
Expand All @@ -74,16 +74,23 @@ export class ConfigMenuComponent implements OnInit {
}
case ConfigActionEnum.DELETE: {
// Open a confirm modal to validate this choice. Available only if updatable is true for this object
const options = Object.assign({}, this.persistenceService.options);
// No need to have arlas-org-filer headers to delete or get by id
if(!!options && !!options['headers']){
if(!!options['headers']['arlas-org-filter']){
delete options['headers']['arlas-org-filter'];
}
}
this.getDialogRef(action).subscribe(id => {
this.persistenceService.get(id).pipe(
this.persistenceService.get(id,options).pipe(
map((p: DataWithLinks) => {
// TODO : DELETE I18N resources
// const key = p.doc_key;
// ['i18n', 'tour'].forEach(zone => ['fr', 'en'].forEach(lg => this.deleteLinkedData(zone, key, lg)));
const parsedConfig = this.configService.parse(p.doc_value);
const previewId = this.configService.getPreview(parsedConfig);
this.persistenceService.deletePreview(previewId);
this.persistenceService.delete(id).subscribe(() => this.actionExecutedEmitter.next(action));
this.persistenceService.deletePreview(previewId,options);
this.persistenceService.delete(id,options).subscribe(() => this.actionExecutedEmitter.next(action));
})
)
.subscribe();
Expand All @@ -94,7 +101,7 @@ export class ConfigMenuComponent implements OnInit {
// redirect to arlas wui-builer with config ID
if (action.url && action.config && action.config.id) {
let url = action.url.concat(action.config.id);
if (!!action.config.org) {
if (!!action.config.org && action.config.org!== 'no_organisation' ) {
url = url.concat('?org=' + action.config.org);
}
this.openUrl(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { of } from 'rxjs';
export class PersistenceService {

private persistenceApi: PersistApi;
private options;
public options;
public isAvailable = false;
public constructor(
@Inject(GET_OPTIONS) private getOptions,
Expand All @@ -34,77 +34,69 @@ export class PersistenceService {
}
}

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

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

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

public exists(id: string): Observable<Exists> {
return from(this.persistenceApi.existsById(id, false, this.options));
public exists(id: string, options = this.options): Observable<Exists> {
return from(this.persistenceApi.existsById(id, false, 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));
public list(zone: string, size: number, page: number, order: string, options = this.options): Observable<DataResource> {
return from(this.persistenceApi.list(zone, size, page, order, false, options));

}
public update(id: string, value: string, lastUpdate: number, name?: string,
readers?: string[], writers?: string[]): Observable<DataWithLinks> {
return from(this.persistenceApi.update(id, value, lastUpdate, name, readers, writers, false, this.options));
readers?: string[], writers?: string[], options = this.options): Observable<DataWithLinks> {
return from(this.persistenceApi.update(id, value, lastUpdate, name, readers, writers, false, this.getOptionsWithoutOrg(options)));
}

public duplicate(zone: string, id: string, newName?: string): Observable<DataWithLinks> {
return this.get(id).pipe(
map(data => this.create(zone, newName ? newName : 'Copy of ' + data.doc_key, data.doc_value)),
public duplicate(zone: string, id: string, newName?: string, options = this.options): Observable<DataWithLinks> {
return this.get(id, this.getOptionsWithoutOrg(options)).pipe(
map(data => this.create(zone, newName ? newName : 'Copy of ' + data.doc_key, data.doc_value, [], [],
this.getOptionsSetOrg(options, data.doc_organization))),
mergeMap(a => a)
);
}

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

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)),
public rename(id: string, newName: string, options = this.options): Observable<DataWithLinks> {
return this.get(id, options).pipe(
map(data => this.update(id, data.doc_value, new Date(data.last_update_date).getTime(),
newName, data.doc_readers, data.doc_writers, options)),
mergeMap(a => a)
);
}

public getGroupsByZone(zone: string) {
return from(this.persistenceApi.getGroupsByZone(zone, false, this.options));
public getGroupsByZone(zone: string, options = this.options) {
return from(this.persistenceApi.getGroupsByZone(zone, false, options));
}

public setOptions(options): void {
this.options = options;
}

/** updates the preview's name, readers and writers */
public updatePreview(previewId: string, readers: string[], writers: string[], newValue?: string) {
this.exists(previewId).subscribe(
public updatePreview(previewId: string, readers: string[], writers: string[], newValue?: string, options = this.options) {
this.exists(previewId, options).subscribe(
exist => {
if (exist.exists) {
this.get(previewId).subscribe({
this.get(previewId, options).subscribe({
next: (data) => {
this.update(data.id, newValue ? newValue : data.doc_value, new Date(data.last_update_date).getTime(), data.doc_key,
readers, writers);
readers, writers, options);
}
});
}
Expand Down Expand Up @@ -134,17 +126,37 @@ export class PersistenceService {
}

/** deletes the preview by its name */
public deletePreview(previewId: string) {
this.exists(previewId).subscribe(
public deletePreview(previewId: string, options = this.options) {
this.exists(previewId, options).subscribe(
exist => {
if (exist.exists) {
this.delete(previewId).pipe(
this.delete(previewId, options).pipe(
catchError(() => /** todo */ of())
).subscribe();
}
});
}

private getOptionsWithoutOrg(options: any) {
const newOptions = Object.assign({}, options);
// No need to have arlas-org-filer headers to delete or get by id
if (!!newOptions && !!newOptions['headers']) {
if (!!newOptions['headers']['arlas-org-filter']) {
delete newOptions['headers']['arlas-org-filter'];
}
}
return newOptions;
}

private getOptionsSetOrg(options: any, org: string) {
const newOptions = Object.assign({}, options);
// No need to have arlas-org-filer headers to delete or get by id
if (!!newOptions && !!newOptions['headers']) {
newOptions['headers']['arlas-org-filter'] = org;;
}
return newOptions;
}

}

export interface PersistenceSetting {
Expand Down

0 comments on commit d0976cd

Please sign in to comment.