feat: enhance canActivate guard to use zod for process ID validation

This commit is contained in:
Lorenz Hilpert
2025-03-10 10:57:54 +01:00
parent 532c7e5e86
commit 8eb5e09490
2 changed files with 9 additions and 6 deletions

View File

@@ -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'),

View File

@@ -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;
} }
} }