@unchainedshop/core-enrollments 4.8.17 → 4.8.19

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.
@@ -0,0 +1,2 @@
1
+ import type { MigrationRepository } from '@unchainedshop/mongodb';
2
+ export default function normalizeContactPhone(repository: MigrationRepository): void;
@@ -0,0 +1,27 @@
1
+ import { normalizePhoneNumber } from '@unchainedshop/utils';
2
+ import { EnrollmentsCollection } from "../db/EnrollmentsCollection.js";
3
+ export default function normalizeContactPhone(repository) {
4
+ repository?.register({
5
+ id: 20260625120100,
6
+ name: 'Normalize enrollment.contact.telNumber to E.164 format',
7
+ up: async ({ logger }) => {
8
+ const Enrollments = await EnrollmentsCollection(repository.db);
9
+ const enrollments = await Enrollments.find({ 'contact.telNumber': { $exists: true, $nin: [null, ''] } }, { projection: { _id: true, contact: true, billingAddress: true, countryCode: true } }).toArray();
10
+ let changed = 0;
11
+ let skipped = 0;
12
+ for (const enrollment of enrollments) {
13
+ const current = enrollment.contact?.telNumber;
14
+ const defaultCountry = enrollment.billingAddress?.countryCode || enrollment.countryCode;
15
+ const normalized = normalizePhoneNumber(current, defaultCountry);
16
+ if (!normalized || normalized === current) {
17
+ if (!normalized)
18
+ skipped += 1;
19
+ continue;
20
+ }
21
+ await Enrollments.updateOne({ _id: enrollment._id }, { $set: { 'contact.telNumber': normalized } });
22
+ changed += 1;
23
+ }
24
+ logger?.info(`Normalize enrollment.contact.telNumber: ${changed} updated, ${skipped} left unchanged (unparseable)`);
25
+ },
26
+ });
27
+ }
@@ -8,7 +8,7 @@ export interface EnrollmentQuery {
8
8
  queryString?: string;
9
9
  }
10
10
  export declare const buildFindSelector: ({ queryString, status, userId }: EnrollmentQuery) => mongodb.Filter<Enrollment>;
11
- export declare const configureEnrollmentsModule: ({ db, options: enrollmentOptions, }: ModuleInput<EnrollmentsSettingsOptions>) => Promise<{
11
+ export declare const configureEnrollmentsModule: ({ db, migrationRepository, options: enrollmentOptions, }: ModuleInput<EnrollmentsSettingsOptions>) => Promise<{
12
12
  count: (query: EnrollmentQuery) => Promise<number>;
13
13
  openEnrollmentWithProduct: ({ productId }: {
14
14
  productId: string;
@@ -35,7 +35,7 @@ export declare const configureEnrollmentsModule: ({ db, options: enrollmentOptio
35
35
  delete: (enrollmentId: string) => Promise<number>;
36
36
  removeEnrollmentPeriodByOrderId: (enrollmentId: string, orderId: string) => Promise<mongodb.WithId<Enrollment> | null>;
37
37
  updateBillingAddress: (enrollmentId: string, fieldValue: Address) => Promise<mongodb.WithId<Enrollment> | null>;
38
- updateContact: (enrollmentId: string, fieldValue: Contact) => Promise<mongodb.WithId<Enrollment> | null>;
38
+ updateContact: (enrollmentId: string, contact: Contact) => Promise<mongodb.WithId<Enrollment> | null>;
39
39
  updateContext: (enrollmentId: string, fieldValue: any) => Promise<mongodb.WithId<Enrollment> | null>;
40
40
  updateDelivery: (enrollmentId: string, fieldValue: {
41
41
  deliveryProviderId?: string;
@@ -1,15 +1,24 @@
1
- import { SortDirection } from '@unchainedshop/utils';
1
+ import { SortDirection, normalizePhoneNumber } from '@unchainedshop/utils';
2
2
  import { EnrollmentStatus, } from "../db/EnrollmentsCollection.js";
3
3
  import { emit, registerEvents } from '@unchainedshop/events';
4
4
  import { generateDbFilterById, buildSortOptions, mongodb, generateDbObjectId, } from '@unchainedshop/mongodb';
5
5
  import { EnrollmentsCollection } from "../db/EnrollmentsCollection.js";
6
6
  import { enrollmentsSettings } from "../enrollments-settings.js";
7
+ import normalizeContactPhoneMigration from "../migrations/20260625120100-normalize-contact-phone.js";
7
8
  const ENROLLMENT_EVENTS = [
8
9
  'ENROLLMENT_ADD_PERIOD',
9
10
  'ENROLLMENT_CREATE',
10
11
  'ENROLLMENT_REMOVE',
11
12
  'ENROLLMENT_UPDATE',
12
13
  ];
14
+ const normalizeContactPhone = (contact, defaultCountry) => {
15
+ if (!contact?.telNumber)
16
+ return contact;
17
+ return {
18
+ ...contact,
19
+ telNumber: normalizePhoneNumber(contact.telNumber, defaultCountry) || contact.telNumber,
20
+ };
21
+ };
13
22
  export const buildFindSelector = ({ queryString, status, userId }) => {
14
23
  const selector = {
15
24
  deleted: null,
@@ -23,7 +32,8 @@ export const buildFindSelector = ({ queryString, status, userId }) => {
23
32
  }
24
33
  return selector;
25
34
  };
26
- export const configureEnrollmentsModule = async ({ db, options: enrollmentOptions = {}, }) => {
35
+ export const configureEnrollmentsModule = async ({ db, migrationRepository, options: enrollmentOptions = {}, }) => {
36
+ normalizeContactPhoneMigration(migrationRepository);
27
37
  registerEvents(ENROLLMENT_EVENTS);
28
38
  enrollmentsSettings.configureSettings(enrollmentOptions);
29
39
  const Enrollments = await EnrollmentsCollection(db);
@@ -153,6 +163,7 @@ export const configureEnrollmentsModule = async ({ db, options: enrollmentOption
153
163
  periods: [],
154
164
  currencyCode,
155
165
  countryCode,
166
+ contact: normalizeContactPhone(enrollmentData.contact, enrollmentData.billingAddress?.countryCode || countryCode),
156
167
  configuration: enrollmentData.configuration || [],
157
168
  log: [],
158
169
  });
@@ -183,7 +194,13 @@ export const configureEnrollmentsModule = async ({ db, options: enrollmentOption
183
194
  }, { returnDocument: 'after' });
184
195
  },
185
196
  updateBillingAddress: updateEnrollmentField('billingAddress'),
186
- updateContact: updateEnrollmentField('contact'),
197
+ updateContact: async (enrollmentId, contact) => {
198
+ const existing = await Enrollments.findOne(generateDbFilterById(enrollmentId), {
199
+ projection: { billingAddress: true, countryCode: true },
200
+ });
201
+ const defaultCountry = existing?.billingAddress?.countryCode || existing?.countryCode;
202
+ return updateEnrollmentField('contact')(enrollmentId, normalizeContactPhone(contact, defaultCountry));
203
+ },
187
204
  updateContext: updateEnrollmentField('meta'),
188
205
  updateDelivery: updateEnrollmentField('delivery'),
189
206
  updatePayment: updateEnrollmentField('payment'),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unchainedshop/core-enrollments",
3
3
  "description": "Subscription and recurring billing module for the Unchained Engine",
4
- "version": "4.8.17",
4
+ "version": "4.8.19",
5
5
  "main": "lib/enrollments-index.js",
6
6
  "types": "lib/enrollments-index.d.ts",
7
7
  "type": "module",
@@ -40,7 +40,7 @@
40
40
  "@unchainedshop/utils": "^4.8.12"
41
41
  },
42
42
  "devDependencies": {
43
- "@types/node": "^25.0.0",
43
+ "@types/node": "^26.2.0",
44
44
  "typescript": "^5.8.3"
45
45
  }
46
46
  }