useMyProfile.ts 3.2 KB

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