@live-change/email-service 0.1.0 → 0.2.4

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 (4) hide show
  1. package/auth.js +95 -3
  2. package/index.js +0 -35
  3. package/package.json +9 -8
  4. package/render.js +3 -2
package/auth.js CHANGED
@@ -14,11 +14,49 @@ const Email = definition.model({
14
14
  }
15
15
  })
16
16
 
17
+ definition.event({
18
+ name: 'emailConnected',
19
+ properties: {
20
+ email: {
21
+ type: String,
22
+ validation: ['nonEmpty', 'email']
23
+ },
24
+ user: {
25
+ type: User,
26
+ validation: ['nonEmpty']
27
+ }
28
+ },
29
+ async execute({ user, email }) {
30
+ await Email.create({
31
+ id: email,
32
+ user, email
33
+ })
34
+ }
35
+ })
36
+
37
+ definition.event({
38
+ name: "emailDisconnected",
39
+ properties: {
40
+ email: {
41
+ type: String,
42
+ validation: ['nonEmpty', 'email']
43
+ },
44
+ user: {
45
+ type: User,
46
+ validation: ['nonEmpty']
47
+ }
48
+ },
49
+ async execute({ user, email }) {
50
+ await Email.delete(email)
51
+ }
52
+ })
53
+
17
54
  definition.trigger({
18
55
  name: "checkNewEmail",
19
56
  properties: {
20
57
  email: {
21
- type: String
58
+ type: String,
59
+ validation: ['nonEmpty', 'email']
22
60
  }
23
61
  },
24
62
  async execute({ email }, context, emit) {
@@ -29,13 +67,67 @@ definition.trigger({
29
67
  })
30
68
 
31
69
  definition.trigger({
32
- name: "createEmail",
70
+ name: "connectEmail",
71
+ properties: {
72
+ email: {
73
+ type: String,
74
+ validation: ['nonEmpty', 'email']
75
+ },
76
+ user: {
77
+ type: User,
78
+ validation: ['nonEmpty']
79
+ }
80
+ },
81
+ async execute({ user, email }, { client, service }, emit) {
82
+ if(!email) throw new Error("no email")
83
+ const emailData = await Email.get(email)
84
+ if(emailData) throw 'taken'
85
+ emit({
86
+ type: 'emailConnected',
87
+ user, email
88
+ })
89
+ return true
90
+ }
91
+ })
92
+
93
+ definition.trigger({
94
+ name: "disconnectEmail",
95
+ properties: {
96
+ email: {
97
+ type: String,
98
+ validation: ['nonEmpty', 'email']
99
+ },
100
+ user: {
101
+ type: User,
102
+ validation: ['nonEmpty']
103
+ }
104
+ },
105
+ async execute({ user, email }, { client, service }, emit) {
106
+ const emailData = await Email.get(email)
107
+ if(!emailData) throw 'notFound'
108
+ emit({
109
+ type: 'emailDisconnected',
110
+ user, email
111
+ })
112
+ return true
113
+ }
114
+ })
115
+
116
+ definition.trigger({
117
+ name: "signInEmail",
33
118
  properties: {
34
119
  email: {
35
120
  type: String
36
121
  }
37
122
  },
38
- async execute(props, context, emit) {
123
+ async execute({ email }, { client, service }, emit) {
124
+ const emailData = await Email.get(email)
125
+ if(!emailData) throw 'emailNotFound'
126
+ const { user } = emailData
127
+ return service.trigger({
128
+ type: 'signIn',
129
+ user, session
130
+ })
39
131
  }
40
132
  })
41
133
 
package/index.js CHANGED
@@ -9,39 +9,4 @@ definition.processor(function(service, app) {
9
9
  service.validators.email = require('./emailValidator.js')
10
10
  })
11
11
 
12
- definition.action({
13
- name: "sendContactFormMail",
14
- properties: {
15
- from: { type: String, validation: ['nonEmpty'] },
16
- name: { type: String, validation: ['nonEmpty']},
17
- subject: { type: String, validation: ['nonEmpty'] },
18
- text: { type: String, validation: ['nonEmpty'] },
19
- html: { type: String },
20
- },
21
- async execute({ from, name, subject, text, html }, { client, service }, emit) {
22
- if(!html) {
23
- const encodedStr = text.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
24
- return '&#'+i.charCodeAt(0)+';'
25
- })
26
- const multiline = encodedStr.replace(/\n/gi, /*'↵*/'<br>')
27
- const withLinks = multiline.replace(
28
- /(?![^<]*>|[^<>]*<\/)((https?:)\/\/[a-z0-9&#%=.\/?_,-]+)/gi, '<a href="$1" target="_blank">$1</a>')
29
- html = withLinks
30
- }
31
-
32
- await service.trigger({
33
- type:"sendEmailMessage",
34
- email: {
35
- from: `${name} <${process.env.CONTACT_FORM_FROM_EMAIL}>`,
36
- to: `${ process.env.CONTACT_FORM_TARGET_NAME} <${process.env.CONTACT_FORM_TARGET_EMAIL}>`,
37
- subject: subject,
38
- text,
39
- html,
40
- replyTo: `${name} <${from}>`
41
- }
42
- })
43
- }
44
- })
45
-
46
-
47
12
  module.exports = definition
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/email-service",
3
- "version": "0.1.0",
3
+ "version": "0.2.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -8,13 +8,13 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/live-change/email-service.git"
11
+ "url": "git+https://github.com/live-change/live-change-services.git"
12
12
  },
13
13
  "license": "MIT",
14
14
  "bugs": {
15
- "url": "https://github.com/live-change/email-service/issues"
15
+ "url": "https://github.com/live-change/live-change-services/issues"
16
16
  },
17
- "homepage": "https://github.com/live-change/email-service",
17
+ "homepage": "https://github.com/live-change/live-change-services",
18
18
  "author": {
19
19
  "email": "michal@laszczewski.pl",
20
20
  "name": "Michał Łaszczewski",
@@ -22,10 +22,11 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "@live-change/framework": "^0.5.7",
25
- "nodemailer": "^6.7.2",
26
25
  "got": "^11.8.3",
27
- "jsdom": "^18.1.1",
26
+ "html-to-text": "8.1.0",
28
27
  "inline-css": "3.0.0",
29
- "html-to-text": "8.1.0"
30
- }
28
+ "jsdom": "^18.1.1",
29
+ "nodemailer": "^6.7.2"
30
+ },
31
+ "gitHead": "9b7bd2322b9d6285600d3cf6ed89473eafa530c2"
31
32
  }
package/render.js CHANGED
@@ -8,8 +8,6 @@ const { URL } = require('url')
8
8
  const definition = require('./definition.js')
9
9
  const config = definition.config
10
10
 
11
- const baseUrl = `http://${config.ssrHost||process.env.SSR_HOST||'localhost'}`+
12
- `:${config.ssrHost||process.env.SSR_PORT||'8001'}`
13
11
  const publicDir = config.publicDir || 'front/public/'
14
12
 
15
13
  function processElement(element, images) {
@@ -46,6 +44,9 @@ function processElement(element, images) {
46
44
  }
47
45
 
48
46
  async function renderEmail(data) {
47
+ const baseUrl = `http://${config.ssrHost||process.env.SSR_HOST||'localhost'}`+
48
+ `:${config.ssrPort||process.env.SSR_PORT||'8001'}`
49
+
49
50
  const encodedData = encodeURIComponent(JSON.stringify(data))
50
51
  const url = `${baseUrl}/_email/${data.action}/${data.contact}/${encodedData}`
51
52
  console.log("RENDER EMAIL", data, "URL", url)