useMyProfileUpdater.ts 3.6 KB

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