Skip to content

1962 test and improve typing of edit tasksedit taskscomponentts#623

Open
correct-horse-battery-bench wants to merge 8 commits intomasterfrom
1962-test-and-improve-typing-of-edit-tasksedit-taskscomponentts
Open

1962 test and improve typing of edit tasksedit taskscomponentts#623
correct-horse-battery-bench wants to merge 8 commits intomasterfrom
1962-test-and-improve-typing-of-edit-tasksedit-taskscomponentts

Conversation

@correct-horse-battery-bench
Copy link
Copy Markdown
Contributor

@correct-horse-battery-bench correct-horse-battery-bench commented Apr 23, 2026

  1. add helper types for ids (some were already existing) as well as type some number, string with typealiases to better communicate what they do for example
    type Seconds = number
    type HashlistId = string
    type HexColor = #${string}` -> using template literal string typescript syntax

  2. Type roles / roleService

The roleService used strings as roles. Define them in const object enums and reference them so that each roleService tests only for the keys that actually exist. An improvement here would be that these are somehow present in the openapi spec so we do not need to define them again. But this is still an improvement as before they were just strings and types arent caught.

  // src/app/core/_services/roles/base/role.service.ts
  export type RoleMapping<K extends string> = Record<K, Array<PermissionValues>>;

  export abstract class RoleService<K extends string> {
    protected constructor(
      private permissionService: PermissionService,
      private readonly roles: RoleMapping<K>
    ) {}

    private hasRoleEntry(key: K): boolean {
      return Array.isArray(this.roles[key]) && this.roles[key].length > 0;
    }

    hasRole(roleName: K): boolean {
      if (!this.hasRoleEntry(roleName)) {
        return false;
      }
      // ... check each permission in this.roles[roleName]
    }
  }


// Usage:
  // src/app/core/_services/roles/agents/agent-role.service.ts
  export const AgentRole = {
    Read: 'read',
    ReadStat: 'readStat',
    ReadAssignment: 'readAssignment',
    Create: 'create',
    Update: 'update'
  } as const;

  // Derived union type: 'read' | 'readStat' | 'readAssignment' | 'create' | 'update'
  export type AgentRole = (typeof AgentRole)[keyof typeof AgentRole];

  @Injectable({ providedIn: 'root' })
  export class AgentRoleService extends RoleService<AgentRole> {
    constructor(permissionService: PermissionService) {
      super(permissionService, {
        [AgentRole.Read]: [Perm.Agent.READ],
        [AgentRole.ReadStat]: [Perm.Agent.READ, Perm.AgentStat.READ],
        [AgentRole.ReadAssignment]: [Perm.AgentAssignment.READ, Perm.Task.READ],
        [AgentRole.Create]: [Perm.Agent.CREATE, Perm.Agent.READ],
        [AgentRole.Update]: [Perm.Agent.UPDATE]
      });
    }
  }


// how to call it



  // before
  if (this.agentRoleService.hasRole('read')) { ... }
  if (this.tasksRoleService.hasRole('create')) { ... }

  // after
  if (this.agentRoleService.hasRole(AgentRole.Read)) { ... }
  if (this.tasksRoleService.hasRole(TaskRole.Create)) { ... }
  1. Remove some dead code found

Issue: hashtopolis/server#1962

@correct-horse-battery-bench correct-horse-battery-bench force-pushed the 1962-test-and-improve-typing-of-edit-tasksedit-taskscomponentts branch from 1787add to 8e8c12d Compare April 23, 2026 08:36
@correct-horse-battery-bench correct-horse-battery-bench marked this pull request as ready for review April 24, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test and improve typing of edit-tasks/edit-tasks.component.ts

1 participant