github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/frontend/bv/src/pages/espace_perso/logic/data.ts (about)

     1  // Ce module définit l'objet de stockage général,
     2  // qui effectue toutes les requêtes avec le serveur
     3  import { UrlServerBase, fileMeta } from "@/shared/logic/controller";
     4  import axios, { AxiosResponse } from "axios";
     5  import Vue from "vue";
     6  import {
     7    Aide,
     8    ChampsAideEditables,
     9    ContentEspacePerso,
    10    OutFicheSanitaire,
    11    InFicheSanitaire,
    12    JoomeoOutput,
    13    LockableFicheSanitaire,
    14    Participant,
    15    PublicDocument,
    16    InCreateDocument,
    17    MetaEspacePerso,
    18    Finances,
    19    PublicSondage,
    20    EditMessage,
    21    PseudoMessage,
    22    MessageKind
    23  } from "@/shared/logic/types";
    24  import { ControllerEspacePerso } from "./controller";
    25  import { EspacePerso } from "@/shared/logic/api";
    26  
    27  export class Data {
    28    controller: ControllerEspacePerso;
    29  
    30    // numero de l'espace perso, à setup au chargement initial
    31    key: string = "";
    32  
    33    metas: MetaEspacePerso | null = null;
    34  
    35    responsable: ContentEspacePerso["responsable"] | null = null;
    36    personnes: NonNullable<ContentEspacePerso["personnes"]> = {};
    37    participants: NonNullable<ContentEspacePerso["participants"]> = [];
    38    camps: NonNullable<ContentEspacePerso["camps"]> = {};
    39    messages: NonNullable<ContentEspacePerso["messages"]> = [];
    40    sondages: NonNullable<ContentEspacePerso["sondages"]> = {};
    41  
    42    finances: Finances | null = null;
    43  
    44    joomeoData: JoomeoOutput | null = null;
    45  
    46    get urlFacture() {
    47      return UrlServerBase + EspacePerso.DownloadFacture(this.key);
    48    }
    49    get urlAttestationPresence() {
    50      return UrlServerBase + EspacePerso.DownloadAttestationPresence(this.key);
    51    }
    52  
    53    constructor(controller: ControllerEspacePerso) {
    54      this.controller = controller;
    55    }
    56  
    57    async loadMetas() {
    58      try {
    59        const resp: AxiosResponse<MetaEspacePerso> = await axios.get(
    60          UrlServerBase + EspacePerso.GetMetas(this.key)
    61        );
    62        this.metas = resp.data;
    63      } catch (error) {
    64        this.controller.notifications.onAxiosError(error);
    65      }
    66    }
    67  
    68    private setupData(data: ContentEspacePerso) {
    69      this.responsable = data.responsable;
    70      this.personnes = data.personnes || {};
    71      this.participants = data.participants || [];
    72      this.camps = data.camps || {};
    73      this.messages = data.messages || [];
    74      this.sondages = data.sondages || {};
    75    }
    76  
    77    // charge depuis le serveur les données initiales de la page
    78    async loadData() {
    79      try {
    80        const resp: AxiosResponse<ContentEspacePerso> = await axios.get(
    81          UrlServerBase + EspacePerso.GetData(this.key)
    82        );
    83        this.setupData(resp.data);
    84      } catch (error) {
    85        this.controller.notifications.onAxiosError(error);
    86      }
    87    }
    88  
    89    async loadFinances() {
    90      try {
    91        const resp: AxiosResponse<Finances> = await axios.get(
    92          UrlServerBase + EspacePerso.GetFinances(this.key)
    93        );
    94        this.finances = resp.data;
    95      } catch (error) {
    96        this.controller.notifications.onAxiosError(error);
    97      }
    98    }
    99  
   100    async loadJoomeoInfos() {
   101      try {
   102        const resp: AxiosResponse<JoomeoOutput> = await axios.get(
   103          UrlServerBase + EspacePerso.LoadJoomeo(this.key)
   104        );
   105        this.joomeoData = resp.data;
   106      } catch (error) {
   107        this.controller.notifications.onAxiosError(error);
   108      }
   109    }
   110  
   111    async transfertFicheSanitaire(idCryptedPersonne: string) {
   112      try {
   113        const resp: AxiosResponse<string[] | null> = await axios.get(
   114          UrlServerBase + EspacePerso.TransfertFicheSanitaire(this.key),
   115          {
   116            params: {
   117              "id-crypted": idCryptedPersonne
   118            }
   119          }
   120        );
   121        const errors = resp.data || [];
   122        if (errors.length == 0) {
   123          this.controller.notifications.success = {
   124            title: "Partage de la fiche sanitaire",
   125            message: "Tous les mails ont bien été envoyés."
   126          };
   127        } else {
   128          this.controller.notifications.notification = {
   129            title: "Erreurs",
   130            message: `Certains mails n'ont pu être envoyés :
   131                                  <ul>${errors
   132                                    .map(s => "<li>" + s + "</li>")
   133                                    .join("")}</ul>`
   134          };
   135        }
   136      } catch (error) {
   137        this.controller.notifications.onAxiosError(error);
   138      }
   139    }
   140  
   141    async creeDocument(params: InCreateDocument, file: File) {
   142      const formData = fileMeta(file, params);
   143      try {
   144        let resp: AxiosResponse<PublicDocument> = await axios.put(
   145          UrlServerBase + EspacePerso.CreateDocument(this.key),
   146          formData
   147        );
   148        return resp.data;
   149      } catch (error) {
   150        this.controller.notifications.onAxiosError(error);
   151      }
   152    }
   153  
   154    async creeVaccin(file: File, idPersonneCrypted: string) {
   155      if (this.metas == null) return;
   156      const params: InCreateDocument = {
   157        id_personne: idPersonneCrypted,
   158        id_contrainte: this.metas.contrainte_vaccin.id_crypted
   159      };
   160      const uploadedDoc = await this.creeDocument(params, file);
   161      if (uploadedDoc === undefined) return;
   162      const pers = this.personnes[idPersonneCrypted];
   163      return (pers.vaccins || []).concat(uploadedDoc);
   164    }
   165  
   166    async updateFicheSanitaire(
   167      idPersonneCrypted: string,
   168      ficheSanitaire: LockableFicheSanitaire,
   169      securiteSociale: string
   170    ) {
   171      try {
   172        const params: InFicheSanitaire = {
   173          id_crypted: idPersonneCrypted,
   174          fiche_sanitaire: ficheSanitaire,
   175          securite_sociale: securiteSociale
   176        };
   177        const resp: AxiosResponse<OutFicheSanitaire> = await axios.post(
   178          UrlServerBase + EspacePerso.UpdateFicheSanitaire(this.key),
   179          params
   180        );
   181        const newFs = resp.data;
   182        this.personnes[idPersonneCrypted].fiche_sanitaire = newFs.fiche_sanitaire;
   183        this.participants.forEach(p => {
   184          p.is_fiche_sanitaire_up_to_date = true;
   185        });
   186        if (this.responsable) {
   187          this.responsable.securite_sociale = newFs.securite_sociale;
   188        }
   189        this.controller.notifications.success = {
   190          title: "Fiche sanitaire",
   191          message: "Votre fiche sanitaire a bien été <b>enregistrée</b>. Merci !"
   192        };
   193      } catch (error) {
   194        this.controller.notifications.onAxiosError(error);
   195      }
   196    }
   197  
   198    async updateParticipants(modifs: Participant[]) {
   199      try {
   200        await axios.post(
   201          UrlServerBase + EspacePerso.UpdateOptionsParticipants(this.key),
   202          {
   203            participants: modifs
   204          }
   205        );
   206        modifs.forEach(newPart => {
   207          const index = this.participants.findIndex(
   208            part => part.id_crypted == newPart.id_crypted
   209          );
   210          Vue.set(this.participants, index, newPart); // VRC
   211        });
   212        this.controller.notifications.success = {
   213          title: "Options",
   214          message: "Les options ont bien été mises à jour."
   215        };
   216      } catch (error) {
   217        this.controller.notifications.onAxiosError(error);
   218      }
   219    }
   220  
   221    async updateAide(aide: ChampsAideEditables) {
   222      if (this.finances == null) return;
   223      try {
   224        const resp: AxiosResponse<Aide> = await axios.post(
   225          UrlServerBase + EspacePerso.UpdateAide(this.key),
   226          aide
   227        );
   228        const modifiedAide = resp.data;
   229        const index = (this.finances.aides || []).findIndex(
   230          a => a.id_crypted == aide.id_crypted
   231        );
   232        Vue.set(this.finances.aides || [], index, modifiedAide); //VRC
   233        this.controller.notifications.success = {
   234          title: "Aide",
   235          message: "L'aide a bien été modifiée."
   236        };
   237      } catch (error) {
   238        this.controller.notifications.onAxiosError(error);
   239      }
   240    }
   241  
   242    async deleteAide(aide: ChampsAideEditables) {
   243      if (this.finances == null) return;
   244      try {
   245        await axios.delete(UrlServerBase + EspacePerso.DeleteAide(this.key), {
   246          params: { "id-crypted": aide.id_crypted }
   247        });
   248        this.finances.aides = (this.finances.aides || []).filter(
   249          a => a.id_crypted != aide.id_crypted
   250        );
   251        this.controller.notifications.success = {
   252          title: "Suppression",
   253          message: "L'aide a bien été supprimée."
   254        };
   255      } catch (error) {
   256        this.controller.notifications.onAxiosError(error);
   257      }
   258    }
   259  
   260    async ajouteAide(aide: ChampsAideEditables, file: File) {
   261      if (this.finances == null) return;
   262      const formData = fileMeta(file, aide);
   263      try {
   264        const resp: AxiosResponse<Aide> = await axios.put(
   265          UrlServerBase + EspacePerso.CreateAide(this.key),
   266          formData
   267        );
   268        const newAide = resp.data;
   269        const docAide = resp.data.document;
   270        newAide.document = docAide;
   271        if (this.finances.aides == null) {
   272          this.finances.aides = [];
   273        }
   274        this.finances.aides.push(newAide);
   275        this.controller.notifications.success = {
   276          title: "Aide",
   277          message:
   278            "L'aide a bien été ajoutée. <i>Elle sera validée par le centre dès que possible.</i>"
   279        };
   280      } catch (error) {
   281        this.controller.notifications.onAxiosError(error);
   282      }
   283    }
   284  
   285    async markConnection() {
   286      try {
   287        await axios.get(UrlServerBase + EspacePerso.MarkConnection(this.key));
   288      } catch (error) {
   289        this.controller.notifications.onAxiosError(error);
   290      }
   291    }
   292  
   293    async saveSondage(sondage: PublicSondage) {
   294      try {
   295        const resp: AxiosResponse<PublicSondage> = await axios.post(
   296          UrlServerBase + EspacePerso.SaveSondage(this.key),
   297          sondage
   298        );
   299        Vue.set(this.sondages, resp.data.id_camp, resp.data); //VRC
   300        this.controller.notifications.success = {
   301          title: "Enquête",
   302          message: "Votre avis a bien été enregistré. Merci !"
   303        };
   304      } catch (error) {
   305        this.controller.notifications.onAxiosError(error);
   306      }
   307    }
   308  
   309    async createMessage(contenu: string) {
   310      const params: EditMessage = { id: -1, contenu };
   311      try {
   312        const resp: AxiosResponse<PseudoMessage> = await axios.put(
   313          UrlServerBase + EspacePerso.CreeMessage(this.key),
   314          params
   315        );
   316        this.messages = [resp.data].concat(this.messages);
   317        this.controller.notifications.success = {
   318          title: "Nouveau message",
   319          message: "Votre message a bien été envoyé."
   320        };
   321      } catch (error) {
   322        this.controller.notifications.onAxiosError(error);
   323      }
   324    }
   325  
   326    async editMessage(params: EditMessage) {
   327      try {
   328        const resp: AxiosResponse<PseudoMessage> = await axios.post(
   329          UrlServerBase + EspacePerso.EditMessage(this.key),
   330          params
   331        );
   332        // l'id d'un pseudo message ne suffit pas, car il peut s'agir d'un paiement
   333        const index = this.messages.findIndex(
   334          mess => mess.id == params.id && mess.kind == MessageKind.MResponsable
   335        );
   336        if (index == -1) return;
   337        Vue.set(this.messages, index, resp.data); // VRC
   338        this.controller.notifications.success = {
   339          title: "Message",
   340          message: "Votre message a bien été modifié."
   341        };
   342      } catch (error) {
   343        this.controller.notifications.onAxiosError(error);
   344      }
   345    }
   346  
   347    async deleteMessage(idMessage: number) {
   348      try {
   349        const resp = await axios.delete(
   350          UrlServerBase + EspacePerso.DeleteMessage(this.key),
   351          {
   352            params: { "id-message": idMessage }
   353          }
   354        );
   355        // l'id d'un pseudo message ne suffit pas, car il peut s'agir d'un paiement
   356        const index = this.messages.findIndex(
   357          mess => mess.id == idMessage && mess.kind == MessageKind.MResponsable
   358        );
   359        if (index == -1) return;
   360        Vue.set(this.messages, index, resp.data); // VRC
   361        this.controller.notifications.success = {
   362          title: "Suppression",
   363          message: "Votre message a bien été supprimé."
   364        };
   365      } catch (error) {
   366        this.controller.notifications.onAxiosError(error);
   367      }
   368    }
   369  
   370    async confirmePlaceliberee(idMessage: number) {
   371      try {
   372        const resp: AxiosResponse<ContentEspacePerso> = await axios.post(
   373          UrlServerBase + EspacePerso.ConfirmePlaceliberee(this.key),
   374          {},
   375          {
   376            params: { "id-message": idMessage }
   377          }
   378        );
   379        this.setupData(resp.data);
   380        this.controller.notifications.success = {
   381          title: "Place confirmée",
   382          message: "La participation au séjour a bien été confirmée. Merci !"
   383        };
   384      } catch (error) {
   385        this.controller.notifications.onAxiosError(error);
   386      }
   387    }
   388  }