Exemplo n.º 1
0
        /*
         * Méthode d'inscription du client
         * Création d'un compte,contact,publicUser
         */
        public Object Inscrire(JObject j)
        {
            string        Identif  = j["compte"]["Identifiant"].ToObject <string>();
            Comptes       cptAjout = null;
            Contacts      ctcAjout = null;
            CompteContact cptCntct = null;
            PublicUser    user     = null;
            Boolean       resu;

            //On va tester selon la réponse de l'identifiant( SIREN ou bien TVA)
            try
            {
                using (var transaction = _context.Database.BeginTransaction())
                {
                    if (Identif.Equals("Siren"))
                    {
                        string siren = j["compte"]["Siren"].ToObject <string>();
                        if (isSirenValid(siren))
                        {
                            var infosFromAPI = getInfosFromAPISIRENAsync(siren).Result;
                            var PaysId       = this.getIdPaysByName(infosFromAPI["records"][0]["fields"]["l7_normalisee"].ToObject <string>());
                            ctcAjout = new Contacts()
                            {
                                Nom     = j["contact"]["Nom"].ToObject <string>(),
                                Prenom  = j["contact"]["Prenom"].ToObject <string>(),
                                Email   = j["contact"]["Email"].ToObject <string>(),
                                Tel     = j["contact"]["Tel"].ToObject <string>(),
                                PaysId  = PaysId,
                                Typectc = "PROSPECT" // ça va refleter le type du client
                            };
                            _context.Contacts.Add(ctcAjout);
                            _context.SaveChanges();

                            cptAjout = new Comptes()
                            {
                                Siren         = siren,
                                Codepostal    = infosFromAPI["records"][0]["fields"]["codpos"].ToObject <string>(),
                                Nom           = infosFromAPI["records"][0]["fields"]["nomen_long"].ToObject <string>(),
                                NomCourt      = infosFromAPI["records"][0]["fields"]["sigle"].ToObject <string>(),
                                Ville         = infosFromAPI["records"][0]["fields"]["nom_dept"].ToObject <string>(),
                                Adresse1      = infosFromAPI["records"][0]["fields"]["l4_normalisee"].ToObject <string>(),
                                PaysId        = PaysId,
                                MainContactId = ctcAjout.Id,
                            };
                        }
                    }

                    else if (Identif.Equals("Tva"))
                    {
                        Console.WriteLine("===============> TVAAA");
                        string tva = j["compte"]["NumTva"].ToObject <string>();
                        if (IsTvaValide(tva))
                        {
                            Console.WriteLine("===============> valid");
                            var infosFromAPI = getInfosFromAPITVAAsync(tva).Result;
                            var adresse      = infosFromAPI["traderAddress"].ToObject <string>();
                            var PaysIdentif  = this.getIdPaysByName(this.getCountryFromCode(infosFromAPI["countryCode"].ToObject <string>()));
                            ctcAjout = new Contacts()
                            {
                                Nom     = j["contact"]["Nom"].ToObject <string>(),
                                Prenom  = j["contact"]["Prenom"].ToObject <string>(),
                                Email   = j["contact"]["Email"].ToObject <string>(),
                                Tel     = j["contact"]["Tel"].ToObject <string>(),
                                PaysId  = PaysIdentif,
                                Typectc = "PROSPECT" // ça va refleter le type du client
                            };
                            _context.Contacts.Add(ctcAjout);
                            _context.SaveChanges();

                            cptAjout = new Comptes()
                            {
                                NumTva        = tva,
                                Codepostal    = getCodePostaleFromAdresse(adresse.ToString()),
                                Nom           = infosFromAPI["traderName"].ToObject <string>(),
                                MainContactId = ctcAjout.Id,
                                PaysId        = PaysIdentif,
                                Ville         = getCityFromAdresse(adresse.ToString())
                            };
                        }
                    }
                    cptCntct = new CompteContact()
                    {
                        Idcnt = ctcAjout.Id,
                        IdCpt = cptAjout.Id
                    };

                    ctcAjout.CompteContact.Add(cptCntct);
                    cptAjout.CompteContact.Add(cptCntct);

                    user = new PublicUser()
                    {
                        Username  = ctcAjout.Email,
                        ContactId = ctcAjout.Id,
                        Password  = j["password"].ToObject <string>(),
                        RoleId    = 1
                    };
                    _context.Comptes.Add(cptAjout);
                    _context.PublicUser.Add(user);
                    _context.CompteContact.Add(cptCntct);
                    _context.SaveChanges();
                    transaction.Commit();

                    var       validationToken = _tokenHelper.BuildMailVerificationToken(user); //Token à envoyer dans un mail de verification avec le mailservice
                    var       ourLink         = "<a href='" + _config["Jwt:Issuer"] + "Inscription/verify?token=" + validationToken + "' > Clicker ici pour confirmer votre compte </a>";
                    MailModel mailModel       = new MailModel()
                    {
                        fromAdr  = "*****@*****.**",
                        toAdr    = user.Username,
                        fromName = "AWBS TARGET",
                        toName   = ctcAjout.Nom,
                        content  = ourLink,
                        subject  = "Test"
                    };
                    _mailService.SendVerificationMail(mailModel);
                }
                resu = true;
                return(new { operation = resu });
            }
            catch (DbUpdateException)
            {
                resu = false;
            }
            return(resu);
        }