Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { HttpException, HttpService, HttpStatus, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { MailerService } from '../mailer/mailer.service';
import { CreateTempUserDto } from './dto/create-temp-user.dto';
import { TempUser, TempUserDocument } from './temp-user.schema';
import * as ejs from 'ejs';
import { ITempUser } from './temp-user.interface';
@Injectable()
export class TempUserService {
constructor(
private readonly httpService: HttpService,
private readonly mailerService: MailerService,
@InjectModel(TempUser.name) private tempUserModel: Model<ITempUser>
) {}
public async create(createTempUser: CreateTempUserDto, structureName: string): Promise<TempUser> {
const userInDb = await this.findOne(createTempUser.email);
if (userInDb) {
throw new HttpException('User already exists', HttpStatus.BAD_REQUEST);
}
const createUser = new this.tempUserModel(createTempUser);
// Send email
this.sendUserMail(createUser, structureName);
createUser.save();
return await this.findOne(createTempUser.email);
}
public async findOne(mail: string): Promise<TempUser | undefined> {
return this.tempUserModel.findOne({ email: mail }).exec();
}
public async findById(id: string): Promise<TempUser | undefined> {
return this.tempUserModel.findById(Types.ObjectId(id)).exec();
}
public async delete(mail: string): Promise<TempUser> {
const userInDb = await this.findOne(mail);
if (!userInDb) {
throw new HttpException('User already exists', HttpStatus.BAD_REQUEST);
}
this.tempUserModel.deleteOne({ email: mail }).exec();
return userInDb;
}
public async updateStructureLinked(createTempUser: CreateTempUserDto): Promise<TempUser> {
const userInDb = await this.tempUserModel
.find({
$and: [
{
email: createTempUser.email,
},
{
pendingStructuresLink: { $in: [createTempUser.pendingStructuresLink[0]] },
},
],
})
.exec();
if (userInDb.length > 0) {
throw new HttpException('User already linked', HttpStatus.UNPROCESSABLE_ENTITY);
}
return this.tempUserModel
.updateOne(
{ email: createTempUser.email },
{ $push: { pendingStructuresLink: createTempUser.pendingStructuresLink[0] } }
)
.exec();
}
/**
* Send email in order to tell the user that an account is alreday fill with his structure info.
* @param user User
*/
private async sendUserMail(user: ITempUser, structureName: string): Promise<any> {
const config = this.mailerService.config;
const ejsPath = this.mailerService.getTemplateLocation(config.templates.tempUserRegistration.ejs);
const jsonConfig = this.mailerService.loadJsonConfig(config.templates.tempUserRegistration.json);
const html = await ejs.renderFile(ejsPath, {
config,
id: user._id,
name: structureName,
});
this.mailerService.send(user.email, jsonConfig.subject, html);
}
}