Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 1.1 KiB · at 8396947

Write escalationEmail(users, userId), which finds who to escalate to for a user by walking up their management chain.

Each user is { id, name, managerId, email }, where managerId and email are optional (absent when the user has no manager or no email). An email that is the empty string counts as no email. If several users share an id, the first one in the list is the one that counts. Ids are arbitrary strings.

The walk:

  1. Find the user whose id is userId. If there is none, fail with { kind: "unknown_user", id: userId }.
  2. Start at that user's manager (the user's own email is never returned). Repeat:
    • If there is no manager id, or it does not match any user, the chain ends: the result is absent.
    • If the manager is a user already reached during this walk (the starting user counts as reached), fail with { kind: "cycle", id } where id is that manager's id.
    • If the manager has an email, return it.
    • Otherwise move on to that manager's manager.

Reaching the top of the chain without finding an email is not an error; the result is simply absent. The users array and its objects must not be modified.