|
1
|
import { Logger, NotificationTypes } from "@medusajs/framework/types" |
|
2
|
import { AbstractNotificationProviderService } from "@medusajs/framework/utils" |
|
3
|
import { MedusaCloudEmailOptions } from "@types" |
|
4
|
|
|
5
|
export class MedusaCloudEmailNotificationProvider extends AbstractNotificationProviderService { |
|
6
|
static identifier = "notification-medusa-cloud-email" |
|
7
|
protected options_: MedusaCloudEmailOptions |
|
8
|
protected logger_: Logger |
|
9
|
|
|
10
|
constructor({}, options: MedusaCloudEmailOptions) { |
|
11
|
super() |
|
12
|
|
|
13
|
this.options_ = options |
|
14
|
} |
|
15
|
|
|
16
|
async send( |
|
17
|
notification: NotificationTypes.ProviderSendNotificationDTO |
|
18
|
): Promise<NotificationTypes.ProviderSendNotificationResultsDTO> { |
|
19
|
const headers = { |
|
20
|
"Content-Type": "application/json", |
|
21
|
Authorization: `Basic ${this.options_.api_key}`, |
|
22
|
} |
|
23
|
|
|
24
|
if (this.options_.sandbox_handle) { |
|
25
|
headers["x-medusa-sandbox-handle"] = this.options_.sandbox_handle |
|
26
|
} |
|
27
|
|
|
28
|
if (this.options_.environment_handle) { |
|
29
|
headers["x-medusa-environment-handle"] = this.options_.environment_handle |
|
30
|
} |
|
31
|
|
|
32
|
try { |
|
33
|
const response = await fetch(`${this.options_.endpoint}/send`, { |
|
34
|
method: "POST", |
|
35
|
headers, |
|
36
|
body: JSON.stringify({ |
|
37
|
to: notification.to, |
|
38
|
from: notification.from, |
|
39
|
attachments: notification.attachments, |
|
40
|
template: notification.template, |
|
41
|
data: notification.data, |
|
42
|
provider_data: notification.provider_data, |
|
43
|
content: notification.content, |
|
44
|
}), |
|
45
|
}) |
|
46
|
const responseBody = await response.json() |
|
47
|
|
|
48
|
if (!response.ok) { |
|
49
|
throw new Error( |
|
50
|
`Failed to send email: ${response.status} - ${response.statusText}: ${responseBody.message}` |
|
51
|
) |
|
52
|
} |
|
53
|
|
|
54
|
return { id: responseBody.id } |
|
55
|
} catch (error) { |
|
56
|
throw new Error(`Failed to send email: ${error.message}`) |
|
57
|
} |
|
58
|
} |
|
59
|
} |