useMyProfileUpdater.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { computed, ComputedRef } from '@nuxtjs/composition-api'
  2. import { Item, Model } from '@vuex-orm/core'
  3. import { repositoryHelper } from '~/services/store/repository'
  4. import { QUERY_TYPE } from '~/types/enums'
  5. import { AnyJson, AnyStore, Historical } from '~/types/interfaces'
  6. import DataPersister from '~/services/data/dataPersister'
  7. import { MyProfile } from '~/models/Access/MyProfile'
  8. import { $accessProfile } from '@/services/profile/accessProfile'
  9. import { useContext } from '@nuxtjs/composition-api'
  10. /**
  11. * @category Use/updater
  12. * @class UseMyProfileUpdater
  13. * Use Classe pour la gestion REST de MyProfile
  14. */
  15. export class UseMyProfileUpdater {
  16. private store!:AnyStore
  17. private $dataPersister!:DataPersister
  18. private myProfile!:MyProfile
  19. constructor (store:AnyStore, $dataPersister:DataPersister) {
  20. this.store = store
  21. this.$dataPersister = $dataPersister
  22. }
  23. /**
  24. * Composition function
  25. */
  26. public invoke (): AnyJson {
  27. const {$ability} = useContext()
  28. const currentAccessId = $accessProfile(this.store).getCurrentAccessId()
  29. this.myProfile = this.getMyProfileInstance(currentAccessId) as MyProfile
  30. const activityYear:ComputedRef<number> = computed(() => this.myProfile.activityYear)
  31. const historical:ComputedRef<Historical> = computed(() => this.myProfile.historical)
  32. return {
  33. updateMyProfile: () => this.updateMyProfile(),
  34. setActivityYear: (activityYear:number) => this.setActivityYear(activityYear),
  35. setHistorical: (historicalChoices:Array<string>) => this.setHistorical(historicalChoices),
  36. setHistoricalRange: (dates:Array<string>) => this.setHistoricalRange(dates),
  37. activityYear,
  38. historical
  39. }
  40. }
  41. /**
  42. * récupère l'instance MyProfile
  43. * @param myProfileId
  44. */
  45. private getMyProfileInstance (myProfileId:any): Item<Model> {
  46. return repositoryHelper.findItemFromModel(MyProfile, parseInt(myProfileId))
  47. }
  48. /**
  49. * Mets à jour l'activity de my profile
  50. * @param activityYear
  51. */
  52. private setActivityYear (activityYear:number) {
  53. if (activityYear <= 0) { throw new Error('year must be positive') }
  54. repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, activityYear, 'activityYear')
  55. }
  56. /**
  57. * Mets à jour l'historical de my profile
  58. * @param historicalChoices
  59. */
  60. private setHistorical (historicalChoices:Array<string>) {
  61. repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, this.getHistoricalEntry(historicalChoices), 'historical')
  62. }
  63. /**
  64. * Mets à jour l'historical de my profile
  65. * @param dates
  66. */
  67. private setHistoricalRange (dates:Array<string>) {
  68. repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, this.getHistoricalRangeEntry(dates), 'historical')
  69. }
  70. /**
  71. * Transform les choix de l'historique en objet JSON reconnaissable coté API
  72. * @param historicalChoices
  73. */
  74. private getHistoricalEntry (historicalChoices:Array<string>) {
  75. const historicalDefault:any = { past: false, future: false, present: false }
  76. for (const historicalChoice of historicalChoices) {
  77. historicalDefault[historicalChoice] = true
  78. }
  79. return historicalDefault
  80. }
  81. /**
  82. * Trasnforme le choix des période en Objet JSON reconnaissable coté API
  83. * @param dates
  84. */
  85. private getHistoricalRangeEntry (dates:Array<string>) {
  86. return { past: false, future: false, present: false, dateStart: dates[0], dateEnd: dates[1] }
  87. }
  88. /**
  89. * Effectue la mise à jour (coté API) de MyProfile
  90. */
  91. private async updateMyProfile (): Promise<any> {
  92. await this.$dataPersister.invoke({
  93. type: QUERY_TYPE.MODEL,
  94. model: MyProfile,
  95. id: this.myProfile.id
  96. })
  97. }
  98. }
  99. export const $useMyProfileUpdater = (store:AnyStore, $dataPersister:DataPersister) => new UseMyProfileUpdater(store, $dataPersister).invoke()