|
1 |
| -import { expect } from 'chai'; |
2 |
| -import { NestFactory } from '@nestjs/core'; |
3 |
| -import { |
4 |
| - SubscriberPreferenceRepository, |
5 |
| - NotificationTemplateRepository, |
6 |
| - PreferenceLevelEnum, |
7 |
| - PreferencesRepository, |
8 |
| -} from '@novu/dal'; |
9 |
| -import { UpsertPreferences } from '@novu/application-generic'; |
10 |
| -import { DEFAULT_WORKFLOW_PREFERENCES, PreferencesTypeEnum } from '@novu/shared'; |
11 |
| -import { UserSession } from '@novu/testing'; |
12 |
| -import { INestApplication } from '@nestjs/common'; |
13 |
| -import { AppModule } from '../../src/app.module'; |
14 |
| -import { preferenceCentralization } from './preference-centralization-migration'; |
15 |
| - |
16 | 1 | describe('Preference Centralization Migration', function () {
|
17 |
| - let app: INestApplication; |
18 |
| - let session: UserSession; |
19 |
| - let subscriberPreferenceRepository: SubscriberPreferenceRepository; |
20 |
| - let notificationTemplateRepository: NotificationTemplateRepository; |
21 |
| - let preferenceRepository: PreferencesRepository; |
22 |
| - let upsertPreferences: UpsertPreferences; |
23 |
| - |
24 |
| - before(async () => { |
25 |
| - app = await NestFactory.create(AppModule, { logger: false }); |
26 |
| - subscriberPreferenceRepository = app.get(SubscriberPreferenceRepository); |
27 |
| - notificationTemplateRepository = app.get(NotificationTemplateRepository); |
28 |
| - preferenceRepository = app.get(PreferencesRepository); |
29 |
| - upsertPreferences = app.get(UpsertPreferences); |
30 |
| - }); |
31 |
| - |
32 |
| - beforeEach(async () => { |
33 |
| - session = new UserSession(); |
34 |
| - await session.initialize(); |
35 |
| - |
36 |
| - // Clean up the repositories before each test |
37 |
| - await subscriberPreferenceRepository._model.deleteMany({}); |
38 |
| - await notificationTemplateRepository._model.deleteMany({}); |
39 |
| - await preferenceRepository._model.deleteMany({}); |
40 |
| - }); |
41 |
| - |
42 |
| - after(async () => { |
43 |
| - await app.close(); |
44 |
| - }); |
45 |
| - |
46 |
| - it('should migrate subscriber global preferences', async function () { |
47 |
| - await subscriberPreferenceRepository.create({ |
48 |
| - _subscriberId: session.subscriberId, |
49 |
| - _environmentId: session.environment._id, |
50 |
| - _organizationId: session.organization._id, |
51 |
| - level: PreferenceLevelEnum.GLOBAL, |
52 |
| - channels: { email: true, sms: true }, |
53 |
| - }); |
54 |
| - |
55 |
| - await preferenceCentralization(); |
56 |
| - |
57 |
| - const updatedPreferences = await preferenceRepository.find({ |
58 |
| - _environmentId: session.environment._id, |
59 |
| - _organizationId: session.organization._id, |
60 |
| - }); |
61 |
| - expect(updatedPreferences.length).to.equal(1); |
62 |
| - expect(updatedPreferences[0].type).to.equal(PreferencesTypeEnum.SUBSCRIBER_GLOBAL); |
63 |
| - expect(updatedPreferences[0].preferences).to.deep.equal({ |
64 |
| - all: { enabled: true, readOnly: false }, |
65 |
| - channels: { |
66 |
| - push: {}, |
67 |
| - chat: {}, |
68 |
| - in_app: {}, |
69 |
| - email: { enabled: true }, |
70 |
| - sms: { enabled: true }, |
71 |
| - }, |
72 |
| - }); |
73 |
| - }); |
74 |
| - |
75 |
| - it('should migrate subscriber workflow preferences', async function () { |
76 |
| - await subscriberPreferenceRepository.create({ |
77 |
| - _subscriberId: session.subscriberId, |
78 |
| - _templateId: NotificationTemplateRepository.createObjectId(), |
79 |
| - _environmentId: session.environment._id, |
80 |
| - _organizationId: session.organization._id, |
81 |
| - level: PreferenceLevelEnum.TEMPLATE, |
82 |
| - channels: { push: true }, |
83 |
| - }); |
84 |
| - |
85 |
| - await preferenceCentralization(); |
86 |
| - |
87 |
| - const updatedPreferences = await preferenceRepository.find({ |
88 |
| - _environmentId: session.environment._id, |
89 |
| - _organizationId: session.organization._id, |
90 |
| - }); |
91 |
| - expect(updatedPreferences.length).to.equal(1); |
92 |
| - expect(updatedPreferences[0].type).to.equal(PreferencesTypeEnum.SUBSCRIBER_WORKFLOW); |
93 |
| - expect(updatedPreferences[0].preferences).to.deep.equal({ |
94 |
| - all: { enabled: true, readOnly: false }, |
95 |
| - channels: { |
96 |
| - push: { enabled: true }, |
97 |
| - chat: {}, |
98 |
| - email: {}, |
99 |
| - in_app: {}, |
100 |
| - sms: {}, |
101 |
| - }, |
102 |
| - }); |
103 |
| - }); |
104 |
| - |
105 |
| - it('should migrate workflows with preference settings and critical flag true', async function () { |
106 |
| - await notificationTemplateRepository.create({ |
107 |
| - _creatorId: session.user._id, |
108 |
| - _environmentId: session.environment._id, |
109 |
| - _organizationId: session.organization._id, |
110 |
| - critical: true, |
111 |
| - preferenceSettings: { email: true, sms: false, push: true, in_app: true, chat: true }, |
112 |
| - }); |
113 |
| - |
114 |
| - await preferenceCentralization(); |
115 |
| - |
116 |
| - const updatedPreferences = await preferenceRepository.find({ |
117 |
| - _environmentId: session.environment._id, |
118 |
| - _organizationId: session.organization._id, |
119 |
| - }); |
120 |
| - expect(updatedPreferences.length).to.equal(2); |
121 |
| - const workflowPreference = updatedPreferences.find( |
122 |
| - (preference) => preference.type === PreferencesTypeEnum.WORKFLOW_RESOURCE |
123 |
| - ); |
124 |
| - const userPreference = updatedPreferences.find( |
125 |
| - (preference) => preference.type === PreferencesTypeEnum.USER_WORKFLOW |
126 |
| - ); |
127 |
| - |
128 |
| - expect(workflowPreference?.preferences).to.deep.equal(DEFAULT_WORKFLOW_PREFERENCES); |
129 |
| - expect(userPreference?.preferences).to.deep.equal({ |
130 |
| - all: { enabled: true, readOnly: true }, |
131 |
| - channels: { |
132 |
| - push: { enabled: true }, |
133 |
| - chat: { enabled: true }, |
134 |
| - email: { enabled: true }, |
135 |
| - in_app: { enabled: true }, |
136 |
| - sms: { enabled: false }, |
137 |
| - }, |
138 |
| - }); |
139 |
| - }); |
140 |
| - |
141 |
| - it('should migrate workflows with preference settings and critical flag false', async function () { |
142 |
| - await notificationTemplateRepository.create({ |
143 |
| - _creatorId: session.user._id, |
144 |
| - _environmentId: session.environment._id, |
145 |
| - _organizationId: session.organization._id, |
146 |
| - critical: false, |
147 |
| - preferenceSettings: { email: true, sms: false, push: true, in_app: true, chat: true }, |
148 |
| - }); |
149 |
| - |
150 |
| - await preferenceCentralization(); |
151 |
| - |
152 |
| - const updatedPreferences = await preferenceRepository.find({ |
153 |
| - _environmentId: session.environment._id, |
154 |
| - _organizationId: session.organization._id, |
155 |
| - }); |
156 |
| - expect(updatedPreferences.length).to.equal(2); |
157 |
| - const workflowPreference = updatedPreferences.find( |
158 |
| - (preference) => preference.type === PreferencesTypeEnum.WORKFLOW_RESOURCE |
159 |
| - ); |
160 |
| - const userPreference = updatedPreferences.find( |
161 |
| - (preference) => preference.type === PreferencesTypeEnum.USER_WORKFLOW |
162 |
| - ); |
163 |
| - |
164 |
| - expect(workflowPreference?.preferences).to.deep.equal(DEFAULT_WORKFLOW_PREFERENCES); |
165 |
| - expect(userPreference?.preferences).to.deep.equal({ |
166 |
| - all: { enabled: true, readOnly: false }, |
167 |
| - channels: { |
168 |
| - push: { enabled: true }, |
169 |
| - chat: { enabled: true }, |
170 |
| - email: { enabled: true }, |
171 |
| - in_app: { enabled: true }, |
172 |
| - sms: { enabled: false }, |
173 |
| - }, |
174 |
| - }); |
175 |
| - }); |
176 |
| - |
177 |
| - it('should migrate workflows without preference settings', async function () { |
178 |
| - await notificationTemplateRepository.create({ |
179 |
| - _creatorId: session.user._id, |
180 |
| - _environmentId: session.environment._id, |
181 |
| - _organizationId: session.organization._id, |
182 |
| - }); |
183 |
| - |
184 |
| - await preferenceCentralization(); |
185 |
| - |
186 |
| - const updatedPreferences = await preferenceRepository.find({ |
187 |
| - _environmentId: session.environment._id, |
188 |
| - _organizationId: session.organization._id, |
189 |
| - }); |
190 |
| - expect(updatedPreferences.length).to.equal(2); |
191 |
| - const workflowPreference = updatedPreferences.find( |
192 |
| - (preference) => preference.type === PreferencesTypeEnum.WORKFLOW_RESOURCE |
193 |
| - ); |
194 |
| - const userPreference = updatedPreferences.find( |
195 |
| - (preference) => preference.type === PreferencesTypeEnum.USER_WORKFLOW |
196 |
| - ); |
197 |
| - |
198 |
| - expect(workflowPreference?.preferences).to.deep.equal(DEFAULT_WORKFLOW_PREFERENCES); |
199 |
| - expect(userPreference?.preferences).to.deep.equal(DEFAULT_WORKFLOW_PREFERENCES); |
200 |
| - }); |
| 2 | + /** |
| 3 | + * IMPORTANT: This migration depends on SubscriberPreferencesRepository which is now removed. |
| 4 | + * Please checkout the `v2.1.0` tag and run the migration from there. |
| 5 | + * @see https://github.com/novuhq/novu/releases/tag/v2.1.0 |
| 6 | + */ |
201 | 7 | });
|
0 commit comments