fix estilo correo de administrador Comunidad
This commit is contained in:
parent
969c5c5d59
commit
9f0f2fc8ef
File diff suppressed because it is too large
Load Diff
|
@ -29,10 +29,12 @@
|
||||||
"@nestjs/platform-express": "^8.0.0",
|
"@nestjs/platform-express": "^8.0.0",
|
||||||
"@nestjs/swagger": "^5.2.1",
|
"@nestjs/swagger": "^5.2.1",
|
||||||
"buffer": "^5.7.1",
|
"buffer": "^5.7.1",
|
||||||
|
"class-validator": "^0.13.2",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"crypto-browserify": "^3.12.0",
|
"crypto-browserify": "^3.12.0",
|
||||||
"md5-typescript": "^1.0.5",
|
"md5-typescript": "^1.0.5",
|
||||||
"mongoose": "^6.4.1",
|
"mongoose": "^6.4.1",
|
||||||
|
"mongoose-unique-validator": "^3.1.0",
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
"rimraf": "^3.0.2",
|
"rimraf": "^3.0.2",
|
||||||
"rxjs": "^7.2.0",
|
"rxjs": "^7.2.0",
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
import { ArgumentsHost, Catch, ConflictException, ExceptionFilter } from '@nestjs/common';
|
||||||
|
import { MongoError } from 'mongodb';
|
||||||
|
|
||||||
|
@Catch(MongoError)
|
||||||
|
export class MongoExceptionFilter implements ExceptionFilter {
|
||||||
|
catch(exception: MongoError, host: ArgumentsHost) {
|
||||||
|
switch (exception.code) {
|
||||||
|
case 11000:
|
||||||
|
console.log('llave duplicada')
|
||||||
|
// duplicate exception
|
||||||
|
// do whatever you want here, for instance send error to client
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,11 +16,12 @@ import { UsersModule } from './users/users.module';
|
||||||
},
|
},
|
||||||
]),
|
]),
|
||||||
MongooseModule.forRoot(
|
MongooseModule.forRoot(
|
||||||
`mongodb+srv://proyecto_4:proyecto_4@proyecto4.yv4fb.mongodb.net/servicio_usuarios?retryWrites=true&w=majority`,
|
`mongodb+srv://proyecto_4:proyecto_4@proyecto4.yv4fb.mongodb.net/servicio_usuarios?retryWrites=true&w=majority`
|
||||||
|
|
||||||
),
|
),
|
||||||
UsersModule,
|
UsersModule,
|
||||||
],
|
],
|
||||||
controllers: [],
|
controllers: [],
|
||||||
providers: [],
|
providers: [],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule { }
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
import { Injectable } from "@nestjs/common";
|
||||||
|
import { Model } from 'mongoose';
|
||||||
|
import { User, UserDocument } from '../schemas/user.schema';
|
||||||
|
import { InjectModel } from '@nestjs/mongoose';
|
||||||
|
import { ValidationArguments, ValidatorConstraint, ValidatorConstraintInterface } from "class-validator";
|
||||||
|
|
||||||
|
|
||||||
|
@ValidatorConstraint({ name: 'UserExists', async: true })
|
||||||
|
@Injectable()
|
||||||
|
export class UserExistsRule implements ValidatorConstraintInterface {
|
||||||
|
constructor(
|
||||||
|
@InjectModel(User.name) private readonly userModel: Model<UserDocument>,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
async validate(value: string) {
|
||||||
|
try {
|
||||||
|
await this.userModel.find({email: value});
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
defaultMessage(args: ValidationArguments) {
|
||||||
|
return `User doesn't exist`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,27 @@
|
||||||
import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';
|
import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose';
|
||||||
import { Document } from 'mongoose';
|
import { Document } from 'mongoose';
|
||||||
|
var uniqueValidator = require('mongoose-unique-validator');
|
||||||
|
import { IsEmail, IsNotEmpty, IsString, Validate } from 'class-validator';
|
||||||
|
import { UserExistsRule } from 'src/exceptions/UserExistsRule';
|
||||||
|
|
||||||
export type UserDocument = User & Document;
|
export type UserDocument = User & Document;
|
||||||
|
|
||||||
@Schema({ collection: 'users' })
|
@Schema({ collection: 'users'})
|
||||||
export class User {
|
export class User {
|
||||||
@Prop()
|
@Prop({index: true})
|
||||||
dni: string;
|
dni!: string;
|
||||||
|
|
||||||
@Prop()
|
@Prop({required: true})
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@Prop()
|
@Prop({required: true})
|
||||||
last_name: string;
|
last_name: string;
|
||||||
|
|
||||||
@Prop()
|
@Prop({required: true, unique: true})
|
||||||
|
@Validate(UserExistsRule)
|
||||||
email: string;
|
email: string;
|
||||||
|
|
||||||
@Prop()
|
@Prop({required: true, unique: true})
|
||||||
phone: number;
|
phone: number;
|
||||||
|
|
||||||
@Prop()
|
@Prop()
|
||||||
|
@ -37,3 +41,4 @@ export class User {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const UserSchema = SchemaFactory.createForClass(User);
|
export const UserSchema = SchemaFactory.createForClass(User);
|
||||||
|
UserSchema.plugin(uniqueValidator);
|
|
@ -1,7 +1,8 @@
|
||||||
import { Controller } from '@nestjs/common';
|
import { Controller, UseFilters } from '@nestjs/common';
|
||||||
import { MessagePattern, Payload } from '@nestjs/microservices';
|
import { MessagePattern, Payload } from '@nestjs/microservices';
|
||||||
import { User, UserDocument } from '../schemas/user.schema';
|
import { User, UserDocument } from '../schemas/user.schema';
|
||||||
import { UsersService } from './users.service';
|
import { UsersService } from './users.service';
|
||||||
|
import { MongoExceptionFilter } from 'src/MongoExceptionFilter';
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
export class UsersController {
|
export class UsersController {
|
||||||
|
@ -13,6 +14,7 @@ export class UsersController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@MessagePattern({ cmd: 'createAdminSystem' })
|
@MessagePattern({ cmd: 'createAdminSystem' })
|
||||||
|
@UseFilters(MongoExceptionFilter)
|
||||||
createUserAdmin(@Payload() user: UserDocument) {
|
createUserAdmin(@Payload() user: UserDocument) {
|
||||||
return this.userService.create(user);
|
return this.userService.create(user);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,12 +28,16 @@ export class UsersService {
|
||||||
let password = user.password;
|
let password = user.password;
|
||||||
let passwordEncriptada = Md5.init(user.password);
|
let passwordEncriptada = Md5.init(user.password);
|
||||||
user.password = passwordEncriptada;
|
user.password = passwordEncriptada;
|
||||||
this.userModel.create(user);
|
|
||||||
|
this.userModel.create(user)
|
||||||
|
|
||||||
|
|
||||||
let community = await this.findCommunity(user.community_id);
|
let community = await this.findCommunity(user.community_id);
|
||||||
user.community_id = community['name'];
|
user.community_id = community['name'];
|
||||||
|
|
||||||
const pattern = { cmd: 'emailCreateUserAdminCommunity' };
|
const pattern = { cmd: 'emailCreateUserAdminCommunity' };
|
||||||
const payload = { email: user['email'], password: password, name: user['name'], date_entry: user['date_entry'] };
|
const payload = { email: user['email'], password: password, name: user['name'],
|
||||||
|
date_entry: user['date_entry'], community_name: community['name'] };
|
||||||
return this.clientNotificationtApp
|
return this.clientNotificationtApp
|
||||||
.send<string>(pattern, payload)
|
.send<string>(pattern, payload)
|
||||||
.pipe(
|
.pipe(
|
||||||
|
@ -141,8 +145,22 @@ export class UsersService {
|
||||||
return this.userModel.deleteOne({ _id: id }).exec();
|
return this.userModel.deleteOne({ _id: id }).exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async validateEmail(email: string) {
|
||||||
|
let repo1 = this.userModel;
|
||||||
|
return new Promise<User>((resolve, reject) => {
|
||||||
|
let repo = repo1;
|
||||||
|
|
||||||
|
repo.find({ email: email }).exec((err, res) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
if (res.length > 0) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue