@unchainedshop/core-orders 4.3.4 → 4.4.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 +9 -9
- 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 +1 -2
- package/lib/module/buildFindSelector.js +1 -2
- 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 +2 -3
- package/lib/module/configureOrderPositionsModule.js +1 -4
- package/lib/module/configureOrdersModule-mutations.d.ts +3 -4
- package/lib/module/configureOrdersModule-mutations.js +2 -3
- package/lib/module/configureOrdersModule-queries.d.ts +2 -3
- package/lib/module/configureOrdersModule-queries.js +3 -3
- package/lib/module/configureOrdersModule.d.ts +39 -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 +11 -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,95 +0,0 @@
|
|
|
1
|
-
import { emit, registerEvents } from '@unchainedshop/events';
|
|
2
|
-
import { generateDbFilterById, generateDbObjectId, mongodb } from '@unchainedshop/mongodb';
|
|
3
|
-
import { OrderDiscount, OrderDiscountTrigger } from '../db/OrderDiscountsCollection.js';
|
|
4
|
-
|
|
5
|
-
const ORDER_DISCOUNT_EVENTS: string[] = [
|
|
6
|
-
'ORDER_CREATE_DISCOUNT',
|
|
7
|
-
'ORDER_UPDATE_DISCOUNT',
|
|
8
|
-
'ORDER_REMOVE_DISCOUNT',
|
|
9
|
-
];
|
|
10
|
-
|
|
11
|
-
export const buildFindOrderDiscountByIdSelector = (orderDiscountId: string) =>
|
|
12
|
-
generateDbFilterById(orderDiscountId) as mongodb.Filter<OrderDiscount>;
|
|
13
|
-
|
|
14
|
-
export const configureOrderDiscountsModule = ({
|
|
15
|
-
OrderDiscounts,
|
|
16
|
-
}: {
|
|
17
|
-
OrderDiscounts: mongodb.Collection<OrderDiscount>;
|
|
18
|
-
}) => {
|
|
19
|
-
registerEvents(ORDER_DISCOUNT_EVENTS);
|
|
20
|
-
|
|
21
|
-
return {
|
|
22
|
-
// Queries
|
|
23
|
-
findOrderDiscount: async ({ discountId }: { discountId: string }, options?: mongodb.FindOptions) => {
|
|
24
|
-
return OrderDiscounts.findOne(buildFindOrderDiscountByIdSelector(discountId), options);
|
|
25
|
-
},
|
|
26
|
-
|
|
27
|
-
findOrderDiscounts: async ({ orderId }: { orderId: string }): Promise<OrderDiscount[]> => {
|
|
28
|
-
const discounts = OrderDiscounts.find({ orderId });
|
|
29
|
-
return discounts.toArray();
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
create: async (
|
|
33
|
-
doc: Omit<OrderDiscount, '_id' | 'created' | 'trigger'> &
|
|
34
|
-
Pick<Partial<OrderDiscount>, '_id' | 'created' | 'trigger'>,
|
|
35
|
-
): Promise<OrderDiscount> => {
|
|
36
|
-
const normalizedTrigger = doc.trigger || OrderDiscountTrigger.USER;
|
|
37
|
-
const { insertedId: discountId } = await OrderDiscounts.insertOne({
|
|
38
|
-
_id: generateDbObjectId(),
|
|
39
|
-
created: new Date(),
|
|
40
|
-
...doc,
|
|
41
|
-
trigger: normalizedTrigger,
|
|
42
|
-
});
|
|
43
|
-
const discount = (await OrderDiscounts.findOne({
|
|
44
|
-
_id: discountId,
|
|
45
|
-
})) as OrderDiscount;
|
|
46
|
-
await emit('ORDER_CREATE_DISCOUNT', { discount });
|
|
47
|
-
return discount;
|
|
48
|
-
},
|
|
49
|
-
|
|
50
|
-
delete: async (orderDiscountId: string) => {
|
|
51
|
-
const selector = buildFindOrderDiscountByIdSelector(orderDiscountId);
|
|
52
|
-
const orderDiscount = await OrderDiscounts.findOneAndDelete(selector);
|
|
53
|
-
await emit('ORDER_REMOVE_DISCOUNT', { discount: orderDiscount });
|
|
54
|
-
return orderDiscount;
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
isDiscountCodeUsed: async ({ code, orderId }): Promise<boolean> => {
|
|
58
|
-
return (
|
|
59
|
-
(await OrderDiscounts.countDocuments({
|
|
60
|
-
code,
|
|
61
|
-
orderId,
|
|
62
|
-
})) > 0
|
|
63
|
-
);
|
|
64
|
-
},
|
|
65
|
-
|
|
66
|
-
findSpareDiscount: async ({ code }) => {
|
|
67
|
-
return OrderDiscounts.findOne({
|
|
68
|
-
code,
|
|
69
|
-
orderId: { $exists: false },
|
|
70
|
-
});
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
update: async (orderDiscountId: string, doc: Partial<OrderDiscount>) => {
|
|
74
|
-
const discount = await OrderDiscounts.findOneAndUpdate(
|
|
75
|
-
{ _id: orderDiscountId },
|
|
76
|
-
{
|
|
77
|
-
$set: {
|
|
78
|
-
updated: new Date(),
|
|
79
|
-
...doc,
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
{ returnDocument: 'after' },
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
await emit('ORDER_UPDATE_DISCOUNT', { discount });
|
|
86
|
-
return discount;
|
|
87
|
-
},
|
|
88
|
-
deleteOrderDiscounts: async (orderId: string) => {
|
|
89
|
-
const { deletedCount } = await OrderDiscounts.deleteMany({ orderId });
|
|
90
|
-
return deletedCount;
|
|
91
|
-
},
|
|
92
|
-
};
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
export type OrderDiscountsModule = ReturnType<typeof configureOrderDiscountsModule>;
|
|
@@ -1,224 +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 { OrderPayment, OrderPaymentStatus } from '../db/OrderPaymentsCollection.js';
|
|
5
|
-
|
|
6
|
-
const ORDER_PAYMENT_EVENTS: string[] = ['ORDER_UPDATE_PAYMENT', 'ORDER_SIGN_PAYMENT', 'ORDER_PAY'];
|
|
7
|
-
|
|
8
|
-
export const buildFindOrderPaymentByIdSelector = (orderPaymentId: string) =>
|
|
9
|
-
generateDbFilterById(orderPaymentId) as mongodb.Filter<OrderPayment>;
|
|
10
|
-
|
|
11
|
-
export const buildFindByContextDataSelector = (context: any) => {
|
|
12
|
-
const contextKeys = Object.keys(context);
|
|
13
|
-
|
|
14
|
-
if (contextKeys.length === 0) return null;
|
|
15
|
-
|
|
16
|
-
const selector: mongodb.Filter<OrderPayment> = contextKeys.reduce(
|
|
17
|
-
(currentSelector, key) =>
|
|
18
|
-
context[key] !== undefined
|
|
19
|
-
? {
|
|
20
|
-
...currentSelector,
|
|
21
|
-
[`context.${key}`]: context[key],
|
|
22
|
-
}
|
|
23
|
-
: currentSelector,
|
|
24
|
-
{},
|
|
25
|
-
);
|
|
26
|
-
return selector;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export const configureOrderPaymentsModule = ({
|
|
30
|
-
OrderPayments,
|
|
31
|
-
}: {
|
|
32
|
-
OrderPayments: mongodb.Collection<OrderPayment>;
|
|
33
|
-
}) => {
|
|
34
|
-
registerEvents(ORDER_PAYMENT_EVENTS);
|
|
35
|
-
|
|
36
|
-
const normalizedStatus = (orderPayment: OrderPayment) => {
|
|
37
|
-
return orderPayment.status === null
|
|
38
|
-
? OrderPaymentStatus.OPEN
|
|
39
|
-
: (orderPayment.status as OrderPaymentStatus);
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const updateStatus = async (
|
|
43
|
-
orderPaymentId: string,
|
|
44
|
-
{
|
|
45
|
-
transactionId,
|
|
46
|
-
status,
|
|
47
|
-
info,
|
|
48
|
-
}: { transactionId?: string; status: OrderPaymentStatus; info?: string },
|
|
49
|
-
) => {
|
|
50
|
-
const date = new Date();
|
|
51
|
-
const modifier: mongodb.UpdateFilter<OrderPayment> = {
|
|
52
|
-
$set: { status, updated: new Date() },
|
|
53
|
-
$push: {
|
|
54
|
-
log: {
|
|
55
|
-
date,
|
|
56
|
-
status,
|
|
57
|
-
info,
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
};
|
|
61
|
-
if (transactionId) {
|
|
62
|
-
// eslint-disable-next-line
|
|
63
|
-
// @ts-ignore
|
|
64
|
-
modifier.$set.transactionId = transactionId;
|
|
65
|
-
}
|
|
66
|
-
if (status === OrderPaymentStatus.PAID) {
|
|
67
|
-
// eslint-disable-next-line
|
|
68
|
-
// @ts-ignore
|
|
69
|
-
modifier.$set.paid = date;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const selector = buildFindOrderPaymentByIdSelector(orderPaymentId);
|
|
73
|
-
return OrderPayments.findOneAndUpdate(selector, modifier, { returnDocument: 'after' });
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
return {
|
|
77
|
-
// Queries
|
|
78
|
-
findOrderPayment: async (
|
|
79
|
-
{
|
|
80
|
-
orderPaymentId,
|
|
81
|
-
}: {
|
|
82
|
-
orderPaymentId: string;
|
|
83
|
-
},
|
|
84
|
-
options?: mongodb.FindOptions,
|
|
85
|
-
) => {
|
|
86
|
-
return OrderPayments.findOne(buildFindOrderPaymentByIdSelector(orderPaymentId), options);
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
findOrderPaymentsByProviderIds: async (
|
|
90
|
-
{
|
|
91
|
-
paymentProviderIds,
|
|
92
|
-
}: {
|
|
93
|
-
paymentProviderIds: string[];
|
|
94
|
-
},
|
|
95
|
-
options?: mongodb.FindOptions,
|
|
96
|
-
): Promise<OrderPayment[]> => {
|
|
97
|
-
if (!paymentProviderIds?.length) return [];
|
|
98
|
-
return OrderPayments.find({ paymentProviderId: { $in: paymentProviderIds } }, options).toArray();
|
|
99
|
-
},
|
|
100
|
-
findOrderPaymentByContextData: async (
|
|
101
|
-
{
|
|
102
|
-
context,
|
|
103
|
-
}: {
|
|
104
|
-
context: any;
|
|
105
|
-
},
|
|
106
|
-
options?: mongodb.FindOptions,
|
|
107
|
-
) => {
|
|
108
|
-
const selector = buildFindByContextDataSelector(context);
|
|
109
|
-
if (!selector) return null;
|
|
110
|
-
return OrderPayments.findOne(selector, options);
|
|
111
|
-
},
|
|
112
|
-
countOrderPaymentsByContextData: async (
|
|
113
|
-
{
|
|
114
|
-
context,
|
|
115
|
-
}: {
|
|
116
|
-
context: any;
|
|
117
|
-
},
|
|
118
|
-
options?: mongodb.FindOptions,
|
|
119
|
-
) => {
|
|
120
|
-
const selector = buildFindByContextDataSelector(context);
|
|
121
|
-
if (!selector) return 0;
|
|
122
|
-
return OrderPayments.countDocuments(selector, options);
|
|
123
|
-
},
|
|
124
|
-
|
|
125
|
-
normalizedStatus,
|
|
126
|
-
|
|
127
|
-
// Mutations
|
|
128
|
-
|
|
129
|
-
create: async (
|
|
130
|
-
doc: Omit<OrderPayment, '_id' | 'created'> & Pick<Partial<OrderPayment>, '_id' | 'created'>,
|
|
131
|
-
): Promise<OrderPayment> => {
|
|
132
|
-
const { insertedId: orderPaymentId } = await OrderPayments.insertOne({
|
|
133
|
-
_id: generateDbObjectId(),
|
|
134
|
-
created: new Date(),
|
|
135
|
-
...doc,
|
|
136
|
-
status: null,
|
|
137
|
-
context: doc.context || {},
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
const orderPayment = (await OrderPayments.findOne(
|
|
141
|
-
buildFindOrderPaymentByIdSelector(orderPaymentId),
|
|
142
|
-
)) as OrderPayment;
|
|
143
|
-
|
|
144
|
-
return orderPayment;
|
|
145
|
-
},
|
|
146
|
-
|
|
147
|
-
logEvent: async (orderPaymentId: string, event: any): Promise<boolean> => {
|
|
148
|
-
const date = new Date();
|
|
149
|
-
const modifier = {
|
|
150
|
-
$push: {
|
|
151
|
-
log: {
|
|
152
|
-
date,
|
|
153
|
-
status: undefined,
|
|
154
|
-
info: JSON.stringify(event),
|
|
155
|
-
},
|
|
156
|
-
},
|
|
157
|
-
};
|
|
158
|
-
await OrderPayments.updateOne(generateDbFilterById(orderPaymentId), modifier);
|
|
159
|
-
|
|
160
|
-
return true;
|
|
161
|
-
},
|
|
162
|
-
|
|
163
|
-
markAsPaid: async (orderPayment: OrderPayment, meta: any) => {
|
|
164
|
-
if (normalizedStatus(orderPayment) !== OrderPaymentStatus.OPEN) return;
|
|
165
|
-
|
|
166
|
-
await updateStatus(orderPayment._id, {
|
|
167
|
-
status: OrderPaymentStatus.PAID,
|
|
168
|
-
info: meta ? JSON.stringify(meta) : 'mark paid manually',
|
|
169
|
-
});
|
|
170
|
-
await emit('ORDER_PAY', { orderPayment });
|
|
171
|
-
},
|
|
172
|
-
|
|
173
|
-
updateContext: async (orderPaymentId: string, context: any) => {
|
|
174
|
-
const selector = buildFindOrderPaymentByIdSelector(orderPaymentId);
|
|
175
|
-
if (!context || Object.keys(context).length === 0) return OrderPayments.findOne(selector, {});
|
|
176
|
-
|
|
177
|
-
const contextSetters = Object.fromEntries(
|
|
178
|
-
Object.entries(context).map(([key, value]) => [`context.${key}`, value]),
|
|
179
|
-
);
|
|
180
|
-
const orderPayment = await OrderPayments.findOneAndUpdate(
|
|
181
|
-
selector,
|
|
182
|
-
{
|
|
183
|
-
$set: {
|
|
184
|
-
...contextSetters,
|
|
185
|
-
updated: new Date(),
|
|
186
|
-
},
|
|
187
|
-
},
|
|
188
|
-
{ returnDocument: 'after' },
|
|
189
|
-
);
|
|
190
|
-
|
|
191
|
-
if (!orderPayment) return null;
|
|
192
|
-
await emit('ORDER_UPDATE_PAYMENT', {
|
|
193
|
-
orderPayment,
|
|
194
|
-
});
|
|
195
|
-
return orderPayment;
|
|
196
|
-
},
|
|
197
|
-
|
|
198
|
-
updateStatus,
|
|
199
|
-
|
|
200
|
-
updateCalculation: async <T extends PricingCalculation>(
|
|
201
|
-
orderPaymentId: string,
|
|
202
|
-
calculation: T[],
|
|
203
|
-
) => {
|
|
204
|
-
return OrderPayments.findOneAndUpdate(
|
|
205
|
-
buildFindOrderPaymentByIdSelector(orderPaymentId),
|
|
206
|
-
{
|
|
207
|
-
$set: {
|
|
208
|
-
calculation,
|
|
209
|
-
updated: new Date(),
|
|
210
|
-
},
|
|
211
|
-
},
|
|
212
|
-
{
|
|
213
|
-
returnDocument: 'after',
|
|
214
|
-
},
|
|
215
|
-
);
|
|
216
|
-
},
|
|
217
|
-
deleteOrderPayments: async (orderId: string) => {
|
|
218
|
-
const { deletedCount } = await OrderPayments.deleteMany({ orderId });
|
|
219
|
-
return deletedCount;
|
|
220
|
-
},
|
|
221
|
-
};
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
export type OrderPaymentsModule = ReturnType<typeof configureOrderPaymentsModule>;
|
|
@@ -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>;
|