@unchainedshop/core-orders 4.8.16 → 4.8.18
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,27 @@
|
|
|
1
|
+
import { normalizePhoneNumber } from '@unchainedshop/utils';
|
|
2
|
+
import { OrdersCollection } from "../orders-index.js";
|
|
3
|
+
export default function normalizeContactPhone(repository) {
|
|
4
|
+
repository?.register({
|
|
5
|
+
id: 20260625120000,
|
|
6
|
+
name: 'Normalize order.contact.telNumber to E.164 format',
|
|
7
|
+
up: async ({ logger }) => {
|
|
8
|
+
const Orders = await OrdersCollection(repository.db);
|
|
9
|
+
const orders = await Orders.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 order of orders) {
|
|
13
|
+
const current = order.contact?.telNumber;
|
|
14
|
+
const defaultCountry = order.billingAddress?.countryCode || order.countryCode;
|
|
15
|
+
const normalized = normalizePhoneNumber(current, defaultCountry);
|
|
16
|
+
if (!normalized || normalized === current) {
|
|
17
|
+
if (!normalized)
|
|
18
|
+
skipped += 1;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
await Orders.updateOne({ _id: order._id }, { $set: { 'contact.telNumber': normalized } });
|
|
22
|
+
changed += 1;
|
|
23
|
+
}
|
|
24
|
+
logger?.info(`Normalize order.contact.telNumber: ${changed} updated, ${skipped} left unchanged (unparseable)`);
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
2
|
+
import { normalizePhoneNumber } from '@unchainedshop/utils';
|
|
2
3
|
import { generateDbFilterById, generateDbObjectId, mongodb, } from '@unchainedshop/mongodb';
|
|
3
4
|
import { OrderStatus } from "../db/OrdersCollection.js";
|
|
4
5
|
const ORDER_EVENTS = [
|
|
@@ -10,6 +11,14 @@ const ORDER_EVENTS = [
|
|
|
10
11
|
];
|
|
11
12
|
export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
12
13
|
registerEvents(ORDER_EVENTS);
|
|
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
|
return {
|
|
14
23
|
create: async ({ userId, orderNumber, currencyCode, countryCode, billingAddress, contact, context, }) => {
|
|
15
24
|
const { insertedId: orderId } = await Orders.insertOne({
|
|
@@ -17,7 +26,7 @@ export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
|
17
26
|
created: new Date(),
|
|
18
27
|
status: null,
|
|
19
28
|
billingAddress,
|
|
20
|
-
contact,
|
|
29
|
+
contact: normalizeContactPhone(contact, billingAddress?.countryCode || countryCode),
|
|
21
30
|
userId,
|
|
22
31
|
currencyCode,
|
|
23
32
|
countryCode,
|
|
@@ -66,9 +75,13 @@ export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
|
66
75
|
},
|
|
67
76
|
updateContact: async (orderId, contact) => {
|
|
68
77
|
const selector = generateDbFilterById(orderId);
|
|
78
|
+
const existing = await Orders.findOne(selector, {
|
|
79
|
+
projection: { billingAddress: true, countryCode: true },
|
|
80
|
+
});
|
|
81
|
+
const defaultCountry = existing?.billingAddress?.countryCode || existing?.countryCode;
|
|
69
82
|
const order = await Orders.findOneAndUpdate(selector, {
|
|
70
83
|
$set: {
|
|
71
|
-
contact,
|
|
84
|
+
contact: normalizeContactPhone(contact, defaultCountry),
|
|
72
85
|
updated: new Date(),
|
|
73
86
|
},
|
|
74
87
|
}, { returnDocument: 'after' });
|
|
@@ -113,7 +126,14 @@ export const configureOrderModuleMutations = ({ Orders, OrderPositions, }) => {
|
|
|
113
126
|
$set.billingAddress = updates.billingAddress;
|
|
114
127
|
}
|
|
115
128
|
if (updates.contact) {
|
|
116
|
-
|
|
129
|
+
let defaultCountry = updates.billingAddress?.countryCode;
|
|
130
|
+
if (!defaultCountry && updates.contact.telNumber) {
|
|
131
|
+
const existing = await Orders.findOne(generateDbFilterById(orderId), {
|
|
132
|
+
projection: { billingAddress: true, countryCode: true },
|
|
133
|
+
});
|
|
134
|
+
defaultCountry = existing?.billingAddress?.countryCode || existing?.countryCode;
|
|
135
|
+
}
|
|
136
|
+
$set.contact = normalizeContactPhone(updates.contact, defaultCountry);
|
|
117
137
|
}
|
|
118
138
|
if (updates.meta && Object.keys(updates.meta).length > 0) {
|
|
119
139
|
const contextSetters = Object.fromEntries(Object.entries(updates.meta).map(([key, value]) => [`context.${key}`, value]));
|
|
@@ -14,6 +14,7 @@ import { configureOrderModuleMutations } from "./configureOrdersModule-mutations
|
|
|
14
14
|
import { configureOrdersModuleQueries } from "./configureOrdersModule-queries.js";
|
|
15
15
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
16
16
|
import renameCurrencyCode from "../migrations/20250502111800-currency-code.js";
|
|
17
|
+
import normalizeOrderContactPhone from "../migrations/20260625120000-normalize-contact-phone.js";
|
|
17
18
|
const ORDER_EVENTS = [
|
|
18
19
|
'ORDER_CHECKOUT',
|
|
19
20
|
'ORDER_CONFIRMED',
|
|
@@ -22,6 +23,7 @@ const ORDER_EVENTS = [
|
|
|
22
23
|
];
|
|
23
24
|
export const configureOrdersModule = async ({ db, migrationRepository, options: orderOptions = {}, }) => {
|
|
24
25
|
renameCurrencyCode(migrationRepository);
|
|
26
|
+
normalizeOrderContactPhone(migrationRepository);
|
|
25
27
|
registerEvents(ORDER_EVENTS);
|
|
26
28
|
ordersSettings.configureSettings(orderOptions);
|
|
27
29
|
const Orders = await OrdersCollection(db);
|
package/package.json
CHANGED