Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async validateGovernmentAsync() {
const g = await this.government;
if (!g) {
const error = new ValidationError();
error.property = 'governmentId';
error.constraints = { isDefined: 'governmentId should not be null or undefined' };
this.errors.push(error);
}
}
validateName() {
if (this.contributorType === ContributorType.INDIVIDUAL) {
if (!this.contrLast || this.contrLast.trim() === '') {
const error = new ValidationError();
error.property = 'contrLast';
error.constraints = { isDefined: 'contrLast should not be null or undefined' };
this.errors.push(error);
}
if (!this.contrFirst || this.contrFirst.trim() === '') {
const error = new ValidationError();
error.property = 'contrFirst';
error.constraints = { isDefined: 'contrFirst should not be null or undefined' };
this.errors.push(error);
}
} else {
if (!this.contrName || this.contrName.trim() === '') {
const error = new ValidationError();
error.property = 'contrName';
error.constraints = { isDefined: 'contrName should not be null or undefined' };
this.errors.push(error);
}
}
}
name?: string,
city?: string,
country?: string,
avatarUrl?: string,
coverUrl?: string
): Promise {
if (!email && !username) throw new ForbiddenException('Username OR email is required');
const existedUser = await this.userRepository.findByUsernameOrEmail({ email, username });
if (existedUser) throw new ForbiddenException('Username or Email has been taken.');
const user = this.userRepository.create({ email, firstname, lastname, name, city, country, avatarUrl, coverUrl });
if (password) {
if (password.length >= 6 && password.length <= 32) user.generatePassword(password);
else {
const error = new ValidationError();
error.property = 'password';
error.constraints = {
minLength: 'Password must be longer than 6 characters',
maxLength: 'Password must not be longer than 32 characters'
};
error.value = password;
error.children = [];
throw new UnprocessedEntityException('Can not save User', [error]);
}
}
if (username) user.username = username;
else user.generateUsernameFromEmail();
return this.userRepository.saveOrFail(user);
}
async validateGovernmentAsync() {
const g = await this.government;
if (!g) {
const error = new ValidationError();
error.property = 'governmentId';
error.constraints = { isDefined: 'governmentId should not be null or undefined' };
this.errors.push(error);
}
}
async validateUserAsync() {
const u = await this.user;
if (!u) {
const error = new ValidationError();
error.property = 'userId';
error.constraints = {isDefined: 'userId should not be null or undefined'};
this.errors.push(error);
}
}
}
async validateUserAsync() {
const u = await this.user;
if (!u) {
const error = new ValidationError();
error.property = 'userId';
error.constraints = {isDefined: 'userId should not be null or undefined'};
this.errors.push(error);
}
}
coverUrl?: string
): Promise {
const user = await this.userRepository.findOneOrFail(id);
if (username) user.username = username;
if (email) user.email = email;
if (firstname) user.firstname = firstname;
if (lastname) user.lastname = lastname;
if (name) user.name = name;
if (city) user.city = city;
if (country) user.country = country;
if (avatarUrl) user.avatarUrl = avatarUrl;
if (coverUrl) user.coverUrl = coverUrl;
if (password) {
if (password.length >= 6 && password.length <= 32) user.generatePassword(password);
else {
const error = new ValidationError();
error.property = 'password';
error.constraints = {
minLength: 'Password must be longer than 6 characters',
maxLength: 'Password must not be longer than 32 characters'
};
error.value = password;
error.children = [];
throw new UnprocessedEntityException('Can not save User', [error]);
}
}
return this.userRepository.saveOrFail(user);
}
async validateCampaignAsync() {
const c = await this.campaign;
if (!c && this.role !== UserRole.GOVERNMENT_ADMIN) {
const error = new ValidationError();
error.property = 'campaignId';
error.constraints = {isDefined: 'campaignId should not be null or undefined'};
this.errors.push(error);
} else if (c && this.role === UserRole.GOVERNMENT_ADMIN) {
const error = new ValidationError();
error.property = 'campaignId';
error.constraints = {notAllowed: 'campaignId cannot be set with GovernmentAdmin as a UserRole'};
this.errors.push(error);
}
}