<commit_before>import { Component } from '../../component.decorator';
import UserService from '../../services/user.service';
import { User } from '../../services/user.service';

@Component({
    templateUrl: require('./registration.component.html')
})
export default class RegistrationComponent {
    private validationErrors: string[];
    private fieldErrors: any;

    private m_fName: string;
    private m_sName: string;
    private m_password: string;
    private m_confirmPassword: string;
    private m_email: string;
    private m_confirmEmail: string;
    private m_postalCode: string;
    private m_policyNumber: string;

    private m_day: string;
    private m_month: string;
    private m_year: string;

    private m_termsConsent: boolean;

    static $inject = ['$state', 'userService'];
    constructor(private $state: angular.ui.IStateService, private userService: UserService) {
        this.validationErrors = [];
    }

    public createUser(): void {
        this.validateFields();

        if (this.validationErrors.length == 0) {
            const user: User  = new User();

            user.fName = this.m_fName;
            user.sName = this.m_sName;
            user.password = <PASSWORD>;
            user.email = this.m_email;
            user.postalCode = this.m_postalCode;
            user.policyNumber = this.m_policyNumber;
            user.birthDate = new Date(parseInt(this.m_year, 10),
                    parseInt(this.m_month, 10) - 1,
                    parseInt(this.m_day, 10));

            this.userService.createUser(user)
                    .then((result: string[]) => {
                        console.warn('result[0]: ' + result[0]);
                        this.$state.go('app.registration-success');
                    })
                    .catch((error) => {
                        console.warn('error: ' + error);
                        if (error === 'InvalidPolicyNum') {
                            this.validationErrors.push('Your policy number is invalid.');
                        } else {
                            this.validationErrors.push(`Status code ${error.status}: ${error.statusText}`);
                        }
                    });
        }
    }

    private validateFields() {
        this.fieldErrors = {};
        this.validationErrors = [];

        this.validateFieldHasText('firstName', this.m_fName, 'First name is required.');
        this.validateFieldHasText('surName', this.m_sName, 'Surname is required.');
        this.validateFieldHasText('policyNumber', this.m_policyNumber, 'Policy number is required.');
        this.validateFieldHasText('email', this.m_email, 'Email is required.');
        this.validateFieldHasText('confirmEmail', this.m_confirmEmail, 'Confirmation email is required.');
        this.validateFieldHasText('password', this.m_password, 'Password is required.');
        this.validateFieldHasText('confirmPassword', this.m_confirmPassword, 'Confirmation password is required.');
        this.validateTrue('termsConsent', this.m_termsConsent, 'You must agree to the terms and conditions before' +
                ' you can register.');<commit_msg>We are now showing a better error message if the user you are trying to create already exists.<commit_after>import { Component } from '../../component.decorator';
import UserService from '../../services/user.service';
import { User } from '../../services/user.service';

@Component({
    templateUrl: require('./registration.component.html')
})
export default class RegistrationComponent {
    private validationErrors: string[];
    private fieldErrors: any;

    private m_fName: string;
    private m_sName: string;
    private m_password: string;
    private m_confirmPassword: string;
    private m_email: string;
    private m_confirmEmail: string;
    private m_postalCode: string;
    private m_policyNumber: string;

    private m_day: string;
    private m_month: string;
    private m_year: string;

    private m_termsConsent: boolean;

    static $inject = ['$state', 'userService'];
    constructor(private $state: angular.ui.IStateService, private userService: UserService) {
        this.validationErrors = [];
    }

    public createUser(): void {
        this.validateFields();

        if (this.validationErrors.length == 0) {
            const user: User  = new User();

            user.fName = this.m_fName;
            user.sName = this.m_sName;
            user.password = <PASSWORD>;
            user.email = this.m_email;
            user.postalCode = this.m_postalCode;
            user.policyNumber = this.m_policyNumber;
            user.birthDate = new Date(parseInt(this.m_year, 10),
                    parseInt(this.m_month, 10) - 1,
                    parseInt(this.m_day, 10));

            this.userService.createUser(user)
                    .then((result: string[]) => {
                        console.warn('result[0]: ' + result[0]);
                        this.$state.go('app.registration-success');
                    })
                    .catch((error) => {
                        console.warn('error: ' + error);
                        if (error === 'InvalidPolicyNum') {
                            this.validationErrors.push('Your policy number is invalid.');
                        } else if (error.data.error.result === 68) {
                            this.validationErrors.push('The user you are trying to create already exists.');
                        } else {
                            this.validationErrors.push(`Status code ${error.status}: ${error.statusText} (${error.data.error.result})`);
                        }
                    });
        }
    }

    private validateFields() {
        this.fieldErrors = {};
        this.validationErrors = [];

        this.validateFieldHasText('firstName', this.m_fName, 'First name is required.');
        this.validateFieldHasText('surName', this.m_sName, 'Surname is required.');
        this.validateFieldHasText('policyNumber', this.m_policyNumber, 'Policy number is required.');
        this.validateFieldHasText('email', this.m_email, 'Email is required.');
        this.validateFieldHasText('confirmEmail', this.m_confirmEmail, 'Confirmation email is required.');
        this.validateFieldHasText('password', this.m_password, 'Password is required.');
        this.validateFieldHasText('confirmPassword', this.m_confirmPassword, 'Confirmation password is required.');
        this.validateTrue('termsConsent', this.m_termsConsent, 'You must agree to the terms and conditions before' +
                ' you can register.');
{'fraction_non_alphanumeric': 0.11899002116722104, 'fraction_numerical': 0.003326277592984578, 'mean_word_length': 2.234718826405868, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '1465648', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1695, 'n_words': 398}