Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
TypeScript Data structure for indexed actions table

I have a series of roles with ascending permissions. I also have series of issue states. I expect both of these to be represented as TypeScript enums.

I wish to provide a series of available actions, for different roles, for issues in different states: enter image description here

I have a couple of related challenges.

  1. I wish to have a data structure where the role/issue-state are indexed by TS enums
  2. I wish to be able to store data in the structure in a legible manner (for easy review by a domain expert), like below
actions[engineer][newReqest] = ["Claim"]
actions[engineer][liveRequest] = ["Submit for review"]
  1. When determining the actions for the current role ("Manager") I should be able to retrieve the inferred actions for the lower roles too ("Engineer" and "Team Lead")

Can anybody point me to the TypeScript object type that best suits the above scenario?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I've attempted a solution in a playground. I supports the domain-expert-legible assignment, but I have some remaining TS errors. It could probably be improved...

enum Roles {
  Engineer,
  TeamLeader,
  Manager
}

enum IssueStates {
  New,
  Live,
  Closed
}

const actions = []
actions[IssueStates.New] = []
actions[IssueStates.Live] = []
actions[IssueStates.Closed] = []
actions[IssueStates.New][Roles.Engineer] = ["Claim"]
actions[IssueStates.Live][Roles.Engineer] = ["Submit"]
actions[IssueStates.Live][Roles.TeamLeader] = ["Accept", "Reject"]
actions[IssueStates.Closed][Roles.Manager] = ["ReOpen"]

const actionsFor = (tblActions: any, state: IssueStates, role: Roles): Array<String> => {
  const forState = tblActions[state]
  const actions = forState.filter((element: any, index: number) => index <= role)
  return actions.flat(2) || []
}

console.log("Team leader for live issue", actionsFor(actions, IssueStates.Live, Roles.TeamLeader))
console.log("Team leader for closed issue", actionsFor(actions, IssueStates.Closed, Roles.TeamLeader))
console.log("Manager for closed issue", actionsFor(actions, IssueStates.Closed, Roles.Manager))

let summary = ""
for(let state in IssueStates) {
  if(!isNaN(+state)) {
    for(let role in Roles) {
      if(!isNaN(+role)) {
        summary += state + ", " + role  + ", " + actionsFor(actions, state, role) + "\n"
      }
    }
  }
}
console.log("Summary",  "\n", summary)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda