@unchainedshop/core-orders 4.3.4 → 4.5.0
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.
- package/README.md +137 -3
- package/lib/db/OrderDeliveriesCollection.d.ts +8 -8
- package/lib/db/OrderDeliveriesCollection.js +6 -9
- package/lib/db/OrderDiscountsCollection.d.ts +7 -7
- package/lib/db/OrderDiscountsCollection.js +5 -8
- package/lib/db/OrderPaymentsCollection.d.ts +8 -8
- package/lib/db/OrderPaymentsCollection.js +6 -9
- package/lib/db/OrderPositionsCollection.d.ts +2 -3
- package/lib/db/OrderPositionsCollection.js +1 -3
- package/lib/db/OrdersCollection.d.ts +13 -10
- package/lib/db/OrdersCollection.js +7 -10
- package/lib/migrations/20250502111800-currency-code.d.ts +1 -2
- package/lib/migrations/20250502111800-currency-code.js +1 -2
- package/lib/module/buildFindSelector.d.ts +2 -3
- package/lib/module/buildFindSelector.js +6 -4
- package/lib/module/configureOrderDeliveriesModule.d.ts +2 -3
- package/lib/module/configureOrderDeliveriesModule.js +2 -7
- package/lib/module/configureOrderDiscountsModule.d.ts +1 -2
- package/lib/module/configureOrderDiscountsModule.js +2 -4
- package/lib/module/configureOrderPaymentsModule.d.ts +2 -3
- package/lib/module/configureOrderPaymentsModule.js +2 -9
- package/lib/module/configureOrderPositionsModule.d.ts +10 -3
- package/lib/module/configureOrderPositionsModule.js +48 -4
- package/lib/module/configureOrdersModule-mutations.d.ts +8 -4
- package/lib/module/configureOrdersModule-mutations.js +23 -3
- package/lib/module/configureOrdersModule-queries.d.ts +28 -4
- package/lib/module/configureOrdersModule-queries.js +127 -3
- package/lib/module/configureOrdersModule.d.ts +58 -40
- package/lib/module/configureOrdersModule.js +17 -23
- package/lib/orders-index.d.ts +13 -14
- package/lib/orders-index.js +13 -14
- package/lib/orders-settings.d.ts +1 -2
- package/lib/orders-settings.js +0 -1
- package/package.json +12 -10
- package/lib/db/OrderDeliveriesCollection.d.ts.map +0 -1
- package/lib/db/OrderDeliveriesCollection.js.map +0 -1
- package/lib/db/OrderDiscountsCollection.d.ts.map +0 -1
- package/lib/db/OrderDiscountsCollection.js.map +0 -1
- package/lib/db/OrderPaymentsCollection.d.ts.map +0 -1
- package/lib/db/OrderPaymentsCollection.js.map +0 -1
- package/lib/db/OrderPositionsCollection.d.ts.map +0 -1
- package/lib/db/OrderPositionsCollection.js.map +0 -1
- package/lib/db/OrdersCollection.d.ts.map +0 -1
- package/lib/db/OrdersCollection.js.map +0 -1
- package/lib/migrations/20250502111800-currency-code.d.ts.map +0 -1
- package/lib/migrations/20250502111800-currency-code.js.map +0 -1
- package/lib/module/buildFindSelector.d.ts.map +0 -1
- package/lib/module/buildFindSelector.js.map +0 -1
- package/lib/module/configureOrderDeliveriesModule.d.ts.map +0 -1
- package/lib/module/configureOrderDeliveriesModule.js.map +0 -1
- package/lib/module/configureOrderDiscountsModule.d.ts.map +0 -1
- package/lib/module/configureOrderDiscountsModule.js.map +0 -1
- package/lib/module/configureOrderPaymentsModule.d.ts.map +0 -1
- package/lib/module/configureOrderPaymentsModule.js.map +0 -1
- package/lib/module/configureOrderPositionsModule.d.ts.map +0 -1
- package/lib/module/configureOrderPositionsModule.js.map +0 -1
- package/lib/module/configureOrdersModule-mutations.d.ts.map +0 -1
- package/lib/module/configureOrdersModule-mutations.js.map +0 -1
- package/lib/module/configureOrdersModule-queries.d.ts.map +0 -1
- package/lib/module/configureOrdersModule-queries.js.map +0 -1
- package/lib/module/configureOrdersModule.d.ts.map +0 -1
- package/lib/module/configureOrdersModule.js.map +0 -1
- package/lib/orders-index.d.ts.map +0 -1
- package/lib/orders-index.js.map +0 -1
- package/lib/orders-settings.d.ts.map +0 -1
- package/lib/orders-settings.js.map +0 -1
- package/src/db/OrderDeliveriesCollection.ts +0 -34
- package/src/db/OrderDiscountsCollection.ts +0 -30
- package/src/db/OrderPaymentsCollection.ts +0 -35
- package/src/db/OrderPositionsCollection.ts +0 -32
- package/src/db/OrdersCollection.ts +0 -87
- package/src/migrations/20250502111800-currency-code.ts +0 -18
- package/src/module/buildFindSelector.test.ts +0 -121
- package/src/module/buildFindSelector.ts +0 -49
- package/src/module/configureOrderDeliveriesModule.ts +0 -157
- package/src/module/configureOrderDiscountsModule.ts +0 -95
- package/src/module/configureOrderPaymentsModule.ts +0 -224
- package/src/module/configureOrderPositionsModule.ts +0 -244
- package/src/module/configureOrdersModule-mutations.ts +0 -178
- package/src/module/configureOrdersModule-queries.ts +0 -159
- package/src/module/configureOrdersModule.ts +0 -257
- package/src/orders-index.ts +0 -13
- package/src/orders-settings.ts +0 -45
- package/tsconfig.json +0 -9
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
import { emit, registerEvents } from '@unchainedshop/events';
|
|
2
|
-
import { generateDbFilterById, generateDbObjectId, mongodb } from '@unchainedshop/mongodb';
|
|
3
|
-
import { PricingCalculation } from '@unchainedshop/utils';
|
|
4
|
-
import { OrderPosition } from '../db/OrderPositionsCollection.js';
|
|
5
|
-
|
|
6
|
-
const ORDER_POSITION_EVENTS: string[] = [
|
|
7
|
-
'ORDER_UPDATE_CART_ITEM',
|
|
8
|
-
'ORDER_REMOVE_CART_ITEM',
|
|
9
|
-
'ORDER_EMPTY_CART',
|
|
10
|
-
'ORDER_ADD_PRODUCT',
|
|
11
|
-
];
|
|
12
|
-
|
|
13
|
-
export interface OrderPositionAggregateParams {
|
|
14
|
-
match?: mongodb.Document;
|
|
15
|
-
matchAfterGroup?: mongodb.Document;
|
|
16
|
-
project?: mongodb.Document;
|
|
17
|
-
group?: mongodb.Document;
|
|
18
|
-
addFields?: mongodb.Document;
|
|
19
|
-
sort?: mongodb.Document;
|
|
20
|
-
limit?: number;
|
|
21
|
-
pipeline?: mongodb.Document[];
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const buildFindOrderPositionByIdSelector = (orderPositionId: string, orderId?: string) =>
|
|
25
|
-
generateDbFilterById(
|
|
26
|
-
orderPositionId,
|
|
27
|
-
orderId ? { orderId } : undefined,
|
|
28
|
-
) as mongodb.Filter<OrderPosition>;
|
|
29
|
-
|
|
30
|
-
export const configureOrderPositionsModule = ({
|
|
31
|
-
OrderPositions,
|
|
32
|
-
}: {
|
|
33
|
-
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
34
|
-
}) => {
|
|
35
|
-
registerEvents(ORDER_POSITION_EVENTS);
|
|
36
|
-
|
|
37
|
-
return {
|
|
38
|
-
// Queries
|
|
39
|
-
findOrderPosition: async ({ itemId }: { itemId: string }, options?: mongodb.FindOptions) => {
|
|
40
|
-
return OrderPositions.findOne(buildFindOrderPositionByIdSelector(itemId), options);
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
findOrderPositions: async ({ orderId }: { orderId: string }): Promise<OrderPosition[]> => {
|
|
44
|
-
const positions = OrderPositions.find({ orderId, quantity: { $gt: 0 } });
|
|
45
|
-
return positions.toArray();
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
delete: async (orderPositionId: string) => {
|
|
49
|
-
const selector = buildFindOrderPositionByIdSelector(orderPositionId);
|
|
50
|
-
const orderPosition = await OrderPositions.findOneAndDelete(selector, {});
|
|
51
|
-
await emit('ORDER_REMOVE_CART_ITEM', {
|
|
52
|
-
orderPosition,
|
|
53
|
-
});
|
|
54
|
-
return orderPosition;
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
removePositions: async ({ orderId }: { orderId: string }): Promise<number> => {
|
|
58
|
-
const result = await OrderPositions.deleteMany({ orderId });
|
|
59
|
-
await emit('ORDER_EMPTY_CART', { orderId, count: result.deletedCount });
|
|
60
|
-
return result.deletedCount;
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
updateProductItem: async ({
|
|
64
|
-
orderPositionId,
|
|
65
|
-
quantity,
|
|
66
|
-
configuration,
|
|
67
|
-
}: {
|
|
68
|
-
orderPositionId: string;
|
|
69
|
-
configuration: { key: string; value: string }[] | null;
|
|
70
|
-
quantity: number | null;
|
|
71
|
-
}) => {
|
|
72
|
-
const modifier: any = {
|
|
73
|
-
$set: {
|
|
74
|
-
updated: new Date(),
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
if (quantity !== null) {
|
|
79
|
-
modifier.$set.quantity = quantity;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (configuration !== null) {
|
|
83
|
-
modifier.$set.configuration = configuration;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const updatedOrderPosition = await OrderPositions.findOneAndUpdate(
|
|
87
|
-
{
|
|
88
|
-
_id: orderPositionId,
|
|
89
|
-
},
|
|
90
|
-
modifier,
|
|
91
|
-
{
|
|
92
|
-
returnDocument: 'after',
|
|
93
|
-
},
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
if (!updatedOrderPosition) return null;
|
|
97
|
-
await emit('ORDER_UPDATE_CART_ITEM', {
|
|
98
|
-
orderPosition: updatedOrderPosition,
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
return updatedOrderPosition;
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
removeProductByIdFromAllOpenPositions: async (productId: string): Promise<string[]> => {
|
|
105
|
-
const positions = await OrderPositions.aggregate([
|
|
106
|
-
{
|
|
107
|
-
$match: {
|
|
108
|
-
productId,
|
|
109
|
-
},
|
|
110
|
-
},
|
|
111
|
-
{
|
|
112
|
-
$lookup: {
|
|
113
|
-
from: 'orders',
|
|
114
|
-
localField: 'orderId',
|
|
115
|
-
foreignField: '_id',
|
|
116
|
-
as: 'order',
|
|
117
|
-
},
|
|
118
|
-
},
|
|
119
|
-
{
|
|
120
|
-
$match: {
|
|
121
|
-
'order.status': null,
|
|
122
|
-
},
|
|
123
|
-
},
|
|
124
|
-
]).toArray();
|
|
125
|
-
|
|
126
|
-
const positionIds = positions.map((o) => o._id);
|
|
127
|
-
await OrderPositions.deleteMany({ _id: { $in: positionIds } });
|
|
128
|
-
await Promise.all(
|
|
129
|
-
positions.map(async (orderPosition) => {
|
|
130
|
-
await emit('ORDER_REMOVE_CART_ITEM', {
|
|
131
|
-
orderPosition,
|
|
132
|
-
});
|
|
133
|
-
}),
|
|
134
|
-
);
|
|
135
|
-
|
|
136
|
-
const orderIdsToRecalculate = positions.map((o) => o.orderId);
|
|
137
|
-
return orderIdsToRecalculate;
|
|
138
|
-
},
|
|
139
|
-
|
|
140
|
-
updateScheduling: async (orderPositionId, scheduling) => {
|
|
141
|
-
return OrderPositions.findOneAndUpdate(
|
|
142
|
-
generateDbFilterById(orderPositionId),
|
|
143
|
-
{
|
|
144
|
-
$set: { scheduling },
|
|
145
|
-
},
|
|
146
|
-
{
|
|
147
|
-
returnDocument: 'after',
|
|
148
|
-
},
|
|
149
|
-
);
|
|
150
|
-
},
|
|
151
|
-
|
|
152
|
-
updateCalculation: async <T extends PricingCalculation>(
|
|
153
|
-
orderPositionId: string,
|
|
154
|
-
calculation: T[],
|
|
155
|
-
) => {
|
|
156
|
-
return OrderPositions.findOneAndUpdate(
|
|
157
|
-
{ _id: orderPositionId },
|
|
158
|
-
{
|
|
159
|
-
$set: { calculation },
|
|
160
|
-
},
|
|
161
|
-
{
|
|
162
|
-
returnDocument: 'after',
|
|
163
|
-
},
|
|
164
|
-
);
|
|
165
|
-
},
|
|
166
|
-
|
|
167
|
-
addProductItem: async (orderPosition: {
|
|
168
|
-
context?: any;
|
|
169
|
-
configuration?: { key: string; value: string }[] | null;
|
|
170
|
-
orderId: string;
|
|
171
|
-
originalProductId: string;
|
|
172
|
-
productId: string;
|
|
173
|
-
quantity: number;
|
|
174
|
-
quotationId?: string;
|
|
175
|
-
}): Promise<OrderPosition> => {
|
|
176
|
-
const { configuration, orderId, originalProductId, productId, quantity, ...scope } = orderPosition;
|
|
177
|
-
|
|
178
|
-
// Search for existing position
|
|
179
|
-
const selector: mongodb.Filter<OrderPosition> = {
|
|
180
|
-
orderId,
|
|
181
|
-
productId,
|
|
182
|
-
originalProductId,
|
|
183
|
-
configuration: configuration ?? null,
|
|
184
|
-
...scope,
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
const upsertedOrderPosition = (await OrderPositions.findOneAndUpdate(
|
|
188
|
-
selector,
|
|
189
|
-
{
|
|
190
|
-
$set: {
|
|
191
|
-
updated: new Date(),
|
|
192
|
-
},
|
|
193
|
-
$inc: { quantity },
|
|
194
|
-
$setOnInsert: {
|
|
195
|
-
_id: generateDbObjectId(),
|
|
196
|
-
created: new Date(),
|
|
197
|
-
calculation: [],
|
|
198
|
-
scheduling: [],
|
|
199
|
-
orderId,
|
|
200
|
-
productId,
|
|
201
|
-
originalProductId,
|
|
202
|
-
configuration,
|
|
203
|
-
...scope,
|
|
204
|
-
},
|
|
205
|
-
},
|
|
206
|
-
{ upsert: true, returnDocument: 'after' },
|
|
207
|
-
)) as OrderPosition;
|
|
208
|
-
|
|
209
|
-
await emit('ORDER_ADD_PRODUCT', { orderPosition: upsertedOrderPosition });
|
|
210
|
-
return upsertedOrderPosition;
|
|
211
|
-
},
|
|
212
|
-
deleteOrderPositions: async (orderId: string) => {
|
|
213
|
-
const { deletedCount } = await OrderPositions.deleteMany({ orderId });
|
|
214
|
-
return deletedCount;
|
|
215
|
-
},
|
|
216
|
-
|
|
217
|
-
aggregatePositions: async ({
|
|
218
|
-
match,
|
|
219
|
-
project,
|
|
220
|
-
group,
|
|
221
|
-
addFields,
|
|
222
|
-
sort,
|
|
223
|
-
limit,
|
|
224
|
-
pipeline,
|
|
225
|
-
}: OrderPositionAggregateParams): Promise<mongodb.Document[]> => {
|
|
226
|
-
const stages: mongodb.Document[] = [];
|
|
227
|
-
|
|
228
|
-
if (pipeline?.length) {
|
|
229
|
-
return await OrderPositions.aggregate(pipeline).toArray();
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
if (match) stages.push({ $match: match });
|
|
233
|
-
if (project) stages.push({ $project: project });
|
|
234
|
-
if (group) stages.push({ $group: group });
|
|
235
|
-
if (addFields) stages.push({ $addFields: addFields });
|
|
236
|
-
if (sort) stages.push({ $sort: sort });
|
|
237
|
-
if (typeof limit === 'number') stages.push({ $limit: limit });
|
|
238
|
-
|
|
239
|
-
return OrderPositions.aggregate(stages).toArray();
|
|
240
|
-
},
|
|
241
|
-
};
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
export type OrderPositionsModule = ReturnType<typeof configureOrderPositionsModule>;
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import { emit, registerEvents } from '@unchainedshop/events';
|
|
2
|
-
import {
|
|
3
|
-
Address,
|
|
4
|
-
Contact,
|
|
5
|
-
generateDbFilterById,
|
|
6
|
-
generateDbObjectId,
|
|
7
|
-
mongodb,
|
|
8
|
-
} from '@unchainedshop/mongodb';
|
|
9
|
-
import { Order, OrderStatus } from '../db/OrdersCollection.js';
|
|
10
|
-
import { OrderPosition } from '../db/OrderPositionsCollection.js';
|
|
11
|
-
|
|
12
|
-
const ORDER_EVENTS: string[] = [
|
|
13
|
-
'ORDER_CREATE',
|
|
14
|
-
'ORDER_REMOVE',
|
|
15
|
-
'ORDER_SET_DELIVERY_PROVIDER',
|
|
16
|
-
'ORDER_SET_PAYMENT_PROVIDER',
|
|
17
|
-
'ORDER_UPDATE',
|
|
18
|
-
];
|
|
19
|
-
|
|
20
|
-
export const configureOrderModuleMutations = ({
|
|
21
|
-
Orders,
|
|
22
|
-
OrderPositions,
|
|
23
|
-
}: {
|
|
24
|
-
Orders: mongodb.Collection<Order>;
|
|
25
|
-
OrderPositions: mongodb.Collection<OrderPosition>;
|
|
26
|
-
}) => {
|
|
27
|
-
registerEvents(ORDER_EVENTS);
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
create: async ({
|
|
31
|
-
userId,
|
|
32
|
-
orderNumber,
|
|
33
|
-
currencyCode,
|
|
34
|
-
countryCode,
|
|
35
|
-
billingAddress,
|
|
36
|
-
contact,
|
|
37
|
-
context,
|
|
38
|
-
}: {
|
|
39
|
-
userId: string;
|
|
40
|
-
billingAddress?: Address;
|
|
41
|
-
contact?: Contact;
|
|
42
|
-
countryCode: string;
|
|
43
|
-
currencyCode: string;
|
|
44
|
-
orderNumber?: string;
|
|
45
|
-
originEnrollmentId?: string;
|
|
46
|
-
context?: Record<string, any>;
|
|
47
|
-
}) => {
|
|
48
|
-
const { insertedId: orderId } = await Orders.insertOne({
|
|
49
|
-
_id: generateDbObjectId(),
|
|
50
|
-
created: new Date(),
|
|
51
|
-
status: null,
|
|
52
|
-
billingAddress,
|
|
53
|
-
contact,
|
|
54
|
-
userId,
|
|
55
|
-
currencyCode,
|
|
56
|
-
countryCode,
|
|
57
|
-
calculation: [],
|
|
58
|
-
log: [],
|
|
59
|
-
orderNumber,
|
|
60
|
-
context: context || {},
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
const order = (await Orders.findOne(generateDbFilterById(orderId), {})) as Order;
|
|
64
|
-
|
|
65
|
-
await emit('ORDER_CREATE', { order });
|
|
66
|
-
return order;
|
|
67
|
-
},
|
|
68
|
-
|
|
69
|
-
delete: async (orderId: string) => {
|
|
70
|
-
const { deletedCount } = await Orders.deleteOne({ _id: orderId });
|
|
71
|
-
if (!deletedCount) return 0;
|
|
72
|
-
await emit('ORDER_REMOVE', { orderId });
|
|
73
|
-
return deletedCount;
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
setCartOwner: async ({ orderId, userId }: { orderId: string; userId: string }) => {
|
|
77
|
-
await Orders.updateOne(generateDbFilterById(orderId), {
|
|
78
|
-
$set: {
|
|
79
|
-
userId,
|
|
80
|
-
},
|
|
81
|
-
});
|
|
82
|
-
},
|
|
83
|
-
|
|
84
|
-
moveCartPositions: async ({
|
|
85
|
-
fromOrderId,
|
|
86
|
-
toOrderId,
|
|
87
|
-
}: {
|
|
88
|
-
fromOrderId: string;
|
|
89
|
-
toOrderId: string;
|
|
90
|
-
}) => {
|
|
91
|
-
await OrderPositions.updateMany(
|
|
92
|
-
{ orderId: fromOrderId },
|
|
93
|
-
{
|
|
94
|
-
$set: {
|
|
95
|
-
orderId: toOrderId,
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
);
|
|
99
|
-
},
|
|
100
|
-
|
|
101
|
-
updateBillingAddress: async (orderId: string, billingAddress: Address) => {
|
|
102
|
-
const selector = generateDbFilterById(orderId);
|
|
103
|
-
const order = await Orders.findOneAndUpdate(
|
|
104
|
-
selector,
|
|
105
|
-
{
|
|
106
|
-
$set: {
|
|
107
|
-
billingAddress,
|
|
108
|
-
updated: new Date(),
|
|
109
|
-
},
|
|
110
|
-
},
|
|
111
|
-
{ returnDocument: 'after' },
|
|
112
|
-
);
|
|
113
|
-
if (!order) return null;
|
|
114
|
-
await emit('ORDER_UPDATE', { order, field: 'billingAddress' });
|
|
115
|
-
return order;
|
|
116
|
-
},
|
|
117
|
-
|
|
118
|
-
updateContact: async (orderId: string, contact: Contact) => {
|
|
119
|
-
const selector = generateDbFilterById(orderId);
|
|
120
|
-
const order = await Orders.findOneAndUpdate(
|
|
121
|
-
selector,
|
|
122
|
-
{
|
|
123
|
-
$set: {
|
|
124
|
-
contact,
|
|
125
|
-
updated: new Date(),
|
|
126
|
-
},
|
|
127
|
-
},
|
|
128
|
-
{ returnDocument: 'after' },
|
|
129
|
-
);
|
|
130
|
-
if (!order) return null;
|
|
131
|
-
await emit('ORDER_UPDATE', { order, field: 'contact' });
|
|
132
|
-
return order;
|
|
133
|
-
},
|
|
134
|
-
|
|
135
|
-
updateCalculationSheet: async (orderId: string, calculation: any) => {
|
|
136
|
-
const selector = generateDbFilterById(orderId);
|
|
137
|
-
const order = await Orders.findOneAndUpdate(
|
|
138
|
-
selector,
|
|
139
|
-
{
|
|
140
|
-
$set: {
|
|
141
|
-
calculation,
|
|
142
|
-
updated: new Date(),
|
|
143
|
-
},
|
|
144
|
-
},
|
|
145
|
-
{ returnDocument: 'after' },
|
|
146
|
-
);
|
|
147
|
-
if (!order) return null;
|
|
148
|
-
await emit('ORDER_UPDATE', { order, field: 'calculation' });
|
|
149
|
-
return order;
|
|
150
|
-
},
|
|
151
|
-
|
|
152
|
-
updateContext: async (orderId: string, context: any) => {
|
|
153
|
-
const selector = generateDbFilterById<Order>(orderId);
|
|
154
|
-
selector.status = { $in: [null, OrderStatus.PENDING] };
|
|
155
|
-
|
|
156
|
-
if (!context || Object.keys(context).length === 0) return Orders.findOne(selector, {});
|
|
157
|
-
|
|
158
|
-
const contextSetters = Object.fromEntries(
|
|
159
|
-
Object.entries(context).map(([key, value]) => [`context.${key}`, value]),
|
|
160
|
-
);
|
|
161
|
-
const order = await Orders.findOneAndUpdate(
|
|
162
|
-
selector,
|
|
163
|
-
{
|
|
164
|
-
$set: {
|
|
165
|
-
...contextSetters,
|
|
166
|
-
updated: new Date(),
|
|
167
|
-
},
|
|
168
|
-
},
|
|
169
|
-
{ returnDocument: 'after' },
|
|
170
|
-
);
|
|
171
|
-
if (!order) return null;
|
|
172
|
-
await emit('ORDER_UPDATE', { order, field: 'context' });
|
|
173
|
-
return order;
|
|
174
|
-
},
|
|
175
|
-
};
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
export type OrderMutations = ReturnType<typeof configureOrderModuleMutations>;
|
|
@@ -1,159 +0,0 @@
|
|
|
1
|
-
import { SortDirection, SortOption } from '@unchainedshop/utils';
|
|
2
|
-
import { generateDbFilterById, buildSortOptions, mongodb } from '@unchainedshop/mongodb';
|
|
3
|
-
import buildFindSelector from './buildFindSelector.js';
|
|
4
|
-
import { Order, OrderQuery } from '../db/OrdersCollection.js';
|
|
5
|
-
|
|
6
|
-
export interface OrderReport {
|
|
7
|
-
newCount: number;
|
|
8
|
-
checkoutCount: number;
|
|
9
|
-
rejectCount: number;
|
|
10
|
-
confirmCount: number;
|
|
11
|
-
fulfillCount: number;
|
|
12
|
-
}
|
|
13
|
-
export interface OrderStatisticsRecord {
|
|
14
|
-
date: string;
|
|
15
|
-
total: {
|
|
16
|
-
amount: number;
|
|
17
|
-
currency: string;
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
export interface OrderAggregateParams {
|
|
21
|
-
match?: Record<string, any>;
|
|
22
|
-
matchAfterGroup?: Record<string, any>;
|
|
23
|
-
project?: Record<string, any>;
|
|
24
|
-
group?: Record<string, any>;
|
|
25
|
-
sort?: Record<string, number>;
|
|
26
|
-
limit?: number;
|
|
27
|
-
addFields?: Record<string, any>;
|
|
28
|
-
pipeline?: any[];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export const configureOrdersModuleQueries = ({ Orders }: { Orders: mongodb.Collection<Order> }) => {
|
|
32
|
-
return {
|
|
33
|
-
isCart: (order: Order) => {
|
|
34
|
-
return order.status === null;
|
|
35
|
-
},
|
|
36
|
-
|
|
37
|
-
cart: async ({
|
|
38
|
-
orderNumber,
|
|
39
|
-
countryCode,
|
|
40
|
-
userId,
|
|
41
|
-
}: {
|
|
42
|
-
countryCode?: string;
|
|
43
|
-
orderNumber?: string;
|
|
44
|
-
userId: string;
|
|
45
|
-
}) => {
|
|
46
|
-
const selector: mongodb.Filter<Order> = {
|
|
47
|
-
countryCode,
|
|
48
|
-
status: { $eq: null },
|
|
49
|
-
userId,
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
if (orderNumber) {
|
|
53
|
-
selector.orderNumber = orderNumber;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const options: mongodb.FindOptions = {
|
|
57
|
-
sort: {
|
|
58
|
-
updated: -1,
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
return Orders.findOne(selector, options);
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
count: async (query: OrderQuery): Promise<number> => {
|
|
65
|
-
const orderCount = await Orders.countDocuments(buildFindSelector(query));
|
|
66
|
-
return orderCount;
|
|
67
|
-
},
|
|
68
|
-
findOrder: async (
|
|
69
|
-
{
|
|
70
|
-
orderId,
|
|
71
|
-
orderNumber,
|
|
72
|
-
}: {
|
|
73
|
-
orderId?: string;
|
|
74
|
-
orderNumber?: string;
|
|
75
|
-
},
|
|
76
|
-
options?: mongodb.FindOptions,
|
|
77
|
-
) => {
|
|
78
|
-
const selector = orderId ? generateDbFilterById(orderId) : { orderNumber };
|
|
79
|
-
return Orders.findOne(selector, options);
|
|
80
|
-
},
|
|
81
|
-
|
|
82
|
-
findCartsToInvalidate: async (maxAgeDays = 30) => {
|
|
83
|
-
const ONE_DAY_IN_MILLISECONDS = 86400000;
|
|
84
|
-
|
|
85
|
-
const minValidDate = new Date(new Date().getTime() - maxAgeDays * ONE_DAY_IN_MILLISECONDS);
|
|
86
|
-
|
|
87
|
-
const orders = await Orders.find({
|
|
88
|
-
status: { $eq: null },
|
|
89
|
-
updated: { $gte: minValidDate },
|
|
90
|
-
}).toArray();
|
|
91
|
-
|
|
92
|
-
return orders;
|
|
93
|
-
},
|
|
94
|
-
|
|
95
|
-
findOrders: async (
|
|
96
|
-
{
|
|
97
|
-
limit,
|
|
98
|
-
offset,
|
|
99
|
-
queryString,
|
|
100
|
-
sort,
|
|
101
|
-
...query
|
|
102
|
-
}: OrderQuery & {
|
|
103
|
-
limit?: number;
|
|
104
|
-
offset?: number;
|
|
105
|
-
sort?: SortOption[];
|
|
106
|
-
},
|
|
107
|
-
options?: mongodb.FindOptions,
|
|
108
|
-
): Promise<Order[]> => {
|
|
109
|
-
const defaultSortOption: SortOption[] = [{ key: 'created', value: SortDirection.DESC }];
|
|
110
|
-
const findOptions: mongodb.FindOptions = {
|
|
111
|
-
skip: offset,
|
|
112
|
-
limit,
|
|
113
|
-
sort: buildSortOptions(sort || defaultSortOption),
|
|
114
|
-
};
|
|
115
|
-
const selector = buildFindSelector({ queryString, ...query });
|
|
116
|
-
|
|
117
|
-
if (queryString) {
|
|
118
|
-
return Orders.find(selector, {
|
|
119
|
-
...options,
|
|
120
|
-
projection: { score: { $meta: 'textScore' } },
|
|
121
|
-
sort: { score: { $meta: 'textScore' } },
|
|
122
|
-
}).toArray();
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return Orders.find(selector, findOptions).toArray();
|
|
126
|
-
},
|
|
127
|
-
orderExists: async ({ orderId }: { orderId: string }): Promise<boolean> => {
|
|
128
|
-
const orderCount = await Orders.countDocuments(generateDbFilterById(orderId), {
|
|
129
|
-
limit: 1,
|
|
130
|
-
});
|
|
131
|
-
return !!orderCount;
|
|
132
|
-
},
|
|
133
|
-
aggregateOrders: async ({
|
|
134
|
-
match,
|
|
135
|
-
project,
|
|
136
|
-
group,
|
|
137
|
-
sort,
|
|
138
|
-
limit,
|
|
139
|
-
addFields,
|
|
140
|
-
pipeline,
|
|
141
|
-
}: OrderAggregateParams): Promise<mongodb.Document[]> => {
|
|
142
|
-
const stages: mongodb.Document[] = [];
|
|
143
|
-
if (pipeline?.length) {
|
|
144
|
-
return await Orders.aggregate(pipeline).toArray();
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (match) stages.push({ $match: match });
|
|
148
|
-
if (project) stages.push({ $project: project });
|
|
149
|
-
if (group) stages.push({ $group: group });
|
|
150
|
-
if (addFields) stages.push({ $addFields: addFields });
|
|
151
|
-
if (sort) stages.push({ $sort: sort });
|
|
152
|
-
if (typeof limit === 'number') stages.push({ $limit: limit });
|
|
153
|
-
|
|
154
|
-
return Orders.aggregate(stages, { allowDiskUse: true }).toArray();
|
|
155
|
-
},
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
export type OrderQueries = ReturnType<typeof configureOrdersModuleQueries>;
|