Skip to content

Commit f67bfb9

Browse files
authored
add from to notification model (medusajs#14102)
1 parent 0cb1202 commit f67bfb9

File tree

9 files changed

+83
-15
lines changed

9 files changed

+83
-15
lines changed

.changeset/yummy-squids-sniff.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@medusajs/notification": patch
3+
"@medusajs/core-flows": patch
4+
"@medusajs/types": patch
5+
---
6+
7+
add from to notification model

packages/core/core-flows/src/notification/steps/send-notifications.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import type { Attachment, INotificationModuleService, NotificationContent } from "@medusajs/framework/types"
1+
import type {
2+
Attachment,
3+
INotificationModuleService,
4+
NotificationContent,
5+
} from "@medusajs/framework/types"
26
import { Modules } from "@medusajs/framework/utils"
37
import { StepResponse, createStep } from "@medusajs/framework/workflows-sdk"
48

@@ -11,6 +15,11 @@ export type SendNotificationsStepInput = {
1115
* the channel. For example, the email address for the email channel.
1216
*/
1317
to: string
18+
/**
19+
* The address to send the notification from, depending on
20+
* the channel. For example, the email address for the email channel.
21+
*/
22+
from?: string | null
1423
/**
1524
* The channel to send the notification through. For example, `email`.
1625
*/

packages/core/types/src/notification/common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ export interface FilterableNotificationProps
147147
* Filter based on the recipient of the notification.
148148
*/
149149
to?: string | string[] | OperatorMap<string | string[]>
150+
/**
151+
* Filter based on the recipient of the notification.
152+
*/
153+
from?: string | string[] | OperatorMap<string | string[]>
150154
/**
151155
* Filter based on the channel through which the notification is sent, such as 'email' or 'sms'
152156
*/

packages/core/types/src/notification/mutations.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export interface CreateNotificationDTO {
1111
* The recipient of the notification. It can be email, phone number, or username, depending on the channel.
1212
*/
1313
to: string
14+
/**
15+
* The sender of the notification. It can be email, phone number, or username, depending on the channel.
16+
*/
17+
from?: string | null
1418
/**
1519
* The channel through which the notification is sent, such as `email` or `sms`.
1620
*/

packages/modules/notification/integration-tests/__tests__/notification-module-service/index.spec.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { INotificationModuleService } from "@medusajs/framework/types"
1+
import {
2+
CreateNotificationDTO,
3+
INotificationModuleService,
4+
} from "@medusajs/framework/types"
25
import {
36
CommonEvents,
47
composeMessage,
@@ -74,19 +77,27 @@ moduleIntegrationTestRunner<INotificationModuleService>({
7477
it("should send a notification and stores it in the database", async () => {
7578
const notification = {
7679
80+
7781
template: "some-template",
7882
channel: "email",
7983
data: {},
80-
}
84+
} as CreateNotificationDTO
8185

8286
const result = await service.createNotifications(notification)
83-
expect(result).toEqual(
84-
expect.objectContaining({
85-
provider_id: "test-provider",
86-
external_id: "external_id",
87-
status: NotificationStatus.SUCCESS,
88-
})
89-
)
87+
const retrieved = await service.retrieveNotification(result.id)
88+
89+
const expected = {
90+
91+
92+
template: "some-template",
93+
channel: "email",
94+
data: {},
95+
provider_id: "test-provider",
96+
external_id: "external_id",
97+
status: NotificationStatus.SUCCESS,
98+
}
99+
expect(result).toEqual(expect.objectContaining(expected))
100+
expect(retrieved).toEqual(expect.objectContaining(expected))
90101
})
91102

92103
it("should send a notification and don't store the content in the database", async () => {

packages/modules/notification/integration-tests/__tests__/notification-module-service/medusa-cloud-email.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const successMedusaCloudEmailResponse = {
1414

1515
const testNotification = {
1616
17+
1718
template: "some-template",
1819
channel: "email",
1920
data: {
@@ -50,6 +51,13 @@ moduleIntegrationTestRunner<INotificationModuleService>({
5051
const result = await service.createNotifications(testNotification)
5152
expect(result).toEqual(
5253
expect.objectContaining({
54+
55+
56+
template: "some-template",
57+
channel: "email",
58+
data: {
59+
link: "https://test.com",
60+
},
5361
provider_id: "cloud",
5462
external_id: "external_id_1",
5563
status: NotificationStatus.SUCCESS,
@@ -66,6 +74,7 @@ moduleIntegrationTestRunner<INotificationModuleService>({
6674
)
6775
expect(JSON.parse(request.body)).toEqual({
6876
77+
6978
template: "some-template",
7079
data: {
7180
link: "https://test.com",

packages/modules/notification/src/migrations/.snapshot-medusa-notification.json

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
"constraint": false,
9797
"primary": false,
9898
"unique": false,
99-
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_notification_provider_deleted_at\" ON \"notification_provider\" (deleted_at) WHERE deleted_at IS NULL"
99+
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_notification_provider_deleted_at\" ON \"notification_provider\" (\"deleted_at\") WHERE deleted_at IS NULL"
100100
},
101101
{
102102
"keyName": "notification_provider_pkey",
@@ -133,6 +133,15 @@
133133
"nullable": false,
134134
"mappedType": "text"
135135
},
136+
"from": {
137+
"name": "from",
138+
"type": "text",
139+
"unsigned": false,
140+
"autoincrement": false,
141+
"primary": false,
142+
"nullable": true,
143+
"mappedType": "text"
144+
},
136145
"channel": {
137146
"name": "channel",
138147
"type": "text",
@@ -290,7 +299,7 @@
290299
"constraint": false,
291300
"primary": false,
292301
"unique": false,
293-
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_notification_receiver_id\" ON \"notification\" (receiver_id) WHERE deleted_at IS NULL"
302+
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_notification_receiver_id\" ON \"notification\" (\"receiver_id\") WHERE deleted_at IS NULL"
294303
},
295304
{
296305
"keyName": "IDX_notification_idempotency_key_unique",
@@ -299,7 +308,7 @@
299308
"constraint": false,
300309
"primary": false,
301310
"unique": false,
302-
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_notification_idempotency_key_unique\" ON \"notification\" (idempotency_key) WHERE deleted_at IS NULL"
311+
"expression": "CREATE UNIQUE INDEX IF NOT EXISTS \"IDX_notification_idempotency_key_unique\" ON \"notification\" (\"idempotency_key\") WHERE deleted_at IS NULL"
303312
},
304313
{
305314
"keyName": "IDX_notification_provider_id",
@@ -308,7 +317,7 @@
308317
"constraint": false,
309318
"primary": false,
310319
"unique": false,
311-
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_notification_provider_id\" ON \"notification\" (provider_id) WHERE deleted_at IS NULL"
320+
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_notification_provider_id\" ON \"notification\" (\"provider_id\") WHERE deleted_at IS NULL"
312321
},
313322
{
314323
"keyName": "IDX_notification_deleted_at",
@@ -317,7 +326,7 @@
317326
"constraint": false,
318327
"primary": false,
319328
"unique": false,
320-
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_notification_deleted_at\" ON \"notification\" (deleted_at) WHERE deleted_at IS NULL"
329+
"expression": "CREATE INDEX IF NOT EXISTS \"IDX_notification_deleted_at\" ON \"notification\" (\"deleted_at\") WHERE deleted_at IS NULL"
321330
},
322331
{
323332
"keyName": "notification_pkey",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Migration } from '@mikro-orm/migrations';
2+
3+
export class Migration20251121123942 extends Migration {
4+
5+
override async up(): Promise<void> {
6+
this.addSql(`alter table if exists "notification" add column if not exists "from" text null;`);
7+
}
8+
9+
override async down(): Promise<void> {
10+
this.addSql(`alter table if exists "notification" drop column if exists "from";`);
11+
}
12+
13+
}

packages/modules/notification/src/models/notification.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export const Notification = model.define("notification", {
66
id: model.id({ prefix: "noti" }).primaryKey(),
77
// This can be an email, phone number, or username, depending on the channel.
88
to: model.text().searchable(),
9+
// This can be an email, phone number, or username, depending on the channel.
10+
from: model.text().searchable().nullable(),
911
channel: model.text(),
1012
// The template name in the provider's system.
1113
template: model.text().nullable(),

0 commit comments

Comments
 (0)