findMatchedRules.tsx 737 B

123456789101112131415161718192021222324252627
  1. import {Actor} from 'sentry/types';
  2. // TODO(ts): add the correct type
  3. export type Rules = Array<any> | null;
  4. /**
  5. * Given a list of rule objects returned from the API, locate the matching
  6. * rules for a specific owner.
  7. */
  8. function findMatchedRules(rules: Rules, owner: Actor) {
  9. if (!rules) {
  10. return undefined;
  11. }
  12. const matchOwner = (actorType: Actor['type'], key: string) =>
  13. (actorType === 'user' && key === owner.email) ||
  14. (actorType === 'team' && key === owner.name);
  15. const actorHasOwner = ([actorType, key]) =>
  16. actorType === owner.type && matchOwner(actorType, key);
  17. return rules
  18. .filter(([_, ruleActors]) => ruleActors.find(actorHasOwner))
  19. .map(([rule]) => rule);
  20. }
  21. export {findMatchedRules};