mirror of
https://dev.azure.com/hugendubel/ISA/_git/ISA-Frontend
synced 2025-12-31 09:37:15 +01:00
feat: enhance canActivate guard to use zod for process ID validation
This commit is contained in:
@@ -3,6 +3,7 @@ import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
|
|||||||
import { ApplicationService } from '@core/application';
|
import { ApplicationService } from '@core/application';
|
||||||
import { Config } from '@core/config';
|
import { Config } from '@core/config';
|
||||||
import { first } from 'rxjs/operators';
|
import { first } from 'rxjs/operators';
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
@Injectable({ providedIn: 'root' })
|
@Injectable({ providedIn: 'root' })
|
||||||
export class CanActivateGoodsInGuard {
|
export class CanActivateGoodsInGuard {
|
||||||
@@ -12,10 +13,8 @@ export class CanActivateGoodsInGuard {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
|
||||||
const process = await this._applicationService
|
const pid = this._config.get('process.ids.goodsIn', z.number());
|
||||||
.getProcessById$(this._config.get('process.ids.goodsIn'))
|
const process = await this._applicationService.getProcessById$(pid).pipe(first()).toPromise();
|
||||||
.pipe(first())
|
|
||||||
.toPromise();
|
|
||||||
if (!process) {
|
if (!process) {
|
||||||
await this._applicationService.createProcess({
|
await this._applicationService.createProcess({
|
||||||
id: this._config.get('process.ids.goodsIn'),
|
id: this._config.get('process.ids.goodsIn'),
|
||||||
|
|||||||
@@ -20,14 +20,18 @@ export class Config {
|
|||||||
get<TOut>(path: string | string[], zSchema?: z.ZodSchema<TOut>): TOut | any {
|
get<TOut>(path: string | string[], zSchema?: z.ZodSchema<TOut>): TOut | any {
|
||||||
let result: JsonValue = this.#config;
|
let result: JsonValue = this.#config;
|
||||||
|
|
||||||
|
if (typeof path === 'string') {
|
||||||
|
path = path.split('.');
|
||||||
|
}
|
||||||
|
|
||||||
for (const p of coerceArray(path)) {
|
for (const p of coerceArray(path)) {
|
||||||
if (typeof result === 'object' && result !== null && !Array.isArray(result)) {
|
if (typeof result === 'object' && result !== null && !Array.isArray(result)) {
|
||||||
result = (result as Record<string, JsonPrimitive>)[p];
|
result = (result as Record<string, JsonPrimitive>)[p];
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
break;
|
||||||
}
|
}
|
||||||
if (result === null || result === undefined) {
|
if (result === null || result === undefined) {
|
||||||
return undefined;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user