@flusys/nestjs-shared 9.1.0 → 9.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,5 +7,4 @@ export declare class PermissionGuard implements CanActivate {
7
7
  constructor(reflector: Reflector, permissionCacheService?: SharedPermissionCacheService | undefined);
8
8
  canActivate(context: ExecutionContext): Promise<boolean>;
9
9
  private normalizeToLogicNode;
10
- private evaluateLogicNode;
11
10
  }
@@ -0,0 +1,10 @@
1
+ import { type IBranchHierarchyOptions } from '../utils/branch-hierarchy.util.js';
2
+ export declare const BRANCH_HIERARCHY_RESOLVER = "BRANCH_HIERARCHY_RESOLVER";
3
+ export interface IBranchHierarchyResolver {
4
+ getDescendantIds(branchIds: string | string[], options?: IBranchHierarchyOptions): Promise<string[]>;
5
+ getAncestorIds(branchId: string, options?: IBranchHierarchyOptions): Promise<string[]>;
6
+ isWithinBranch(branchId: string, ancestorId: string): Promise<boolean>;
7
+ getGrantedBranchIds(userId: string, companyId?: string): Promise<string[]>;
8
+ getGrantedDescendantIds(userId: string, branchIds: string | string[], options?: IBranchHierarchyOptions): Promise<string[]>;
9
+ hasBranchAccess(userId: string, branchId: string): Promise<boolean>;
10
+ }
@@ -0,0 +1,19 @@
1
+ export declare const COMPANY_ACCESS_RESOLVER = "COMPANY_ACCESS_RESOLVER";
2
+ export interface IAccessibleCompany {
3
+ id: string;
4
+ name: string;
5
+ }
6
+ export interface IAccessibleBranch {
7
+ id: string;
8
+ companyId: string;
9
+ parentId: string | null;
10
+ name: string;
11
+ }
12
+ export interface IUserCompanyAccess {
13
+ companies: IAccessibleCompany[];
14
+ branches: IAccessibleBranch[];
15
+ }
16
+ export interface ICompanyAccessResolver {
17
+ getUserAccess(userId: string): Promise<IUserCompanyAccess>;
18
+ getCompanyMembers(companyId: string, branchIds?: string[]): Promise<string[]>;
19
+ }
@@ -0,0 +1,27 @@
1
+ import { type ILoggedUserInfo } from './logged-user-info.interface.js';
2
+ export declare const EMAIL_ADAPTER = "EMAIL_ADAPTER";
3
+ export interface IEmailAdapterRecipients {
4
+ to: string[];
5
+ cc?: string[];
6
+ bcc?: string[];
7
+ replyTo?: string;
8
+ emailConfigId?: string;
9
+ }
10
+ export interface IEmailAdapterContent extends IEmailAdapterRecipients {
11
+ subject: string;
12
+ html?: string;
13
+ text?: string;
14
+ }
15
+ export interface IEmailAdapterTemplate extends IEmailAdapterRecipients {
16
+ templateSlug: string;
17
+ variables?: Record<string, unknown>;
18
+ }
19
+ export interface IEmailAdapterResult {
20
+ success: boolean;
21
+ messageId?: string;
22
+ error?: string;
23
+ }
24
+ export interface IEmailAdapter {
25
+ send(options: IEmailAdapterContent, user: ILoggedUserInfo | null): Promise<IEmailAdapterResult>;
26
+ sendTemplate(options: IEmailAdapterTemplate, user: ILoggedUserInfo | null): Promise<IEmailAdapterResult>;
27
+ }
@@ -0,0 +1,45 @@
1
+ export declare const EXPRESSION_ARITHMETIC_OPS: readonly ["sum", "subtract", "multiply", "divide", "modulo", "power", "average", "min", "max"];
2
+ export type ExpressionArithmeticOp = (typeof EXPRESSION_ARITHMETIC_OPS)[number];
3
+ export declare const EXPRESSION_COMPARISONS: readonly ["equals", "not_equals", "greater_than", "less_than", "greater_or_equal", "less_or_equal", "contains", "not_contains", "starts_with", "ends_with", "in", "not_in", "is_empty", "is_not_empty", "is_true", "is_false", "matches", "not_matches"];
4
+ export type ExpressionComparison = (typeof EXPRESSION_COMPARISONS)[number];
5
+ export declare const UNARY_EXPRESSION_COMPARISONS: readonly ExpressionComparison[];
6
+ export declare const EXPRESSION_DURATION_UNITS: readonly ["second", "minute", "hour", "day", "week", "month", "year"];
7
+ export type ExpressionDurationUnit = (typeof EXPRESSION_DURATION_UNITS)[number];
8
+ export declare const EXPRESSION_DATE_PARTS: readonly ["year", "month", "day", "weekday", "hour", "minute"];
9
+ export type ExpressionDatePart = (typeof EXPRESSION_DATE_PARTS)[number];
10
+ export interface IExpressionCondition {
11
+ left: Expression;
12
+ comparison: ExpressionComparison;
13
+ right?: Expression;
14
+ }
15
+ export interface IExpressionConditionGroup {
16
+ operator: 'AND' | 'OR';
17
+ conditions: Array<IExpressionCondition | IExpressionConditionGroup>;
18
+ }
19
+ export type Expression = {
20
+ type: 'literal';
21
+ value: string | number | boolean | null;
22
+ } | {
23
+ type: 'field_ref';
24
+ ref: string;
25
+ } | {
26
+ type: 'arithmetic';
27
+ op: ExpressionArithmeticOp;
28
+ operands: Expression[];
29
+ } | {
30
+ type: 'concat';
31
+ parts: Expression[];
32
+ separator?: string;
33
+ } | {
34
+ type: 'template';
35
+ template: string;
36
+ } | {
37
+ type: 'function';
38
+ name: string;
39
+ args: Expression[];
40
+ } | {
41
+ type: 'if';
42
+ condition: IExpressionConditionGroup;
43
+ whenTrue: Expression;
44
+ whenFalse?: Expression;
45
+ };
@@ -1,8 +1,12 @@
1
1
  export * from './ai-assistant-adapter.interface.js';
2
2
  export * from './api.interface.js';
3
+ export * from './branch-hierarchy-resolver.interface.js';
4
+ export * from './company-access-resolver.interface.js';
3
5
  export * from './datasource.interface.js';
6
+ export * from './email-adapter.interface.js';
4
7
  export * from './event-bus.interface.js';
5
8
  export * from './event-manager-adapter.interface.js';
9
+ export * from './expression.interface.js';
6
10
  export * from './identity.interface.js';
7
11
  export * from './logged-user-info.interface.js';
8
12
  export * from './message-ref.interface.js';
@@ -1,5 +1,5 @@
1
1
  import { HybridCache } from '../../classes/hybrid-cache.class.js';
2
- import type { PermissionGuardConfig } from '../../interfaces/index.js';
2
+ import type { ILogicNode, PermissionGuardConfig } from '../../interfaces/index.js';
3
3
  import { type PermissionCacheKeyOptions } from '../../utils/permission-cache-key.util.js';
4
4
  export declare class SharedPermissionCacheService {
5
5
  private readonly cache?;
@@ -14,6 +14,11 @@ export declare class SharedPermissionCacheService {
14
14
  companyId?: string | null;
15
15
  branchId?: string | null;
16
16
  } | null | undefined, requiredPermission: string): Promise<void>;
17
+ assertPermissionLogic(user: {
18
+ id?: string | null;
19
+ companyId?: string | null;
20
+ branchId?: string | null;
21
+ } | null | undefined, logic: ILogicNode): Promise<void>;
17
22
  isCacheConfigured(): boolean;
18
23
  matchPermission(userPermissions: string[], requiredPermission: string): boolean;
19
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flusys/nestjs-shared",
3
- "version": "9.1.0",
3
+ "version": "9.1.2",
4
4
  "description": "Common shared utilities for Flusys NestJS applications",
5
5
  "type": "module",
6
6
  "main": "./fesm/index.js",
@@ -95,7 +95,7 @@
95
95
  "class-transformer": "^0.5.1",
96
96
  "class-validator": "^0.15.0",
97
97
  "keyv": "^5.0.0",
98
- "typeorm": "^0.3.0",
98
+ "typeorm": "^0.3.31",
99
99
  "typeorm-naming-strategies": "^4.1.0",
100
100
  "winston": "^3.0.0",
101
101
  "winston-transport": "^4.9.0",
@@ -104,6 +104,7 @@
104
104
  "rxjs": "^7.8.0"
105
105
  },
106
106
  "dependencies": {
107
- "@flusys/nestjs-core": "9.1.0"
107
+ "@flusys/nestjs-core": "9.1.2",
108
+ "re2js": "^2.8.6"
108
109
  }
109
110
  }
@@ -0,0 +1,15 @@
1
+ export interface IBranchHierarchyNode {
2
+ id: string;
3
+ parentId?: string | null;
4
+ }
5
+ export interface IBranchHierarchyOptions {
6
+ includeSelf?: boolean;
7
+ maxDepth?: number;
8
+ }
9
+ type IdInput = string | null | undefined | ReadonlyArray<string | null | undefined>;
10
+ export declare function normalizeBranchIds(ids: IdInput): string[];
11
+ export declare function collectBranchDescendantIds(nodes: readonly IBranchHierarchyNode[], rootIds: IdInput, { includeSelf, maxDepth }?: IBranchHierarchyOptions): string[];
12
+ export declare function collectBranchAncestorIds(nodes: readonly IBranchHierarchyNode[], branchId: string | null | undefined, { includeSelf, maxDepth }?: IBranchHierarchyOptions): string[];
13
+ export declare function isBranchWithin(nodes: readonly IBranchHierarchyNode[], branchId: string | null | undefined, ancestorId: string | null | undefined): boolean;
14
+ export declare function getBranchPath<T extends IBranchHierarchyNode>(nodes: readonly T[], branchId: string | null | undefined): T[];
15
+ export {};
@@ -0,0 +1,35 @@
1
+ import { type Expression, type IExpressionConditionGroup } from '../interfaces/expression.interface.js';
2
+ export declare const EXPRESSION_LIMITS: {
3
+ readonly MAX_DEPTH: 16;
4
+ readonly MAX_NODES: 500;
5
+ readonly MAX_TEMPLATE_LENGTH: 4000;
6
+ readonly MAX_TEXT_LENGTH: 100000;
7
+ readonly MAX_PAD_LENGTH: 1000;
8
+ };
9
+ export type ExpressionErrorCode = 'invalid_node' | 'unknown_function' | 'wrong_arg_count' | 'unknown_comparison' | 'unknown_ref' | 'too_deep' | 'too_many_nodes' | 'template_too_long' | 'text_too_long' | 'expected_number' | 'expected_date' | 'division_by_zero' | 'invalid_unit' | 'invalid_date_part' | 'regex_too_long' | 'regex_invalid' | 'regex_too_complex' | 'regex_input_too_long';
10
+ export declare class ExpressionError extends Error {
11
+ readonly code: ExpressionErrorCode;
12
+ readonly variables: Record<string, string | number>;
13
+ constructor(code: ExpressionErrorCode, variables?: Record<string, string | number>);
14
+ }
15
+ export declare function isExpressionError(error: unknown): error is ExpressionError;
16
+ export interface IExpressionFunction {
17
+ minArgs: number;
18
+ maxArgs?: number;
19
+ regexArgs?: readonly number[];
20
+ call(args: unknown[], ctx: IExpressionContext): unknown;
21
+ }
22
+ export interface IExpressionContext {
23
+ resolveRef(ref: string): unknown;
24
+ now?: () => Date;
25
+ }
26
+ export interface IExpressionValidationOptions {
27
+ isKnownRef?: (ref: string) => boolean;
28
+ }
29
+ export declare function expressionToText(value: unknown): string | null;
30
+ export declare function expressionToNumber(value: unknown): number | null;
31
+ export declare const EXPRESSION_FUNCTIONS: Readonly<Record<string, IExpressionFunction>>;
32
+ export declare function evaluateExpression(expression: Expression, ctx: IExpressionContext): unknown;
33
+ export declare function evaluateConditionGroup(group: IExpressionConditionGroup, ctx: IExpressionContext): boolean;
34
+ export declare function assertValidExpression(expression: unknown, options?: IExpressionValidationOptions): void;
35
+ export declare function assertValidConditionGroup(group: unknown, options?: IExpressionValidationOptions): void;
package/utils/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
+ export * from './branch-hierarchy.util.js';
1
2
  export * from './cache-tenant.util.js';
2
3
  export * from './date-time.util.js';
3
4
  export * from './event-pattern.util.js';
5
+ export * from './expression.util.js';
4
6
  export * from './html-sanitizer.util.js';
5
7
  export * from './message-ref.util.js';
6
8
  export * from './optional-module.util.js';
@@ -8,5 +10,6 @@ export * from './permission-cache-key.util.js';
8
10
  export * from './permission-match.util.js';
9
11
  export * from './query-helpers.util.js';
10
12
  export * from './request.util.js';
13
+ export * from './safe-regex.util.js';
11
14
  export * from './string.util.js';
12
15
  export * from './token.util.js';
@@ -1 +1,19 @@
1
+ import { type IPermissionResolver, type IPermissionScope } from '../interfaces/permission-resolver.interface.js';
2
+ import { type ILogicNode } from '../interfaces/permission.interface.js';
3
+ import { type SharedPermissionCacheService } from '../modules/shared-permission-cache/shared-permission-cache.service.js';
1
4
  export declare function matchesPermission(granted: readonly string[], required: string): boolean;
5
+ export interface IPermissionLogicResult {
6
+ passed: boolean;
7
+ missingPermissions: string[];
8
+ operator: 'AND' | 'OR';
9
+ }
10
+ export declare function evaluatePermissionLogic(node: ILogicNode, granted: readonly string[]): IPermissionLogicResult;
11
+ export declare function permissionLogicCodes(node: ILogicNode): string[];
12
+ export declare const MAX_PERMISSION_LOGIC_DEPTH = 5;
13
+ export declare const MAX_PERMISSION_LOGIC_CODES = 50;
14
+ export declare function permissionLogicProblem(value: unknown): 'invalid' | 'empty' | 'too_many' | null;
15
+ export interface IPermissionSources {
16
+ resolver?: IPermissionResolver;
17
+ cache?: SharedPermissionCacheService;
18
+ }
19
+ export declare function loadPermissionCodes(scope: IPermissionScope, sources: IPermissionSources): Promise<string[]>;
@@ -6,6 +6,12 @@ export interface ICompanyFilterConfig {
6
6
  columnName?: string;
7
7
  }
8
8
  export declare function applyCompanyFilter<T extends ObjectLiteral>(query: SelectQueryBuilder<T>, config: ICompanyFilterConfig, user: ILoggedUserInfo | null | undefined): SelectQueryBuilder<T>;
9
+ export interface IBranchFilterConfig {
10
+ entityAlias: string;
11
+ columnName?: string;
12
+ includeUnassigned?: boolean;
13
+ }
14
+ export declare function applyBranchFilter<T extends ObjectLiteral>(query: SelectQueryBuilder<T>, config: IBranchFilterConfig, branchIds: ReadonlyArray<string | null | undefined>): SelectQueryBuilder<T>;
9
15
  export declare function buildCompanyWhereCondition<T extends Record<string, unknown>>(baseWhere: T, isCompanyFeatureEnabled: boolean, user: ILoggedUserInfo | null | undefined): T & {
10
16
  companyId?: string;
11
17
  };
@@ -0,0 +1,10 @@
1
+ import { RE2JS } from 're2js';
2
+ export declare const SAFE_REGEX_LIMITS: {
3
+ readonly MAX_PATTERN_LENGTH: 500;
4
+ readonly MAX_INPUT_LENGTH: 10000;
5
+ readonly MAX_REPEAT: 100;
6
+ readonly MAX_REPEAT_EXPANSION: 300;
7
+ };
8
+ export type SafeRegexProblem = 'too_long' | 'invalid' | 'too_complex';
9
+ export declare function safeRegexProblem(pattern: string): SafeRegexProblem | null;
10
+ export declare function compileSafeRegex(pattern: string): RE2JS;