@unchainedshop/core-events 3.0.0-alpha2 → 3.0.0-alpha4

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.
@@ -1,5 +1,5 @@
1
1
  import type { mongodb } from '@unchainedshop/mongodb';
2
- import { TimestampFields } from '@unchainedshop/types/common.js';
2
+ import type { TimestampFields } from '@unchainedshop/mongodb';
3
3
  export type Event = {
4
4
  _id?: string;
5
5
  type: string;
@@ -1 +1 @@
1
- {"version":3,"file":"EventsCollection.d.ts","sourceRoot":"","sources":["../../src/db/EventsCollection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAIjE,MAAM,MAAM,KAAK,GAAG;IAClB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,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,OAAc,OAAO,CAAC,EAAE,uCAsBpD,CAAC"}
1
+ {"version":3,"file":"EventsCollection.d.ts","sourceRoot":"","sources":["../../src/db/EventsCollection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAI9D,MAAM,MAAM,KAAK,GAAG;IAClB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,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,OAAc,OAAO,CAAC,EAAE,uCAsBpD,CAAC"}
@@ -1,3 +1,3 @@
1
1
  export type { Event } from './db/EventsCollection.js';
2
- export { configureEventsModule } from './module/configureEventsModule.js';
2
+ export * from './module/configureEventsModule.js';
3
3
  //# sourceMappingURL=events-index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"events-index.d.ts","sourceRoot":"","sources":["../src/events-index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AAEtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC"}
1
+ {"version":3,"file":"events-index.d.ts","sourceRoot":"","sources":["../src/events-index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AAEtD,cAAc,mCAAmC,CAAC"}
@@ -1,2 +1,2 @@
1
- export { configureEventsModule } from './module/configureEventsModule.js';
1
+ export * from './module/configureEventsModule.js';
2
2
  //# sourceMappingURL=events-index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"events-index.js","sourceRoot":"","sources":["../src/events-index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC"}
1
+ {"version":3,"file":"events-index.js","sourceRoot":"","sources":["../src/events-index.ts"],"names":[],"mappings":"AAEA,cAAc,mCAAmC,CAAC"}
@@ -1,4 +1,5 @@
1
- import { ModuleMutations } from '@unchainedshop/types/core.js';
2
- import { Event } from '../db/EventsCollection.js';
3
- export declare const configureEventHistoryAdapter: (mutations: ModuleMutations<Event>) => void;
1
+ import { RawPayloadType } from '@unchainedshop/events/EventDirector.js';
2
+ export declare const configureEventHistoryAdapter: (createFn: ({ type, payload, context }: RawPayloadType<any> & {
3
+ type: string;
4
+ }) => Promise<unknown>) => void;
4
5
  //# sourceMappingURL=configureEventHistoryAdapter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"configureEventHistoryAdapter.d.ts","sourceRoot":"","sources":["../../src/module/configureEventHistoryAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAE/D,OAAO,EAAE,KAAK,EAAE,MAAM,2BAA2B,CAAC;AAElD,eAAO,MAAM,4BAA4B,cAAe,eAAe,CAAC,KAAK,CAAC,SAgB7E,CAAC"}
1
+ {"version":3,"file":"configureEventHistoryAdapter.d.ts","sourceRoot":"","sources":["../../src/module/configureEventHistoryAdapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,wCAAwC,CAAC;AAExE,eAAO,MAAM,4BAA4B,aAC7B,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,cAAc,CAAC,GAAG,CAAC,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,KAAK,OAAO,CAAC,OAAO,CAAC,SAiBnG,CAAC"}
@@ -1,12 +1,12 @@
1
1
  import { getEmitHistoryAdapter, setEmitHistoryAdapter } from '@unchainedshop/events';
2
- export const configureEventHistoryAdapter = (mutations) => {
2
+ export const configureEventHistoryAdapter = (createFn) => {
3
3
  if (!getEmitHistoryAdapter()) {
4
4
  const adapter = {
5
5
  subscribe: () => {
6
6
  // Do nothing
7
7
  },
8
8
  publish: async (eventName, { payload, context = {} }) => {
9
- await mutations.create({
9
+ await createFn({
10
10
  type: eventName,
11
11
  payload,
12
12
  context,
@@ -1 +1 @@
1
- {"version":3,"file":"configureEventHistoryAdapter.js","sourceRoot":"","sources":["../../src/module/configureEventHistoryAdapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAe,MAAM,uBAAuB,CAAC;AAGlG,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAC,SAAiC,EAAE,EAAE;IAChF,IAAI,CAAC,qBAAqB,EAAE,EAAE,CAAC;QAC7B,MAAM,OAAO,GAAgB;YAC3B,SAAS,EAAE,GAAG,EAAE;gBACd,aAAa;YACf,CAAC;YACD,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,EAAE,EAAE,EAAE;gBACtD,MAAM,SAAS,CAAC,MAAM,CAAC;oBACrB,IAAI,EAAE,SAAS;oBACf,OAAO;oBACP,OAAO;iBACR,CAAC,CAAC;YACL,CAAC;SACF,CAAC;QACF,qBAAqB,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;AACH,CAAC,CAAC"}
1
+ {"version":3,"file":"configureEventHistoryAdapter.js","sourceRoot":"","sources":["../../src/module/configureEventHistoryAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAe,MAAM,uBAAuB,CAAC;AAGlG,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAC1C,QAAkG,EAClG,EAAE;IACF,IAAI,CAAC,qBAAqB,EAAE,EAAE,CAAC;QAC7B,MAAM,OAAO,GAAgB;YAC3B,SAAS,EAAE,GAAG,EAAE;gBACd,aAAa;YACf,CAAC;YACD,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,EAAE,EAAE,EAAE;gBACtD,MAAM,QAAQ,CAAC;oBACb,IAAI,EAAE,SAAS;oBACf,OAAO;oBACP,OAAO;iBACR,CAAC,CAAC;YACL,CAAC;SACF,CAAC;QACF,qBAAqB,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;AACH,CAAC,CAAC"}
@@ -1,7 +1,11 @@
1
1
  import type { mongodb } from '@unchainedshop/mongodb';
2
- import { SortOption } from '@unchainedshop/types/api.js';
3
- import { ModuleCreateMutation, ModuleInput } from '@unchainedshop/types/core.js';
2
+ import { SortOption } from '@unchainedshop/utils';
3
+ import { ModuleInput } from '@unchainedshop/mongodb';
4
4
  import { Event } from '../db/EventsCollection.js';
5
+ export type EventReport = {
6
+ emitCount: number;
7
+ type: string;
8
+ };
5
9
  export type EventQuery = {
6
10
  types?: Array<string>;
7
11
  queryString?: string;
@@ -12,7 +16,7 @@ export declare const buildFindSelector: ({ types, queryString, created }: EventQ
12
16
  $text?: any;
13
17
  created?: any;
14
18
  };
15
- export interface EventsModule extends ModuleCreateMutation<Event> {
19
+ export interface EventsModule {
16
20
  findEvent: (params: mongodb.Filter<Event> & {
17
21
  eventId: string;
18
22
  }, options?: mongodb.FindOptions) => Promise<Event>;
@@ -22,7 +26,13 @@ export interface EventsModule extends ModuleCreateMutation<Event> {
22
26
  sort?: Array<SortOption>;
23
27
  }, options?: mongodb.FindOptions) => Promise<Array<Event>>;
24
28
  type: (event: Event) => string;
29
+ create: (doc: Event) => Promise<string | null>;
25
30
  count: (query: EventQuery) => Promise<number>;
31
+ getReport: (params?: {
32
+ from?: Date;
33
+ to?: Date;
34
+ types?: string[];
35
+ }) => Promise<EventReport[]>;
26
36
  }
27
37
  export declare const configureEventsModule: ({ db, }: ModuleInput<Record<string, never>>) => Promise<EventsModule>;
28
38
  //# sourceMappingURL=configureEventsModule.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"configureEventsModule.d.ts","sourceRoot":"","sources":["../../src/module/configureEventsModule.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAItD,OAAO,EAAiB,UAAU,EAAE,MAAM,6BAA6B,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAE,WAAW,EAAmB,MAAM,8BAA8B,CAAC;AAClG,OAAO,EAAoB,KAAK,EAAE,MAAM,2BAA2B,CAAC;AAIpE,MAAM,MAAM,UAAU,GAAG;IACvB,KAAK,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,IAAI,CAAC;CAChB,CAAC;AAEF,eAAO,MAAM,iBAAiB,oCAAqC,UAAU;W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BAAqB,YAE/B,WAAW,CAAC,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,KAAG,OAAO,CAAC,YAAY,CAoG3D,CAAC"}
@@ -1,8 +1,7 @@
1
- import { generateDbFilterById, generateDbMutations, buildSortOptions } from '@unchainedshop/mongodb';
1
+ import { generateDbFilterById, buildSortOptions, generateDbObjectId } from '@unchainedshop/mongodb';
2
2
  import { getRegisteredEvents } from '@unchainedshop/events';
3
- import { SortDirection } from '@unchainedshop/types/api.js';
3
+ import { SortDirection } from '@unchainedshop/utils';
4
4
  import { EventsCollection } from '../db/EventsCollection.js';
5
- import { EventsSchema } from '../db/EventsSchema.js';
6
5
  import { configureEventHistoryAdapter } from './configureEventHistoryAdapter.js';
7
6
  export const buildFindSelector = ({ types, queryString, created }) => {
8
7
  const selector = {};
@@ -16,12 +15,17 @@ export const buildFindSelector = ({ types, queryString, created }) => {
16
15
  };
17
16
  export const configureEventsModule = async ({ db, }) => {
18
17
  const Events = await EventsCollection(db);
19
- const mutations = generateDbMutations(Events, EventsSchema, {
20
- hasCreateOnly: true,
21
- });
22
- configureEventHistoryAdapter(mutations);
18
+ const create = async (doc) => {
19
+ const result = await Events.insertOne({
20
+ _id: generateDbObjectId(),
21
+ created: new Date(),
22
+ ...doc,
23
+ });
24
+ return result.insertedId;
25
+ };
26
+ await configureEventHistoryAdapter(create);
23
27
  return {
24
- ...mutations,
28
+ create,
25
29
  findEvent: async ({ eventId, ...rest }, options) => {
26
30
  const selector = eventId ? generateDbFilterById(eventId) : rest;
27
31
  return Events.findOne(selector, options);
@@ -44,6 +48,58 @@ export const configureEventsModule = async ({ db, }) => {
44
48
  const count = await Events.countDocuments(buildFindSelector(query));
45
49
  return count;
46
50
  },
51
+ getReport: async ({ from, to, types } = { from: null, to: null, types: null }) => {
52
+ const pipeline = [];
53
+ const matchConditions = [];
54
+ // build date filter based on provided values it can be a range if both to and from is supplied
55
+ // a upper or lowe limit if either from or to is provided
56
+ // or all if none is provided
57
+ if (from || to) {
58
+ const dateConditions = [];
59
+ if (from) {
60
+ const fromDate = new Date(from);
61
+ dateConditions.push({
62
+ $or: [{ created: { $gte: fromDate } }, { updated: { $gte: fromDate } }],
63
+ });
64
+ }
65
+ if (to) {
66
+ const toDate = new Date(to);
67
+ dateConditions.push({
68
+ $or: [{ created: { $lte: toDate } }, { updated: { $lte: toDate } }],
69
+ });
70
+ }
71
+ if (dateConditions.length > 0) {
72
+ matchConditions.push({ $and: dateConditions });
73
+ }
74
+ }
75
+ // build types filter if type is provided or ignore types if it is not provided
76
+ if (types && Array.isArray(types) && types.length) {
77
+ matchConditions.push({ type: { $in: types } });
78
+ }
79
+ if (matchConditions.length > 0) {
80
+ pipeline.push({
81
+ $match: {
82
+ $and: matchConditions,
83
+ },
84
+ });
85
+ }
86
+ pipeline.push(...[
87
+ {
88
+ $group: {
89
+ _id: '$type',
90
+ emitCount: { $sum: 1 },
91
+ },
92
+ },
93
+ {
94
+ $project: {
95
+ _id: 0,
96
+ type: '$_id',
97
+ emitCount: 1,
98
+ },
99
+ },
100
+ ]);
101
+ return Events.aggregate(pipeline).toArray();
102
+ },
47
103
  };
48
104
  };
49
105
  //# sourceMappingURL=configureEventsModule.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"configureEventsModule.js","sourceRoot":"","sources":["../../src/module/configureEventsModule.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AACrG,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAc,MAAM,6BAA6B,CAAC;AAExE,OAAO,EAAE,gBAAgB,EAAS,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAC;AAQjF,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,EAA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AAC;KACF,CAAC;AACJ,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unchainedshop/core-events",
3
- "version": "3.0.0-alpha2",
3
+ "version": "3.0.0-alpha4",
4
4
  "main": "lib/events-index.js",
5
5
  "exports": {
6
6
  ".": "./lib/events-index.js",
@@ -31,15 +31,13 @@
31
31
  },
32
32
  "homepage": "https://github.com/unchainedshop/unchained#readme",
33
33
  "dependencies": {
34
- "@unchainedshop/events": "^3.0.0-alpha2",
35
- "@unchainedshop/utils": "^3.0.0-alpha2",
36
- "simpl-schema": "^3.4.6"
34
+ "@unchainedshop/events": "^3.0.0-alpha4",
35
+ "@unchainedshop/utils": "^3.0.0-alpha4"
37
36
  },
38
37
  "devDependencies": {
39
- "@types/node": "^20.14.12",
40
- "@unchainedshop/types": "^3.0.0-alpha2",
38
+ "@types/node": "^22.7.8",
41
39
  "jest": "^29.7.0",
42
- "ts-jest": "^29.2.3",
43
- "typescript": "^5.5.4"
40
+ "ts-jest": "^29.2.5",
41
+ "typescript": "^5.6.3"
44
42
  }
45
43
  }
@@ -1,6 +1,6 @@
1
1
  import type { mongodb } from '@unchainedshop/mongodb';
2
2
  import { buildDbIndexes } from '@unchainedshop/mongodb';
3
- import { TimestampFields } from '@unchainedshop/types/common.js';
3
+ import type { TimestampFields } from '@unchainedshop/mongodb';
4
4
 
5
5
  const TWO_DAYS_SEC = 172800;
6
6
 
@@ -1,3 +1,3 @@
1
1
  export type { Event } from './db/EventsCollection.js';
2
2
 
3
- export { configureEventsModule } from './module/configureEventsModule.js';
3
+ export * from './module/configureEventsModule.js';
@@ -1,30 +1,36 @@
1
- import { buildFindSelector } from "./configureEventsModule.js";
1
+ import { buildFindSelector } from './configureEventsModule.js';
2
2
 
3
- describe('Event', () => {
4
- describe('buildFindSelector', () => {
5
- it('Return correct filter object when passed create, queryString, types', async () => {
6
- expect(buildFindSelector({created: new Date("2022-12-03T18:23:38.278Z"), queryString: "Hello world", types: ['PRODUCT_CREATED']})).toEqual({
7
- type: { '$in': [ 'PRODUCT_CREATED' ] },
8
- '$text': { '$search': 'Hello world' },
9
- created: { '$gte': new Date( "2022-12-03T18:23:38.278Z" )}
10
- })
3
+ describe('buildFindSelector', () => {
4
+ it('Return correct filter object when passed create, queryString, types', () => {
5
+ expect(
6
+ buildFindSelector({
7
+ created: new Date('2022-12-03T18:23:38.278Z'),
8
+ queryString: 'Hello world',
9
+ types: ['PRODUCT_CREATED'],
10
+ }),
11
+ ).toEqual({
12
+ type: { $in: ['PRODUCT_CREATED'] },
13
+ $text: { $search: 'Hello world' },
14
+ created: { $gte: new Date('2022-12-03T18:23:38.278Z') },
15
+ });
11
16
  });
12
17
 
13
- it('Return correct filter object when passed create, queryString', async () => {
14
- expect(buildFindSelector({created: new Date("2022-12-03T18:23:38.278Z"), queryString: "Hello world"})).toEqual({
15
- '$text': { '$search': 'Hello world' },
16
- created: { '$gte': new Date( "2022-12-03T18:23:38.278Z" )}
17
- })
18
- });
18
+ it('Return correct filter object when passed create, queryString', () => {
19
+ expect(
20
+ buildFindSelector({ created: new Date('2022-12-03T18:23:38.278Z'), queryString: 'Hello world' }),
21
+ ).toEqual({
22
+ $text: { $search: 'Hello world' },
23
+ created: { $gte: new Date('2022-12-03T18:23:38.278Z') },
24
+ });
25
+ });
19
26
 
20
- it('Return correct filter object when passed create', async () => {
21
- expect(buildFindSelector({created: new Date("2022-12-03T18:23:38.278Z")})).toEqual({
22
- created: { '$gte': new Date( "2022-12-03T18:23:38.278Z" )}
23
- })
24
- });
27
+ it('Return correct filter object when passed create', () => {
28
+ expect(buildFindSelector({ created: new Date('2022-12-03T18:23:38.278Z') })).toEqual({
29
+ created: { $gte: new Date('2022-12-03T18:23:38.278Z') },
30
+ });
31
+ });
25
32
 
26
- it('Return correct filter object when passed no argument', async () => {
27
- expect(buildFindSelector({})).toEqual({})
28
- });
29
- })
33
+ it('Return correct filter object when passed no argument', () => {
34
+ expect(buildFindSelector({})).toEqual({});
35
+ });
30
36
  });
@@ -1,15 +1,16 @@
1
- import { ModuleMutations } from '@unchainedshop/types/core.js';
2
1
  import { getEmitHistoryAdapter, setEmitHistoryAdapter, EmitAdapter } from '@unchainedshop/events';
3
- import { Event } from '../db/EventsCollection.js';
2
+ import { RawPayloadType } from '@unchainedshop/events/EventDirector.js';
4
3
 
5
- export const configureEventHistoryAdapter = (mutations: ModuleMutations<Event>) => {
4
+ export const configureEventHistoryAdapter = (
5
+ createFn: ({ type, payload, context }: RawPayloadType<any> & { type: string }) => Promise<unknown>,
6
+ ) => {
6
7
  if (!getEmitHistoryAdapter()) {
7
8
  const adapter: EmitAdapter = {
8
9
  subscribe: () => {
9
10
  // Do nothing
10
11
  },
11
12
  publish: async (eventName, { payload, context = {} }) => {
12
- await mutations.create({
13
+ await createFn({
13
14
  type: eventName,
14
15
  payload,
15
16
  context,
@@ -1,13 +1,17 @@
1
1
  import type { mongodb } from '@unchainedshop/mongodb';
2
2
 
3
- import { generateDbFilterById, generateDbMutations, buildSortOptions } from '@unchainedshop/mongodb';
3
+ import { generateDbFilterById, buildSortOptions, generateDbObjectId } from '@unchainedshop/mongodb';
4
4
  import { getRegisteredEvents } from '@unchainedshop/events';
5
- import { SortDirection, SortOption } from '@unchainedshop/types/api.js';
6
- import { ModuleCreateMutation, ModuleInput, ModuleMutations } from '@unchainedshop/types/core.js';
5
+ import { SortDirection, SortOption } from '@unchainedshop/utils';
6
+ import { ModuleInput } from '@unchainedshop/mongodb';
7
7
  import { EventsCollection, Event } from '../db/EventsCollection.js';
8
- import { EventsSchema } from '../db/EventsSchema.js';
9
8
  import { configureEventHistoryAdapter } from './configureEventHistoryAdapter.js';
10
9
 
10
+ export type EventReport = {
11
+ emitCount: number;
12
+ type: string;
13
+ };
14
+
11
15
  export type EventQuery = {
12
16
  types?: Array<string>;
13
17
  queryString?: string;
@@ -23,7 +27,7 @@ export const buildFindSelector = ({ types, queryString, created }: EventQuery) =
23
27
  return selector;
24
28
  };
25
29
 
26
- export interface EventsModule extends ModuleCreateMutation<Event> {
30
+ export interface EventsModule {
27
31
  findEvent: (
28
32
  params: mongodb.Filter<Event> & { eventId: string },
29
33
  options?: mongodb.FindOptions,
@@ -39,8 +43,10 @@ export interface EventsModule extends ModuleCreateMutation<Event> {
39
43
  ) => Promise<Array<Event>>;
40
44
 
41
45
  type: (event: Event) => string;
46
+ create: (doc: Event) => Promise<string | null>;
42
47
 
43
48
  count: (query: EventQuery) => Promise<number>;
49
+ getReport: (params?: { from?: Date; to?: Date; types?: string[] }) => Promise<EventReport[]>;
44
50
  }
45
51
 
46
52
  export const configureEventsModule = async ({
@@ -48,14 +54,20 @@ export const configureEventsModule = async ({
48
54
  }: ModuleInput<Record<string, never>>): Promise<EventsModule> => {
49
55
  const Events = await EventsCollection(db);
50
56
 
51
- const mutations = generateDbMutations<Event>(Events, EventsSchema, {
52
- hasCreateOnly: true,
53
- }) as ModuleMutations<Event>;
57
+ const create = async (doc) => {
58
+ const result = await Events.insertOne({
59
+ _id: generateDbObjectId(),
60
+ created: new Date(),
61
+ ...doc,
62
+ });
63
+ return result.insertedId as string;
64
+ };
54
65
 
55
- configureEventHistoryAdapter(mutations);
66
+ await configureEventHistoryAdapter(create);
56
67
 
57
68
  return {
58
- ...mutations,
69
+ create,
70
+
59
71
  findEvent: async ({ eventId, ...rest }, options) => {
60
72
  const selector = eventId ? generateDbFilterById<Event>(eventId) : rest;
61
73
  return Events.findOne(selector, options);
@@ -81,5 +93,62 @@ export const configureEventsModule = async ({
81
93
  const count = await Events.countDocuments(buildFindSelector(query));
82
94
  return count;
83
95
  },
96
+
97
+ getReport: async ({ from, to, types } = { from: null, to: null, types: null }) => {
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
103
+ if (from || to) {
104
+ const dateConditions = [];
105
+ if (from) {
106
+ const fromDate = new Date(from);
107
+ dateConditions.push({
108
+ $or: [{ created: { $gte: fromDate } }, { updated: { $gte: fromDate } }],
109
+ });
110
+ }
111
+ if (to) {
112
+ const toDate = new Date(to);
113
+ dateConditions.push({
114
+ $or: [{ created: { $lte: toDate } }, { updated: { $lte: toDate } }],
115
+ });
116
+ }
117
+ if (dateConditions.length > 0) {
118
+ matchConditions.push({ $and: dateConditions });
119
+ }
120
+ }
121
+ // build types filter if type is provided or ignore types if it is not provided
122
+ if (types && Array.isArray(types) && types.length) {
123
+ matchConditions.push({ type: { $in: types } });
124
+ }
125
+ if (matchConditions.length > 0) {
126
+ pipeline.push({
127
+ $match: {
128
+ $and: matchConditions,
129
+ },
130
+ });
131
+ }
132
+
133
+ pipeline.push(
134
+ ...[
135
+ {
136
+ $group: {
137
+ _id: '$type',
138
+ emitCount: { $sum: 1 },
139
+ },
140
+ },
141
+ {
142
+ $project: {
143
+ _id: 0,
144
+ type: '$_id',
145
+ emitCount: 1,
146
+ },
147
+ },
148
+ ],
149
+ );
150
+
151
+ return Events.aggregate(pipeline).toArray() as Promise<EventReport[]>;
152
+ },
84
153
  };
85
154
  };
@@ -1,3 +0,0 @@
1
- import SimpleSchema from 'simpl-schema';
2
- export declare const EventsSchema: SimpleSchema;
3
- //# sourceMappingURL=EventsSchema.d.ts.map
@@ -1 +0,0 @@
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"}
@@ -1,9 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,12 +0,0 @@
1
- import { Schemas } from '@unchainedshop/utils';
2
- import SimpleSchema from 'simpl-schema';
3
-
4
- export const EventsSchema = new SimpleSchema(
5
- {
6
- type: { type: String, required: true },
7
- payload: { type: Object, blackbox: true },
8
- context: { type: Object, blackbox: true },
9
- ...Schemas.timestampFields,
10
- },
11
- { requiredByDefault: false },
12
- );