useMyProfileUpdater.ts 3.8 KB

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