Skip to content
Snippets Groups Projects
user.util.ts 1.37 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { Pipe, PipeTransform } from '@angular/core';
    import UserRepresentation from '@keycloak/keycloak-admin-client/lib/defs/userRepresentation';
    
    import { OrganisationseinheitError, OrganisationseinheitErrorType } from './user.model';
    
    
    export const KEYCLOAK_ERROR_MESSAGES: { [type: string]: string } = {
    
      [OrganisationseinheitErrorType.NAME_CONFLICT]: 'Der Name exisitert bereits.',
      [OrganisationseinheitErrorType.NAME_MISSING]: 'Bitte den Namen angeben.',
    
      [OrganisationseinheitErrorType.ID_MISSING]:
        'Bitte mindestens eine Organisationseinheit ID angeben.',
    
    export const KEYCLOAK_CREATE_GROUPS_ERROR_STATUS: {
      [status: number]: OrganisationseinheitErrorType;
    } = {
      409: OrganisationseinheitErrorType.NAME_CONFLICT,
      400: OrganisationseinheitErrorType.NAME_MISSING,
    
    export function getOrganisationseinheitErrorMessage(error: OrganisationseinheitError): string {
    
      return KEYCLOAK_ERROR_MESSAGES[error.errorType] ?? '';
    
    
    @Pipe({
      name: 'userName',
      standalone: true,
    })
    export class UserNamePipe implements PipeTransform {
      transform(user: UserRepresentation): string {
        if (!user.firstName || !user.lastName) return 'Unbekannter Benutzer';
        return `${user.firstName} ${user.lastName}`;
      }
    }
    
    OZGCloud's avatar
    OZGCloud committed
    
    export function sortUsersByLastName(users: UserRepresentation[]): UserRepresentation[] {
      return users.sort((a, b) => (a.lastName ?? '').localeCompare(b.lastName ?? ''));
    }