useMyProfileUpdater.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {repositoryHelper} from "~/services/store/repository";
  2. import {QUERY_TYPE} from "~/types/enums";
  3. import {computed} from '@nuxtjs/composition-api'
  4. import {accessState, AnyJson, AnyStore} 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 = computed(()=>this.myProfile.activityYear)
  26. const 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. activityYear,
  32. historical
  33. }
  34. }
  35. /**
  36. * Créer une nouvelle instance MyProfile
  37. * @param myProfile
  38. */
  39. private createNewMyProfileInstance(myProfile:accessState): MyProfile{
  40. const myProfileInstance = repositoryHelper.createNewModelInstance(MyProfile) as MyProfile
  41. myProfileInstance.id = myProfile.id
  42. myProfileInstance.activityYear = myProfile.activityYear
  43. myProfileInstance.historical = myProfile.historical
  44. repositoryHelper.persist(MyProfile, myProfileInstance)
  45. return myProfileInstance
  46. }
  47. /**
  48. * Mets à jour l'activity de my profile
  49. * @param activityYear
  50. */
  51. private setActivityYear(activityYear:number){
  52. if(activityYear <= 0)
  53. 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. * Transform les choix de l'historique en objet JSON reconnaissable coté API
  65. * @param historicalChoices
  66. */
  67. private getHistoricalEntry(historicalChoices:Array<string>){
  68. const historicalDefault:any = {'past':false, 'future':false, 'present':false}
  69. for(const historicalChoice of historicalChoices){
  70. historicalDefault[historicalChoice] = true
  71. }
  72. return historicalDefault;
  73. }
  74. /**
  75. * Effectue la mise à jour (coté API) de MyProfile
  76. */
  77. private async updateMyProfile(): Promise<any>{
  78. await this.$dataPersister.invoke({
  79. type: QUERY_TYPE.MODEL,
  80. model: MyProfile,
  81. id: this.myProfile.id
  82. })
  83. }
  84. }
  85. export const $useMyProfileUpdater = (store:AnyStore, $dataPersister:DataPersister) => new UseMyProfileUpdater(store, $dataPersister).invoke()