github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/core/utils/mails/render.go (about)

     1  package mails
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"html/template"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	rd "github.com/benoitkugler/goACVE/server/core/rawdata"
    11  )
    12  
    13  var (
    14  	FuncMap = template.FuncMap{
    15  		"safe": func(s string) template.HTML { return template.HTML(s) },
    16  	}
    17  
    18  	templates parsedTemplates
    19  )
    20  
    21  type MailRenderer interface {
    22  	Render(messagePerso string) (string, error)
    23  }
    24  
    25  type parsedTemplates struct {
    26  	NotifieMessage         *template.Template
    27  	Preinscription         *template.Template
    28  	ValideMail             *template.Template
    29  	DebloqueFicheSanitaire *template.Template
    30  	AccuseReceptionSimple  *template.Template
    31  	NotifEnvoisDocs        *template.Template
    32  	NotifDirecteur         *template.Template
    33  	InviteEquipier         *template.Template
    34  	RenvoieLienEspacePerso *template.Template
    35  	NotificationDon        *template.Template
    36  	NotifFusion            *template.Template
    37  }
    38  
    39  // InitTemplates charge les templates depuis le dossier
    40  // donné par `ressourcesPath`
    41  func InitTemplates(ressourcesPath string) {
    42  	fp := func(filename string) string {
    43  		return filepath.Join(ressourcesPath, "templates_mails", filename)
    44  	}
    45  	templates.NotifieMessage = template.Must(template.New("").Funcs(FuncMap).ParseFiles(
    46  		fp("base.html"), fp("notifie_message.html")))
    47  
    48  	templates.Preinscription = template.Must(template.New("").Funcs(FuncMap).ParseFiles(
    49  		fp("base.html"), fp("preinscription.html")))
    50  
    51  	templates.ValideMail = template.Must(template.New("").Funcs(FuncMap).ParseFiles(
    52  		fp("base.html"), fp("valide_mail.html")))
    53  
    54  	templates.DebloqueFicheSanitaire = template.Must(template.New("").Funcs(FuncMap).ParseFiles(
    55  		fp("base.html"), fp("debloque_fiche_sanitaire.html")))
    56  
    57  	templates.AccuseReceptionSimple = template.Must(template.New("").Funcs(FuncMap).ParseFiles(
    58  		fp("base.html"), fp("accuse_reception_simple.html"), fp("coordonnees_centre.html")))
    59  
    60  	templates.NotifEnvoisDocs = template.Must(template.New("").Funcs(FuncMap).ParseFiles(fp("notif_envois_docs.html")))
    61  
    62  	templates.NotifDirecteur = template.Must(template.New("").Funcs(FuncMap).ParseFiles(
    63  		fp("base.html"), fp("notifie_directeur.html")))
    64  
    65  	templates.InviteEquipier = template.Must(template.New("").Funcs(FuncMap).ParseFiles(
    66  		fp("base.html"), fp("invite_equipier.html")))
    67  
    68  	templates.RenvoieLienEspacePerso = template.Must(template.New("").Funcs(FuncMap).ParseFiles(
    69  		fp("base.html"), fp("renvoie_lien_espace_perso.html")))
    70  
    71  	templates.NotificationDon = template.Must(template.New("").Funcs(FuncMap).ParseFiles(
    72  		fp("base.html"), fp("notifie_don.html")))
    73  
    74  	templates.NotifFusion = template.Must(template.New("").Funcs(FuncMap).ParseFiles(
    75  		fp("base.html"), fp("notifie_fusion_dossier.html")))
    76  }
    77  
    78  type Contact struct {
    79  	Prenom string
    80  	Sexe   rd.Sexe
    81  }
    82  
    83  func (c Contact) Salutations() string {
    84  	if c.Prenom == "" {
    85  		return "Bonjour,"
    86  	}
    87  	var out string
    88  	if c.Sexe == "M" {
    89  		out = "Cher"
    90  	} else if c.Sexe == "F" {
    91  		out = "Chère"
    92  	} else {
    93  		out = "Bonjour"
    94  	}
    95  	return fmt.Sprintf("%s %s,", out, c.Prenom)
    96  }
    97  
    98  type Participant struct {
    99  	NomPrenom, Sexe string
   100  	Prenom          string
   101  	DateNaissance   rd.Date
   102  	LabelCamp       string
   103  }
   104  
   105  type DetailsParticipant struct {
   106  	Participant
   107  	Attente            rd.ListeAttente
   108  	Groupe             string
   109  	NeedFicheSanitaire bool
   110  }
   111  
   112  type champsCommuns struct {
   113  	Contact       Contact
   114  	Title         string
   115  	FooterTitle   string
   116  	FooterInfos   string
   117  	SignatureMail string
   118  }
   119  
   120  type TargetRespo struct {
   121  	Lien, NomPrenom string
   122  }
   123  
   124  type paramsPreinscription struct {
   125  	champsCommuns
   126  	Mail         string
   127  	Responsables []TargetRespo
   128  }
   129  
   130  type paramsValideMail struct {
   131  	champsCommuns
   132  	UrlValideInscription string
   133  }
   134  
   135  type paramsDebloqueFicheSanitaire struct {
   136  	champsCommuns
   137  	NomPrenomParticipant      string
   138  	NewMail                   string
   139  	UrlDebloqueFicheSanitaire string
   140  }
   141  
   142  type paramsNotifieMessage struct {
   143  	champsCommuns
   144  	Contenu         string
   145  	LienEspacePerso string
   146  }
   147  
   148  type paramsNotifFusion struct {
   149  	champsCommuns
   150  	LienEspacePerso string
   151  }
   152  
   153  type paramsAccuseReceptionSimple struct {
   154  	Sejour string
   155  	champsCommuns
   156  }
   157  
   158  type paramsNotifEnvoisDocs struct {
   159  	LabelCamp string
   160  	Envois    rd.Envois
   161  }
   162  
   163  type Responsable struct {
   164  	Contact
   165  	Mail, Tels string
   166  }
   167  
   168  type paramsNotifieDirecteur struct {
   169  	champsCommuns
   170  	Directeur    Contact
   171  	Participants []Participant
   172  	Responsable  Responsable
   173  	InfoLines    []string
   174  	LabelCamp    string
   175  }
   176  
   177  type paramsInviteEquipier struct {
   178  	champsCommuns
   179  	Equipier       Contact
   180  	LabelCamp      string
   181  	LienFormulaire string
   182  }
   183  
   184  type ResumeDossier struct {
   185  	Responsable rd.BasePersonne
   186  	Lien        string
   187  	CampsMap    rd.Camps
   188  }
   189  
   190  func (r ResumeDossier) Camps() string {
   191  	var tmp []string
   192  	for _, camp := range r.CampsMap {
   193  		tmp = append(tmp, camp.Label().String())
   194  	}
   195  	return strings.Join(tmp, ", ")
   196  }
   197  
   198  type paramsRenvoieLienEspacePerso struct {
   199  	champsCommuns
   200  	Mail     string
   201  	Dossiers []ResumeDossier
   202  }
   203  
   204  type paramsNotificationDon struct {
   205  	champsCommuns
   206  	Montant string
   207  }
   208  
   209  func newChampCommuns(contact Contact, title string) champsCommuns {
   210  	return champsCommuns{
   211  		Contact:       contact,
   212  		Title:         title,
   213  		FooterTitle:   rd.Asso.Title,
   214  		FooterInfos:   rd.Asso.Infos,
   215  		SignatureMail: rd.SignatureMail,
   216  	}
   217  }
   218  
   219  func render(temp *template.Template, name string, data interface{}) (string, error) {
   220  	buf := new(bytes.Buffer)
   221  	err := temp.ExecuteTemplate(buf, name, data)
   222  	return buf.String(), err
   223  }
   224  
   225  func NewAccuseReceptionSimple(camp rd.Camp, contact Contact) (string, error) {
   226  	commun := newChampCommuns(contact, "Inscription validée")
   227  	p := paramsAccuseReceptionSimple{
   228  		Sejour:        camp.Label().String(),
   229  		champsCommuns: commun,
   230  	}
   231  	return render(templates.AccuseReceptionSimple, "base.html", p)
   232  }
   233  
   234  func NewNotifieMessage(contact Contact, title, contenu, lienEspacePerso string) (string, error) {
   235  	p := paramsNotifieMessage{
   236  		champsCommuns:   newChampCommuns(contact, title),
   237  		Contenu:         contenu,
   238  		LienEspacePerso: lienEspacePerso,
   239  	}
   240  	p.SignatureMail += "<br/><br/><i>Merci de ne pas répondre directement à ce mail mais d'utiliser votre espace de suivi (ci-dessus).</i>"
   241  	return render(templates.NotifieMessage, "base.html", p)
   242  }
   243  
   244  func NewPreinscription(mail string, resp []TargetRespo) (string, error) {
   245  	commun := newChampCommuns(Contact{}, "Inscription rapide")
   246  	commun.SignatureMail = "<i>Ps : Ceci est un mail automatique, merci de ne pas y répondre.</i>"
   247  	p := paramsPreinscription{
   248  		champsCommuns: commun,
   249  		Mail:          mail,
   250  		Responsables:  resp,
   251  	}
   252  	return render(templates.Preinscription, "base.html", p)
   253  }
   254  
   255  func NewRenvoieLienEspacePerso(mail string, dossiers []ResumeDossier) (string, error) {
   256  	commun := newChampCommuns(Contact{}, "Espace de suivi")
   257  	commun.SignatureMail = "<i>Ps : Ceci est un mail automatique, merci de ne pas y répondre.</i>"
   258  	p := paramsRenvoieLienEspacePerso{
   259  		champsCommuns: commun,
   260  		Mail:          mail,
   261  		Dossiers:      dossiers,
   262  	}
   263  	return render(templates.RenvoieLienEspacePerso, "base.html", p)
   264  }
   265  
   266  func NewValideMail(urlValideInscription string, contact Contact) (string, error) {
   267  	commun := newChampCommuns(contact, "Confirmation de l'adresse mail")
   268  	commun.SignatureMail = "<i>Ps : Ceci est un mail automatique, merci de ne pas y répondre.</i>"
   269  	p := paramsValideMail{
   270  		UrlValideInscription: urlValideInscription,
   271  		champsCommuns:        commun,
   272  	}
   273  	return render(templates.ValideMail, "base.html", p)
   274  }
   275  
   276  func NewDebloqueFicheSanitaire(urlDebloqueFicheSanitaire, newMail, nomPrenom string) (string, error) {
   277  	commun := newChampCommuns(Contact{}, "Accès à la fiche sanitaire")
   278  	commun.SignatureMail = "<i>Ps : Ceci est un mail automatique, merci de ne pas y répondre.</i>"
   279  	p := paramsDebloqueFicheSanitaire{
   280  		UrlDebloqueFicheSanitaire: urlDebloqueFicheSanitaire,
   281  		NewMail:                   newMail,
   282  		NomPrenomParticipant:      nomPrenom,
   283  		champsCommuns:             commun,
   284  	}
   285  	return render(templates.DebloqueFicheSanitaire, "base.html", p)
   286  }
   287  
   288  // paramsNotifieDirecteur est à compléter
   289  func NewNotifieDirecteur(directeur Contact, participants []Participant, responsable Responsable,
   290  	infoLines []string, labelCamp string) (string, error) {
   291  	p := paramsNotifieDirecteur{
   292  		champsCommuns: newChampCommuns(directeur, "Nouvelle inscription"),
   293  		Directeur:     directeur,
   294  		Participants:  participants,
   295  		Responsable:   responsable,
   296  		InfoLines:     infoLines,
   297  		LabelCamp:     labelCamp,
   298  	}
   299  	return render(templates.NotifDirecteur, "base.html", p)
   300  }
   301  
   302  func NewNotifieEnvoiDocs(camp rd.Camp) (string, error) {
   303  	p := paramsNotifEnvoisDocs{
   304  		Envois:    camp.Envois,
   305  		LabelCamp: camp.Label().String(),
   306  	}
   307  	return render(templates.NotifEnvoisDocs, "notif_envois_docs.html", p)
   308  }
   309  
   310  func NewInviteEquipier(camp rd.Camp, directeur string, equipier rd.Personne, lienForumaire string) (string, error) {
   311  	cc := newChampCommuns(Contact{}, "Bienvenue dans l'équipe !")
   312  	if directeur != "" {
   313  		cc.SignatureMail = directeur + "<br/><i>Ps : Ceci est un mail automatique.</i>"
   314  	} else {
   315  		cc.SignatureMail = "<i>Ps : Ceci est un mail automatique, merci de ne pas y répondre.</i>"
   316  	}
   317  	p := paramsInviteEquipier{
   318  		champsCommuns: cc,
   319  		Equipier: Contact{
   320  			Prenom: equipier.FPrenom(),
   321  			Sexe:   equipier.Sexe,
   322  		},
   323  		LabelCamp:      camp.Label().String(),
   324  		LienFormulaire: lienForumaire,
   325  	}
   326  	return render(templates.InviteEquipier, "base.html", p)
   327  }
   328  
   329  func NewNotificationDon(contact Contact, montant rd.Euros) (string, error) {
   330  	cc := newChampCommuns(contact, "Merci pour votre don !")
   331  	cc.SignatureMail = "L'équipe ACVE"
   332  	p := paramsNotificationDon{
   333  		champsCommuns: cc,
   334  		Montant:       montant.String(),
   335  	}
   336  	return render(templates.NotificationDon, "base.html", p)
   337  }
   338  
   339  func NewNotifFusion(contact Contact, lienEspacePerso string) (string, error) {
   340  	p := paramsNotifFusion{
   341  		champsCommuns:   newChampCommuns(contact, "Fusion de votre dossier"),
   342  		LienEspacePerso: lienEspacePerso,
   343  	}
   344  	p.SignatureMail += "<br/><br/><i>Merci de ne pas répondre directement à ce mail mais d'utiliser votre espace de suivi (ci-dessus).</i>"
   345  	return render(templates.NotifFusion, "base.html", p)
   346  }