homeBranchTarget.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { isAdminLike as isAdminLikeRole } from "@/lib/frontend/auth/roles";
  2. import { isValidBranchParam } from "@/lib/frontend/params";
  3. export const OVERVIEW_BRANCH_FALLBACK = "NL01";
  4. export const OVERVIEW_BRANCH_SOURCE = Object.freeze({
  5. NONE: "none",
  6. USER: "user",
  7. ROUTE: "route",
  8. STORED: "stored",
  9. FALLBACK: "fallback",
  10. API_FIRST: "api-first",
  11. });
  12. function normalizeBranch(value) {
  13. if (typeof value !== "string") return null;
  14. const trimmed = value.trim();
  15. return isValidBranchParam(trimmed) ? trimmed : null;
  16. }
  17. function normalizeAvailableBranches(value) {
  18. if (!Array.isArray(value)) return null;
  19. const deduped = [];
  20. for (const item of value) {
  21. const normalized = normalizeBranch(item);
  22. if (!normalized) continue;
  23. if (!deduped.includes(normalized)) deduped.push(normalized);
  24. }
  25. return deduped;
  26. }
  27. export function resolveOverviewBranchTarget(input = {}) {
  28. const role = input?.role ?? null;
  29. const routeBranch = normalizeBranch(input?.routeBranch);
  30. const storedBranch = normalizeBranch(input?.storedBranch);
  31. const ownBranch = normalizeBranch(input?.userBranchId);
  32. const fallbackBranch =
  33. normalizeBranch(input?.fallbackBranch) || OVERVIEW_BRANCH_FALLBACK;
  34. const availableBranches = normalizeAvailableBranches(input?.availableBranches);
  35. if (role === "branch") {
  36. return {
  37. branch: ownBranch,
  38. source: ownBranch
  39. ? OVERVIEW_BRANCH_SOURCE.USER
  40. : OVERVIEW_BRANCH_SOURCE.NONE,
  41. shouldFetchBranches: false,
  42. };
  43. }
  44. if (!isAdminLikeRole(role)) {
  45. return {
  46. branch: null,
  47. source: OVERVIEW_BRANCH_SOURCE.NONE,
  48. shouldFetchBranches: false,
  49. };
  50. }
  51. const candidates = [
  52. { source: OVERVIEW_BRANCH_SOURCE.ROUTE, branch: routeBranch },
  53. { source: OVERVIEW_BRANCH_SOURCE.STORED, branch: storedBranch },
  54. { source: OVERVIEW_BRANCH_SOURCE.FALLBACK, branch: fallbackBranch },
  55. ];
  56. if (Array.isArray(availableBranches)) {
  57. if (availableBranches.length === 0) {
  58. return {
  59. branch: null,
  60. source: OVERVIEW_BRANCH_SOURCE.NONE,
  61. shouldFetchBranches: false,
  62. };
  63. }
  64. for (const candidate of candidates) {
  65. if (!candidate.branch) continue;
  66. if (availableBranches.includes(candidate.branch)) {
  67. return {
  68. branch: candidate.branch,
  69. source: candidate.source,
  70. shouldFetchBranches: false,
  71. };
  72. }
  73. }
  74. return {
  75. branch: availableBranches[0],
  76. source: OVERVIEW_BRANCH_SOURCE.API_FIRST,
  77. shouldFetchBranches: false,
  78. };
  79. }
  80. const firstLocal = candidates.find((candidate) => Boolean(candidate.branch));
  81. if (!firstLocal) {
  82. return {
  83. branch: null,
  84. source: OVERVIEW_BRANCH_SOURCE.NONE,
  85. shouldFetchBranches: true,
  86. };
  87. }
  88. return {
  89. branch: firstLocal.branch,
  90. source: firstLocal.source,
  91. shouldFetchBranches:
  92. firstLocal.source === OVERVIEW_BRANCH_SOURCE.FALLBACK,
  93. };
  94. }