<commit_before>﻿using System;

namespace LcRest
{
    /// <summary>
    /// Lead Generation APIs: newsletter, referral
    /// </summary>
    public class LeadGeneration
    {
        #region Newsletter
        public static void PostNewsletter(string email, bool isServiceProfessional, string marketingSource, Locale locale)
        {
            var emailExists = Client.CheckEmailAvailability(email) > 0;

            if (emailExists)
            {
                throw new Exception("Email is already registered, please log-in or request a password reset");
            }

            var additionalSource = "&lead-generation=newsletter";
            var utm = (marketingSource ?? "") + additionalSource;
            var userID = 0;

            using (var db = new LcDatabase())
            {
                userID = (int)db.QueryValue(@"
                    DECLARE @UserID int

                    BEGIN TRANSACTION;

                    -- Create UserProfile record to save email and generate UserID
                    INSERT INTO UserProfile (
                        Email
                    ) VALUES (
                        @0
                    )
                    SET @UserID = @@Identity

                    -- Create user account record, but account disabled
                    INSERT INTO Users (
		                UserID,
		                IsProvider,
		                IsCustomer,
                        AccountStatusID,
                        loconomicsMarketingCampaigns,

		                FirstName,
		                LastName,
		                MiddleIn,
		                SecondLastName,

                        marketingSource,
                        preferredLanguageID,
                        preferredCountryID,

		                CreatedDate,
		                UpdatedDate,
		                ModifiedBy,
		                Active
                    ) VALUES (
                        @UserID,
                        @1, -- Is professional
                        1, -- Is client
                        -1,
                        1,
                        
                        '',
                        '',
                        '',
                        '',

                        @2,
                        @3,
                        @4,

                        getdate(),
                        getdate(),
                        'sys',
                        1 -- Active
                    )

                    -- NOTE: since there is no Membership record with password, is not an actual Loconomics User Account
                    -- just what we need on this case

                    COMMIT TRANSACTION;
                    SELECT @UserID
                ",
                email,
                isServiceProfessional,
                utm,
                locale.languageID,
                locale.countryID);
            }

            //LcMessaging.SendNewsletterSubscription(userID, email);
        }
        #endregion
    }
}<commit_msg>Bugfix SQL transaction management, that allowed partial generated user record (corrupted) #675<commit_after>﻿using System;

namespace LcRest
{
    /// <summary>
    /// Lead Generation APIs: newsletter, referral
    /// </summary>
    public class LeadGeneration
    {
        #region Newsletter
        public static void PostNewsletter(string email, bool isServiceProfessional, string marketingSource, Locale locale)
        {
            var emailExists = Client.CheckEmailAvailability(email) > 0;

            if (emailExists)
            {
                throw new Exception("Email is already registered, please log-in or request a password reset");
            }

            var additionalSource = "&lead-generation=newsletter";
            var utm = (marketingSource ?? "") + additionalSource;
            var userID = 0;

            using (var db = new LcDatabase())
            {
                userID = (int)db.QueryValue(@"
                    DECLARE @UserID int

                    BEGIN TRANSACTION

                    -- Create UserProfile record to save email and generate UserID
                    INSERT INTO UserProfile (
                        Email
                    ) VALUES (
                        @0
                    )
                    SET @UserID = @@Identity

                    -- Create user account record, but account disabled
                    INSERT INTO Users (
		                UserID,
		                IsProvider,
		                IsCustomer,
                        AccountStatusID,
                        loconomicsMarketingCampaigns,

		                FirstName,
		                LastName,
		                MiddleIn,
		                SecondLastName,

                        marketingSource,
                        preferredLanguageID,
                        preferredCountryID,

		                CreatedDate,
		                UpdatedDate,
		                ModifiedBy,
		                Active
                    ) VALUES (
                        @UserID,
                        @1, -- Is professional
                        1, -- Is client
                        -1,
                        1,
                        
                        '',
                        '',
                        '',
                        '',

                        @2,
                        @3,
                        @4,

                        getdate(),
                        getdate(),
                        'sys',
                        1 -- Active
                    )

                    -- NOTE: since there is no Membership record with password, is not an actual Loconomics User Account
                    -- just what we need on this case
                    
                    IF @@ERROR <> 0
                        ROLLBACK TRANSACTION
                    ELSE
                        COMMIT TRANSACTION

                    SELECT @UserID
                ",
                email,
                isServiceProfessional,
                utm,
                locale.languageID,
                locale.countryID);
            }

            //LcMessaging.SendNewsletterSubscription(userID, email);
        }
        #endregion
    }
}
{'fraction_non_alphanumeric': 0.059878322126160746, 'fraction_numerical': 0.004162664105027218, 'mean_word_length': 0.8266081871345029, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4723949', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1188, 'n_words': 347}