module.exports = Self => {
    Self.validatesPresenceOf('name', {
        message: 'Name cannot be blank'
    });

    Self.validatesPresenceOf('bic', {
        message: 'Swift / BIC cannot be empty'
    });

    Self.validatesUniquenessOf('bic', {
        message: 'This BIC already exist'
    });

    Self.validatesPresenceOf('countryFk', {
        message: 'CountryFK cannot be empty'
    });

    Self.validateAsync('bic', checkBic, {
        message: 'Bank entity id must be specified'
    });
    async function checkBic(err, done) {
        const filter = {
            fields: ['code'],
            where: {id: this.countryFk}
        };
        const country = await Self.app.models.Country.findOne(filter);
        const code = country ? country.code.toLowerCase() : null;

        if (code == 'es' && !this.id)
            err();
        done();
    }
};