@unchainedshop/core-events 4.0.0-rc.9 → 4.0.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/lib/db/EventsCollection.d.ts +1 -1
- package/lib/db/EventsCollection.d.ts.map +1 -1
- package/lib/db/EventsCollection.js +14 -10
- package/lib/db/EventsCollection.js.map +1 -1
- package/lib/db/EventsSchema.d.ts +3 -0
- package/lib/db/EventsSchema.d.ts.map +1 -0
- package/lib/db/EventsSchema.js +9 -0
- package/lib/db/EventsSchema.js.map +1 -0
- package/lib/module/configureEventsModule.d.ts +7 -4
- package/lib/module/configureEventsModule.d.ts.map +1 -1
- package/lib/module/configureEventsModule.js +46 -38
- package/lib/module/configureEventsModule.js.map +1 -1
- package/package.json +3 -3
- package/src/db/EventsCollection.ts +16 -11
- package/src/module/buildFindSelector.test.ts +11 -5
- package/src/module/configureEventsModule.ts +64 -52
- package/tsconfig.json +2 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EventsCollection.d.ts","sourceRoot":"","sources":["../../src/db/EventsCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiC,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAEhF,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAIzD,MAAM,MAAM,KAAK,GAAG;IAClB,GAAG,
|
|
1
|
+
{"version":3,"file":"EventsCollection.d.ts","sourceRoot":"","sources":["../../src/db/EventsCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiC,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAEhF,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAIzD,MAAM,MAAM,KAAK,GAAG;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC,GAAG,eAAe,CAAC;AAEpB,eAAO,MAAM,gBAAgB,GAAU,IAAI,OAAO,CAAC,EAAE,uCA2BpD,CAAC"}
|
|
@@ -3,22 +3,26 @@ import { buildDbIndexes } from '@unchainedshop/mongodb';
|
|
|
3
3
|
const TWO_DAYS_SEC = 172800;
|
|
4
4
|
export const EventsCollection = async (db) => {
|
|
5
5
|
const Events = db.collection('events');
|
|
6
|
+
if (!isDocumentDBCompatModeEnabled()) {
|
|
7
|
+
await buildDbIndexes(Events, [
|
|
8
|
+
{
|
|
9
|
+
index: { _id: 'text', type: 'text' },
|
|
10
|
+
options: {
|
|
11
|
+
weights: {
|
|
12
|
+
_id: 8,
|
|
13
|
+
type: 4,
|
|
14
|
+
},
|
|
15
|
+
name: 'events_fulltext_search',
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
]);
|
|
19
|
+
}
|
|
6
20
|
await buildDbIndexes(Events, [
|
|
7
21
|
{
|
|
8
22
|
index: { created: -1 },
|
|
9
23
|
options: { expireAfterSeconds: TWO_DAYS_SEC, name: 'created' },
|
|
10
24
|
},
|
|
11
25
|
{ index: { type: 1 }, options: { name: 'type' } },
|
|
12
|
-
!isDocumentDBCompatModeEnabled() && {
|
|
13
|
-
index: { _id: 'text', type: 'text' },
|
|
14
|
-
options: {
|
|
15
|
-
weights: {
|
|
16
|
-
_id: 8,
|
|
17
|
-
type: 4,
|
|
18
|
-
},
|
|
19
|
-
name: 'events_fulltext_search',
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
26
|
]);
|
|
23
27
|
return Events;
|
|
24
28
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EventsCollection.js","sourceRoot":"","sources":["../../src/db/EventsCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,6BAA6B,EAAW,MAAM,wBAAwB,CAAC;AAChF,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;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AC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EventsSchema.d.ts","sourceRoot":"","sources":["../../src/db/EventsSchema.ts"],"names":[],"mappings":"AACA,OAAO,YAAY,MAAM,cAAc,CAAC;AAExC,eAAO,MAAM,YAAY,cAQxB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Schemas } from '@unchainedshop/utils';
|
|
2
|
+
import SimpleSchema from 'simpl-schema';
|
|
3
|
+
export const EventsSchema = new SimpleSchema({
|
|
4
|
+
type: { type: String, required: true },
|
|
5
|
+
payload: { type: Object, blackbox: true },
|
|
6
|
+
context: { type: Object, blackbox: true },
|
|
7
|
+
...Schemas.timestampFields,
|
|
8
|
+
}, { requiredByDefault: false });
|
|
9
|
+
//# sourceMappingURL=EventsSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EventsSchema.js","sourceRoot":"","sources":["../../src/db/EventsSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,YAAY,MAAM,cAAc,CAAC;AAExC,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,YAAY,CAC1C;IACE,IAAI,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;IACtC,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;IACzC,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,EAAE;IACzC,GAAG,OAAO,CAAC,eAAe;CAC3B,EACD,EAAE,iBAAiB,EAAE,KAAK,EAAE,CAC7B,CAAC"}
|
|
@@ -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;
|
|
@@ -16,10 +19,10 @@ export declare const buildFindSelector: ({ types, queryString, created }: EventQ
|
|
|
16
19
|
created?: any;
|
|
17
20
|
};
|
|
18
21
|
export declare const configureEventsModule: ({ db }: ModuleInput<Record<string, never>>) => Promise<{
|
|
19
|
-
create: (doc: Event) => Promise<string>;
|
|
22
|
+
create: (doc: Omit<Event, "_id" | "created"> & Pick<Partial<Event>, "_id" | "created">) => Promise<string>;
|
|
20
23
|
findEvent: ({ eventId, ...rest }: mongodb.Filter<Event> & {
|
|
21
24
|
eventId: string;
|
|
22
|
-
}, options?: mongodb.FindOptions) => Promise<Event>;
|
|
25
|
+
}, options?: mongodb.FindOptions) => Promise<Event | null>;
|
|
23
26
|
findEvents: ({ limit, offset, sort, ...query }: EventQuery & {
|
|
24
27
|
limit?: number;
|
|
25
28
|
offset?: number;
|
|
@@ -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,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,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"}
|
|
@@ -3,6 +3,8 @@ import { getRegisteredEvents } from '@unchainedshop/events';
|
|
|
3
3
|
import { SortDirection } from '@unchainedshop/utils';
|
|
4
4
|
import { EventsCollection } from '../db/EventsCollection.js';
|
|
5
5
|
import { configureEventHistoryAdapter } from './configureEventHistoryAdapter.js';
|
|
6
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
7
|
+
const logger = createLogger('unchained:core-events');
|
|
6
8
|
export const buildFindSelector = ({ types, queryString, created }) => {
|
|
7
9
|
const selector = {};
|
|
8
10
|
if (types && Array.isArray(types))
|
|
@@ -11,8 +13,11 @@ export const buildFindSelector = ({ types, queryString, created }) => {
|
|
|
11
13
|
assertDocumentDBCompatMode();
|
|
12
14
|
selector.$text = { $search: queryString };
|
|
13
15
|
}
|
|
14
|
-
if (created)
|
|
15
|
-
selector.created =
|
|
16
|
+
if (created) {
|
|
17
|
+
selector.created = created?.end
|
|
18
|
+
? { $gte: created.start || new Date(0), $lte: created.end }
|
|
19
|
+
: { $gte: created.start || new Date(0) };
|
|
20
|
+
}
|
|
16
21
|
return selector;
|
|
17
22
|
};
|
|
18
23
|
export const configureEventsModule = async ({ db }) => {
|
|
@@ -50,49 +55,48 @@ export const configureEventsModule = async ({ db }) => {
|
|
|
50
55
|
const count = await Events.countDocuments(buildFindSelector(query));
|
|
51
56
|
return count;
|
|
52
57
|
},
|
|
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
|
|
58
|
+
getReport: async ({ dateRange, types, } = {}) => {
|
|
59
|
+
const match = {};
|
|
62
60
|
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
|
-
});
|
|
61
|
+
const conditions = [];
|
|
62
|
+
if (dateRange.start) {
|
|
63
|
+
const fromDate = new Date(dateRange.start);
|
|
64
|
+
conditions.push({ created: { $gte: fromDate } });
|
|
69
65
|
}
|
|
70
|
-
if (dateRange
|
|
71
|
-
const toDate = new Date(dateRange
|
|
72
|
-
|
|
73
|
-
$or: [{ created: { $lte: toDate } }, { updated: { $lte: toDate } }],
|
|
74
|
-
});
|
|
66
|
+
if (dateRange.end) {
|
|
67
|
+
const toDate = new Date(dateRange.end);
|
|
68
|
+
conditions.push({ created: { $lte: toDate } });
|
|
75
69
|
}
|
|
76
|
-
if (
|
|
77
|
-
|
|
70
|
+
if (conditions.length) {
|
|
71
|
+
match.$or = conditions;
|
|
78
72
|
}
|
|
79
73
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
matchConditions.push({ type: { $in: types } });
|
|
74
|
+
if (types?.length) {
|
|
75
|
+
match.type = { $in: types };
|
|
83
76
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
77
|
+
const pipeline = [
|
|
78
|
+
Object.keys(match).length ? { $match: match } : null,
|
|
79
|
+
{
|
|
80
|
+
$group: {
|
|
81
|
+
_id: {
|
|
82
|
+
type: '$type',
|
|
83
|
+
day: {
|
|
84
|
+
$dateToString: { format: '%Y-%m-%d', date: '$created' },
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
count: { $sum: 1 },
|
|
88
88
|
},
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
pipeline.push(...[
|
|
89
|
+
},
|
|
92
90
|
{
|
|
93
91
|
$group: {
|
|
94
|
-
_id: '$type',
|
|
95
|
-
|
|
92
|
+
_id: '$_id.type',
|
|
93
|
+
detail: {
|
|
94
|
+
$push: {
|
|
95
|
+
date: '$_id.day',
|
|
96
|
+
count: '$count',
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
emitCount: { $sum: '$count' },
|
|
96
100
|
},
|
|
97
101
|
},
|
|
98
102
|
{
|
|
@@ -100,10 +104,14 @@ export const configureEventsModule = async ({ db }) => {
|
|
|
100
104
|
_id: 0,
|
|
101
105
|
type: '$_id',
|
|
102
106
|
emitCount: 1,
|
|
107
|
+
detail: 1,
|
|
103
108
|
},
|
|
104
109
|
},
|
|
105
|
-
|
|
106
|
-
|
|
110
|
+
{ $sort: { type: 1 } },
|
|
111
|
+
].filter(Boolean);
|
|
112
|
+
const report = await Events.aggregate(pipeline).toArray();
|
|
113
|
+
logger.info(report);
|
|
114
|
+
return report ?? [];
|
|
107
115
|
},
|
|
108
116
|
};
|
|
109
117
|
};
|
|
@@ -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;
|
|
1
|
+
{"version":3,"file":"configureEventsModule.js","sourceRoot":"","sources":["../../src/module/configureEventsModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,oBAAoB,EACpB,gBAAgB,EA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,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,CAA4B,CAAC;YAE7C,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,SAAS,CAAc,QAAQ,CAAC,CAAC,OAAO,EAAE,CAAC;YACvE,MAAM,CAAC,IAAI,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
|
|
3
|
+
"version": "4.0.0",
|
|
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
|
|
35
|
-
"@unchainedshop/utils": "^4.0.0
|
|
34
|
+
"@unchainedshop/events": "^4.0.0",
|
|
35
|
+
"@unchainedshop/utils": "^4.0.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@types/node": "^24.0.13",
|
|
@@ -5,7 +5,7 @@ import { TimestampFields } from '@unchainedshop/mongodb';
|
|
|
5
5
|
const TWO_DAYS_SEC = 172800;
|
|
6
6
|
|
|
7
7
|
export type Event = {
|
|
8
|
-
_id
|
|
8
|
+
_id: string;
|
|
9
9
|
type: string;
|
|
10
10
|
context?: Record<string, unknown>;
|
|
11
11
|
payload?: Record<string, unknown>;
|
|
@@ -14,22 +14,27 @@ export type Event = {
|
|
|
14
14
|
export const EventsCollection = async (db: mongodb.Db) => {
|
|
15
15
|
const Events = db.collection<Event>('events');
|
|
16
16
|
|
|
17
|
+
if (!isDocumentDBCompatModeEnabled()) {
|
|
18
|
+
await buildDbIndexes(Events, [
|
|
19
|
+
{
|
|
20
|
+
index: { _id: 'text', type: 'text' },
|
|
21
|
+
options: {
|
|
22
|
+
weights: {
|
|
23
|
+
_id: 8,
|
|
24
|
+
type: 4,
|
|
25
|
+
},
|
|
26
|
+
name: 'events_fulltext_search',
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
|
|
17
32
|
await buildDbIndexes(Events, [
|
|
18
33
|
{
|
|
19
34
|
index: { created: -1 },
|
|
20
35
|
options: { expireAfterSeconds: TWO_DAYS_SEC, name: 'created' },
|
|
21
36
|
},
|
|
22
37
|
{ index: { type: 1 }, options: { name: 'type' } },
|
|
23
|
-
!isDocumentDBCompatModeEnabled() && {
|
|
24
|
-
index: { _id: 'text', type: 'text' },
|
|
25
|
-
options: {
|
|
26
|
-
weights: {
|
|
27
|
-
_id: 8,
|
|
28
|
-
type: 4,
|
|
29
|
-
},
|
|
30
|
-
name: 'events_fulltext_search',
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
38
|
]);
|
|
34
39
|
|
|
35
40
|
return Events;
|
|
@@ -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', () => {
|
|
@@ -10,7 +10,9 @@ import { getRegisteredEvents } from '@unchainedshop/events';
|
|
|
10
10
|
import { SortDirection, SortOption, DateFilterInput } from '@unchainedshop/utils';
|
|
11
11
|
import { EventsCollection, Event } from '../db/EventsCollection.js';
|
|
12
12
|
import { configureEventHistoryAdapter } from './configureEventHistoryAdapter.js';
|
|
13
|
+
import { createLogger } from '@unchainedshop/logger';
|
|
13
14
|
|
|
15
|
+
const logger = createLogger('unchained:core-events');
|
|
14
16
|
export interface EventReport {
|
|
15
17
|
emitCount: number;
|
|
16
18
|
type: string;
|
|
@@ -19,7 +21,7 @@ export interface EventReport {
|
|
|
19
21
|
export interface EventQuery {
|
|
20
22
|
types?: string[];
|
|
21
23
|
queryString?: string;
|
|
22
|
-
created?: Date;
|
|
24
|
+
created?: { end?: Date; start?: Date };
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
export const buildFindSelector = ({ types, queryString, created }: EventQuery) => {
|
|
@@ -30,14 +32,20 @@ export const buildFindSelector = ({ types, queryString, created }: EventQuery) =
|
|
|
30
32
|
assertDocumentDBCompatMode();
|
|
31
33
|
selector.$text = { $search: queryString };
|
|
32
34
|
}
|
|
33
|
-
if (created)
|
|
35
|
+
if (created) {
|
|
36
|
+
selector.created = created?.end
|
|
37
|
+
? { $gte: created.start || new Date(0), $lte: created.end }
|
|
38
|
+
: { $gte: created.start || new Date(0) };
|
|
39
|
+
}
|
|
34
40
|
return selector;
|
|
35
41
|
};
|
|
36
42
|
|
|
37
43
|
export const configureEventsModule = async ({ db }: ModuleInput<Record<string, never>>) => {
|
|
38
44
|
const Events = await EventsCollection(db);
|
|
39
45
|
|
|
40
|
-
const create = async (
|
|
46
|
+
const create = async (
|
|
47
|
+
doc: Omit<Event, '_id' | 'created'> & Pick<Partial<Event>, '_id' | 'created'>,
|
|
48
|
+
) => {
|
|
41
49
|
const result = await Events.insertOne({
|
|
42
50
|
_id: generateDbObjectId(),
|
|
43
51
|
created: new Date(),
|
|
@@ -54,7 +62,7 @@ export const configureEventsModule = async ({ db }: ModuleInput<Record<string, n
|
|
|
54
62
|
findEvent: async (
|
|
55
63
|
{ eventId, ...rest }: mongodb.Filter<Event> & { eventId: string },
|
|
56
64
|
options?: mongodb.FindOptions,
|
|
57
|
-
)
|
|
65
|
+
) => {
|
|
58
66
|
const selector = eventId ? generateDbFilterById<Event>(eventId) : rest;
|
|
59
67
|
return Events.findOne(selector, options);
|
|
60
68
|
},
|
|
@@ -89,66 +97,70 @@ export const configureEventsModule = async ({ db }: ModuleInput<Record<string, n
|
|
|
89
97
|
return count;
|
|
90
98
|
},
|
|
91
99
|
|
|
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
|
|
100
|
+
getReport: async ({
|
|
101
|
+
dateRange,
|
|
102
|
+
types,
|
|
103
|
+
}: { dateRange?: DateFilterInput; types?: string[] } = {}): Promise<EventReport[]> => {
|
|
104
|
+
const match: any = {};
|
|
105
|
+
|
|
103
106
|
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
|
-
});
|
|
107
|
+
const conditions: any[] = [];
|
|
108
|
+
if (dateRange.start) {
|
|
109
|
+
const fromDate = new Date(dateRange.start);
|
|
110
|
+
conditions.push({ created: { $gte: fromDate } });
|
|
110
111
|
}
|
|
111
|
-
if (dateRange
|
|
112
|
-
const toDate = new Date(dateRange
|
|
113
|
-
|
|
114
|
-
$or: [{ created: { $lte: toDate } }, { updated: { $lte: toDate } }],
|
|
115
|
-
});
|
|
112
|
+
if (dateRange.end) {
|
|
113
|
+
const toDate = new Date(dateRange.end);
|
|
114
|
+
conditions.push({ created: { $lte: toDate } });
|
|
116
115
|
}
|
|
117
|
-
if (
|
|
118
|
-
|
|
116
|
+
if (conditions.length) {
|
|
117
|
+
match.$or = conditions;
|
|
119
118
|
}
|
|
120
119
|
}
|
|
121
|
-
|
|
122
|
-
if (types
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
if (matchConditions.length > 0) {
|
|
126
|
-
pipeline.push({
|
|
127
|
-
$match: {
|
|
128
|
-
$and: matchConditions,
|
|
129
|
-
},
|
|
130
|
-
});
|
|
120
|
+
|
|
121
|
+
if (types?.length) {
|
|
122
|
+
match.type = { $in: types };
|
|
131
123
|
}
|
|
132
124
|
|
|
133
|
-
pipeline
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
125
|
+
const pipeline = [
|
|
126
|
+
Object.keys(match).length ? { $match: match } : null,
|
|
127
|
+
{
|
|
128
|
+
$group: {
|
|
129
|
+
_id: {
|
|
130
|
+
type: '$type',
|
|
131
|
+
day: {
|
|
132
|
+
$dateToString: { format: '%Y-%m-%d', date: '$created' },
|
|
133
|
+
},
|
|
139
134
|
},
|
|
135
|
+
count: { $sum: 1 },
|
|
140
136
|
},
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
$group: {
|
|
140
|
+
_id: '$_id.type',
|
|
141
|
+
detail: {
|
|
142
|
+
$push: {
|
|
143
|
+
date: '$_id.day',
|
|
144
|
+
count: '$count',
|
|
145
|
+
},
|
|
146
146
|
},
|
|
147
|
+
emitCount: { $sum: '$count' },
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
$project: {
|
|
152
|
+
_id: 0,
|
|
153
|
+
type: '$_id',
|
|
154
|
+
emitCount: 1,
|
|
155
|
+
detail: 1,
|
|
147
156
|
},
|
|
148
|
-
|
|
149
|
-
|
|
157
|
+
},
|
|
158
|
+
{ $sort: { type: 1 } },
|
|
159
|
+
].filter(Boolean) as mongodb.BSON.Document[];
|
|
150
160
|
|
|
151
|
-
|
|
161
|
+
const report = await Events.aggregate<EventReport>(pipeline).toArray();
|
|
162
|
+
logger.info(report);
|
|
163
|
+
return report ?? [];
|
|
152
164
|
},
|
|
153
165
|
};
|
|
154
166
|
};
|