useMyProfile.ts 3.1 KB

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