systemSetupInfo.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useLocalStorage } from '@vueuse/core'
  3. import { defineStore } from 'pinia'
  4. import { computed } from 'vue'
  5. import {
  6. EnumSystemSetupInfoStatus,
  7. EnumSystemSetupInfoType,
  8. } from '#shared/graphql/types.ts'
  9. import MutationHandler from '#shared/server/apollo/handler/MutationHandler.ts'
  10. import QueryHandler from '#shared/server/apollo/handler/QueryHandler.ts'
  11. import { useApplicationStore } from '#shared/stores/application.ts'
  12. import { useSystemSetupUnlockMutation } from '../graphql/mutations/systemSetupUnlock.api.ts'
  13. import { useSystemSetupInfoLazyQuery } from '../graphql/queries/systemSetupInfo.api.ts'
  14. import type { SystemSetupInfoStorage } from '../types/setup-info.ts'
  15. export const useSystemSetupInfoStore = defineStore('systemSetupInfo', () => {
  16. const systemSetupInfo = useLocalStorage<SystemSetupInfoStorage>(
  17. 'systemSetupInfo',
  18. {},
  19. )
  20. const systemSetupInfoQuery = new QueryHandler(
  21. useSystemSetupInfoLazyQuery({
  22. fetchPolicy: 'network-only',
  23. }),
  24. )
  25. const setSystemSetupInfo = async () => {
  26. const systemSetupInfoResult = await systemSetupInfoQuery.query()
  27. const newSystemSetupInfo = systemSetupInfoResult.data?.systemSetupInfo
  28. if (newSystemSetupInfo) {
  29. systemSetupInfo.value = {
  30. ...systemSetupInfo.value,
  31. type:
  32. systemSetupInfo.value.type === EnumSystemSetupInfoType.Import &&
  33. newSystemSetupInfo.type === EnumSystemSetupInfoType.Manual
  34. ? systemSetupInfo.value.type
  35. : newSystemSetupInfo.type,
  36. status: newSystemSetupInfo.status,
  37. }
  38. }
  39. }
  40. const application = useApplicationStore()
  41. const getImportPath = () => {
  42. const pathPrefix = '/guided-setup/import'
  43. let importBackend = application.config.import_backend
  44. if (application.config.import_mode) {
  45. return `${pathPrefix}/${importBackend}/status`
  46. }
  47. if (systemSetupInfo.value.importSource) {
  48. importBackend = systemSetupInfo.value.importSource
  49. }
  50. const importBackendRoute = importBackend ? `/${importBackend}` : ''
  51. return `${pathPrefix}${importBackendRoute}`
  52. }
  53. const getSystemSetupInfoRedirectPath = (
  54. status?: string,
  55. type?: string,
  56. lockValue?: string,
  57. ) => {
  58. if (!status || status === EnumSystemSetupInfoStatus.New)
  59. return '/guided-setup'
  60. if (status === EnumSystemSetupInfoStatus.Automated) {
  61. return '/guided-setup/automated'
  62. }
  63. if (status === EnumSystemSetupInfoStatus.InProgress) {
  64. if (!type) return '/guided-setup'
  65. if (type === EnumSystemSetupInfoType.Manual) {
  66. if (lockValue && type === 'manual') {
  67. return '/guided-setup/manual'
  68. }
  69. return '/guided-setup'
  70. }
  71. if (type === EnumSystemSetupInfoType.Import) {
  72. return getImportPath()
  73. }
  74. }
  75. return '/guided-setup'
  76. }
  77. const redirectPath = computed(() => {
  78. const { status, type, lockValue } = systemSetupInfo.value
  79. return getSystemSetupInfoRedirectPath(status, type || '', lockValue)
  80. })
  81. const redirectNeeded = (currentRoutePath: string) => {
  82. // Allow sub-paths for auto wizard execution + imports
  83. if (
  84. systemSetupInfo.value.status === EnumSystemSetupInfoStatus.Automated ||
  85. (systemSetupInfo.value.type === EnumSystemSetupInfoType.Import &&
  86. systemSetupInfo.value.importSource)
  87. ) {
  88. return !currentRoutePath.startsWith(redirectPath.value)
  89. }
  90. return currentRoutePath !== redirectPath.value
  91. }
  92. const systemSetupDone = computed(() => {
  93. const { status } = systemSetupInfo.value
  94. return (
  95. status === EnumSystemSetupInfoStatus.Done ||
  96. application.config.system_init_done
  97. )
  98. })
  99. const systemSetupAlreadyStarted = computed(() => {
  100. const { status, lockValue } = systemSetupInfo.value
  101. return status === EnumSystemSetupInfoStatus.InProgress && !lockValue
  102. })
  103. const systemSetupUnlock = (callback: () => void) => {
  104. const { lockValue } = systemSetupInfo.value
  105. if (!lockValue) return
  106. const unlockMutation = new MutationHandler(
  107. useSystemSetupUnlockMutation({
  108. variables: {
  109. value: lockValue,
  110. },
  111. }),
  112. )
  113. unlockMutation
  114. .send()
  115. .then(() => {
  116. systemSetupInfo.value = {}
  117. callback()
  118. })
  119. .catch(() => {})
  120. }
  121. return {
  122. redirectPath,
  123. redirectNeeded,
  124. setSystemSetupInfo,
  125. systemSetupUnlock,
  126. systemSetupInfo,
  127. systemSetupDone,
  128. systemSetupAlreadyStarted,
  129. }
  130. })