@unchainedshop/core-enrollments 3.0.0-alpha7 → 3.0.0-rc2
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/EnrollmentsCollection.d.ts +50 -1
- package/lib/db/EnrollmentsCollection.d.ts.map +1 -1
- package/lib/db/EnrollmentsCollection.js +7 -0
- package/lib/db/EnrollmentsCollection.js.map +1 -1
- package/lib/director/EnrollmentAdapter.js.map +1 -1
- package/lib/enrollments-index.d.ts +1 -4
- package/lib/enrollments-index.d.ts.map +1 -1
- package/lib/enrollments-index.js +1 -4
- package/lib/enrollments-index.js.map +1 -1
- package/lib/enrollments-settings.d.ts +3 -3
- package/lib/enrollments-settings.d.ts.map +1 -1
- package/lib/enrollments-settings.js.map +1 -1
- package/lib/module/configureEnrollmentsModule.d.ts +41 -54
- package/lib/module/configureEnrollmentsModule.d.ts.map +1 -1
- package/lib/module/configureEnrollmentsModule.js +22 -143
- package/lib/module/configureEnrollmentsModule.js.map +1 -1
- package/lib/module/configureEnrollmentsModule_BACKUP_54085.d.ts +59 -0
- package/lib/module/configureEnrollmentsModule_BACKUP_54085.d.ts.map +1 -0
- package/lib/module/configureEnrollmentsModule_BACKUP_54085.js +203 -0
- package/lib/module/configureEnrollmentsModule_BACKUP_54085.js.map +1 -0
- package/lib/module/configureEnrollmentsModule_BASE_54085.d.ts +70 -0
- package/lib/module/configureEnrollmentsModule_BASE_54085.d.ts.map +1 -0
- package/lib/module/configureEnrollmentsModule_BASE_54085.js +324 -0
- package/lib/module/configureEnrollmentsModule_BASE_54085.js.map +1 -0
- package/lib/module/configureEnrollmentsModule_LOCAL_54085.d.ts +58 -0
- package/lib/module/configureEnrollmentsModule_LOCAL_54085.d.ts.map +1 -0
- package/lib/module/configureEnrollmentsModule_LOCAL_54085.js +196 -0
- package/lib/module/configureEnrollmentsModule_LOCAL_54085.js.map +1 -0
- package/lib/module/configureEnrollmentsModule_REMOTE_54085.d.ts +71 -0
- package/lib/module/configureEnrollmentsModule_REMOTE_54085.d.ts.map +1 -0
- package/lib/module/configureEnrollmentsModule_REMOTE_54085.js +331 -0
- package/lib/module/configureEnrollmentsModule_REMOTE_54085.js.map +1 -0
- package/lib/types.d.ts +8 -9
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js.map +1 -1
- package/package.json +10 -13
- package/src/db/EnrollmentsCollection.ts +49 -1
- package/src/enrollments-index.ts +1 -6
- package/src/enrollments-settings.ts +2 -3
- package/src/module/buildFindSelector.test.ts +1 -1
- package/src/module/configureEnrollmentsModule.ts +77 -339
- package/tsconfig.json +5 -7
- package/src/db/EnrollmentStatus.ts +0 -6
- package/src/director/EnrollmentAdapter.ts +0 -82
- package/src/director/EnrollmentDirector.ts +0 -60
- package/src/director/EnrollmentError.ts +0 -6
- package/src/module/periodForReferenceDate.test.ts +0 -23
- package/src/types.ts +0 -113
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { EnrollmentData, IEnrollmentAdapter, IEnrollmentDirector } from '../types.js';
|
|
2
|
-
import { ProductPlan } from '@unchainedshop/core-products';
|
|
3
|
-
import { log, LogLevel } from '@unchainedshop/logger';
|
|
4
|
-
import { BaseDirector } from '@unchainedshop/utils';
|
|
5
|
-
|
|
6
|
-
const baseDirector = BaseDirector<IEnrollmentAdapter>('EnrollmentDirector', {
|
|
7
|
-
adapterSortKey: 'orderIndex',
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
const findAppropriateAdapters = (productPlan?: ProductPlan) =>
|
|
11
|
-
baseDirector.getAdapters({
|
|
12
|
-
adapterFilter: (Adapter: IEnrollmentAdapter) => {
|
|
13
|
-
const activated = Adapter.isActivatedFor(productPlan);
|
|
14
|
-
if (!activated) {
|
|
15
|
-
log(`Enrollment Director -> ${Adapter.key} (${Adapter.version}) skipped`, {
|
|
16
|
-
level: LogLevel.Warning,
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
return activated;
|
|
20
|
-
},
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
export const EnrollmentDirector: IEnrollmentDirector = {
|
|
24
|
-
...baseDirector,
|
|
25
|
-
|
|
26
|
-
transformOrderItemToEnrollment: async ({ orderPosition, product }, doc, unchainedAPI) => {
|
|
27
|
-
const Adapter = findAppropriateAdapters(product.plan)?.[0];
|
|
28
|
-
if (!Adapter) {
|
|
29
|
-
throw new Error('No suitable enrollment plugin available for this item');
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const enrollmentPlan = await Adapter.transformOrderItemToEnrollmentPlan(orderPosition, unchainedAPI);
|
|
33
|
-
|
|
34
|
-
const enrollmentData: EnrollmentData = {
|
|
35
|
-
...doc,
|
|
36
|
-
...enrollmentPlan,
|
|
37
|
-
configuration: [],
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
return enrollmentData;
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
actions: async (enrollmentContext, unchainedAPI) => {
|
|
44
|
-
const context = { ...enrollmentContext, ...unchainedAPI };
|
|
45
|
-
|
|
46
|
-
// Resolve adapter
|
|
47
|
-
const product = await context.modules.products.findProduct({
|
|
48
|
-
productId: context.enrollment.productId,
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
const Adapter = findAppropriateAdapters(product?.plan)?.[0];
|
|
52
|
-
|
|
53
|
-
if (!Adapter) {
|
|
54
|
-
throw new Error('No suitable enrollment plugin available for this plan configuration');
|
|
55
|
-
}
|
|
56
|
-
const adapter = Adapter.actions(context);
|
|
57
|
-
|
|
58
|
-
return adapter;
|
|
59
|
-
},
|
|
60
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { periodForReferenceDate } from '../director/EnrollmentAdapter.js';
|
|
2
|
-
|
|
3
|
-
describe('periodForReferenceDate', () => {
|
|
4
|
-
it('Should return 1 week interval from When passed a given date', () => {
|
|
5
|
-
expect(periodForReferenceDate(new Date('2022-12-03T17:00:00.000Z'))).toEqual({
|
|
6
|
-
start: new Date('2022-12-03T17:00:00.000Z'),
|
|
7
|
-
end: new Date('2022-12-10T17:00:00.000Z'),
|
|
8
|
-
});
|
|
9
|
-
});
|
|
10
|
-
it('Should return 2 week interval from When passed 2 as interval', () => {
|
|
11
|
-
expect(periodForReferenceDate(new Date('2022-12-03T17:00:00.000Z'), 2)).toEqual({
|
|
12
|
-
start: new Date('2022-12-03T17:00:00.000Z'),
|
|
13
|
-
end: new Date('2022-12-17T17:00:00.000Z'),
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('Should return 2 HOURS when interval is set to HOURS', () => {
|
|
18
|
-
expect(periodForReferenceDate(new Date('2022-12-03T17:00:00.000Z'), 2, 'HOURS')).toEqual({
|
|
19
|
-
start: new Date('2022-12-03T17:00:00.000Z'),
|
|
20
|
-
end: new Date('2022-12-03T19:00:00.000Z'),
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
});
|
package/src/types.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import type { IBaseAdapter, IBaseDirector } from '@unchainedshop/utils';
|
|
2
|
-
import type { TimestampFields, LogFields, Address, Contact } from '@unchainedshop/mongodb';
|
|
3
|
-
import { Product, ProductPlan } from '@unchainedshop/core-products';
|
|
4
|
-
import { OrderPosition } from '@unchainedshop/core-orders';
|
|
5
|
-
import { UnchainedCore } from '@unchainedshop/core';
|
|
6
|
-
|
|
7
|
-
export enum EnrollmentStatus {
|
|
8
|
-
INITIAL = 'INITIAL',
|
|
9
|
-
ACTIVE = 'ACTIVE',
|
|
10
|
-
PAUSED = 'PAUSED',
|
|
11
|
-
TERMINATED = 'TERMINATED',
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface EnrollmentPeriod {
|
|
15
|
-
start: Date;
|
|
16
|
-
end: Date;
|
|
17
|
-
orderId?: string;
|
|
18
|
-
isTrial?: boolean;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface EnrollmentPlan {
|
|
22
|
-
configuration: Array<{ key: string; value: string }>;
|
|
23
|
-
productId: string;
|
|
24
|
-
quantity: number;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export type EnrollmentQuery = {
|
|
28
|
-
status?: Array<EnrollmentStatus>;
|
|
29
|
-
userId?: string;
|
|
30
|
-
queryString?: string;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export type Enrollment = {
|
|
34
|
-
_id?: string;
|
|
35
|
-
billingAddress: Address;
|
|
36
|
-
configuration: Array<{ key: string; value: string }>;
|
|
37
|
-
contact: Contact;
|
|
38
|
-
context?: any;
|
|
39
|
-
countryCode?: string;
|
|
40
|
-
currencyCode?: string;
|
|
41
|
-
delivery: {
|
|
42
|
-
deliveryProviderId?: string;
|
|
43
|
-
context?: any;
|
|
44
|
-
};
|
|
45
|
-
enrollmentNumber?: string;
|
|
46
|
-
expires?: Date;
|
|
47
|
-
meta?: any;
|
|
48
|
-
payment: {
|
|
49
|
-
paymentProviderId?: string;
|
|
50
|
-
context?: any;
|
|
51
|
-
};
|
|
52
|
-
periods: Array<EnrollmentPeriod>;
|
|
53
|
-
productId: string;
|
|
54
|
-
quantity?: number;
|
|
55
|
-
status: string;
|
|
56
|
-
userId: string;
|
|
57
|
-
} & LogFields &
|
|
58
|
-
TimestampFields;
|
|
59
|
-
|
|
60
|
-
// Director
|
|
61
|
-
|
|
62
|
-
export type EnrollmentContext = {
|
|
63
|
-
enrollment: Enrollment;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
export interface EnrollmentAdapterActions {
|
|
67
|
-
configurationForOrder: (params: {
|
|
68
|
-
period: EnrollmentPeriod;
|
|
69
|
-
products: Array<Product>;
|
|
70
|
-
}) => Promise<any>;
|
|
71
|
-
isOverdue: () => Promise<boolean>;
|
|
72
|
-
isValidForActivation: () => Promise<boolean>;
|
|
73
|
-
nextPeriod: () => Promise<EnrollmentPeriod>;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export type IEnrollmentAdapter = IBaseAdapter & {
|
|
77
|
-
isActivatedFor: (productPlan?: ProductPlan) => boolean;
|
|
78
|
-
|
|
79
|
-
transformOrderItemToEnrollmentPlan: (
|
|
80
|
-
orderPosition: OrderPosition,
|
|
81
|
-
unchainedAPI: UnchainedCore,
|
|
82
|
-
) => Promise<EnrollmentPlan>;
|
|
83
|
-
|
|
84
|
-
actions: (params: EnrollmentContext & UnchainedCore) => EnrollmentAdapterActions;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
export interface EnrollmentData {
|
|
88
|
-
billingAddress: Address;
|
|
89
|
-
configuration?: Array<{ key: string; value: string }>;
|
|
90
|
-
contact: Contact;
|
|
91
|
-
countryCode?: string;
|
|
92
|
-
currencyCode?: string;
|
|
93
|
-
delivery: Enrollment['delivery'];
|
|
94
|
-
meta?: any;
|
|
95
|
-
orderIdForFirstPeriod?: string;
|
|
96
|
-
payment: Enrollment['payment'];
|
|
97
|
-
productId: string;
|
|
98
|
-
quantity: number;
|
|
99
|
-
userId: string;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export type IEnrollmentDirector = IBaseDirector<IEnrollmentAdapter> & {
|
|
103
|
-
transformOrderItemToEnrollment: (
|
|
104
|
-
item: { orderPosition: OrderPosition; product: Product },
|
|
105
|
-
doc: Omit<EnrollmentData, 'configuration' | 'productId' | 'quantity'>,
|
|
106
|
-
unchainedAPI: UnchainedCore,
|
|
107
|
-
) => Promise<EnrollmentData>;
|
|
108
|
-
|
|
109
|
-
actions: (
|
|
110
|
-
enrollmentContext: EnrollmentContext,
|
|
111
|
-
unchainedAPI: UnchainedCore,
|
|
112
|
-
) => Promise<EnrollmentAdapterActions>;
|
|
113
|
-
};
|