github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/acvegestion/identifie.go (about) 1 package acvegestion 2 3 import ( 4 "errors" 5 6 "github.com/benoitkugler/goACVE/server/core/apiserver" 7 rd "github.com/benoitkugler/goACVE/server/core/rawdata" 8 "github.com/benoitkugler/goACVE/server/shared" 9 ) 10 11 // unifie la procédure de rapprochement d'une personne temporaire 12 // avec une personne existante 13 type identifieOut interface { 14 // modifie sur place le destinataire 15 // la table de 'idTarget' dépend du context 16 // dans le cas d'un nouveau profil on charge simplement l'item, attendu par le client 17 updateTarget(tx rd.DB, paramsIn apiserver.IdentifiePersonneIn, idTarget int64) (err error) 18 } 19 20 type identEquipierOut apiserver.IdentifieEquipierOut 21 22 func (t *identEquipierOut) updateTarget(tx rd.DB, paramsIn apiserver.IdentifiePersonneIn, idTarget int64) (err error) { 23 if paramsIn.Rattache { 24 row := tx.QueryRow("UPDATE equipiers SET id_personne = $1 WHERE id = $2 RETURNING *", paramsIn.IdPersonneTarget, idTarget) 25 t.Equipier, err = rd.ScanEquipier(row) 26 } else { 27 t.Equipier, err = rd.SelectEquipier(tx, idTarget) 28 } 29 return err 30 } 31 32 type identParticipantOut apiserver.IdentifieParticipantOut 33 34 func (t *identParticipantOut) updateTarget(tx rd.DB, paramsIn apiserver.IdentifiePersonneIn, idTarget int64) (err error) { 35 if paramsIn.Rattache { 36 row := tx.QueryRow("UPDATE participants SET id_personne = $1 WHERE id = $2 RETURNING *", paramsIn.IdPersonneTarget, idTarget) 37 t.Participant, err = rd.ScanParticipant(row) 38 } else { 39 t.Participant, err = rd.SelectParticipant(tx, idTarget) 40 } 41 return err 42 } 43 44 type identResponsableOut apiserver.IdentifieResponsableOut 45 46 func (t *identResponsableOut) updateTarget(tx rd.DB, paramsIn apiserver.IdentifiePersonneIn, idTarget int64) (err error) { 47 if paramsIn.Rattache { 48 row := tx.QueryRow("UPDATE factures SET id_personne = $1 WHERE id = $2 RETURNING *", paramsIn.IdPersonneTarget, idTarget) 49 t.Facture, err = rd.ScanFacture(row) 50 } else { 51 t.Facture, err = rd.SelectFacture(tx, idTarget) 52 } 53 return err 54 } 55 56 type identParticipantsimpleOut apiserver.IdentifieParticipantsimpleOut 57 58 func (t *identParticipantsimpleOut) updateTarget(tx rd.DB, paramsIn apiserver.IdentifiePersonneIn, idTarget int64) (err error) { 59 if paramsIn.Rattache { 60 row := tx.QueryRow("UPDATE participantsimples SET id_personne = $1 WHERE id = $2 RETURNING *", paramsIn.IdPersonneTarget, idTarget) 61 t.Participantsimple, err = rd.ScanParticipantsimple(row) 62 } else { 63 t.Participantsimple, err = rd.SelectParticipantsimple(tx, idTarget) 64 } 65 66 return err 67 } 68 69 // 1 - modifie sur place `outTarget`; 2 -renvoie les changements relatif à la personne 70 func (ct Controller) identifiePersonne(in apiserver.IdentifiePersonneIn, idTarget int64, outTarget identifieOut) (out apiserver.IdentifiePersonneOut, err error) { 71 if in.Rattache && in.IdPersonneTmp == in.IdPersonneTarget { 72 return out, errors.New("Impossible de rattacher une personne à elle-même.") 73 } 74 75 tx, err := ct.DB.Begin() 76 if err != nil { 77 return out, err 78 } 79 80 if !in.Rattache { 81 // on marque simplement la personne actuelle comme non temporaire 82 row := tx.QueryRow("UPDATE personnes SET is_temporaire = FALSE WHERE id = $1 RETURNING *", in.IdPersonneTmp) 83 out.Personne, err = rd.ScanPersonne(row) 84 if err != nil { 85 return out, shared.Rollback(tx, err) 86 } 87 out.DeletedPersonne = -1 88 err = outTarget.updateTarget(ct.DB, in, idTarget) // aucune modification 89 if err != nil { 90 return out, shared.Rollback(tx, err) 91 } 92 err = tx.Commit() 93 return out, err 94 } 95 96 // on commence par appliquer les modifications (imposées par le client) 97 // en se restreignant aux champs concernés 98 out.Personne, err = rd.SelectPersonne(tx, in.IdPersonneTarget) 99 if err != nil { 100 return out, shared.Rollback(tx, err) 101 } 102 out.Personne.BasePersonne = in.Modifications // on applique les changements 103 out.Personne, err = out.Personne.Update(tx) 104 if err != nil { 105 return out, shared.Rollback(tx, err) 106 } 107 108 // on supprime les doublons dictés par le client 109 // liens 110 _, err = rd.DeleteDocumentPersonnesByIdDocuments(tx, in.SupprimeDocuments...) 111 if err != nil { 112 return out, shared.Rollback(tx, err) 113 } 114 // contenu 115 _, err = rd.DeleteContenuDocumentsByIdDocuments(tx, in.SupprimeDocuments...) 116 if err != nil { 117 return out, shared.Rollback(tx, err) 118 } 119 // documents 120 out.DeletedDocs, err = rd.DeleteDocumentsByIds(tx, in.SupprimeDocuments...) 121 if err != nil { 122 return out, shared.Rollback(tx, err) 123 } 124 125 // on redirige les documents de la personne temporaire vers la nouvelle personne 126 rows, err := tx.Query("UPDATE document_personnes SET id_personne = $1 WHERE id_personne = $2 RETURNING *", in.IdPersonneTarget, in.IdPersonneTmp) 127 if err != nil { 128 return out, shared.Rollback(tx, err) 129 } 130 out.Documents, err = rd.ScanDocumentPersonnes(rows) 131 if err != nil { 132 return out, shared.Rollback(tx, err) 133 } 134 135 // on redirige l'item (equipier, facture, participant, participantsimple) vers la nouvelle personne 136 err = outTarget.updateTarget(tx, in, idTarget) 137 if err != nil { 138 return out, shared.Rollback(tx, err) 139 } 140 141 // concernant la personne "temporaire" il y a deux cas : 142 // - soit elle est vraiment temporaire (cas usuel) 143 // - soit elle est déjà authentifié, et le client demande quand même 144 // une redirection 145 old, err := rd.SelectPersonne(tx, in.IdPersonneTmp) 146 if err != nil { 147 return out, shared.Rollback(tx, err) 148 } 149 if old.IsTemporaire { 150 // on la supprime 151 _, err = rd.DeletePersonneById(tx, in.IdPersonneTmp) 152 if err != nil { 153 return out, shared.Rollback(tx, err) 154 } 155 out.DeletedPersonne = in.IdPersonneTmp 156 } else { 157 // sinon on la laisse 158 out.DeletedPersonne = -1 159 } 160 err = tx.Commit() 161 return out, err 162 } 163 164 func (ct Controller) identifieEquipier(in apiserver.IdentifieEquipierIn) (apiserver.IdentifieEquipierOut, error) { 165 var out apiserver.IdentifieEquipierOut 166 outPer, err := ct.identifiePersonne(in.IdentifiePersonneIn, in.IdEquipier, (*identEquipierOut)(&out)) 167 out.IdentifiePersonneOut = outPer 168 return out, err 169 } 170 171 func (ct Controller) identifieParticipant(in apiserver.IdentifieParticipantIn) (apiserver.IdentifieParticipantOut, error) { 172 var out apiserver.IdentifieParticipantOut 173 outPer, err := ct.identifiePersonne(in.IdentifiePersonneIn, in.IdParticipant, (*identParticipantOut)(&out)) 174 out.IdentifiePersonneOut = outPer 175 return out, err 176 } 177 178 func (ct Controller) identifieResponsable(in apiserver.IdentifieResponsableIn) (apiserver.IdentifieResponsableOut, error) { 179 var out apiserver.IdentifieResponsableOut 180 outPer, err := ct.identifiePersonne(in.IdentifiePersonneIn, in.IdFacture, (*identResponsableOut)(&out)) 181 out.IdentifiePersonneOut = outPer 182 return out, err 183 } 184 185 func (ct Controller) identifieParticipantsimple(in apiserver.IdentifieParticipantsimpleIn) (apiserver.IdentifieParticipantsimpleOut, error) { 186 var out apiserver.IdentifieParticipantsimpleOut 187 outPer, err := ct.identifiePersonne(in.IdentifiePersonneIn, in.IdParticipantsimple, (*identParticipantsimpleOut)(&out)) 188 out.IdentifiePersonneOut = outPer 189 return out, err 190 }