Skip to content

Commit 76ec27f

Browse files
committed
update helper
1 parent 38041b2 commit 76ec27f

File tree

6 files changed

+74
-2
lines changed

6 files changed

+74
-2
lines changed

.env.example

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ REFRESH_TOKEN_EXPIRE=604800000
2222
# Twilio
2323
TWILIO_SID=
2424
TWILIO_TOKEN=
25-
TWILIO_PHONE_NUMBER=
25+
TWILIO_PHONE_NUMBER=
26+
27+
# SendGrid
28+
SEND_GRID_API_KEY=
29+
SEND_GRID_SENDER_NAME=
30+
SEND_GRID_EMAIL=

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@nestjs/swagger": "^4.8.0",
5252
"@nestjs/typeorm": "^7.1.5",
5353
"@nestjs/websockets": "^7.6.15",
54+
"@sendgrid/mail": "^7.4.4",
5455
"@types/socket.io": "^2.1.13",
5556
"axios": "^0.21.1",
5657
"bcryptjs": "^2.4.3",
@@ -115,7 +116,7 @@
115116
"lint-staged": {
116117
"*.(js|ts|json)": [
117118
"prettier --write",
118-
"git add ."
119+
"git add"
119120
]
120121
}
121122
}

src/config/environment.ts

+4
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ export const Environment = {
1313
TWILIO_SID: process.env.TWILIO_SID,
1414
TWILIO_TOKEN: process.env.TWILIO_TOKEN,
1515
TWILIO_PHONE_NUMBER: process.env.TWILIO_PHONE_NUMBER,
16+
17+
SEND_GRID_API_KEY: process.env.SEND_GRID_API_KEY,
18+
SEND_GRID_SENDER_NAME: process.env.SEND_GRID_SENDER_NAME,
19+
SEND_GRID_EMAIL: process.env.SEND_GRID_MAIL,
1620
};

src/helpers/mailer.helper.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import config from '$config';
2+
import * as sgMail from '@sendgrid/mail';
3+
sgMail.setApiKey(config.ENV.SEND_GRID_API_KEY);
4+
5+
export async function sendEmail(receiver: string, subject: string, content: string, html: string) {
6+
const msg = {
7+
to: receiver,
8+
from: {
9+
name: config.ENV.SEND_GRID_SENDER_NAME,
10+
email: config.ENV.SEND_GRID_EMAIL,
11+
}, // Use the email address or domain you verified above
12+
subject: subject,
13+
text: content,
14+
html: html,
15+
};
16+
17+
return await sgMail.send(msg);
18+
}

src/helpers/utils.hepler.ts

+21
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,24 @@ export async function verifyGoogle(accessToken: string) {
3939
return { ...profile, id: profile.id, email: profile.email };
4040
} catch (error) {}
4141
}
42+
43+
export function randomOTP(length: number = 6): string {
44+
const digits = '0123456789';
45+
const digitsLength = digits.length;
46+
let result = '';
47+
for (let i = 0; i < length; i++) {
48+
const index = Math.floor(Math.random() * digitsLength);
49+
result += digits[index];
50+
}
51+
return result;
52+
}
53+
54+
export function randomString(length = 6) {
55+
let result = '';
56+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
57+
let charactersLength = characters.length;
58+
for (let i = 0; i < length; i++) {
59+
result += characters.charAt(Math.floor(Math.random() * charactersLength));
60+
}
61+
return result;
62+
}

yarn.lock

+23
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,29 @@
768768
"@angular-devkit/core" "11.2.6"
769769
"@angular-devkit/schematics" "11.2.6"
770770

771+
"@sendgrid/client@^7.4.3":
772+
version "7.4.3"
773+
resolved "https://registry.yarnpkg.com/@sendgrid/client/-/client-7.4.3.tgz#bc71aae05de2cd9aa607f4eb1d886711d09f9595"
774+
integrity sha512-tTaHx893w5iqG0sVtUnMyRchuwYF95k4UOkmov1MouMIeMUbNvbalITo7cG7YSXUTY9rT2t4eBY6HcEBCVeqfg==
775+
dependencies:
776+
"@sendgrid/helpers" "^7.4.3"
777+
axios "^0.21.1"
778+
779+
"@sendgrid/helpers@^7.4.3":
780+
version "7.4.3"
781+
resolved "https://registry.yarnpkg.com/@sendgrid/helpers/-/helpers-7.4.3.tgz#231a4405266cfa291054df8b668fe3a4dc9387aa"
782+
integrity sha512-Wt+68g1sVEM5UspJh34O/cxtv6BBbtAIk7U9B3PB2ySOtPs9e6hI1QkgYVwpNmkt7k2p86muUNyma/Aig25agg==
783+
dependencies:
784+
deepmerge "^4.2.2"
785+
786+
"@sendgrid/mail@^7.4.4":
787+
version "7.4.4"
788+
resolved "https://registry.yarnpkg.com/@sendgrid/mail/-/mail-7.4.4.tgz#cc31915342d0233592baa6e80b74beaf32e0bf2b"
789+
integrity sha512-9+dyArajxbPY7eJJAd2eps7MVsXW9E4Y294gWeMYku0/Id/qAivexidjstvkkVlZdlrHbEsGDlnMEcw2eXOiFg==
790+
dependencies:
791+
"@sendgrid/client" "^7.4.3"
792+
"@sendgrid/helpers" "^7.4.3"
793+
771794
"@sinonjs/commons@^1.7.0":
772795
version "1.8.3"
773796
resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.3.tgz#3802ddd21a50a949b6721ddd72da36e67e7f1b2d"

0 commit comments

Comments
 (0)