@unchainedshop/core-orders 2.14.2 → 3.0.0-alpha3
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/lib/db/OrderDeliveriesCollection.d.ts +1 -1
- package/lib/db/OrderDeliveriesCollection.d.ts.map +1 -1
- package/lib/db/OrderDiscountsCollection.d.ts +1 -1
- package/lib/db/OrderDiscountsCollection.d.ts.map +1 -1
- package/lib/db/OrderPaymentsCollection.d.ts +1 -1
- package/lib/db/OrderPaymentsCollection.d.ts.map +1 -1
- package/lib/db/OrderPositionsCollection.d.ts +1 -1
- package/lib/db/OrderPositionsCollection.d.ts.map +1 -1
- package/lib/db/OrdersCollection.d.ts +1 -1
- package/lib/db/OrdersCollection.d.ts.map +1 -1
- package/lib/director/OrderDiscountAdapter.d.ts +1 -1
- package/lib/director/OrderDiscountAdapter.d.ts.map +1 -1
- package/lib/director/OrderDiscountAdapter.js.map +1 -1
- package/lib/director/OrderDiscountDirector.d.ts +1 -1
- package/lib/director/OrderDiscountDirector.d.ts.map +1 -1
- package/lib/director/OrderPricingAdapter.d.ts +20 -2
- package/lib/director/OrderPricingAdapter.d.ts.map +1 -1
- package/lib/director/OrderPricingAdapter.js.map +1 -1
- package/lib/director/OrderPricingDirector.d.ts +16 -1
- package/lib/director/OrderPricingDirector.d.ts.map +1 -1
- package/lib/director/OrderPricingDirector.js.map +1 -1
- package/lib/director/OrderPricingSheet.d.ts +34 -2
- package/lib/director/OrderPricingSheet.d.ts.map +1 -1
- package/lib/director/OrderPricingSheet.js +9 -2
- package/lib/director/OrderPricingSheet.js.map +1 -1
- package/lib/module/configureOrderDeliveriesModule.d.ts +32 -4
- package/lib/module/configureOrderDeliveriesModule.d.ts.map +1 -1
- package/lib/module/configureOrderDeliveriesModule.js +8 -18
- package/lib/module/configureOrderDeliveriesModule.js.map +1 -1
- package/lib/module/configureOrderDiscountsModule.d.ts +22 -4
- package/lib/module/configureOrderDiscountsModule.d.ts.map +1 -1
- package/lib/module/configureOrderDiscountsModule.js +20 -29
- package/lib/module/configureOrderDiscountsModule.js.map +1 -1
- package/lib/module/configureOrderPaymentsModule.d.ts +46 -4
- package/lib/module/configureOrderPaymentsModule.d.ts.map +1 -1
- package/lib/module/configureOrderPaymentsModule.js +7 -19
- package/lib/module/configureOrderPaymentsModule.js.map +1 -1
- package/lib/module/configureOrderPositionsModule.d.ts +58 -4
- package/lib/module/configureOrderPositionsModule.d.ts.map +1 -1
- package/lib/module/configureOrderPositionsModule.js +11 -51
- package/lib/module/configureOrderPositionsModule.js.map +1 -1
- package/lib/module/configureOrdersModule-mutations.d.ts +32 -8
- package/lib/module/configureOrdersModule-mutations.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-mutations.js +21 -51
- package/lib/module/configureOrdersModule-mutations.js.map +1 -1
- package/lib/module/configureOrdersModule-processing.d.ts +22 -4
- package/lib/module/configureOrdersModule-processing.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-processing.js +5 -14
- package/lib/module/configureOrdersModule-processing.js.map +1 -1
- package/lib/module/configureOrdersModule-queries.d.ts +23 -3
- package/lib/module/configureOrdersModule-queries.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-queries.js +5 -4
- package/lib/module/configureOrdersModule-queries.js.map +1 -1
- package/lib/module/configureOrdersModule-transformations.d.ts +15 -1
- package/lib/module/configureOrdersModule-transformations.d.ts.map +1 -1
- package/lib/module/configureOrdersModule-transformations.js +4 -5
- package/lib/module/configureOrdersModule-transformations.js.map +1 -1
- package/lib/module/configureOrdersModule.d.ts +20 -2
- package/lib/module/configureOrdersModule.d.ts.map +1 -1
- package/lib/module/configureOrdersModule.js +17 -9
- package/lib/module/configureOrdersModule.js.map +1 -1
- package/lib/orders-index.d.ts +10 -12
- package/lib/orders-index.d.ts.map +1 -1
- package/lib/orders-index.js +10 -11
- package/lib/orders-index.js.map +1 -1
- package/lib/orders-settings.d.ts +18 -1
- package/lib/orders-settings.d.ts.map +1 -1
- package/lib/orders-settings.js.map +1 -1
- package/lib/service/migrateOrderCartService.d.ts +8 -1
- package/lib/service/migrateOrderCartService.d.ts.map +1 -1
- package/lib/service/migrateOrderCartService.js +2 -2
- package/lib/service/migrateOrderCartService.js.map +1 -1
- package/lib/service/nextUserCartService.d.ts +9 -1
- package/lib/service/nextUserCartService.d.ts.map +1 -1
- package/lib/service/nextUserCartService.js.map +1 -1
- package/lib/service/orderServices.d.ts +6 -1
- package/lib/service/orderServices.d.ts.map +1 -1
- package/lib/service/orderServices.js.map +1 -1
- package/lib/types.d.ts +116 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +25 -0
- package/lib/types.js.map +1 -0
- package/package.json +6 -8
- package/src/db/OrderDeliveriesCollection.ts +1 -1
- package/src/db/OrderDiscountsCollection.ts +1 -1
- package/src/db/OrderPaymentsCollection.ts +1 -1
- package/src/db/OrderPositionsCollection.ts +1 -1
- package/src/db/OrdersCollection.ts +1 -1
- package/src/director/OrderDiscountAdapter.ts +1 -1
- package/src/director/OrderPricingAdapter.ts +26 -6
- package/src/director/OrderPricingDirector.ts +21 -5
- package/src/director/OrderPricingSheet.ts +25 -6
- package/src/module/buildFindSelector.test.ts +7 -2
- package/src/module/configureOrderDeliveriesModule.ts +65 -29
- package/src/module/configureOrderDiscountsModule.ts +70 -43
- package/src/module/configureOrderPaymentsModule.ts +94 -29
- package/src/module/configureOrderPositionsModule.ts +82 -94
- package/src/module/configureOrdersModule-mutations.ts +88 -82
- package/src/module/configureOrdersModule-processing.ts +38 -23
- package/src/module/configureOrdersModule-queries.ts +33 -13
- package/src/module/configureOrdersModule-transformations.ts +28 -6
- package/src/module/configureOrdersModule.ts +57 -21
- package/src/orders-index.ts +12 -16
- package/src/orders-settings.ts +20 -1
- package/src/service/migrateOrderCartService.ts +14 -7
- package/src/service/nextUserCartService.ts +13 -1
- package/src/service/orderServices.ts +7 -3
- package/src/types.ts +129 -0
- package/lib/db/OrderDeliveriesSchema.d.ts +0 -3
- package/lib/db/OrderDeliveriesSchema.d.ts.map +0 -1
- package/lib/db/OrderDeliveriesSchema.js +0 -20
- package/lib/db/OrderDeliveriesSchema.js.map +0 -1
- package/lib/db/OrderDiscountsSchema.d.ts +0 -3
- package/lib/db/OrderDiscountsSchema.d.ts.map +0 -1
- package/lib/db/OrderDiscountsSchema.js +0 -12
- package/lib/db/OrderDiscountsSchema.js.map +0 -1
- package/lib/db/OrderPaymentsSchema.d.ts +0 -3
- package/lib/db/OrderPaymentsSchema.d.ts.map +0 -1
- package/lib/db/OrderPaymentsSchema.js +0 -20
- package/lib/db/OrderPaymentsSchema.js.map +0 -1
- package/lib/db/OrderPositionsSchema.d.ts +0 -3
- package/lib/db/OrderPositionsSchema.d.ts.map +0 -1
- package/lib/db/OrderPositionsSchema.js +0 -37
- package/lib/db/OrderPositionsSchema.js.map +0 -1
- package/lib/db/OrdersSchema.d.ts +0 -3
- package/lib/db/OrdersSchema.d.ts.map +0 -1
- package/lib/db/OrdersSchema.js +0 -30
- package/lib/db/OrdersSchema.js.map +0 -1
- package/src/db/OrderDeliveriesSchema.ts +0 -24
- package/src/db/OrderDiscountsSchema.ts +0 -15
- package/src/db/OrderPaymentsSchema.ts +0 -24
- package/src/db/OrderPositionsSchema.ts +0 -41
- package/src/db/OrdersSchema.ts +0 -34
|
@@ -1,12 +1,28 @@
|
|
|
1
|
+
import { BasePricingDirector, PricingDiscount, IPricingDirector } from '@unchainedshop/utils';
|
|
2
|
+
import { IOrderPricingSheet, OrderPricingCalculation, OrderPricingSheet } from './OrderPricingSheet.js';
|
|
3
|
+
import { Order, OrderDelivery, OrderPayment, OrderPosition } from '../types.js';
|
|
1
4
|
import {
|
|
2
5
|
IOrderPricingAdapter,
|
|
3
|
-
IOrderPricingDirector,
|
|
4
6
|
OrderPricingAdapterContext,
|
|
5
|
-
OrderPricingCalculation,
|
|
6
7
|
OrderPricingContext,
|
|
7
|
-
} from '
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
} from './OrderPricingAdapter.js';
|
|
9
|
+
|
|
10
|
+
export type OrderPrice = { _id?: string; amount: number; currency: string };
|
|
11
|
+
|
|
12
|
+
export type OrderPricingDiscount = PricingDiscount & {
|
|
13
|
+
delivery?: OrderDelivery;
|
|
14
|
+
item?: OrderPosition;
|
|
15
|
+
order?: Order;
|
|
16
|
+
payment?: OrderPayment;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type IOrderPricingDirector<DiscountConfiguration = unknown> = IPricingDirector<
|
|
20
|
+
OrderPricingContext,
|
|
21
|
+
OrderPricingCalculation,
|
|
22
|
+
OrderPricingAdapterContext,
|
|
23
|
+
IOrderPricingSheet,
|
|
24
|
+
IOrderPricingAdapter<DiscountConfiguration>
|
|
25
|
+
>;
|
|
10
26
|
|
|
11
27
|
const baseDirector = BasePricingDirector<
|
|
12
28
|
OrderPricingContext,
|
|
@@ -1,10 +1,29 @@
|
|
|
1
|
-
import { BasePricingSheet } from '@unchainedshop/utils';
|
|
2
1
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
BasePricingSheet,
|
|
3
|
+
IPricingSheet,
|
|
4
|
+
IBasePricingSheet,
|
|
5
|
+
PricingCalculation,
|
|
6
|
+
PricingSheetParams,
|
|
7
|
+
} from '@unchainedshop/utils';
|
|
8
|
+
|
|
9
|
+
export interface OrderPricingCalculation extends PricingCalculation {
|
|
10
|
+
discountId?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export enum OrderPricingRowCategory {
|
|
14
|
+
Items = 'ITEMS',
|
|
15
|
+
Discounts = 'DISCOUNTS',
|
|
16
|
+
Taxes = 'TAXES',
|
|
17
|
+
Delivery = 'DELIVERY',
|
|
18
|
+
Payment = 'PAYMENT',
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface IOrderPricingSheet extends IPricingSheet<OrderPricingCalculation> {
|
|
22
|
+
addDelivery: (params: { amount: number; taxAmount: number; meta?: any }) => void;
|
|
23
|
+
addDiscount: (params: { amount: number; taxAmount: number; discountId: string; meta?: any }) => void;
|
|
24
|
+
addItems: (params: { amount: number; taxAmount: number; meta?: any }) => void;
|
|
25
|
+
addPayment: (params: { amount: number; taxAmount: number; meta?: any }) => void;
|
|
26
|
+
}
|
|
8
27
|
|
|
9
28
|
export const OrderPricingSheet = (
|
|
10
29
|
params: PricingSheetParams<OrderPricingCalculation>,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { OrderStatus } from "../types.js"
|
|
1
2
|
import {buildFindByIdSelector as buildFindByIdSelectorForDelivery} from "./configureOrderDeliveriesModule.js"
|
|
2
3
|
import {buildFindByIdSelector as buildFindByIdSelectorForDiscount } from "./configureOrderDiscountsModule.js"
|
|
3
4
|
import {buildFindByIdSelector as buildFindByIdSelectorForPayment, buildFindByContextDataSelector } from "./configureOrderPaymentsModule.js"
|
|
@@ -13,9 +14,13 @@ describe('OrderPosition', () => {
|
|
|
13
14
|
});
|
|
14
15
|
|
|
15
16
|
it('Return filter object when passed no argument includeCarts, queryString, status, userId and (status should take precedence over includeCarts', async () => {
|
|
16
|
-
expect(buildFindSelectorForOrder({includeCarts: false, queryString: "hello world", status:
|
|
17
|
+
expect(buildFindSelectorForOrder({includeCarts: false, queryString: "hello world", status: [OrderStatus.CONFIRMED], userId: "admin-id"})).toEqual({
|
|
17
18
|
userId: 'admin-id',
|
|
18
|
-
status:
|
|
19
|
+
"status": {
|
|
20
|
+
"$in": [
|
|
21
|
+
"CONFIRMED",
|
|
22
|
+
],
|
|
23
|
+
},
|
|
19
24
|
'$text': { '$search': 'hello world' }
|
|
20
25
|
}
|
|
21
26
|
)
|
|
@@ -1,15 +1,65 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { mongodb, generateDbFilterById,
|
|
3
|
-
import { OrdersModule } from '@unchainedshop/types/orders.js';
|
|
4
|
-
import {
|
|
5
|
-
OrderDeliveriesModule,
|
|
6
|
-
OrderDelivery,
|
|
7
|
-
OrderDeliveryStatus,
|
|
8
|
-
} from '@unchainedshop/types/orders.deliveries.js';
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/core';
|
|
2
|
+
import { mongodb, generateDbFilterById, generateDbObjectId } from '@unchainedshop/mongodb';
|
|
9
3
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
4
|
+
import { Order, OrderDelivery, OrderDeliveryStatus, OrderDiscount } from '../types.js';
|
|
5
|
+
import { DeliveryLocation, IDeliveryPricingSheet } from '@unchainedshop/core-delivery';
|
|
12
6
|
import { DeliveryDirector } from '@unchainedshop/core-delivery';
|
|
7
|
+
import { OrderPricingDiscount } from '../director/OrderPricingDirector.js';
|
|
8
|
+
|
|
9
|
+
export type OrderDeliveriesModule = {
|
|
10
|
+
// Queries
|
|
11
|
+
findDelivery: (
|
|
12
|
+
params: { orderDeliveryId: string },
|
|
13
|
+
options?: mongodb.FindOptions,
|
|
14
|
+
) => Promise<OrderDelivery>;
|
|
15
|
+
|
|
16
|
+
// Transformations
|
|
17
|
+
discounts: (
|
|
18
|
+
orderDelivery: OrderDelivery,
|
|
19
|
+
params: { order: Order; orderDiscount: OrderDiscount },
|
|
20
|
+
unchainedAPI: UnchainedCore,
|
|
21
|
+
) => Array<OrderPricingDiscount>;
|
|
22
|
+
isBlockingOrderConfirmation: (
|
|
23
|
+
orderDelivery: OrderDelivery,
|
|
24
|
+
unchainedAPI: UnchainedCore,
|
|
25
|
+
) => Promise<boolean>;
|
|
26
|
+
isBlockingOrderFullfillment: (orderDelivery: OrderDelivery) => boolean;
|
|
27
|
+
normalizedStatus: (orderDelivery: OrderDelivery) => string;
|
|
28
|
+
pricingSheet: (
|
|
29
|
+
orderDelivery: OrderDelivery,
|
|
30
|
+
currency: string,
|
|
31
|
+
unchainedAPI: UnchainedCore,
|
|
32
|
+
) => IDeliveryPricingSheet;
|
|
33
|
+
|
|
34
|
+
// Mutations
|
|
35
|
+
create: (doc: OrderDelivery) => Promise<OrderDelivery>;
|
|
36
|
+
delete: (orderDeliveryId: string) => Promise<number>;
|
|
37
|
+
|
|
38
|
+
markAsDelivered: (orderDelivery: OrderDelivery) => Promise<void>;
|
|
39
|
+
|
|
40
|
+
activePickUpLocation: (
|
|
41
|
+
orderDelivery: OrderDelivery,
|
|
42
|
+
unchainedAPI: UnchainedCore,
|
|
43
|
+
) => Promise<DeliveryLocation | null>;
|
|
44
|
+
|
|
45
|
+
send: (
|
|
46
|
+
orderDelivery: OrderDelivery,
|
|
47
|
+
params: { order: Order; deliveryContext?: any },
|
|
48
|
+
unchainedAPI: UnchainedCore,
|
|
49
|
+
) => Promise<OrderDelivery>;
|
|
50
|
+
|
|
51
|
+
updateContext: (orderDeliveryId: string, context: any) => Promise<OrderDelivery | null>;
|
|
52
|
+
|
|
53
|
+
updateStatus: (
|
|
54
|
+
orderDeliveryId: string,
|
|
55
|
+
params: { status: OrderDeliveryStatus; info?: string },
|
|
56
|
+
) => Promise<OrderDelivery>;
|
|
57
|
+
|
|
58
|
+
updateCalculation: (
|
|
59
|
+
orderDelivery: OrderDelivery,
|
|
60
|
+
unchainedAPI: UnchainedCore,
|
|
61
|
+
) => Promise<OrderDelivery>;
|
|
62
|
+
};
|
|
13
63
|
|
|
14
64
|
const ORDER_DELIVERY_EVENTS: string[] = ['ORDER_DELIVER', 'ORDER_UPDATE_DELIVERY'];
|
|
15
65
|
|
|
@@ -18,18 +68,11 @@ export const buildFindByIdSelector = (orderDeliveryId: string) =>
|
|
|
18
68
|
|
|
19
69
|
export const configureOrderDeliveriesModule = ({
|
|
20
70
|
OrderDeliveries,
|
|
21
|
-
updateCalculation,
|
|
22
71
|
}: {
|
|
23
72
|
OrderDeliveries: mongodb.Collection<OrderDelivery>;
|
|
24
|
-
updateCalculation: OrdersModule['updateCalculation'];
|
|
25
73
|
}): OrderDeliveriesModule => {
|
|
26
74
|
registerEvents(ORDER_DELIVERY_EVENTS);
|
|
27
75
|
|
|
28
|
-
const mutations = generateDbMutations<OrderDelivery>(OrderDeliveries, OrderDeliveriesSchema, {
|
|
29
|
-
permanentlyDeleteByDefault: true,
|
|
30
|
-
hasCreateOnly: false,
|
|
31
|
-
}) as ModuleMutations<OrderDelivery>;
|
|
32
|
-
|
|
33
76
|
const normalizedStatus: OrderDeliveriesModule['normalizedStatus'] = (orderDelivery) => {
|
|
34
77
|
return orderDelivery.status === null
|
|
35
78
|
? OrderDeliveryStatus.OPEN
|
|
@@ -40,8 +83,6 @@ export const configureOrderDeliveriesModule = ({
|
|
|
40
83
|
orderDeliveryId,
|
|
41
84
|
{ status, info },
|
|
42
85
|
) => {
|
|
43
|
-
log(`OrderDelivery ${orderDeliveryId} -> New Status: ${status}`);
|
|
44
|
-
|
|
45
86
|
const date = new Date();
|
|
46
87
|
const modifier: mongodb.UpdateFilter<OrderDelivery> = {
|
|
47
88
|
$set: { status, updated: new Date() },
|
|
@@ -131,7 +172,9 @@ export const configureOrderDeliveriesModule = ({
|
|
|
131
172
|
// Mutations
|
|
132
173
|
|
|
133
174
|
create: async (doc) => {
|
|
134
|
-
const orderDeliveryId = await
|
|
175
|
+
const { insertedId: orderDeliveryId } = await OrderDeliveries.insertOne({
|
|
176
|
+
_id: generateDbObjectId(),
|
|
177
|
+
created: new Date(),
|
|
135
178
|
...doc,
|
|
136
179
|
context: doc.context || {},
|
|
137
180
|
status: null,
|
|
@@ -142,7 +185,7 @@ export const configureOrderDeliveriesModule = ({
|
|
|
142
185
|
},
|
|
143
186
|
|
|
144
187
|
delete: async (orderDeliveryId) => {
|
|
145
|
-
const deletedCount = await
|
|
188
|
+
const { deletedCount } = await OrderDeliveries.deleteOne({ _id: orderDeliveryId });
|
|
146
189
|
return deletedCount;
|
|
147
190
|
},
|
|
148
191
|
|
|
@@ -190,11 +233,9 @@ export const configureOrderDeliveriesModule = ({
|
|
|
190
233
|
return orderDelivery;
|
|
191
234
|
},
|
|
192
235
|
|
|
193
|
-
updateContext: async (orderDeliveryId, context
|
|
236
|
+
updateContext: async (orderDeliveryId, context) => {
|
|
194
237
|
const selector = buildFindByIdSelector(orderDeliveryId);
|
|
195
238
|
if (!context || Object.keys(context).length === 0) return OrderDeliveries.findOne(selector, {});
|
|
196
|
-
|
|
197
|
-
log(`OrderDelivery ${orderDeliveryId} -> Update Context`, { context });
|
|
198
239
|
const contextSetters = Object.fromEntries(
|
|
199
240
|
Object.entries(context).map(([key, value]) => [`context.${key}`, value]),
|
|
200
241
|
);
|
|
@@ -211,7 +252,6 @@ export const configureOrderDeliveriesModule = ({
|
|
|
211
252
|
);
|
|
212
253
|
|
|
213
254
|
if (result.ok) {
|
|
214
|
-
await updateCalculation(result.value.orderId, unchainedAPI);
|
|
215
255
|
await emit('ORDER_UPDATE_DELIVERY', {
|
|
216
256
|
orderDelivery: result.value,
|
|
217
257
|
});
|
|
@@ -224,10 +264,6 @@ export const configureOrderDeliveriesModule = ({
|
|
|
224
264
|
updateStatus,
|
|
225
265
|
|
|
226
266
|
updateCalculation: async (orderDelivery, unchainedAPI) => {
|
|
227
|
-
log(`OrderDelivery ${orderDelivery._id} -> Update Calculation`, {
|
|
228
|
-
orderId: orderDelivery.orderId,
|
|
229
|
-
});
|
|
230
|
-
|
|
231
267
|
const calculation = await unchainedAPI.modules.delivery.calculate(
|
|
232
268
|
{
|
|
233
269
|
item: orderDelivery,
|
|
@@ -1,12 +1,50 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { OrdersModule } from '@unchainedshop/types/orders.js';
|
|
3
|
-
import { OrderDiscount, OrderDiscountsModule } from '@unchainedshop/types/orders.discounts.js';
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/core';
|
|
4
2
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
5
|
-
import {
|
|
6
|
-
import { generateDbFilterById, generateDbMutations, mongodb } from '@unchainedshop/mongodb';
|
|
7
|
-
import { OrderDiscountsSchema } from '../db/OrderDiscountsSchema.js';
|
|
3
|
+
import { generateDbFilterById, generateDbObjectId, mongodb } from '@unchainedshop/mongodb';
|
|
8
4
|
import { OrderDiscountTrigger } from '../db/OrderDiscountTrigger.js';
|
|
9
5
|
import { OrderDiscountDirector } from '../director/OrderDiscountDirector.js';
|
|
6
|
+
import { Order, OrderDiscount } from '../types.js';
|
|
7
|
+
import {
|
|
8
|
+
IPricingSheet,
|
|
9
|
+
PricingCalculation,
|
|
10
|
+
DiscountAdapterActions,
|
|
11
|
+
DiscountContext,
|
|
12
|
+
} from '@unchainedshop/utils';
|
|
13
|
+
|
|
14
|
+
export type OrderDiscountsModule = {
|
|
15
|
+
// Queries
|
|
16
|
+
findOrderDiscount: (
|
|
17
|
+
params: { discountId: string },
|
|
18
|
+
options?: mongodb.FindOptions,
|
|
19
|
+
) => Promise<OrderDiscount>;
|
|
20
|
+
findOrderDiscounts: (params: { orderId: string }) => Promise<Array<OrderDiscount>>;
|
|
21
|
+
|
|
22
|
+
// Transformations
|
|
23
|
+
interface: (
|
|
24
|
+
orderDiscount: OrderDiscount,
|
|
25
|
+
unchainedAPI: UnchainedCore,
|
|
26
|
+
) => Promise<DiscountAdapterActions<any>>;
|
|
27
|
+
|
|
28
|
+
isValid: (orderDiscount: OrderDiscount, unchainedAPI: UnchainedCore) => Promise<boolean>;
|
|
29
|
+
|
|
30
|
+
// Adapter
|
|
31
|
+
configurationForPricingAdapterKey: (
|
|
32
|
+
orderDiscount: OrderDiscount,
|
|
33
|
+
adapterKey: string,
|
|
34
|
+
calculationSheet: IPricingSheet<PricingCalculation>,
|
|
35
|
+
pricingContext: DiscountContext & UnchainedCore,
|
|
36
|
+
) => Promise<any>;
|
|
37
|
+
|
|
38
|
+
// Mutations
|
|
39
|
+
createManualOrderDiscount: (
|
|
40
|
+
params: { code: string; order: Order },
|
|
41
|
+
unchainedAPI: UnchainedCore,
|
|
42
|
+
) => Promise<OrderDiscount>;
|
|
43
|
+
|
|
44
|
+
create: (doc: OrderDiscount) => Promise<OrderDiscount>;
|
|
45
|
+
update: (orderDiscountId: string, doc: OrderDiscount) => Promise<OrderDiscount>;
|
|
46
|
+
delete: (orderDiscountId: string, unchainedAPI: UnchainedCore) => Promise<OrderDiscount>;
|
|
47
|
+
};
|
|
10
48
|
|
|
11
49
|
const ORDER_DISCOUNT_EVENTS: string[] = [
|
|
12
50
|
'ORDER_CREATE_DISCOUNT',
|
|
@@ -25,18 +63,11 @@ export const buildFindByIdSelector = (orderDiscountId: string) =>
|
|
|
25
63
|
|
|
26
64
|
export const configureOrderDiscountsModule = ({
|
|
27
65
|
OrderDiscounts,
|
|
28
|
-
updateCalculation,
|
|
29
66
|
}: {
|
|
30
67
|
OrderDiscounts: mongodb.Collection<OrderDiscount>;
|
|
31
|
-
updateCalculation: OrdersModule['updateCalculation'];
|
|
32
68
|
}): OrderDiscountsModule => {
|
|
33
69
|
registerEvents(ORDER_DISCOUNT_EVENTS);
|
|
34
70
|
|
|
35
|
-
const mutations = generateDbMutations<OrderDiscount>(OrderDiscounts, OrderDiscountsSchema, {
|
|
36
|
-
permanentlyDeleteByDefault: true,
|
|
37
|
-
hasCreateOnly: false,
|
|
38
|
-
}) as ModuleMutations<OrderDiscount>;
|
|
39
|
-
|
|
40
71
|
const getAdapter = async (orderDiscount: OrderDiscount, unchainedAPI: UnchainedCore) => {
|
|
41
72
|
const order = await unchainedAPI.modules.orders.findOrder({
|
|
42
73
|
orderId: orderDiscount.orderId,
|
|
@@ -51,12 +82,12 @@ export const configureOrderDiscountsModule = ({
|
|
|
51
82
|
|
|
52
83
|
const createDiscount: OrderDiscountsModule['create'] = async (doc) => {
|
|
53
84
|
const normalizedTrigger = doc.trigger || OrderDiscountTrigger.USER;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
85
|
+
const { insertedId: discountId } = await OrderDiscounts.insertOne({
|
|
86
|
+
_id: generateDbObjectId(),
|
|
87
|
+
created: new Date(),
|
|
88
|
+
...doc,
|
|
89
|
+
trigger: normalizedTrigger,
|
|
57
90
|
});
|
|
58
|
-
|
|
59
|
-
const discountId = await mutations.create({ ...doc, trigger: normalizedTrigger });
|
|
60
91
|
const discount = await OrderDiscounts.findOne(buildFindByIdSelector(discountId));
|
|
61
92
|
return discount;
|
|
62
93
|
};
|
|
@@ -64,33 +95,28 @@ export const configureOrderDiscountsModule = ({
|
|
|
64
95
|
const deleteDiscount: OrderDiscountsModule['delete'] = async (orderDiscountId, unchainedAPI) => {
|
|
65
96
|
const selector = buildFindByIdSelector(orderDiscountId);
|
|
66
97
|
const discount = await OrderDiscounts.findOne(selector, {});
|
|
67
|
-
|
|
68
|
-
log(`OrderDiscounts -> Remove Discount ${orderDiscountId}`, {
|
|
69
|
-
orderId: discount.orderId,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
98
|
if (discount.trigger === OrderDiscountTrigger.USER) {
|
|
73
99
|
// Release
|
|
74
100
|
const adapter = await getAdapter(discount, unchainedAPI);
|
|
75
101
|
if (!adapter) return null;
|
|
76
102
|
await adapter.release();
|
|
77
|
-
|
|
78
|
-
await OrderDiscounts.deleteOne(selector);
|
|
79
|
-
await updateCalculation(discount.orderId, unchainedAPI);
|
|
80
|
-
} else {
|
|
81
|
-
await OrderDiscounts.deleteOne(selector);
|
|
82
103
|
}
|
|
104
|
+
await OrderDiscounts.deleteOne(selector);
|
|
83
105
|
await emit('ORDER_REMOVE_DISCOUNT', { discount });
|
|
84
106
|
return discount;
|
|
85
107
|
};
|
|
86
108
|
|
|
87
109
|
const updateDiscount: OrderDiscountsModule['update'] = async (orderDiscountId, doc) => {
|
|
88
|
-
await
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
110
|
+
const discount = await OrderDiscounts.findOneAndUpdate(
|
|
111
|
+
generateDbFilterById(orderDiscountId),
|
|
112
|
+
{
|
|
113
|
+
$set: {
|
|
114
|
+
updated: new Date(),
|
|
115
|
+
...doc,
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
{ returnDocument: 'after' },
|
|
119
|
+
);
|
|
94
120
|
await emit('ORDER_UPDATE_DISCOUNT', { discount });
|
|
95
121
|
return discount;
|
|
96
122
|
};
|
|
@@ -110,15 +136,10 @@ export const configureOrderDiscountsModule = ({
|
|
|
110
136
|
{ code, orderId }: { code: string; orderId: string },
|
|
111
137
|
unchainedAPI: UnchainedCore,
|
|
112
138
|
) => {
|
|
113
|
-
log(`OrderDiscounts -> Try to grab ${code}`, { orderId });
|
|
114
|
-
|
|
115
139
|
const existingDiscount = await OrderDiscounts.findOne({ code, orderId });
|
|
116
|
-
|
|
117
140
|
if (existingDiscount) throw new Error(OrderDiscountErrorCode.CODE_ALREADY_PRESENT);
|
|
118
|
-
|
|
119
141
|
const discount = await OrderDiscounts.findOne({ code, orderId: null });
|
|
120
142
|
if (!discount) return null;
|
|
121
|
-
|
|
122
143
|
const discountId = discount._id;
|
|
123
144
|
try {
|
|
124
145
|
const updatedDiscount = await updateDiscount(discountId, { orderId });
|
|
@@ -199,7 +220,6 @@ export const configureOrderDiscountsModule = ({
|
|
|
199
220
|
|
|
200
221
|
try {
|
|
201
222
|
const reservedDiscount = await reserveDiscount(newDiscount, unchainedAPI);
|
|
202
|
-
await updateCalculation(order._id, unchainedAPI);
|
|
203
223
|
await emit('ORDER_ADD_DISCOUNT', { discount: reserveDiscount });
|
|
204
224
|
return reservedDiscount;
|
|
205
225
|
} catch (error) {
|
|
@@ -224,10 +244,17 @@ export const configureOrderDiscountsModule = ({
|
|
|
224
244
|
delete: deleteDiscount,
|
|
225
245
|
|
|
226
246
|
update: async (orderDiscountId, doc) => {
|
|
227
|
-
await
|
|
247
|
+
const discount = await OrderDiscounts.findOneAndUpdate(
|
|
248
|
+
generateDbFilterById(orderDiscountId),
|
|
249
|
+
{
|
|
250
|
+
$set: {
|
|
251
|
+
updated: new Date(),
|
|
252
|
+
...doc,
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
{ returnDocument: 'after' },
|
|
256
|
+
);
|
|
228
257
|
|
|
229
|
-
const selector = buildFindByIdSelector(orderDiscountId);
|
|
230
|
-
const discount = await OrderDiscounts.findOne(selector, {});
|
|
231
258
|
await emit('ORDER_UPDATE_DISCOUNT', { discount });
|
|
232
259
|
return discount;
|
|
233
260
|
},
|
|
@@ -1,14 +1,94 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { OrdersModule } from '@unchainedshop/types/orders.js';
|
|
3
|
-
import {
|
|
4
|
-
OrderPayment,
|
|
5
|
-
OrderPaymentsModule,
|
|
6
|
-
OrderPaymentStatus,
|
|
7
|
-
} from '@unchainedshop/types/orders.payments.js';
|
|
1
|
+
import { UnchainedCore } from '@unchainedshop/core';
|
|
8
2
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
3
|
+
import { generateDbFilterById, generateDbObjectId, mongodb } from '@unchainedshop/mongodb';
|
|
4
|
+
import { Order, OrderDiscount, OrderPayment, OrderPaymentStatus } from '../types.js';
|
|
5
|
+
import { IPaymentPricingSheet } from '@unchainedshop/core-payment';
|
|
6
|
+
import { OrderPricingDiscount } from '../director/OrderPricingDirector.js';
|
|
7
|
+
|
|
8
|
+
export type OrderPaymentsModule = {
|
|
9
|
+
// Queries
|
|
10
|
+
findOrderPayment: (
|
|
11
|
+
params: {
|
|
12
|
+
orderPaymentId: string;
|
|
13
|
+
},
|
|
14
|
+
options?: mongodb.FindOptions,
|
|
15
|
+
) => Promise<OrderPayment>;
|
|
16
|
+
|
|
17
|
+
findOrderPaymentByContextData: (
|
|
18
|
+
params: {
|
|
19
|
+
context: any;
|
|
20
|
+
},
|
|
21
|
+
options?: mongodb.FindOptions,
|
|
22
|
+
) => Promise<OrderPayment>;
|
|
23
|
+
|
|
24
|
+
countOrderPaymentsByContextData: (
|
|
25
|
+
params: {
|
|
26
|
+
context: any;
|
|
27
|
+
},
|
|
28
|
+
options?: mongodb.FindOptions,
|
|
29
|
+
) => Promise<number>;
|
|
30
|
+
|
|
31
|
+
// Transformations
|
|
32
|
+
discounts: (
|
|
33
|
+
orderPayment: OrderPayment,
|
|
34
|
+
params: { order: Order; orderDiscount: OrderDiscount },
|
|
35
|
+
unchainedAPI: UnchainedCore,
|
|
36
|
+
) => Array<OrderPricingDiscount>;
|
|
37
|
+
isBlockingOrderConfirmation: (
|
|
38
|
+
orderPayment: OrderPayment,
|
|
39
|
+
unchainedAPI: UnchainedCore,
|
|
40
|
+
) => Promise<boolean>;
|
|
41
|
+
isBlockingOrderFullfillment: (orderPayment: OrderPayment) => boolean;
|
|
42
|
+
normalizedStatus: (orderPayment: OrderPayment) => string;
|
|
43
|
+
pricingSheet: (
|
|
44
|
+
orderPayment: OrderPayment,
|
|
45
|
+
currency: string,
|
|
46
|
+
unchainedAPI: UnchainedCore,
|
|
47
|
+
) => IPaymentPricingSheet;
|
|
48
|
+
|
|
49
|
+
// Mutations
|
|
50
|
+
create: (doc: OrderPayment) => Promise<OrderPayment>;
|
|
51
|
+
|
|
52
|
+
cancel: (
|
|
53
|
+
orderPayment: OrderPayment,
|
|
54
|
+
paymentContext: {
|
|
55
|
+
transactionContext: any;
|
|
56
|
+
userId: string;
|
|
57
|
+
},
|
|
58
|
+
unchainedAPI: UnchainedCore,
|
|
59
|
+
) => Promise<OrderPayment>;
|
|
60
|
+
|
|
61
|
+
confirm: (
|
|
62
|
+
orderPayment: OrderPayment,
|
|
63
|
+
paymentContext: {
|
|
64
|
+
transactionContext: any;
|
|
65
|
+
userId: string;
|
|
66
|
+
},
|
|
67
|
+
unchainedAPI: UnchainedCore,
|
|
68
|
+
) => Promise<OrderPayment>;
|
|
69
|
+
|
|
70
|
+
charge: (
|
|
71
|
+
orderPayment: OrderPayment,
|
|
72
|
+
paymentContext: {
|
|
73
|
+
transactionContext: any;
|
|
74
|
+
userId: string;
|
|
75
|
+
},
|
|
76
|
+
unchainedAPI: UnchainedCore,
|
|
77
|
+
) => Promise<OrderPayment>;
|
|
78
|
+
|
|
79
|
+
logEvent: (orderPaymentId: string, event: any) => Promise<boolean>;
|
|
80
|
+
|
|
81
|
+
markAsPaid: (payment: OrderPayment, meta: any) => Promise<void>;
|
|
82
|
+
|
|
83
|
+
updateContext: (orderPaymentId: string, context: any) => Promise<OrderPayment | null>;
|
|
84
|
+
|
|
85
|
+
updateStatus: (
|
|
86
|
+
orderPaymentId: string,
|
|
87
|
+
params: { transactionId?: string; status: OrderPaymentStatus; info?: string },
|
|
88
|
+
) => Promise<OrderPayment>;
|
|
89
|
+
|
|
90
|
+
updateCalculation: (orderPayment: OrderPayment, unchainedAPI: UnchainedCore) => Promise<OrderPayment>;
|
|
91
|
+
};
|
|
12
92
|
|
|
13
93
|
const ORDER_PAYMENT_EVENTS: string[] = ['ORDER_UPDATE_PAYMENT', 'ORDER_SIGN_PAYMENT', 'ORDER_PAY'];
|
|
14
94
|
|
|
@@ -35,18 +115,11 @@ export const buildFindByContextDataSelector = (context: any): mongodb.Filter<Ord
|
|
|
35
115
|
|
|
36
116
|
export const configureOrderPaymentsModule = ({
|
|
37
117
|
OrderPayments,
|
|
38
|
-
updateCalculation,
|
|
39
118
|
}: {
|
|
40
119
|
OrderPayments: mongodb.Collection<OrderPayment>;
|
|
41
|
-
updateCalculation: OrdersModule['updateCalculation'];
|
|
42
120
|
}): OrderPaymentsModule => {
|
|
43
121
|
registerEvents(ORDER_PAYMENT_EVENTS);
|
|
44
122
|
|
|
45
|
-
const mutations = generateDbMutations<OrderPayment>(OrderPayments, OrderPaymentsSchema, {
|
|
46
|
-
permanentlyDeleteByDefault: true,
|
|
47
|
-
hasCreateOnly: false,
|
|
48
|
-
}) as ModuleMutations<OrderPayment>;
|
|
49
|
-
|
|
50
123
|
const normalizedStatus: OrderPaymentsModule['normalizedStatus'] = (orderPayment) => {
|
|
51
124
|
return orderPayment.status === null
|
|
52
125
|
? OrderPaymentStatus.OPEN
|
|
@@ -70,8 +143,6 @@ export const configureOrderPaymentsModule = ({
|
|
|
70
143
|
orderPaymentId,
|
|
71
144
|
{ status, transactionId, info },
|
|
72
145
|
) => {
|
|
73
|
-
log(`OrderPayment ${orderPaymentId} -> New Status: ${status}`);
|
|
74
|
-
|
|
75
146
|
const date = new Date();
|
|
76
147
|
const modifier: mongodb.UpdateFilter<OrderPayment> = {
|
|
77
148
|
$set: { status, updated: new Date() },
|
|
@@ -155,7 +226,9 @@ export const configureOrderPaymentsModule = ({
|
|
|
155
226
|
// Mutations
|
|
156
227
|
|
|
157
228
|
create: async (doc) => {
|
|
158
|
-
const orderPaymentId = await
|
|
229
|
+
const { insertedId: orderPaymentId } = await OrderPayments.insertOne({
|
|
230
|
+
_id: generateDbObjectId(),
|
|
231
|
+
created: new Date(),
|
|
159
232
|
...doc,
|
|
160
233
|
status: null,
|
|
161
234
|
context: doc.context || {},
|
|
@@ -293,13 +366,10 @@ export const configureOrderPaymentsModule = ({
|
|
|
293
366
|
await emit('ORDER_PAY', { orderPayment });
|
|
294
367
|
},
|
|
295
368
|
|
|
296
|
-
updateContext: async (orderPaymentId, context
|
|
369
|
+
updateContext: async (orderPaymentId, context) => {
|
|
297
370
|
const selector = buildFindByIdSelector(orderPaymentId);
|
|
298
371
|
if (!context || Object.keys(context).length === 0) return OrderPayments.findOne(selector, {});
|
|
299
372
|
|
|
300
|
-
log(`OrderPayment ${orderPaymentId} -> Update Context`, {
|
|
301
|
-
context,
|
|
302
|
-
});
|
|
303
373
|
const contextSetters = Object.fromEntries(
|
|
304
374
|
Object.entries(context).map(([key, value]) => [`context.${key}`, value]),
|
|
305
375
|
);
|
|
@@ -315,7 +385,6 @@ export const configureOrderPaymentsModule = ({
|
|
|
315
385
|
);
|
|
316
386
|
|
|
317
387
|
if (result.ok) {
|
|
318
|
-
await updateCalculation(result.value.orderId, unchainedAPI);
|
|
319
388
|
await emit('ORDER_UPDATE_PAYMENT', {
|
|
320
389
|
orderPayment: result.value,
|
|
321
390
|
});
|
|
@@ -328,10 +397,6 @@ export const configureOrderPaymentsModule = ({
|
|
|
328
397
|
updateStatus,
|
|
329
398
|
|
|
330
399
|
updateCalculation: async (orderPayment, unchainedAPI) => {
|
|
331
|
-
log(`OrderPayment ${orderPayment._id} -> Update Calculation`, {
|
|
332
|
-
orderId: orderPayment.orderId,
|
|
333
|
-
});
|
|
334
|
-
|
|
335
400
|
const calculation = await unchainedAPI.modules.payment.paymentProviders.calculate(
|
|
336
401
|
{
|
|
337
402
|
item: orderPayment,
|