Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

login.service.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { ref } from 'vue'
  2. import {
  3. fetchQueueByProfileId,
  4. updateQueueByProfileId,
  5. fetchMembershipsByProfileId,
  6. fetchResponsesByProfileId,
  7. fetchProfileByProfileId,
  8. Chatter,
  9. StonkAlert,
  10. } from '../services/index.js'
  11. import { surveyFactory } from '../utils/index.js'
  12. /**
  13. * Logged in profile state manager
  14. * Sort of a util and service hybrid
  15. */
  16. class Login {
  17. constructor() {
  18. this._loading = ref(true)
  19. // Profile entity instance for self
  20. this._profile = null
  21. // Make reactive with vue observer
  22. this.id = ref(null)
  23. this.groupings = []
  24. this.queue = []
  25. this.responses = []
  26. this.tags = []
  27. this.toaster = null
  28. this.chatter = null
  29. }
  30. get isLoading() {
  31. return this._loading.value
  32. }
  33. /**
  34. * Track login separate from complete-ess
  35. * so a user can login but attached profile
  36. * can forward to survey
  37. * @returns {boolean}
  38. */
  39. get isLoggedIn() {
  40. return this.id.value != null && this.isLoading == false
  41. }
  42. /**
  43. * Combine questions retrieved from the database and
  44. * questions defined in out lang file and
  45. * copare to responses stored
  46. * @returns {boolean}
  47. */
  48. get isComplete() {
  49. return (
  50. this.responses.length == surveyFactory.questionsFromDb.length &&
  51. surveyFactory.questionsFromDb.length > 0
  52. )
  53. }
  54. /**
  55. * Check that some responses are set
  56. * @returns {boolean}
  57. */
  58. get hasResponses() {
  59. return this.responses.length && this.responses.length > 0
  60. }
  61. /**
  62. * Groupings pending pair
  63. */
  64. get pendingGroupings() {
  65. return this.groupings.filter(grouping => grouping.is_paired == false)
  66. }
  67. get pairedGroupings() {
  68. return this.groupings.filter(grouping => grouping.is_paired == true)
  69. }
  70. /**
  71. * Login a profile by id number
  72. * @param {number} profileId
  73. * @returns {number} stored reactive id
  74. */
  75. async login(profileId, cb) {
  76. this._loading.value = true
  77. // First check if profile exists
  78. console.warn('[Login Service warn]: Logging in:', profileId)
  79. // TODO: You can probably use this call to get responses, groupings and tags
  80. this._profile = await fetchProfileByProfileId(profileId)
  81. this.id.value = this._profile.profile_id
  82. if (!this.id.value) {
  83. console.error(
  84. `[Login Service error]: No profile found for profile_id: ${profileId}`,
  85. )
  86. }
  87. // Then grab groupings and queue for display
  88. await this.getGroupings()
  89. await this.getQueue()
  90. // Then hook into chat services
  91. try {
  92. await this.setupChatter()
  93. console.warn(
  94. `[Login Service warn]: ${profileId} subscribed to:`,
  95. this.chatter.subscriptions,
  96. )
  97. } catch (err) {
  98. console.error(err)
  99. }
  100. // Finally setup notifications and sse handling
  101. this.setupToaster(cb)
  102. console.warn('[Login Service warn]: Login SUCCESSFUL')
  103. this._loading.value = false
  104. return this.id.value
  105. }
  106. logout() {
  107. console.warn('[Login Service warn]: Logging out:', this.id.value)
  108. this.id.value = null
  109. if (this.toaster) {
  110. this.toaster.stop()
  111. }
  112. if (this.chatter) {
  113. this.chatter.stop()
  114. }
  115. }
  116. async getTags() {
  117. try {
  118. const tags = []
  119. this.setTags(tags)
  120. } catch (err) {
  121. console.error(`[Login Service]: ${err}`)
  122. }
  123. }
  124. setTags(tags) {
  125. this.tags = tags
  126. }
  127. async getResponses() {
  128. try {
  129. const responseList = await fetchResponsesByProfileId(this.id.value)
  130. this.setResponses(responseList)
  131. } catch (err) {
  132. console.error(`[Login Service]: ${err}`)
  133. }
  134. }
  135. setResponses(responses) {
  136. this.responses = responses
  137. }
  138. async getGroupings() {
  139. try {
  140. this.groupings = await fetchMembershipsByProfileId(this.id.value)
  141. } catch (err) {
  142. console.error(`[Login Service]: ${err}`)
  143. }
  144. }
  145. async getQueue() {
  146. this.queue = await fetchQueueByProfileId(this.id.value)
  147. }
  148. async updateQueue(profileId, targetId, reinsert) {
  149. this.queue = await updateQueueByProfileId(profileId, targetId, reinsert)
  150. }
  151. /**
  152. * For push notifications and chat
  153. */
  154. setupToaster(waveCb) {
  155. this.toaster = new StonkAlert(this.id.value, waveCb)
  156. }
  157. async setupChatter() {
  158. this.chatter = new Chatter()
  159. // Use the reactive id object
  160. await this.chatter.setup(this.id.value, this.groupings)
  161. }
  162. }
  163. const currentProfile = new Login()
  164. export { currentProfile, Login }