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

login.service.js 5.0KB

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