useMyProfileUpdater.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import {repositoryHelper} from "~/services/store/repository";
  2. import {QUERY_TYPE} from "~/types/enums";
  3. import {computed, ComputedRef} from '@nuxtjs/composition-api'
  4. import {accessState, AnyJson, AnyStore, Historical} from "~/types/interfaces";
  5. import DataPersister from "~/services/dataPersister/dataPersister";
  6. import {MyProfile} from "~/models/Access/MyProfile";
  7. import {Item, Model} from "@vuex-orm/core";
  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)
  50. throw new Error('year must be positive')
  51. repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, activityYear, 'activityYear')
  52. }
  53. /**
  54. * Mets à jour l'historical de my profile
  55. * @param historicalChoices
  56. */
  57. private setHistorical(historicalChoices:Array<string>){
  58. repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, this.getHistoricalEntry(historicalChoices), 'historical')
  59. }
  60. /**
  61. * Mets à jour l'historical de my profile
  62. * @param dates
  63. */
  64. private setHistoricalRange(dates:Array<string>){
  65. repositoryHelper.updateStoreFromField(MyProfile, this.myProfile, this.getHistoricalRangeEntry(dates), 'historical')
  66. }
  67. /**
  68. * Transform les choix de l'historique en objet JSON reconnaissable coté API
  69. * @param historicalChoices
  70. */
  71. private getHistoricalEntry(historicalChoices:Array<string>){
  72. const historicalDefault:any = {'past':false, 'future':false, 'present':false}
  73. for(const historicalChoice of historicalChoices){
  74. historicalDefault[historicalChoice] = true
  75. }
  76. return historicalDefault;
  77. }
  78. /**
  79. * Trasnforme le choix des période en Objet JSON reconnaissable coté API
  80. * @param dates
  81. */
  82. private getHistoricalRangeEntry(dates:Array<string>){
  83. return {'past':false, 'future':false, 'present':false, dateStart:dates[0], dateEnd:dates[1]}
  84. }
  85. /**
  86. * Effectue la mise à jour (coté API) de MyProfile
  87. */
  88. private async updateMyProfile(): Promise<any>{
  89. await this.$dataPersister.invoke({
  90. type: QUERY_TYPE.MODEL,
  91. model: MyProfile,
  92. id: this.myProfile.id
  93. })
  94. }
  95. }
  96. export const $useMyProfileUpdater = (store:AnyStore, $dataPersister:DataPersister) => new UseMyProfileUpdater(store, $dataPersister).invoke()