@unchainedshop/core-events 4.0.0-rc.13 → 4.0.0-rc.15
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/module/configureEventsModule.d.ts +5 -2
- package/lib/module/configureEventsModule.d.ts.map +1 -1
- package/lib/module/configureEventsModule.js +44 -38
- package/lib/module/configureEventsModule.js.map +1 -1
- package/package.json +3 -3
- package/src/module/buildFindSelector.test.ts +11 -5
- package/src/module/configureEventsModule.ts +58 -50
|
@@ -8,7 +8,10 @@ export interface EventReport {
|
|
|
8
8
|
export interface EventQuery {
|
|
9
9
|
types?: string[];
|
|
10
10
|
queryString?: string;
|
|
11
|
-
created?:
|
|
11
|
+
created?: {
|
|
12
|
+
end?: Date;
|
|
13
|
+
start?: Date;
|
|
14
|
+
};
|
|
12
15
|
}
|
|
13
16
|
export declare const buildFindSelector: ({ types, queryString, created }: EventQuery) => {
|
|
14
17
|
type?: any;
|
|
@@ -27,7 +30,7 @@ export declare const configureEventsModule: ({ db }: ModuleInput<Record<string,
|
|
|
27
30
|
}) => Promise<Event[]>;
|
|
28
31
|
type: (event: Event) => string;
|
|
29
32
|
count: (query: EventQuery) => Promise<number>;
|
|
30
|
-
getReport: ({ dateRange, types }?: {
|
|
33
|
+
getReport: ({ dateRange, types, }?: {
|
|
31
34
|
dateRange?: DateFilterInput;
|
|
32
35
|
types?: string[];
|
|
33
36
|
}) => Promise<EventReport[]>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configureEventsModule.d.ts","sourceRoot":"","sources":["../../src/module/configureEventsModule.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EAIP,WAAW,EAEZ,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAiB,UAAU,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAClF,OAAO,EAAoB,KAAK,EAAE,MAAM,2BAA2B,CAAC;AAGpE,MAAM,WAAW,WAAW;IAC1B,SAAS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AAE,CAAC;KACrB,KAAG,OAAO,CAAC,KAAK,EAAE,CAAC;kBASN,KAAK;mBAOE,UAAU;wCAQ5B;QAAE,SAAS,CAAC,EAAE,eAAe,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE,KAAQ,OAAO,CAAC,WAAW,EAAE,CAAC;EA+DrF,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,qBAAqB,CAAC,CAAC,CAAC"}
|
|
@@ -11,8 +11,11 @@ export const buildFindSelector = ({ types, queryString, created }) => {
|
|
|
11
11
|
assertDocumentDBCompatMode();
|
|
12
12
|
selector.$text = { $search: queryString };
|
|
13
13
|
}
|
|
14
|
-
if (created)
|
|
15
|
-
selector.created =
|
|
14
|
+
if (created) {
|
|
15
|
+
selector.created = created?.end
|
|
16
|
+
? { $gte: created.start || new Date(0), $lte: created.end }
|
|
17
|
+
: { $gte: created.start || new Date(0) };
|
|
18
|
+
}
|
|
16
19
|
return selector;
|
|
17
20
|
};
|
|
18
21
|
export const configureEventsModule = async ({ db }) => {
|
|
@@ -50,49 +53,48 @@ export const configureEventsModule = async ({ db }) => {
|
|
|
50
53
|
const count = await Events.countDocuments(buildFindSelector(query));
|
|
51
54
|
return count;
|
|
52
55
|
},
|
|
53
|
-
getReport: async ({ dateRange, types } = {
|
|
54
|
-
|
|
55
|
-
types: null,
|
|
56
|
-
}) => {
|
|
57
|
-
const pipeline = [];
|
|
58
|
-
const matchConditions = [];
|
|
59
|
-
// build date filter based on provided values it can be a range if both to and from is supplied
|
|
60
|
-
// a upper or lowe limit if either from or to is provided
|
|
61
|
-
// or all if none is provided
|
|
56
|
+
getReport: async ({ dateRange, types, } = {}) => {
|
|
57
|
+
const match = {};
|
|
62
58
|
if (dateRange?.start || dateRange?.end) {
|
|
63
|
-
const
|
|
64
|
-
if (dateRange
|
|
65
|
-
const fromDate = new Date(dateRange
|
|
66
|
-
|
|
67
|
-
$or: [{ created: { $gte: fromDate } }, { updated: { $gte: fromDate } }],
|
|
68
|
-
});
|
|
59
|
+
const conditions = [];
|
|
60
|
+
if (dateRange.start) {
|
|
61
|
+
const fromDate = new Date(dateRange.start);
|
|
62
|
+
conditions.push({ created: { $gte: fromDate } });
|
|
69
63
|
}
|
|
70
|
-
if (dateRange
|
|
71
|
-
const toDate = new Date(dateRange
|
|
72
|
-
|
|
73
|
-
$or: [{ created: { $lte: toDate } }, { updated: { $lte: toDate } }],
|
|
74
|
-
});
|
|
64
|
+
if (dateRange.end) {
|
|
65
|
+
const toDate = new Date(dateRange.end);
|
|
66
|
+
conditions.push({ created: { $lte: toDate } });
|
|
75
67
|
}
|
|
76
|
-
if (
|
|
77
|
-
|
|
68
|
+
if (conditions.length) {
|
|
69
|
+
match.$or = conditions;
|
|
78
70
|
}
|
|
79
71
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
matchConditions.push({ type: { $in: types } });
|
|
72
|
+
if (types?.length) {
|
|
73
|
+
match.type = { $in: types };
|
|
83
74
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
75
|
+
const pipeline = [
|
|
76
|
+
Object.keys(match).length ? { $match: match } : null,
|
|
77
|
+
{
|
|
78
|
+
$group: {
|
|
79
|
+
_id: {
|
|
80
|
+
type: '$type',
|
|
81
|
+
day: {
|
|
82
|
+
$dateToString: { format: '%Y-%m-%d', date: '$created' },
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
count: { $sum: 1 },
|
|
88
86
|
},
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
pipeline.push(...[
|
|
87
|
+
},
|
|
92
88
|
{
|
|
93
89
|
$group: {
|
|
94
|
-
_id: '$type',
|
|
95
|
-
|
|
90
|
+
_id: '$_id.type',
|
|
91
|
+
detail: {
|
|
92
|
+
$push: {
|
|
93
|
+
date: '$_id.day',
|
|
94
|
+
count: '$count',
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
emitCount: { $sum: '$count' },
|
|
96
98
|
},
|
|
97
99
|
},
|
|
98
100
|
{
|
|
@@ -100,10 +102,14 @@ export const configureEventsModule = async ({ db }) => {
|
|
|
100
102
|
_id: 0,
|
|
101
103
|
type: '$_id',
|
|
102
104
|
emitCount: 1,
|
|
105
|
+
detail: 1,
|
|
103
106
|
},
|
|
104
107
|
},
|
|
105
|
-
|
|
106
|
-
|
|
108
|
+
{ $sort: { type: 1 } },
|
|
109
|
+
].filter(Boolean);
|
|
110
|
+
const report = await Events.aggregate(pipeline).toArray();
|
|
111
|
+
console.log(report);
|
|
112
|
+
return report ?? [];
|
|
107
113
|
},
|
|
108
114
|
};
|
|
109
115
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configureEventsModule.js","sourceRoot":"","sources":["../../src/module/configureEventsModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,oBAAoB,EACpB,gBAAgB,EAChB,kBAAkB,EAElB,0BAA0B,GAC3B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,aAAa,EAA+B,MAAM,sBAAsB,CAAC;AAClF,OAAO,EAAE,gBAAgB,EAAS,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAC;AAajF,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,EAAE,KAAK,EAAE,WAAW,EAAE,OAAO,EAAc,EAAE,EAAE;IAC/E,MAAM,QAAQ,GAA+C,EAAE,CAAC;IAEhE,IAAI,KAAK,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,QAAQ,CAAC,IAAI,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;IAClE,IAAI,WAAW,EAAE,CAAC;QAChB,0BAA0B,EAAE,CAAC;QAC7B,QAAQ,CAAC,KAAK,GAAG,EAAE,OAAO,EAAE,WAAW,EAA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AAQ,GAAG;gBACf,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI;gBACpD;oBACE,MAAM,EAAE;wBACN,GAAG,EAAE;4BACH,IAAI,EAAE,OAAO;4BACb,GAAG,EAAE;gCACH,aAAa,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,EAAE;6BACxD;yBACF;wBACD,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE;qBACnB;iBACF;gBACD;oBACE,MAAM,EAAE;wBACN,GAAG,EAAE,WAAW;wBAChB,MAAM,EAAE;4BACN,KAAK,EAAE;gCACL,IAAI,EAAE,UAAU;gCAChB,KAAK,EAAE,QAAQ;6BAChB;yBACF;wBACD,SAAS,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE;qBAC9B;iBACF;gBACD;oBACE,QAAQ,EAAE;wBACR,GAAG,EAAE,CAAC;wBACN,IAAI,EAAE,MAAM;wBACZ,SAAS,EAAE,CAAC;wBACZ,MAAM,EAAE,CAAC;qBACV;iBACF;gBACD,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE;aACvB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;YAElB,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,SAAS,CAAc,QAAQ,CAAC,CAAC,OAAO,EAAE,CAAC;YACvE,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YACpB,OAAO,MAAM,IAAI,EAAE,CAAC;QACtB,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-events",
|
|
3
|
-
"version": "4.0.0-rc.
|
|
3
|
+
"version": "4.0.0-rc.15",
|
|
4
4
|
"main": "lib/events-index.js",
|
|
5
5
|
"types": "lib/events-index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@unchainedshop/events": "^4.0.0-rc.
|
|
35
|
-
"@unchainedshop/utils": "^4.0.0-rc.
|
|
34
|
+
"@unchainedshop/events": "^4.0.0-rc.15",
|
|
35
|
+
"@unchainedshop/utils": "^4.0.0-rc.15"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/node": "^24.0.13",
|
|
@@ -6,7 +6,7 @@ describe('buildFindSelector', () => {
|
|
|
6
6
|
it('Return correct filter object when passed create, queryString, types', () => {
|
|
7
7
|
assert.deepStrictEqual(
|
|
8
8
|
buildFindSelector({
|
|
9
|
-
created: new Date('2022-12-03T18:23:38.278Z'),
|
|
9
|
+
created: { start: new Date('2022-12-03T18:23:38.278Z') },
|
|
10
10
|
queryString: 'Hello world',
|
|
11
11
|
types: ['PRODUCT_CREATED'],
|
|
12
12
|
}),
|
|
@@ -20,7 +20,10 @@ describe('buildFindSelector', () => {
|
|
|
20
20
|
|
|
21
21
|
it('Return correct filter object when passed create, queryString', () => {
|
|
22
22
|
assert.deepStrictEqual(
|
|
23
|
-
buildFindSelector({
|
|
23
|
+
buildFindSelector({
|
|
24
|
+
created: { start: new Date('2022-12-03T18:23:38.278Z') },
|
|
25
|
+
queryString: 'Hello world',
|
|
26
|
+
}),
|
|
24
27
|
{
|
|
25
28
|
$text: { $search: 'Hello world' },
|
|
26
29
|
created: { $gte: new Date('2022-12-03T18:23:38.278Z') },
|
|
@@ -29,9 +32,12 @@ describe('buildFindSelector', () => {
|
|
|
29
32
|
});
|
|
30
33
|
|
|
31
34
|
it('Return correct filter object when passed create', () => {
|
|
32
|
-
assert.deepStrictEqual(
|
|
33
|
-
created: {
|
|
34
|
-
|
|
35
|
+
assert.deepStrictEqual(
|
|
36
|
+
buildFindSelector({ created: { start: new Date('2022-12-03T18:23:38.278Z') } }),
|
|
37
|
+
{
|
|
38
|
+
created: { $gte: new Date('2022-12-03T18:23:38.278Z') },
|
|
39
|
+
},
|
|
40
|
+
);
|
|
35
41
|
});
|
|
36
42
|
|
|
37
43
|
it('Return correct filter object when passed no argument', () => {
|
|
@@ -19,7 +19,7 @@ export interface EventReport {
|
|
|
19
19
|
export interface EventQuery {
|
|
20
20
|
types?: string[];
|
|
21
21
|
queryString?: string;
|
|
22
|
-
created?: Date;
|
|
22
|
+
created?: { end?: Date; start?: Date };
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export const buildFindSelector = ({ types, queryString, created }: EventQuery) => {
|
|
@@ -30,7 +30,11 @@ export const buildFindSelector = ({ types, queryString, created }: EventQuery) =
|
|
|
30
30
|
assertDocumentDBCompatMode();
|
|
31
31
|
selector.$text = { $search: queryString };
|
|
32
32
|
}
|
|
33
|
-
if (created)
|
|
33
|
+
if (created) {
|
|
34
|
+
selector.created = created?.end
|
|
35
|
+
? { $gte: created.start || new Date(0), $lte: created.end }
|
|
36
|
+
: { $gte: created.start || new Date(0) };
|
|
37
|
+
}
|
|
34
38
|
return selector;
|
|
35
39
|
};
|
|
36
40
|
|
|
@@ -89,66 +93,70 @@ export const configureEventsModule = async ({ db }: ModuleInput<Record<string, n
|
|
|
89
93
|
return count;
|
|
90
94
|
},
|
|
91
95
|
|
|
92
|
-
getReport: async (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const pipeline = [];
|
|
99
|
-
const matchConditions = [];
|
|
100
|
-
// build date filter based on provided values it can be a range if both to and from is supplied
|
|
101
|
-
// a upper or lowe limit if either from or to is provided
|
|
102
|
-
// or all if none is provided
|
|
96
|
+
getReport: async ({
|
|
97
|
+
dateRange,
|
|
98
|
+
types,
|
|
99
|
+
}: { dateRange?: DateFilterInput; types?: string[] } = {}): Promise<EventReport[]> => {
|
|
100
|
+
const match: any = {};
|
|
101
|
+
|
|
103
102
|
if (dateRange?.start || dateRange?.end) {
|
|
104
|
-
const
|
|
105
|
-
if (dateRange
|
|
106
|
-
const fromDate = new Date(dateRange
|
|
107
|
-
|
|
108
|
-
$or: [{ created: { $gte: fromDate } }, { updated: { $gte: fromDate } }],
|
|
109
|
-
});
|
|
103
|
+
const conditions: any[] = [];
|
|
104
|
+
if (dateRange.start) {
|
|
105
|
+
const fromDate = new Date(dateRange.start);
|
|
106
|
+
conditions.push({ created: { $gte: fromDate } });
|
|
110
107
|
}
|
|
111
|
-
if (dateRange
|
|
112
|
-
const toDate = new Date(dateRange
|
|
113
|
-
|
|
114
|
-
$or: [{ created: { $lte: toDate } }, { updated: { $lte: toDate } }],
|
|
115
|
-
});
|
|
108
|
+
if (dateRange.end) {
|
|
109
|
+
const toDate = new Date(dateRange.end);
|
|
110
|
+
conditions.push({ created: { $lte: toDate } });
|
|
116
111
|
}
|
|
117
|
-
if (
|
|
118
|
-
|
|
112
|
+
if (conditions.length) {
|
|
113
|
+
match.$or = conditions;
|
|
119
114
|
}
|
|
120
115
|
}
|
|
121
|
-
|
|
122
|
-
if (types
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
if (matchConditions.length > 0) {
|
|
126
|
-
pipeline.push({
|
|
127
|
-
$match: {
|
|
128
|
-
$and: matchConditions,
|
|
129
|
-
},
|
|
130
|
-
});
|
|
116
|
+
|
|
117
|
+
if (types?.length) {
|
|
118
|
+
match.type = { $in: types };
|
|
131
119
|
}
|
|
132
120
|
|
|
133
|
-
pipeline
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
121
|
+
const pipeline = [
|
|
122
|
+
Object.keys(match).length ? { $match: match } : null,
|
|
123
|
+
{
|
|
124
|
+
$group: {
|
|
125
|
+
_id: {
|
|
126
|
+
type: '$type',
|
|
127
|
+
day: {
|
|
128
|
+
$dateToString: { format: '%Y-%m-%d', date: '$created' },
|
|
129
|
+
},
|
|
139
130
|
},
|
|
131
|
+
count: { $sum: 1 },
|
|
140
132
|
},
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
$group: {
|
|
136
|
+
_id: '$_id.type',
|
|
137
|
+
detail: {
|
|
138
|
+
$push: {
|
|
139
|
+
date: '$_id.day',
|
|
140
|
+
count: '$count',
|
|
141
|
+
},
|
|
146
142
|
},
|
|
143
|
+
emitCount: { $sum: '$count' },
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
$project: {
|
|
148
|
+
_id: 0,
|
|
149
|
+
type: '$_id',
|
|
150
|
+
emitCount: 1,
|
|
151
|
+
detail: 1,
|
|
147
152
|
},
|
|
148
|
-
|
|
149
|
-
|
|
153
|
+
},
|
|
154
|
+
{ $sort: { type: 1 } },
|
|
155
|
+
].filter(Boolean);
|
|
150
156
|
|
|
151
|
-
|
|
157
|
+
const report = await Events.aggregate<EventReport>(pipeline).toArray();
|
|
158
|
+
console.log(report);
|
|
159
|
+
return report ?? [];
|
|
152
160
|
},
|
|
153
161
|
};
|
|
154
162
|
};
|