github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/frontend/bv/src/pages/inscription/complet/controller.ts (about) 1 import { Inscriptions } from "@/shared/logic/api"; 2 import { BaseController } from "@/shared/logic/controller"; 3 import { 4 Bus, 5 DataInscription, 6 EnregistreInscriptionOut, 7 ParticipantInscription, 8 SchemaPaiement, 9 Semaine, 10 Sexe 11 } from "@/shared/logic/types"; 12 import { nullDateString } from "@/shared/logic/utils"; 13 import Axios, { AxiosResponse } from "axios"; 14 import { apiUrl, parsePreselected } from "../shared"; 15 16 const preselected = parsePreselected(); 17 18 export class Controller extends BaseController { 19 data: DataInscription | null = null; 20 21 async loadData() { 22 try { 23 const rep: AxiosResponse<DataInscription> = await Axios.get( 24 apiUrl(Inscriptions.HandleLoadDataInscriptions), 25 { params: { preselected } } 26 ); 27 const data = rep.data; 28 (data.initial_inscription.participants || []).forEach(p => { 29 p.id_camp = Number(data.preselect_camp); 30 }); 31 this.data = data; 32 } catch (err) { 33 this.notifications.onAxiosError(err); 34 } 35 } 36 37 getCamp(idCamp: number) { 38 if (this.data == null) return null; 39 return (this.data.camps || []).filter(c => c.id == idCamp)[0] || null; 40 } 41 42 withAcompte() { 43 if (this.data == null) return false; 44 let withAcompte = false; 45 (this.data.initial_inscription.participants || []).forEach(part => { 46 const camp = this.getCamp(part.id_camp); 47 if (camp == null) { 48 return; 49 } 50 withAcompte = 51 withAcompte || camp.schema_paiement == SchemaPaiement.SPAcompte; 52 }); 53 return withAcompte; 54 } 55 56 // prend en compte une éventulle pré-sélection du séjour 57 newParticipant(): ParticipantInscription { 58 return { 59 nom: "", 60 prenom: "", 61 date_naissance: nullDateString, 62 sexe: Sexe.SAucun, 63 id_camp: this.data == null ? -1 : Number(this.data.preselect_camp), 64 lienid: { valid: false, id: 0, crypted: "" }, 65 options: { 66 bus: Bus.BAucun, 67 materiel_ski: { 68 need: "", 69 mode: "", 70 casque: true, 71 poids: 0, 72 taille: 0, 73 pointure: 0 74 } 75 }, 76 options_prix: { 77 jour: [], 78 quotient_familial: 0, 79 semaine: Semaine.SComplet, 80 statut: 0 81 } 82 }; 83 } 84 85 async checkMail(mail: string) { 86 try { 87 const resp: AxiosResponse<{ mail_found: boolean }> = await Axios.get( 88 apiUrl(Inscriptions.CheckMail), 89 { 90 params: { 91 mail: mail, 92 origin: window.location.href 93 } 94 } 95 ); 96 return resp.data.mail_found; 97 } catch (error) { 98 C.notifications.onAxiosError(error); 99 } 100 } 101 102 async validInscription() { 103 if (this.data == null) return; 104 try { 105 const rep: AxiosResponse<EnregistreInscriptionOut> = await Axios.post( 106 apiUrl(Inscriptions.EnregistreInscriptionComplete), 107 this.data.initial_inscription 108 ); 109 return rep.data; 110 } catch (err) { 111 this.notifications.onAxiosError(err); 112 } 113 } 114 } 115 116 export const C = new Controller();