@skroz/profile-api 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/dto/ConfirmEmailInput.d.ts +1 -1
  2. package/dist/dto/ConfirmEmailInput.js +3 -3
  3. package/dist/dto/ForgotPasswordInput.d.ts +1 -1
  4. package/dist/dto/ForgotPasswordInput.js +3 -3
  5. package/dist/dto/LoginInput.d.ts +2 -2
  6. package/dist/dto/LoginInput.js +38 -5
  7. package/dist/dto/PasswordInput.d.ts +1 -1
  8. package/dist/dto/PasswordInput.js +3 -3
  9. package/dist/dto/RecoverPasswordInput.d.ts +2 -2
  10. package/dist/dto/RecoverPasswordInput.js +38 -5
  11. package/dist/dto/RegisterInput.d.ts +2 -2
  12. package/dist/dto/RegisterInput.js +38 -5
  13. package/dist/dto/SendTokenPayload.d.ts +2 -2
  14. package/dist/dto/SendTokenPayload.js +3 -4
  15. package/dist/dto/StatusPayload.d.ts +1 -1
  16. package/dist/dto/StatusPayload.js +2 -3
  17. package/dist/dto/UpdateEmailInput.d.ts +1 -1
  18. package/dist/dto/UpdateEmailInput.js +3 -3
  19. package/dist/dto/UpdatePasswordInput.d.ts +2 -2
  20. package/dist/dto/UpdatePasswordInput.js +38 -5
  21. package/dist/dto/UpdateProfileInput.d.ts +1 -1
  22. package/dist/dto/UpdateProfileInput.js +3 -3
  23. package/dist/dto/index.d.ts +11 -11
  24. package/dist/dto/index.js +42 -24
  25. package/dist/email-templates/base/content.pug +32 -0
  26. package/dist/email-templates/base/header.pug +28 -0
  27. package/dist/email-templates/base/layout.pug +34 -0
  28. package/dist/email-templates/base/mixins.pug +16 -0
  29. package/dist/email-templates/confirmEmail.pug +16 -0
  30. package/dist/email-templates/forgotPassword.pug +16 -0
  31. package/dist/email-templates/notification.pug +14 -0
  32. package/dist/email-templates/tempPassword.pug +14 -0
  33. package/dist/resolvers/AuthResolver.d.ts +1 -24
  34. package/dist/resolvers/AuthResolver.js +6 -3
  35. package/dist/resolvers/ProfileResolver.d.ts +1 -18
  36. package/dist/resolvers/ProfileResolver.js +8 -2
  37. package/dist/services/ProfileEmailService.js +2 -2
  38. package/dist/types/index.d.ts +1 -1
  39. package/package.json +3 -3
  40. package/src/dto/ConfirmEmailInput.ts +1 -1
  41. package/src/dto/ForgotPasswordInput.ts +1 -1
  42. package/src/dto/LoginInput.ts +2 -2
  43. package/src/dto/PasswordInput.ts +1 -1
  44. package/src/dto/RecoverPasswordInput.ts +2 -2
  45. package/src/dto/RegisterInput.ts +2 -2
  46. package/src/dto/SendTokenPayload.ts +2 -2
  47. package/src/dto/StatusPayload.ts +1 -1
  48. package/src/dto/UpdateEmailInput.ts +1 -1
  49. package/src/dto/UpdatePasswordInput.ts +2 -2
  50. package/src/dto/UpdateProfileInput.ts +1 -1
  51. package/src/dto/index.ts +11 -11
  52. package/src/email-templates/base/content.pug +32 -0
  53. package/src/email-templates/base/header.pug +28 -0
  54. package/src/email-templates/base/layout.pug +34 -0
  55. package/src/email-templates/base/mixins.pug +16 -0
  56. package/src/email-templates/confirmEmail.pug +16 -0
  57. package/src/email-templates/forgotPassword.pug +16 -0
  58. package/src/email-templates/notification.pug +14 -0
  59. package/src/email-templates/tempPassword.pug +14 -0
  60. package/src/resolvers/AuthResolver.ts +76 -30
  61. package/src/resolvers/ProfileResolver.ts +125 -98
  62. package/src/services/ProfileEmailService.ts +147 -147
  63. package/src/types/index.ts +1 -1
@@ -2,157 +2,157 @@ import AwsSes from '@os-team/aws-ses';
2
2
  import pug from 'pug';
3
3
  import path from 'path';
4
4
  import {
5
- AuthUser,
6
- EmailConfig,
7
- ProfileEmailTemplate,
8
- ProfileLocales,
5
+ AuthUser,
6
+ EmailConfig,
7
+ ProfileEmailTemplate,
8
+ ProfileLocales,
9
9
  } from '../types';
10
10
 
11
11
  export type EmailType = 'auth' | 'activity' | 'info' | 'admin' | 'other';
12
12
 
13
13
  export class ProfileEmailService {
14
- private ses: AwsSes;
15
-
16
- private config: EmailConfig;
17
-
18
- private locales: ProfileLocales;
19
-
20
- constructor(config: EmailConfig, locales: ProfileLocales) {
21
- this.ses = new AwsSes({ region: 'eu-west-1' });
22
- this.config = config;
23
- this.locales = locales;
24
- }
25
-
26
- private getWebsiteUrl() {
27
- return this.config.websiteUrl || `https://${this.config.domain}`;
28
- }
29
-
30
- private render(templateName: string, vars: any) {
31
- const templatePath = path.resolve(
32
- this.config.templateDir,
33
- `${templateName}.pug`
34
- );
35
- return pug.renderFile(templatePath, {
36
- domain: this.config.domain,
37
- websiteUrl: this.getWebsiteUrl(),
38
- primaryBrandColor: this.config.primaryBrandColor,
39
- logoUrl: this.config.logoUrl || '',
40
- ...vars,
41
- });
42
- }
43
-
44
- private getFrom(type: EmailType) {
45
- const { domain } = this.config;
46
- const username = this.config.fromEmailUsername;
47
-
48
- switch (type) {
49
- case 'auth':
50
- return `${domain} <${username}@auth.${domain}>`;
51
- case 'activity':
52
- return `${domain} <${username}@a.${domain}>`;
53
- case 'info':
54
- return `${domain} <${username}@i.${domain}>`;
55
- case 'admin':
56
- return `${domain} <${username}@admin.${domain}>`;
57
- default:
58
- return `${domain} <${username}@o.${domain}>`;
14
+ private ses: AwsSes;
15
+
16
+ private config: EmailConfig;
17
+
18
+ private locales: ProfileLocales;
19
+
20
+ constructor(config: EmailConfig, locales: ProfileLocales) {
21
+ this.ses = new AwsSes({ region: 'eu-west-1' });
22
+ this.config = config;
23
+ this.locales = locales;
24
+ }
25
+
26
+ private getWebsiteUrl() {
27
+ return this.config.websiteUrl || `https://${this.config.domain}`;
28
+ }
29
+
30
+ private render(templateName: string, vars: any) {
31
+ const templatePath = path.resolve(
32
+ this.config.templateDir || path.resolve(__dirname, '../email-templates'),
33
+ `${templateName}.pug`
34
+ );
35
+ return pug.renderFile(templatePath, {
36
+ domain: this.config.domain,
37
+ websiteUrl: this.getWebsiteUrl(),
38
+ primaryBrandColor: this.config.primaryBrandColor,
39
+ logoUrl: this.config.logoUrl || '',
40
+ link: vars.linkHref ? { href: vars.linkHref, label: vars.linkTitle } : undefined,
41
+ ...vars,
42
+ });
43
+ }
44
+
45
+ private getFrom(type: EmailType) {
46
+ const { domain } = this.config;
47
+ const username = this.config.fromEmailUsername;
48
+
49
+ switch (type) {
50
+ case 'auth':
51
+ return `${domain} <${username}@auth.${domain}>`;
52
+ case 'activity':
53
+ return `${domain} <${username}@a.${domain}>`;
54
+ case 'info':
55
+ return `${domain} <${username}@i.${domain}>`;
56
+ case 'admin':
57
+ return `${domain} <${username}@admin.${domain}>`;
58
+ default:
59
+ return `${domain} <${username}@o.${domain}>`;
60
+ }
61
+ }
62
+
63
+ async send(
64
+ to: string,
65
+ subject: string,
66
+ template: string,
67
+ vars: any,
68
+ type: EmailType = 'other'
69
+ ) {
70
+ const html = this.render(template, vars);
71
+ const from = this.getFrom(type);
72
+
73
+ return this.ses.send({
74
+ Destination: { ToAddresses: [to] },
75
+ Message: {
76
+ Subject: { Data: subject, Charset: 'UTF-8' },
77
+ Body: { Html: { Data: html, Charset: 'UTF-8' } },
78
+ },
79
+ Source: from,
80
+ });
81
+ }
82
+
83
+ async sendToken(
84
+ user: AuthUser,
85
+ type: 'confirmation' | 'recovery',
86
+ token: string
87
+ ) {
88
+ if (!user.email) return false;
89
+
90
+ const isConfirm = type === 'confirmation';
91
+ const emailLocales = isConfirm
92
+ ? this.locales.email.confirmEmail
93
+ : this.locales.email.forgotPassword;
94
+
95
+ const template = isConfirm
96
+ ? ProfileEmailTemplate.CONFIRM_EMAIL
97
+ : ProfileEmailTemplate.FORGOT_PASSWORD;
98
+
99
+ return this.send(
100
+ user.email,
101
+ emailLocales.subject,
102
+ template,
103
+ {
104
+ header: emailLocales.header,
105
+ text: emailLocales.text,
106
+ linkTitle: emailLocales.linkTitle,
107
+ linkHref: `${this.getWebsiteUrl()}/${type}?token=${token}&email=${user.email
108
+ }`,
109
+ confirmationToken: token,
110
+ },
111
+ 'auth'
112
+ );
113
+ }
114
+
115
+ async sendTemporaryPassword(user: AuthUser, tempPassword: string) {
116
+ if (!user.email) return false;
117
+
118
+ const emailLocales = this.locales.email.tempPassword;
119
+
120
+ return this.send(
121
+ user.email,
122
+ emailLocales.subject,
123
+ ProfileEmailTemplate.TEMP_PASSWORD,
124
+ {
125
+ header: emailLocales.header.replace('{{tempPassword}}', tempPassword),
126
+ text: emailLocales.text,
127
+ linkTitle: emailLocales.linkTitle,
128
+ linkHref: this.getWebsiteUrl(),
129
+ },
130
+ 'auth'
131
+ );
132
+ }
133
+
134
+ /* Generic method for any custom project emails */
135
+ async sendGenericEmail(
136
+ user: AuthUser,
137
+ subject: string,
138
+ text: string,
139
+ type: EmailType = 'info',
140
+ vars: any = {}
141
+ ) {
142
+ if (!user.email) return false;
143
+
144
+ return this.send(
145
+ user.email,
146
+ subject,
147
+ ProfileEmailTemplate.GENERIC_NOTIFICATION,
148
+ {
149
+ header: subject,
150
+ text,
151
+ linkTitle: 'Go to Website',
152
+ linkHref: this.getWebsiteUrl(),
153
+ ...vars,
154
+ },
155
+ type
156
+ );
59
157
  }
60
- }
61
-
62
- async send(
63
- to: string,
64
- subject: string,
65
- template: string,
66
- vars: any,
67
- type: EmailType = 'other'
68
- ) {
69
- const html = this.render(template, vars);
70
- const from = this.getFrom(type);
71
-
72
- return this.ses.send({
73
- Destination: { ToAddresses: [to] },
74
- Message: {
75
- Subject: { Data: subject, Charset: 'UTF-8' },
76
- Body: { Html: { Data: html, Charset: 'UTF-8' } },
77
- },
78
- Source: from,
79
- });
80
- }
81
-
82
- async sendToken(
83
- user: AuthUser,
84
- type: 'confirmation' | 'recovery',
85
- token: string
86
- ) {
87
- if (!user.email) return false;
88
-
89
- const isConfirm = type === 'confirmation';
90
- const emailLocales = isConfirm
91
- ? this.locales.email.confirmEmail
92
- : this.locales.email.forgotPassword;
93
-
94
- const template = isConfirm
95
- ? ProfileEmailTemplate.CONFIRM_EMAIL
96
- : ProfileEmailTemplate.FORGOT_PASSWORD;
97
-
98
- return this.send(
99
- user.email,
100
- emailLocales.subject,
101
- template,
102
- {
103
- header: emailLocales.header,
104
- text: emailLocales.text,
105
- linkTitle: emailLocales.linkTitle,
106
- linkHref: `${this.getWebsiteUrl()}/${type}?token=${token}&email=${
107
- user.email
108
- }`,
109
- confirmationToken: token,
110
- },
111
- 'auth'
112
- );
113
- }
114
-
115
- async sendTemporaryPassword(user: AuthUser, tempPassword: string) {
116
- if (!user.email) return false;
117
-
118
- const emailLocales = this.locales.email.tempPassword;
119
-
120
- return this.send(
121
- user.email,
122
- emailLocales.subject,
123
- ProfileEmailTemplate.TEMP_PASSWORD,
124
- {
125
- header: emailLocales.header.replace('{{tempPassword}}', tempPassword),
126
- text: emailLocales.text,
127
- linkTitle: emailLocales.linkTitle,
128
- linkHref: this.getWebsiteUrl(),
129
- },
130
- 'auth'
131
- );
132
- }
133
-
134
- /* Generic method for any custom project emails */
135
- async sendGenericEmail(
136
- user: AuthUser,
137
- subject: string,
138
- text: string,
139
- type: EmailType = 'info',
140
- vars: any = {}
141
- ) {
142
- if (!user.email) return false;
143
-
144
- return this.send(
145
- user.email,
146
- subject,
147
- ProfileEmailTemplate.GENERIC_NOTIFICATION,
148
- {
149
- header: subject,
150
- text,
151
- linkTitle: 'Go to Website',
152
- linkHref: this.getWebsiteUrl(),
153
- ...vars,
154
- },
155
- type
156
- );
157
- }
158
158
  }
@@ -35,7 +35,7 @@ export interface EmailConfig extends ProfileAuthConfig {
35
35
  primaryBrandColor: string;
36
36
  logoUrl?: string;
37
37
  fromEmailUsername?: string; // e.g. "support" or "info"
38
- templateDir: string;
38
+ templateDir?: string;
39
39
  isOnlineSeconds: number;
40
40
  isOnlineRecentlySeconds: number;
41
41
  }