committed by
GitHub
19 changed files with 358 additions and 57 deletions
@ -0,0 +1,90 @@ |
|||
///
|
|||
/// Copyright © 2016-2024 The Thingsboard Authors
|
|||
///
|
|||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
/// you may not use this file except in compliance with the License.
|
|||
/// You may obtain a copy of the License at
|
|||
///
|
|||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|||
///
|
|||
/// Unless required by applicable law or agreed to in writing, software
|
|||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
/// See the License for the specific language governing permissions and
|
|||
/// limitations under the License.
|
|||
///
|
|||
|
|||
import { Injectable } from '@angular/core'; |
|||
import { |
|||
HttpErrorResponse, |
|||
HttpEvent, |
|||
HttpHandler, |
|||
HttpInterceptor, |
|||
HttpRequest, |
|||
HttpStatusCode |
|||
} from '@angular/common/http'; |
|||
import { Observable, of, throwError } from 'rxjs'; |
|||
import { catchError, switchMap } from 'rxjs/operators'; |
|||
import { MatDialog } from '@angular/material/dialog'; |
|||
import { |
|||
EntityConflictDialogComponent |
|||
} from '@shared/components/dialog/entity-conflict-dialog/entity-conflict-dialog.component'; |
|||
import { HasId } from '@shared/models/base-data'; |
|||
import { HasVersion } from '@shared/models/entity.models'; |
|||
import { getInterceptorConfig } from './interceptor.util'; |
|||
|
|||
@Injectable() |
|||
export class EntityConflictInterceptor implements HttpInterceptor { |
|||
|
|||
constructor( |
|||
private dialog: MatDialog, |
|||
) {} |
|||
|
|||
intercept(request: HttpRequest<unknown & HasId & HasVersion>, next: HttpHandler): Observable<HttpEvent<unknown>> { |
|||
if (!request.url.startsWith('/api/')) { |
|||
return next.handle(request); |
|||
} |
|||
|
|||
return next.handle(request).pipe( |
|||
catchError((error: HttpErrorResponse) => { |
|||
if (error.status !== HttpStatusCode.Conflict) { |
|||
return throwError(() => error); |
|||
} |
|||
|
|||
return this.handleConflictError(request, next, error); |
|||
}) |
|||
); |
|||
} |
|||
|
|||
private handleConflictError( |
|||
request: HttpRequest<unknown & HasId & HasVersion>, |
|||
next: HttpHandler, |
|||
error: HttpErrorResponse |
|||
): Observable<HttpEvent<unknown>> { |
|||
if (getInterceptorConfig(request).ignoreVersionConflict) { |
|||
return throwError(() => error); |
|||
} |
|||
|
|||
return this.openConflictDialog(request.body, error.error.message).pipe( |
|||
switchMap(result => { |
|||
if (result) { |
|||
return next.handle(this.updateRequestVersion(request)); |
|||
} |
|||
return of(null); |
|||
}) |
|||
); |
|||
} |
|||
|
|||
private updateRequestVersion(request: HttpRequest<unknown & HasId & HasVersion>): HttpRequest<unknown & HasId & HasVersion> { |
|||
const body = { ...request.body, version: null }; |
|||
return request.clone({ body }); |
|||
} |
|||
|
|||
private openConflictDialog(entity: unknown & HasId & HasVersion, message: string): Observable<boolean> { |
|||
const dialogRef = this.dialog.open(EntityConflictDialogComponent, { |
|||
data: { message, entity } |
|||
}); |
|||
|
|||
return dialogRef.afterClosed(); |
|||
} |
|||
} |
|||
@ -0,0 +1,46 @@ |
|||
///
|
|||
/// Copyright © 2016-2024 The Thingsboard Authors
|
|||
///
|
|||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
/// you may not use this file except in compliance with the License.
|
|||
/// You may obtain a copy of the License at
|
|||
///
|
|||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|||
///
|
|||
/// Unless required by applicable law or agreed to in writing, software
|
|||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
/// See the License for the specific language governing permissions and
|
|||
/// limitations under the License.
|
|||
///
|
|||
|
|||
import { HttpRequest } from '@angular/common/http'; |
|||
import { InterceptorConfig } from '@core/interceptors/interceptor-config'; |
|||
import { InterceptorHttpParams } from '@core/interceptors/interceptor-http-params'; |
|||
|
|||
const internalUrlPrefixes = [ |
|||
'/api/auth/token', |
|||
'/api/rpc' |
|||
]; |
|||
|
|||
export const getInterceptorConfig = (req: HttpRequest<unknown>): InterceptorConfig => { |
|||
let config: InterceptorConfig; |
|||
if (req.params && req.params instanceof InterceptorHttpParams) { |
|||
config = (req.params as InterceptorHttpParams).interceptorConfig; |
|||
} else { |
|||
config = new InterceptorConfig(); |
|||
} |
|||
if (isInternalUrlPrefix(req.url)) { |
|||
config.ignoreLoading = true; |
|||
} |
|||
return config; |
|||
}; |
|||
|
|||
const isInternalUrlPrefix = (url: string): boolean => { |
|||
for (const prefix of internalUrlPrefixes) { |
|||
if (url.startsWith(prefix)) { |
|||
return true; |
|||
} |
|||
} |
|||
return false; |
|||
}; |
|||
@ -0,0 +1,53 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2024 The Thingsboard Authors |
|||
|
|||
Licensed under the Apache License, Version 2.0 (the "License"); |
|||
you may not use this file except in compliance with the License. |
|||
You may obtain a copy of the License at |
|||
|
|||
http://www.apache.org/licenses/LICENSE-2.0 |
|||
|
|||
Unless required by applicable law or agreed to in writing, software |
|||
distributed under the License is distributed on an "AS IS" BASIS, |
|||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
See the License for the specific language governing permissions and |
|||
limitations under the License. |
|||
|
|||
--> |
|||
<mat-toolbar color="primary"> |
|||
<h2 class="main-label">{{ 'entity.version-conflict.label' | translate }}</h2> |
|||
<span fxFlex></span> |
|||
<button mat-icon-button |
|||
(click)="onCancel()" |
|||
type="button"> |
|||
<mat-icon class="material-icons">close</mat-icon> |
|||
</button> |
|||
</mat-toolbar> |
|||
<div mat-dialog-content> |
|||
<div class="message-container"> |
|||
<span>{{ data.message }}.</span> |
|||
<span> |
|||
{{ 'entity.version-conflict.link' | translate: |
|||
{ entityType: (entityTypeTranslations.get(data.entity.id.entityType).type | translate) } |
|||
}} |
|||
<a class="cursor-pointer" (click)="onLinkClick($event)">{{ 'entity.link' | translate }}</a>. |
|||
</span> |
|||
<span>{{ 'entity.version-conflict.message' | translate }}</span> |
|||
</div> |
|||
</div> |
|||
<div mat-dialog-actions fxLayout="row" fxLayoutAlign="end center"> |
|||
<button mat-button color="primary" |
|||
type="button" |
|||
(click)="onCancel()" |
|||
cdkFocusInitial |
|||
> |
|||
{{ 'entity.version-conflict.cancel' | translate }} |
|||
</button> |
|||
<button mat-raised-button color="primary" |
|||
type="submit" |
|||
(click)="onConfirm()" |
|||
> |
|||
{{ 'entity.version-conflict.overwrite' | translate }} |
|||
</button> |
|||
</div> |
|||
@ -0,0 +1,26 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0 |
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
$conflict-dialog-width: 700px; |
|||
|
|||
:host { |
|||
.main-label { |
|||
padding-left: 8px; |
|||
} |
|||
|
|||
.message-container { |
|||
max-width: #{$conflict-dialog-width}; |
|||
} |
|||
} |
|||
@ -0,0 +1,61 @@ |
|||
///
|
|||
/// Copyright © 2016-2024 The Thingsboard Authors
|
|||
///
|
|||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
/// you may not use this file except in compliance with the License.
|
|||
/// You may obtain a copy of the License at
|
|||
///
|
|||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|||
///
|
|||
/// Unless required by applicable law or agreed to in writing, software
|
|||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
/// See the License for the specific language governing permissions and
|
|||
/// limitations under the License.
|
|||
///
|
|||
|
|||
import { Component, Inject } from '@angular/core'; |
|||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; |
|||
import { SharedModule } from '@shared/shared.module'; |
|||
import { ImportExportService } from '@shared/import-export/import-export.service'; |
|||
import { CommonModule } from '@angular/common'; |
|||
import { entityTypeTranslations } from '@shared/models/entity-type.models'; |
|||
import { EntityInfoData } from '@shared/models/entity.models'; |
|||
|
|||
interface EntityConflictDialogData { |
|||
message: string; |
|||
entity: EntityInfoData; |
|||
} |
|||
|
|||
@Component({ |
|||
selector: 'tb-entity-conflict-dialog', |
|||
templateUrl: 'entity-conflict-dialog.component.html', |
|||
styleUrls: ['./entity-conflict-dialog.component.scss'], |
|||
standalone: true, |
|||
imports: [ |
|||
CommonModule, |
|||
SharedModule, |
|||
], |
|||
}) |
|||
export class EntityConflictDialogComponent { |
|||
readonly entityTypeTranslations = entityTypeTranslations; |
|||
|
|||
constructor( |
|||
@Inject(MAT_DIALOG_DATA) public data: EntityConflictDialogData, |
|||
private dialogRef: MatDialogRef<EntityConflictDialogComponent>, |
|||
private importExportService: ImportExportService, |
|||
) {} |
|||
|
|||
onCancel(): void { |
|||
this.dialogRef.close(false); |
|||
} |
|||
|
|||
onConfirm(): void { |
|||
this.dialogRef.close(true); |
|||
} |
|||
|
|||
onLinkClick(event: MouseEvent): void { |
|||
event.preventDefault(); |
|||
this.importExportService.exportEntity(this.data.entity); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue