1 import {
2 CreateNotificationDTO,
3 INotificationModuleService,
4 } from "@medusajs/framework/types"
5 import {
6 CommonEvents,
7 composeMessage,
8 Module,
9 Modules,
10 NotificationEvents,
11 NotificationStatus,
12 } from "@medusajs/framework/utils"
13 import {
14 MockEventBusService,
15 moduleIntegrationTestRunner,
16 } from "@medusajs/test-utils"
17 import { NotificationModuleService } from "@services"
18 import { resolve } from "path"
19
20 let moduleOptions = {
21 providers: [
22 {
23 resolve: resolve(
24 process.cwd() +
25 "/integration-tests/__fixtures__/providers/default-provider"
26 ),
27 id: "test-provider",
28 options: {
29 name: "Test provider",
30 channels: ["email"],
31 },
32 },
33 ],
34 }
35
36 jest.setTimeout(30000)
37
38 moduleIntegrationTestRunner<INotificationModuleService>({
39 moduleName: Modules.NOTIFICATION,
40 moduleOptions,
41 testSuite: ({ service }) =>
42 describe("Notification Module Service", () => {
43 let eventBusEmitSpy
44
45 beforeEach(() => {
46 eventBusEmitSpy = jest.spyOn(MockEventBusService.prototype, "emit")
47 })
48
49 afterEach(() => {
50 eventBusEmitSpy.mockClear()
51 })
52
53 it(`should export the appropriate linkable configuration`, () => {
54 const linkable = Module(Modules.NOTIFICATION, {
55 service: NotificationModuleService,
56 }).linkable
57
58 expect(Object.keys(linkable)).toEqual(["notification"])
59
60 Object.keys(linkable).forEach((key) => {
61 delete linkable[key].toJSON
62 })
63
64 expect(linkable).toEqual({
65 notification: {
66 id: {
67 linkable: "notification_id",
68 entity: "Notification",
69 primaryKey: "id",
70 serviceName: "notification",
71 field: "notification",
72 },
73 },
74 })
75 })
76
77 it("should send a notification and stores it in the database", async () => {
78 const notification = {
79 to: "admin@medusa.com",
80 from: "sender@verified.com",
81 template: "some-template",
82 channel: "email",
83 data: { link: "http://test.com" },
84 provider_data: { cc: "cc@test.com" },
85 } as CreateNotificationDTO
86
87 const result = await service.createNotifications(notification)
88 const retrieved = await service.retrieveNotification(result.id)
89
90 const expected = {
91 to: "admin@medusa.com",
92 from: "sender@verified.com",
93 template: "some-template",
94 channel: "email",
95 data: { link: "http://test.com" },
96 provider_data: { cc: "cc@test.com" },
97 provider_id: "test-provider",
98 external_id: "external_id",
99 status: NotificationStatus.SUCCESS,
100 }
101 expect(result).toEqual(expect.objectContaining(expected))
102 expect(retrieved).toEqual(expect.objectContaining(expected))
103 })
104
105 it("should send a notification and don't store the content in the database", async () => {
106 const notification = {
107 to: "admin@medusa.com",
108 template: "signup-template",
109 channel: "email",
110 data: {},
111 content: {
112 html: "<p>Welcome to medusa</p>",
113 },
114 }
115
116 const result = await service.createNotifications(notification)
117 const dbEntry = await service.retrieveNotification(result.id)
118
119 expect(dbEntry).toEqual(
120 expect.objectContaining({
121 provider_id: "test-provider",
122 external_id: "external_id",
123 status: NotificationStatus.SUCCESS,
124 template: "signup-template",
125 })
126 )
127 expect(dbEntry).not.toHaveProperty("content")
128 })
129
130 it("should send a notification without a template", async () => {
131 const notification = {
132 to: "admin@medusa.com",
133 channel: "email",
134 content: {
135 html: "<p>Welcome to medusa</p>",
136 },
137 }
138
139 const result = await service.createNotifications(notification)
140 const dbEntry = await service.retrieveNotification(result.id)
141
142 expect(dbEntry).toEqual(
143 expect.objectContaining({
144 provider_id: "test-provider",
145 external_id: "external_id",
146 status: NotificationStatus.SUCCESS,
147 template: null,
148 })
149 )
150 expect(dbEntry).not.toHaveProperty("content")
151 })
152
153 it("should emit an event when a notification is created", async () => {
154 const notification = {
155 to: "admin@medusa.com",
156 template: "some-template",
157 channel: "email",
158 data: {},
159 }
160
161 const result = await service.createNotifications(notification)
162
163 expect(eventBusEmitSpy).toHaveBeenCalledTimes(1)
164 expect(eventBusEmitSpy.mock.calls[0][0]).toHaveLength(2)
165 expect(eventBusEmitSpy).toHaveBeenNthCalledWith(
166 1,
167 expect.arrayContaining([
168 composeMessage(NotificationEvents.NOTIFICATION_CREATED, {
169 data: { id: result.id },
170 object: "notification",
171 source: Modules.NOTIFICATION,
172 action: CommonEvents.CREATED,
173 }),
174 composeMessage(NotificationEvents.NOTIFICATION_UPDATED, {
175 data: { id: result.id },
176 object: "notification",
177 source: Modules.NOTIFICATION,
178 action: CommonEvents.UPDATED,
179 }),
180 ]),
181 {
182 internal: true,
183 }
184 )
185 })
186
187 it("should ensures the same notification is not sent twice", async () => {
188 const notification = {
189 to: "admin@medusa.com",
190 template: "some-template",
191 channel: "email",
192 data: {},
193 idempotency_key: "idempotency-key",
194 }
195
196 const result = await service.createNotifications(notification)
197 expect(result).toEqual(
198 expect.objectContaining({
199 provider_id: "test-provider",
200 external_id: "external_id",
201 status: NotificationStatus.SUCCESS,
202 })
203 )
204
205 const secondResult = await service.createNotifications(notification)
206 expect(secondResult).toBe(undefined)
207 })
208
209 it("should manage the status of multiple notification properly in any scenarios", async () => {
210 const notification1 = {
211 to: "admin@medusa.com",
212 template: "some-template",
213 channel: "email",
214 data: {},
215 idempotency_key: "idempotency-key",
216 }
217
218 const notification2 = {
219 to: "0000000000",
220 template: "some-template",
221 channel: "sms",
222 data: {},
223 idempotency_key: "idempotency-key-2",
224 }
225
226 const notification3 = {
227 to: "admin@medusa.com",
228 template: "some-template",
229 channel: "email",
230 data: {},
231 idempotency_key: "idempotency-key-3",
232 }
233
234 const notification4 = {
235 to: "fail",
236 template: "some-template",
237 channel: "email",
238 data: {},
239 idempotency_key: "idempotency-key-4",
240 }
241
242 const err = await service
243 .createNotifications([
244 notification1,
245 notification2,
246 notification3,
247 notification4,
248 ])
249 .catch((e) => e)
250
251 const notifications = await service.listNotifications()
252
253 expect(notifications).toHaveLength(4)
254
255 const notification1Result = notifications.find(
256 (n) => n.idempotency_key === "idempotency-key"
257 )!
258 expect(notification1Result.status).toEqual(NotificationStatus.SUCCESS)
259
260 const notification2Result = notifications.find(
261 (n) => n.idempotency_key === "idempotency-key-2"
262 )!
263 expect(notification2Result.status).toEqual(NotificationStatus.FAILURE)
264
265 const notification3Result = notifications.find(
266 (n) => n.idempotency_key === "idempotency-key-3"
267 )!
268 expect(notification3Result.status).toEqual(NotificationStatus.SUCCESS)
269
270 const notification4Result = notifications.find(
271 (n) => n.idempotency_key === "idempotency-key-4"
272 )!
273 expect(notification4Result.status).toEqual(NotificationStatus.FAILURE)
274
275 expect(err).toBeTruthy()
276 expect(err.message).toEqual(
277 `Could not find a notification provider for channel: sms for notification id ${notification2Result.id}
278 Failed to send notification with id ${notification4Result.id}:
279 Failed to send notification`
280 )
281 })
282 }),
283 })