@zeeshan60/event-processor 1.0.6 → 1.0.7

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/README.md CHANGED
@@ -11,6 +11,7 @@ This package provides the core building blocks for implementing event sourcing p
11
11
  - **Base Interfaces**: `Event` and `Model` interfaces for type-safe event sourcing
12
12
  - **Abstract Stores**: `EventStore` and `ModelStore` base classes
13
13
  - **In-Memory Implementations**: Ready-to-use in-memory stores for testing
14
+ - **Environment Configuration**: Detect and configure behavior based on runtime environment (Google Cloud Functions vs Client)
14
15
 
15
16
  ## Installation
16
17
 
@@ -61,14 +62,82 @@ const model = event.apply(null);
61
62
  await modelStore.save(model);
62
63
  ```
63
64
 
65
+ ## Environment Configuration
66
+
67
+ The event-processor package includes environment detection to identify whether it's running in Google Cloud Functions or a client application. This enables environment-specific behavior.
68
+
69
+ ### Quick Start
70
+
71
+ ```typescript
72
+ import { EventProcessorSource, setEventProcessorSource, isGCFun, isClient } from '@zeeshan60/event-processor';
73
+
74
+ // Option 1: Set via environment variable (recommended)
75
+ // export EVENT_PROCESSOR_SOURCE=GOOGLE_CLOUD_FUNCTION
76
+
77
+ // Option 2: Set programmatically
78
+ setEventProcessorSource(EventProcessorSource.GOOGLE_CLOUD_FUNCTION);
79
+
80
+ // Check the environment
81
+ if (isGCFun()) {
82
+ console.log('Running in Google Cloud Functions');
83
+ }
84
+
85
+ if (isClient()) {
86
+ console.log('Running in client application');
87
+ }
88
+ ```
89
+
90
+ ### Available Helper Methods
91
+
92
+ - `isGCFun()`: Returns `true` if running in Google Cloud Functions
93
+ - `isClient()`: Returns `true` if running in a client application
94
+ - `isUnknown()`: Returns `true` if environment is not configured
95
+ - `setEventProcessorSource(source)`: Set the environment programmatically
96
+ - `getEventProcessorSource()`: Get the current environment
97
+
98
+ For detailed environment configuration, see [ENVIRONMENT_CONFIG.md](./ENVIRONMENT_CONFIG.md).
99
+
100
+ ## Documentation
101
+
102
+ ### 📚 Essential Reading
103
+
104
+ - **[DUAL_CONTEXT_FLOW.md](./DUAL_CONTEXT_FLOW.md)** - 🎯 **START HERE!** Comprehensive guide explaining how the package operates in client vs Google Cloud Functions contexts, complete with diagrams and examples
105
+ - **[QUICK_REFERENCE.md](./QUICK_REFERENCE.md)** - Quick reference cheat sheet for common patterns, troubleshooting, and best practices
106
+ - **[ENVIRONMENT_CONFIG.md](./ENVIRONMENT_CONFIG.md)** - Environment configuration details
107
+
108
+ ### Why Dual-Context Matters
109
+
110
+ The event-processor package serves **two distinct purposes**:
111
+
112
+ 1. **Client (Mobile App)**: Creates events from user actions, applies them to local models
113
+ 2. **Google Cloud Functions**: Processes events from Firestore, generates friend-perspective mirror events
114
+
115
+ Understanding when to create events vs when to process them is critical to avoid:
116
+ - Duplicate events
117
+ - Infinite loops
118
+ - Incorrect activity attribution
119
+
120
+ **Read [DUAL_CONTEXT_FLOW.md](./DUAL_CONTEXT_FLOW.md) before integrating this package.**
121
+
64
122
  ## Architecture
65
123
 
66
124
  - **Event**: Base interface for all events with `apply()` method
67
125
  - **Model**: Base interface for all models/aggregates
68
126
  - **EventStore**: Abstract class for event persistence
69
127
  - **ModelStore**: Abstract class for model/read-model persistence
128
+ - **EventProcessorSource**: Context detection (CLIENT vs GOOGLE_CLOUD_FUNCTION)
129
+
130
+ ### Event Handlers
131
+
132
+ - **TransactionEventHandler**: Handles transaction lifecycle (create, update, delete)
133
+ - **FriendEventHandler**: Manages friend relationships
134
+ - **UserEventHandler**: Handles user profile changes
135
+ - **GroupEventHandler**: Manages group entities
136
+ - **GroupTransactionEventHandler**: Processes group transactions
137
+ - **ActivityEventHandler**: Creates activity log entries
70
138
 
71
139
  ## Projects Using This Package
72
140
 
73
- - `money-rabit-backend`: Firebase Functions backend
74
- - `mobile`: Mobile application
141
+ - `gcloud-functions`: Google Cloud Functions backend (server-side event processing)
142
+ - `mobile`: Mobile application (client-side event creation)
143
+ - `client-events`: Client-side event definitions (extends this package)
@@ -1 +1 @@
1
- {"version":3,"file":"TransactionEventHandler.d.ts","sourceRoot":"","sources":["../src/TransactionEventHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAQH,gBAAgB,EAChB,KAAK,EACR,MAAM,GAAG,CAAC;AACX,OAAO,EAAC,qBAAqB,EAAC,MAAM,GAAG,CAAC;AACxC,OAAO,EAAC,qBAAqB,EAAC,MAAM,GAAG,CAAC;AACxC,OAAO,EAAC,cAAc,EAAC,MAAM,GAAG,CAAC;AACjC,OAAO,EAAC,oBAAoB,EAAC,MAAM,wBAAwB,CAAC;AAK5D,qBAAa,uBAAuB;IAE5B,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,gBAAgB;IACxB,OAAO,CAAC,oBAAoB,CAAC;gBAHrB,UAAU,EAAE,qBAAqB,EACjC,UAAU,EAAE,qBAAqB,EACjC,gBAAgB,EAAE,cAAc,EAChC,oBAAoB,CAAC,EAAE,oBAAoB,YAAA;IAGjD,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,gBAAgB,CAAC;YA2B5C,iBAAiB;CAmJlC"}
1
+ {"version":3,"file":"TransactionEventHandler.d.ts","sourceRoot":"","sources":["../src/TransactionEventHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAQH,gBAAgB,EAChB,KAAK,EACR,MAAM,GAAG,CAAC;AACX,OAAO,EAAC,qBAAqB,EAAC,MAAM,GAAG,CAAC;AACxC,OAAO,EAAC,qBAAqB,EAAC,MAAM,GAAG,CAAC;AACxC,OAAO,EAAC,cAAc,EAAC,MAAM,GAAG,CAAC;AACjC,OAAO,EAAC,oBAAoB,EAAC,MAAM,wBAAwB,CAAC;AAM5D,qBAAa,uBAAuB;IAE5B,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,gBAAgB;IACxB,OAAO,CAAC,oBAAoB,CAAC;gBAHrB,UAAU,EAAE,qBAAqB,EACjC,UAAU,EAAE,qBAAqB,EACjC,gBAAgB,EAAE,cAAc,EAChC,oBAAoB,CAAC,EAAE,oBAAoB,YAAA;IAGjD,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,gBAAgB,CAAC;YAmC5C,iBAAiB;CA2JlC"}
@@ -4,6 +4,7 @@ exports.TransactionEventHandler = void 0;
4
4
  const _1 = require(".");
5
5
  const splitTypeUtils_1 = require("./utils/splitTypeUtils");
6
6
  const userPathUtils_1 = require("./utils/userPathUtils");
7
+ const environment_1 = require("./config/environment");
7
8
  const uuid_1 = require("uuid");
8
9
  class TransactionEventHandler {
9
10
  constructor(modelStore, eventStore, txUserModelStore, activityEventHandler) {
@@ -26,6 +27,15 @@ class TransactionEventHandler {
26
27
  if (!validEventTypes.includes(eventType)) {
27
28
  throw new Error(`Unknown transaction event type: ${eventType}`);
28
29
  }
30
+ if ((0, environment_1.isGCFun)()) {
31
+ console.log(`[GCFunction] TransactionEventHandler handling ${eventType} event (streamId: ${event.streamId})`);
32
+ }
33
+ else if ((0, environment_1.isClient)()) {
34
+ console.log(`[Client] TransactionEventHandler handling ${eventType} event (streamId: ${event.streamId})`);
35
+ }
36
+ else {
37
+ console.log(`[Unknown] TransactionEventHandler handling ${eventType} event (streamId: ${event.streamId})`);
38
+ }
29
39
  const existingModel = await this.modelStore.getByStreamIdWhereDeletedIsFalse(event.streamId);
30
40
  const model = event.apply(existingModel);
31
41
  await this.modelStore.save(model);
@@ -37,6 +47,15 @@ class TransactionEventHandler {
37
47
  }
38
48
  async createMirrorEvent(originalEvent, transactionModel) {
39
49
  const eventType = originalEvent.constructor.name;
50
+ if ((0, environment_1.isGCFun)()) {
51
+ console.log(`[GCFunction] Creating mirror event for ${eventType} (recipient: ${transactionModel.recipientUserId})`);
52
+ }
53
+ else if ((0, environment_1.isClient)()) {
54
+ console.log(`[Client] Creating mirror event for ${eventType} (recipient: ${transactionModel.recipientUserId})`);
55
+ }
56
+ else {
57
+ console.log(`[Unknown] Creating mirror event for ${eventType} (recipient: ${transactionModel.recipientUserId})`);
58
+ }
40
59
  const recipientUserModel = await this.txUserModelStore.getByStreamId(transactionModel.recipientUserId);
41
60
  const recipientPath = (0, userPathUtils_1.getUserEventPath)(recipientUserModel, transactionModel.recipientUserId);
42
61
  const existingMirrorModel = await this.modelStore.findByUserAndLogicalTransactionId(transactionModel.recipientUserId, transactionModel.userId, transactionModel.logicalTransactionId);
@@ -1 +1 @@
1
- {"version":3,"file":"UserEventHandler.d.ts","sourceRoot":"","sources":["../src/UserEventHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,sBAAsB,EACtB,mBAAmB,EACnB,sBAAsB,EACtB,sBAAsB,EACtB,WAAW,EACX,qBAAqB,EACrB,0BAA0B,EAC1B,SAAS,EACZ,MAAM,GAAG,CAAC;AACX,OAAO,EAAC,cAAc,EAAC,MAAM,GAAG,CAAC;AACjC,OAAO,EAAC,oBAAoB,EAAC,MAAM,wBAAwB,CAAC;AAE5D,qBAAa,gBAAgB;IAErB,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,oBAAoB,CAAC;gBADrB,UAAU,EAAE,cAAc,EAC1B,oBAAoB,CAAC,EAAE,oBAAoB,YAAA;IAGjD,iBAAiB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC;IAUzD,4BAA4B,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,SAAS,CAAC;IAU/E,yBAAyB,CAAC,KAAK,EAAE,mBAAmB,GAAG,OAAO,CAAC,SAAS,CAAC;IAUzE,4BAA4B,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,SAAS,CAAC;IAU/E,4BAA4B,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,SAAS,CAAC;IAU/E,iBAAiB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC;IAUzD,2BAA2B,CAAC,KAAK,EAAE,qBAAqB,GAAG,OAAO,CAAC,SAAS,CAAC;IAU7E,gCAAgC,CAAC,KAAK,EAAE,0BAA0B,GAAG,OAAO,CAAC,SAAS,CAAC;CAShG"}
1
+ {"version":3,"file":"UserEventHandler.d.ts","sourceRoot":"","sources":["../src/UserEventHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,sBAAsB,EACtB,mBAAmB,EACnB,sBAAsB,EACtB,sBAAsB,EACtB,WAAW,EACX,qBAAqB,EACrB,0BAA0B,EAC1B,SAAS,EACZ,MAAM,GAAG,CAAC;AACX,OAAO,EAAC,cAAc,EAAC,MAAM,GAAG,CAAC;AACjC,OAAO,EAAC,oBAAoB,EAAC,MAAM,wBAAwB,CAAC;AAG5D,qBAAa,gBAAgB;IAErB,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,oBAAoB,CAAC;gBADrB,UAAU,EAAE,cAAc,EAC1B,oBAAoB,CAAC,EAAE,oBAAoB,YAAA;IAGjD,iBAAiB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC;IAWzD,4BAA4B,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,SAAS,CAAC;IAU/E,yBAAyB,CAAC,KAAK,EAAE,mBAAmB,GAAG,OAAO,CAAC,SAAS,CAAC;IAUzE,4BAA4B,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,SAAS,CAAC;IAU/E,4BAA4B,CAAC,KAAK,EAAE,sBAAsB,GAAG,OAAO,CAAC,SAAS,CAAC;IAU/E,iBAAiB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC;IAUzD,2BAA2B,CAAC,KAAK,EAAE,qBAAqB,GAAG,OAAO,CAAC,SAAS,CAAC;IAU7E,gCAAgC,CAAC,KAAK,EAAE,0BAA0B,GAAG,OAAO,CAAC,SAAS,CAAC;CAShG"}
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UserEventHandler = void 0;
4
+ const EventBusSingleton_1 = require("./common/EventBusSingleton");
4
5
  class UserEventHandler {
5
6
  constructor(modelStore, activityEventHandler) {
6
7
  this.modelStore = modelStore;
@@ -13,6 +14,7 @@ class UserEventHandler {
13
14
  if (this.activityEventHandler) {
14
15
  await this.activityEventHandler.createActivityLog(event, existingModel, updatedModel);
15
16
  }
17
+ (0, EventBusSingleton_1.getEventBus)().publish(event);
16
18
  return updatedModel;
17
19
  }
18
20
  async handlePlaceholderUserCreated(event) {
@@ -0,0 +1,12 @@
1
+ import { Observable } from 'rxjs';
2
+ import { Event } from './Event';
3
+ export type EventFilter<T extends Event = Event> = (event: Event) => event is T;
4
+ export declare class EventBus {
5
+ private subject;
6
+ constructor();
7
+ publish(event: Event): void;
8
+ subscribe<T extends Event = Event>(eventFilter: EventFilter<T>, callback: (event: T) => void): () => void;
9
+ observable<T extends Event = Event>(eventFilter: EventFilter<T>): Observable<T>;
10
+ complete(): void;
11
+ }
12
+ //# sourceMappingURL=EventBus.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventBus.d.ts","sourceRoot":"","sources":["../../src/common/EventBus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,UAAU,EAAE,MAAM,MAAM,CAAC;AAE3C,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,KAAK,GAAG,KAAK,IAAI,CAAC,KAAK,EAAE,KAAK,KAAK,KAAK,IAAI,CAAC,CAAC;AAEhF,qBAAa,QAAQ;IACjB,OAAO,CAAC,OAAO,CAAiB;;IAMhC,OAAO,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI;IAI3B,SAAS,CAAC,CAAC,SAAS,KAAK,GAAG,KAAK,EAC7B,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,EAC3B,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GAC7B,MAAM,IAAI;IAQb,UAAU,CAAC,CAAC,SAAS,KAAK,GAAG,KAAK,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;IAI/E,QAAQ,IAAI,IAAI;CAGnB"}
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventBus = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ const operators_1 = require("rxjs/operators");
6
+ class EventBus {
7
+ constructor() {
8
+ this.subject = new rxjs_1.Subject();
9
+ }
10
+ publish(event) {
11
+ this.subject.next(event);
12
+ }
13
+ subscribe(eventFilter, callback) {
14
+ const subscription = this.subject
15
+ .pipe((0, operators_1.filter)(eventFilter))
16
+ .subscribe(callback);
17
+ return () => subscription.unsubscribe();
18
+ }
19
+ observable(eventFilter) {
20
+ return this.subject.pipe((0, operators_1.filter)(eventFilter));
21
+ }
22
+ complete() {
23
+ this.subject.complete();
24
+ }
25
+ }
26
+ exports.EventBus = EventBus;
@@ -0,0 +1,4 @@
1
+ import { EventBus } from './EventBus';
2
+ export declare function getEventBus(): EventBus;
3
+ export declare function resetEventBus(): void;
4
+ //# sourceMappingURL=EventBusSingleton.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EventBusSingleton.d.ts","sourceRoot":"","sources":["../../src/common/EventBusSingleton.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAItC,wBAAgB,WAAW,IAAI,QAAQ,CAKtC;AAED,wBAAgB,aAAa,IAAI,IAAI,CAKpC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEventBus = getEventBus;
4
+ exports.resetEventBus = resetEventBus;
5
+ const EventBus_1 = require("./EventBus");
6
+ let eventBusInstance;
7
+ function getEventBus() {
8
+ if (!eventBusInstance) {
9
+ eventBusInstance = new EventBus_1.EventBus();
10
+ }
11
+ return eventBusInstance;
12
+ }
13
+ function resetEventBus() {
14
+ if (eventBusInstance) {
15
+ eventBusInstance.complete();
16
+ }
17
+ eventBusInstance = undefined;
18
+ }
@@ -0,0 +1,25 @@
1
+ export declare enum EventProcessorSource {
2
+ GOOGLE_CLOUD_FUNCTION = "GOOGLE_CLOUD_FUNCTION",
3
+ CLIENT = "CLIENT",
4
+ UNKNOWN = "UNKNOWN"
5
+ }
6
+ declare class EventProcessorEnvironment {
7
+ private source;
8
+ constructor();
9
+ private initializeFromEnv;
10
+ setSource(source: EventProcessorSource): void;
11
+ getSource(): EventProcessorSource;
12
+ isGCFun(): boolean;
13
+ isClient(): boolean;
14
+ isUnknown(): boolean;
15
+ reset(): void;
16
+ }
17
+ export declare const eventProcessorEnv: EventProcessorEnvironment;
18
+ export declare function setEventProcessorSource(source: EventProcessorSource): void;
19
+ export declare function getEventProcessorSource(): EventProcessorSource;
20
+ export declare function isGCFun(): boolean;
21
+ export declare function isClient(): boolean;
22
+ export declare function isUnknown(): boolean;
23
+ export declare function resetEventProcessorSource(): void;
24
+ export {};
25
+ //# sourceMappingURL=environment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"environment.d.ts","sourceRoot":"","sources":["../../src/config/environment.ts"],"names":[],"mappings":"AAAA,oBAAY,oBAAoB;IAC9B,qBAAqB,0BAA0B;IAC/C,MAAM,WAAW;IACjB,OAAO,YAAY;CACpB;AAED,cAAM,yBAAyB;IAC7B,OAAO,CAAC,MAAM,CAAsD;;IAMpE,OAAO,CAAC,iBAAiB;IAYzB,SAAS,CAAC,MAAM,EAAE,oBAAoB,GAAG,IAAI;IAI7C,SAAS,IAAI,oBAAoB;IAIjC,OAAO,IAAI,OAAO;IAIlB,QAAQ,IAAI,OAAO;IAInB,SAAS,IAAI,OAAO;IAIpB,KAAK,IAAI,IAAI;CAGd;AAED,eAAO,MAAM,iBAAiB,2BAAkC,CAAC;AAEjE,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,oBAAoB,GAAG,IAAI,CAE1E;AAED,wBAAgB,uBAAuB,IAAI,oBAAoB,CAE9D;AAED,wBAAgB,OAAO,IAAI,OAAO,CAEjC;AAED,wBAAgB,QAAQ,IAAI,OAAO,CAElC;AAED,wBAAgB,SAAS,IAAI,OAAO,CAEnC;AAED,wBAAgB,yBAAyB,IAAI,IAAI,CAEhD"}
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.eventProcessorEnv = exports.EventProcessorSource = void 0;
4
+ exports.setEventProcessorSource = setEventProcessorSource;
5
+ exports.getEventProcessorSource = getEventProcessorSource;
6
+ exports.isGCFun = isGCFun;
7
+ exports.isClient = isClient;
8
+ exports.isUnknown = isUnknown;
9
+ exports.resetEventProcessorSource = resetEventProcessorSource;
10
+ var EventProcessorSource;
11
+ (function (EventProcessorSource) {
12
+ EventProcessorSource["GOOGLE_CLOUD_FUNCTION"] = "GOOGLE_CLOUD_FUNCTION";
13
+ EventProcessorSource["CLIENT"] = "CLIENT";
14
+ EventProcessorSource["UNKNOWN"] = "UNKNOWN";
15
+ })(EventProcessorSource || (exports.EventProcessorSource = EventProcessorSource = {}));
16
+ class EventProcessorEnvironment {
17
+ constructor() {
18
+ this.source = EventProcessorSource.UNKNOWN;
19
+ this.initializeFromEnv();
20
+ }
21
+ initializeFromEnv() {
22
+ const envSource = process.env.EVENT_PROCESSOR_SOURCE;
23
+ if (envSource === EventProcessorSource.GOOGLE_CLOUD_FUNCTION) {
24
+ this.source = EventProcessorSource.GOOGLE_CLOUD_FUNCTION;
25
+ }
26
+ else if (envSource === EventProcessorSource.CLIENT) {
27
+ this.source = EventProcessorSource.CLIENT;
28
+ }
29
+ else {
30
+ this.source = EventProcessorSource.UNKNOWN;
31
+ }
32
+ }
33
+ setSource(source) {
34
+ this.source = source;
35
+ }
36
+ getSource() {
37
+ return this.source;
38
+ }
39
+ isGCFun() {
40
+ return this.source === EventProcessorSource.GOOGLE_CLOUD_FUNCTION;
41
+ }
42
+ isClient() {
43
+ return this.source === EventProcessorSource.CLIENT;
44
+ }
45
+ isUnknown() {
46
+ return this.source === EventProcessorSource.UNKNOWN;
47
+ }
48
+ reset() {
49
+ this.initializeFromEnv();
50
+ }
51
+ }
52
+ exports.eventProcessorEnv = new EventProcessorEnvironment();
53
+ function setEventProcessorSource(source) {
54
+ exports.eventProcessorEnv.setSource(source);
55
+ }
56
+ function getEventProcessorSource() {
57
+ return exports.eventProcessorEnv.getSource();
58
+ }
59
+ function isGCFun() {
60
+ return exports.eventProcessorEnv.isGCFun();
61
+ }
62
+ function isClient() {
63
+ return exports.eventProcessorEnv.isClient();
64
+ }
65
+ function isUnknown() {
66
+ return exports.eventProcessorEnv.isUnknown();
67
+ }
68
+ function resetEventProcessorSource() {
69
+ exports.eventProcessorEnv.reset();
70
+ }
@@ -0,0 +1,4 @@
1
+ export declare class UserController {
2
+ loginUser(userFSId: string): Promise<string>;
3
+ }
4
+ //# sourceMappingURL=UserController.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"UserController.d.ts","sourceRoot":"","sources":["../../src/controllers/UserController.ts"],"names":[],"mappings":"AAIA,qBAAa,cAAc;IACjB,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CA0BrD"}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UserController = void 0;
4
+ const EventBusSingleton_1 = require("../common/EventBusSingleton");
5
+ const UserEvents_1 = require("../UserEvents");
6
+ const rxjs_1 = require("rxjs");
7
+ class UserController {
8
+ async loginUser(userFSId) {
9
+ const eventBus = (0, EventBusSingleton_1.getEventBus)();
10
+ const isUserCreatedForFirebaseUid = (event) => {
11
+ return event instanceof UserEvents_1.UserCreated && event.firebaseUid === userFSId;
12
+ };
13
+ try {
14
+ const event = await (0, rxjs_1.firstValueFrom)(eventBus.observable(isUserCreatedForFirebaseUid).pipe((0, rxjs_1.timeout)(10000)));
15
+ if (!event.userId) {
16
+ throw new Error('UserCreated event missing userId');
17
+ }
18
+ return event.userId;
19
+ }
20
+ catch (error) {
21
+ if (error.name === 'TimeoutError') {
22
+ throw new Error(`Timeout waiting for UserCreated event for firebaseUid: ${userFSId}`);
23
+ }
24
+ throw error;
25
+ }
26
+ }
27
+ }
28
+ exports.UserController = UserController;
package/dist/events.d.ts CHANGED
@@ -27,4 +27,5 @@ export { GroupTransactionEventHandler } from './GroupTransactionEventHandler';
27
27
  export { ActivityEventHandler } from './ActivityEventHandler';
28
28
  export { reverseSplitType, calculateMirrorAmountAndIsOwed } from './utils/splitTypeUtils';
29
29
  export { getUserEventPath } from './utils/userPathUtils';
30
+ export { EventProcessorSource, setEventProcessorSource, getEventProcessorSource, isGCFun, isClient, isUnknown, resetEventProcessorSource, eventProcessorEnv } from './config/environment';
30
31
  //# sourceMappingURL=events.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AACA,cAAc,qBAAqB,CAAC;AACpC,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC;AAGpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAG/B,cAAc,oBAAoB,CAAC;AACnC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,oCAAoC,CAAC;AACnD,cAAc,oCAAoC,CAAC;AACnD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAG9C,OAAO,EAAC,uBAAuB,EAAC,MAAM,2BAA2B,CAAC;AAClE,OAAO,EAAC,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAC,kBAAkB,EAAC,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAC,iBAAiB,EAAC,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAC,4BAA4B,EAAC,MAAM,gCAAgC,CAAC;AAC5E,OAAO,EAAC,oBAAoB,EAAC,MAAM,wBAAwB,CAAC;AAG5D,OAAO,EAAC,gBAAgB,EAAE,8BAA8B,EAAC,MAAM,wBAAwB,CAAC;AACxF,OAAO,EAAC,gBAAgB,EAAC,MAAM,uBAAuB,CAAC"}
1
+ {"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AACA,cAAc,qBAAqB,CAAC;AACpC,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC;AAGpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAG/B,cAAc,oBAAoB,CAAC;AACnC,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AACzC,cAAc,yBAAyB,CAAC;AACxC,cAAc,yBAAyB,CAAC;AACxC,cAAc,oCAAoC,CAAC;AACnD,cAAc,oCAAoC,CAAC;AACnD,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC;AAG9C,OAAO,EAAC,uBAAuB,EAAC,MAAM,2BAA2B,CAAC;AAClE,OAAO,EAAC,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAC,kBAAkB,EAAC,MAAM,sBAAsB,CAAC;AACxD,OAAO,EAAC,iBAAiB,EAAC,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAC,4BAA4B,EAAC,MAAM,gCAAgC,CAAC;AAC5E,OAAO,EAAC,oBAAoB,EAAC,MAAM,wBAAwB,CAAC;AAG5D,OAAO,EAAC,gBAAgB,EAAE,8BAA8B,EAAC,MAAM,wBAAwB,CAAC;AACxF,OAAO,EAAC,gBAAgB,EAAC,MAAM,uBAAuB,CAAC;AAGvD,OAAO,EACL,oBAAoB,EACpB,uBAAuB,EACvB,uBAAuB,EACvB,OAAO,EACP,QAAQ,EACR,SAAS,EACT,yBAAyB,EACzB,iBAAiB,EAClB,MAAM,sBAAsB,CAAC"}
package/dist/events.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.getUserEventPath = exports.calculateMirrorAmountAndIsOwed = exports.reverseSplitType = exports.ActivityEventHandler = exports.GroupTransactionEventHandler = exports.GroupEventHandler = exports.FriendEventHandler = exports.UserEventHandler = exports.TransactionEventHandler = void 0;
17
+ exports.eventProcessorEnv = exports.resetEventProcessorSource = exports.isUnknown = exports.isClient = exports.isGCFun = exports.getEventProcessorSource = exports.setEventProcessorSource = exports.EventProcessorSource = exports.getUserEventPath = exports.calculateMirrorAmountAndIsOwed = exports.reverseSplitType = exports.ActivityEventHandler = exports.GroupTransactionEventHandler = exports.GroupEventHandler = exports.FriendEventHandler = exports.UserEventHandler = exports.TransactionEventHandler = void 0;
18
18
  // Re-export all event types
19
19
  __exportStar(require("./TransactionEvents"), exports);
20
20
  __exportStar(require("./UserEvents"), exports);
@@ -58,3 +58,13 @@ Object.defineProperty(exports, "reverseSplitType", { enumerable: true, get: func
58
58
  Object.defineProperty(exports, "calculateMirrorAmountAndIsOwed", { enumerable: true, get: function () { return splitTypeUtils_1.calculateMirrorAmountAndIsOwed; } });
59
59
  var userPathUtils_1 = require("./utils/userPathUtils");
60
60
  Object.defineProperty(exports, "getUserEventPath", { enumerable: true, get: function () { return userPathUtils_1.getUserEventPath; } });
61
+ // Re-export environment configuration
62
+ var environment_1 = require("./config/environment");
63
+ Object.defineProperty(exports, "EventProcessorSource", { enumerable: true, get: function () { return environment_1.EventProcessorSource; } });
64
+ Object.defineProperty(exports, "setEventProcessorSource", { enumerable: true, get: function () { return environment_1.setEventProcessorSource; } });
65
+ Object.defineProperty(exports, "getEventProcessorSource", { enumerable: true, get: function () { return environment_1.getEventProcessorSource; } });
66
+ Object.defineProperty(exports, "isGCFun", { enumerable: true, get: function () { return environment_1.isGCFun; } });
67
+ Object.defineProperty(exports, "isClient", { enumerable: true, get: function () { return environment_1.isClient; } });
68
+ Object.defineProperty(exports, "isUnknown", { enumerable: true, get: function () { return environment_1.isUnknown; } });
69
+ Object.defineProperty(exports, "resetEventProcessorSource", { enumerable: true, get: function () { return environment_1.resetEventProcessorSource; } });
70
+ Object.defineProperty(exports, "eventProcessorEnv", { enumerable: true, get: function () { return environment_1.eventProcessorEnv; } });
package/dist/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './events';
2
+ export { UserController } from './controllers/UserController';
3
+ export { resetEventBus } from './common/EventBusSingleton';
2
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,UAAU,CAAC;AAGzB,OAAO,EAAC,cAAc,EAAC,MAAM,8BAA8B,CAAC;AAG5D,OAAO,EAAC,aAAa,EAAC,MAAM,4BAA4B,CAAC"}
package/dist/index.js CHANGED
@@ -14,5 +14,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.resetEventBus = exports.UserController = void 0;
17
18
  // Export all backend event definitions here
18
19
  __exportStar(require("./events"), exports);
20
+ // Export controllers
21
+ var UserController_1 = require("./controllers/UserController");
22
+ Object.defineProperty(exports, "UserController", { enumerable: true, get: function () { return UserController_1.UserController; } });
23
+ // Export singleton utilities for testing
24
+ var EventBusSingleton_1 = require("./common/EventBusSingleton");
25
+ Object.defineProperty(exports, "resetEventBus", { enumerable: true, get: function () { return EventBusSingleton_1.resetEventBus; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeeshan60/event-processor",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "description": "Shared event sourcing infrastructure for Loan Tracker projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -32,6 +32,7 @@
32
32
  "typescript": "^5.3.3"
33
33
  },
34
34
  "dependencies": {
35
+ "rxjs": "^7.8.1",
35
36
  "uuid": "^13.0.0"
36
37
  }
37
38
  }