Skip to content
Snippets Groups Projects
Unverified Commit ab0c2254 authored by Nabilsenko's avatar Nabilsenko Committed by GitHub
Browse files

Merge pull request #9 from ivanalglave/password-mail

feat: password mailed to user after signup using mailgun
parents a34ddbd4 18f8ea72
No related branches found
No related tags found
No related merge requests found
Source diff could not be displayed: it is too large. Options to address this: view the blob.
......@@ -25,6 +25,7 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs-modules/mailer": "^1.8.1",
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/jwt": "^9.0.0",
......@@ -35,6 +36,7 @@
"class-validator": "^0.13.2",
"eslint-import-resolver-typescript": "^3.5.2",
"fastify": "^4.9.2",
"mailgun-js": "^0.22.0",
"mongoose": "^6.7.2",
"multer": "^1.4.5-lts.1",
"passport": "^0.6.0",
......@@ -50,7 +52,9 @@
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "28.1.8",
"@types/multer": "^1.4.7",
"@types/node": "^16.0.0",
"@types/nodemailer": "^6.4.7",
"@types/passport-jwt": "^3.0.8",
"@types/passport-local": "^1.0.34",
"@types/supertest": "^2.0.11",
......
......@@ -14,8 +14,7 @@ import { ResourcesModule } from './resources/resources.module';
InternshipsModule,
ResourcesModule,
MongooseModule.forRoot(config.mongodb.uri),
LoginModule
LoginModule,
],
})
export class AppModule {}
......@@ -12,8 +12,14 @@ interface IMongodbConfig {
uri: string;
}
interface IMailgunConfig {
apiKey: string;
domain: string;
}
export interface IConfig {
server: IServerConfig;
resources: IResources;
mongodb: IMongodbConfig;
mailgun: IMailgunConfig;
}
......@@ -8,5 +8,9 @@
},
"mongodb": {
"uri": "mongodb://localhost:27017/internship-manager"
},
"mailgun": {
"apiKey": "5d9beaef19aaa5bb01aa59bd48a12501-f7d687c0-d4d95492",
"domain": "sandboxab7a8915c6f942d599319a95f74da7a6.mailgun.org"
}
}
......@@ -8,5 +8,9 @@
},
"mongodb": {
"uri": "mongodb://localhost:27017/internship-manager"
},
"mailgun": {
"apiKey": "5d9beaef19aaa5bb01aa59bd48a12501-f7d687c0-d4d95492",
"domain": "sandboxab7a8915c6f942d599319a95f74da7a6.mailgun.org"
}
}
......@@ -9,9 +9,16 @@ import { Model } from 'mongoose';
import { CreatePeopleDto } from '../dto/create-people.dto';
import { UpdatePeopleDto } from '../dto/update-people.dto';
import { People } from '../schemas/people.schema';
import * as Mailgun from 'mailgun-js';
import config from 'src/config';
@Injectable()
export class PeopleDao {
private mg = Mailgun({
apiKey: config.mailgun.apiKey,
domain: config.mailgun.domain,
});
constructor(
@InjectModel(People.name)
private readonly _peopleModel: Model<People>,
......@@ -19,11 +26,15 @@ export class PeopleDao {
login = (email: string, password: string): Promise<People | void> =>
new Promise((resolve, reject) => {
this._peopleModel.findOne({email : email, passwordHash : password}, (err, value) => {
if (err) reject(err.message);
if (!value) reject(new NotFoundException('Email or password is incorrect!'));
resolve(value);
});
this._peopleModel.findOne(
{ email: email, passwordHash: password },
(err, value) => {
if (err) reject(err.message);
if (!value)
reject(new NotFoundException('Email or password is incorrect!'));
resolve(value);
},
);
});
find = (): Promise<People[]> =>
......@@ -37,7 +48,7 @@ export class PeopleDao {
findById = (id: string): Promise<People | void> =>
new Promise((resolve, reject) => {
this._peopleModel.findOne({ id: id }, {}, {}, (err, value) => {
this._peopleModel.findById(id, (err, value) => {
if (err) reject(err.message);
if (!value) reject(new NotFoundException());
resolve(value);
......@@ -50,6 +61,7 @@ export class PeopleDao {
new this._peopleModel(people).save((err, value) => {
if (err) reject(err.message);
if (!value) reject(new InternalServerErrorException());
this.sendPassword(people.email, people.passwordHash);
resolve(value);
});
});
......@@ -98,4 +110,21 @@ export class PeopleDao {
secret += alphabet.charAt(randomInt(alphabet.length));
return secret;
};
async sendPassword(email: string, password: string) {
const data = {
from: 'Internship Manager <internshipmanager@master.il.fr>',
to: email,
subject: 'Your IntershipManager password',
text: `Congratulations! Your account is activated. Your InternshipManager password is "${password}"`,
};
await this.mg.messages().send(data, function (error, body) {
if (error) {
console.log(error);
} else {
console.log(body);
}
});
}
}
......@@ -5,11 +5,12 @@ import { UpdatePeopleDto } from './dto/update-people.dto';
import { PeopleEntity } from './entities/people.entity';
@Injectable()
export class PeopleService {
constructor(private readonly _peopleDao: PeopleDao) {}
constructor(
private readonly _peopleDao: PeopleDao,
) {}
login = (email: string, password: string): Promise<PeopleEntity | void> =>
this._peopleDao.login(email, password);
......@@ -19,8 +20,9 @@ export class PeopleService {
findOne = (id: string): Promise<PeopleEntity | void> =>
this._peopleDao.findById(id);
create = (people: CreatePeopleDto): Promise<PeopleEntity> =>
this._peopleDao.save(people);
create (people: CreatePeopleDto): Promise<PeopleEntity> {
return this._peopleDao.save(people);
}
update = (
id: string,
......@@ -30,4 +32,5 @@ export class PeopleService {
delete = (id: string): Promise<PeopleEntity | void> =>
this._peopleDao.findByIdAndRemove(id);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment