@theelena/shared 1.0.1 → 1.0.3

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.
@@ -0,0 +1,112 @@
1
+ // src/events/base.ts
2
+ var EventChannels = {
3
+ restaurant: (restaurantId) => `restaurant:${restaurantId}`,
4
+ user: (userId) => `user:${userId}`,
5
+ global: () => "global"
6
+ };
7
+ var BaseEvent = class {
8
+ /**
9
+ * Event payload data
10
+ */
11
+ data;
12
+ /**
13
+ * Event creation timestamp (milliseconds since epoch)
14
+ */
15
+ timestamp;
16
+ constructor(data) {
17
+ this.data = data;
18
+ this.timestamp = Date.now();
19
+ }
20
+ /**
21
+ * Deduce scope from event type prefix
22
+ * Override in subclass if custom scope logic is needed
23
+ */
24
+ getScope() {
25
+ const domain = this.type.split(".")[0] ?? "";
26
+ const restaurantDomains = ["order", "product", "sale", "customer", "table", "category"];
27
+ if (restaurantDomains.includes(domain)) {
28
+ return "restaurant";
29
+ }
30
+ const userDomains = ["notification", "system", "alert"];
31
+ if (userDomains.includes(domain)) {
32
+ return "user";
33
+ }
34
+ return "global";
35
+ }
36
+ /**
37
+ * Get the Redis channel for this event based on context
38
+ */
39
+ getChannel(context) {
40
+ const scope = this.getScope();
41
+ switch (scope) {
42
+ case "restaurant":
43
+ return EventChannels.restaurant(context.restaurantId);
44
+ case "user":
45
+ return EventChannels.user(context.userId);
46
+ case "global":
47
+ return EventChannels.global();
48
+ }
49
+ }
50
+ /**
51
+ * Get all channels this event should be published to
52
+ * By default, only returns the primary channel
53
+ * Override for multi-channel publishing
54
+ */
55
+ getChannels(context) {
56
+ return [this.getChannel(context)];
57
+ }
58
+ /**
59
+ * Serialize event for transmission
60
+ */
61
+ toJSON() {
62
+ return {
63
+ type: this.type,
64
+ data: this.data,
65
+ timestamp: this.timestamp
66
+ };
67
+ }
68
+ /**
69
+ * Create event instance from serialized data
70
+ * Must be implemented by concrete event classes for deserialization
71
+ */
72
+ static fromJSON(json) {
73
+ return new this(json.data);
74
+ }
75
+ };
76
+
77
+ // src/events/order.events.ts
78
+ var OrderCreatedEvent = class extends BaseEvent {
79
+ type = "order.created";
80
+ };
81
+ var OrderStatusChangedEvent = class extends BaseEvent {
82
+ type = "order.status_changed";
83
+ };
84
+
85
+ // src/events/table.events.ts
86
+ var TableStatusChangedEvent = class extends BaseEvent {
87
+ type = "table.status_changed";
88
+ };
89
+
90
+ // src/events/notification.events.ts
91
+ var SystemNotificationEvent = class extends BaseEvent {
92
+ type = "system.notification";
93
+ };
94
+ var AlertEvent = class extends BaseEvent {
95
+ type = "alert.triggered";
96
+ };
97
+
98
+ // src/events/staff.events.ts
99
+ var StaffJoinedEvent = class extends BaseEvent {
100
+ type = "staff.joined";
101
+ };
102
+
103
+ export {
104
+ EventChannels,
105
+ BaseEvent,
106
+ OrderCreatedEvent,
107
+ OrderStatusChangedEvent,
108
+ TableStatusChangedEvent,
109
+ SystemNotificationEvent,
110
+ AlertEvent,
111
+ StaffJoinedEvent
112
+ };
@@ -0,0 +1,172 @@
1
+ /**
2
+ * Event scope types for channel routing
3
+ */
4
+ type EventScope = "restaurant" | "user" | "global";
5
+ /**
6
+ * Context required for channel resolution
7
+ */
8
+ interface EventContext {
9
+ restaurantId: string;
10
+ userId: string;
11
+ }
12
+ /**
13
+ * Serialized event format for SSE transmission
14
+ */
15
+ interface SerializedEvent<TData = unknown> {
16
+ type: string;
17
+ data: TData;
18
+ timestamp: number;
19
+ }
20
+ /**
21
+ * Channel name generators for event routing
22
+ */
23
+ declare const EventChannels: {
24
+ readonly restaurant: (restaurantId: string) => string;
25
+ readonly user: (userId: string) => string;
26
+ readonly global: () => string;
27
+ };
28
+ /**
29
+ * Base class for all application events
30
+ *
31
+ * Scope is automatically deduced from event type prefix:
32
+ * - 'order.*', 'product.*', 'sale.*', 'customer.*' → restaurant scope
33
+ * - 'notification.*', 'system.*' → user scope (owner messages)
34
+ * - default → global scope
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * class OrderCreatedEvent extends BaseEvent<OrderCreatedData> {
39
+ * readonly type = 'order.created' as const;
40
+ * }
41
+ *
42
+ * const event = new OrderCreatedEvent({ orderId: '123', ... });
43
+ * const channel = event.getChannel({ restaurantId: 'abc', userId: 'xyz' });
44
+ * // Returns: 'restaurant:abc'
45
+ * ```
46
+ */
47
+ declare abstract class BaseEvent<TData = unknown> {
48
+ /**
49
+ * Event type identifier - used for routing and filtering
50
+ * Convention: '{domain}.{action}' e.g., 'order.created', 'notification.sent'
51
+ */
52
+ abstract readonly type: string;
53
+ /**
54
+ * Event payload data
55
+ */
56
+ readonly data: TData;
57
+ /**
58
+ * Event creation timestamp (milliseconds since epoch)
59
+ */
60
+ readonly timestamp: number;
61
+ constructor(data: TData);
62
+ /**
63
+ * Deduce scope from event type prefix
64
+ * Override in subclass if custom scope logic is needed
65
+ */
66
+ protected getScope(): EventScope;
67
+ /**
68
+ * Get the Redis channel for this event based on context
69
+ */
70
+ getChannel(context: EventContext): string;
71
+ /**
72
+ * Get all channels this event should be published to
73
+ * By default, only returns the primary channel
74
+ * Override for multi-channel publishing
75
+ */
76
+ getChannels(context: EventContext): string[];
77
+ /**
78
+ * Serialize event for transmission
79
+ */
80
+ toJSON(): SerializedEvent<TData>;
81
+ /**
82
+ * Create event instance from serialized data
83
+ * Must be implemented by concrete event classes for deserialization
84
+ */
85
+ static fromJSON<T extends BaseEvent>(this: new (data: any) => T, json: SerializedEvent): T;
86
+ }
87
+ /**
88
+ * Type helper to extract data type from event class
89
+ */
90
+ type EventData<E extends BaseEvent> = E extends BaseEvent<infer D> ? D : never;
91
+ /**
92
+ * Type helper for event class constructor
93
+ */
94
+ type EventClass<E extends BaseEvent = BaseEvent> = new (data: EventData<E>) => E;
95
+
96
+ interface OrderCreatedData {
97
+ orderId: string;
98
+ orderNumber: string;
99
+ orderType: "takeout" | "dine_in" | "reservation";
100
+ customerName?: string;
101
+ tableName?: string;
102
+ }
103
+ interface OrderStatusChangedData {
104
+ orderId: string;
105
+ orderNumber: string;
106
+ oldStatus: string;
107
+ newStatus: string;
108
+ updatedBy: string;
109
+ }
110
+ declare class OrderCreatedEvent extends BaseEvent<OrderCreatedData> {
111
+ readonly type: "order.created";
112
+ }
113
+ declare class OrderStatusChangedEvent extends BaseEvent<OrderStatusChangedData> {
114
+ readonly type: "order.status_changed";
115
+ }
116
+
117
+ interface TableStatusChangedData {
118
+ tableId: string;
119
+ tableNumber: string;
120
+ oldStatus: "disponible" | "ocupada" | "reservada";
121
+ newStatus: "disponible" | "ocupada" | "reservada";
122
+ reason?: string;
123
+ }
124
+ declare class TableStatusChangedEvent extends BaseEvent<TableStatusChangedData> {
125
+ readonly type: "table.status_changed";
126
+ }
127
+
128
+ interface SystemNotificationData {
129
+ title: string;
130
+ message: string;
131
+ severity: "info" | "warning" | "error" | "success";
132
+ actionUrl?: string;
133
+ }
134
+ interface AlertData {
135
+ alertId: string;
136
+ alertType: string;
137
+ message: string;
138
+ metadata?: Record<string, unknown>;
139
+ }
140
+ declare class SystemNotificationEvent extends BaseEvent<SystemNotificationData> {
141
+ readonly type: "system.notification";
142
+ }
143
+ declare class AlertEvent extends BaseEvent<AlertData> {
144
+ readonly type: "alert.triggered";
145
+ }
146
+
147
+ /**
148
+ * Staff Events
149
+ * Module: Staff Management (Module 15)
150
+ *
151
+ * Events related to staff members joining organizations
152
+ */
153
+
154
+ interface StaffJoinedData {
155
+ memberId: string;
156
+ userName: string;
157
+ userEmail: string;
158
+ role: string;
159
+ restaurantId: string;
160
+ restaurantName: string;
161
+ joinedAt: string;
162
+ }
163
+ /**
164
+ * StaffJoinedEvent
165
+ * Fired when a new staff member accepts an invitation and joins an organization
166
+ * Scope: restaurant (prefix "staff.*" → routes to restaurant:{restaurantId})
167
+ */
168
+ declare class StaffJoinedEvent extends BaseEvent<StaffJoinedData> {
169
+ readonly type: "staff.joined";
170
+ }
171
+
172
+ export { type AlertData, AlertEvent, BaseEvent, EventChannels, type EventClass, type EventContext, type EventData, type EventScope, type OrderCreatedData, OrderCreatedEvent, type OrderStatusChangedData, OrderStatusChangedEvent, type SerializedEvent, type StaffJoinedData, StaffJoinedEvent, type SystemNotificationData, SystemNotificationEvent, type TableStatusChangedData, TableStatusChangedEvent };
@@ -0,0 +1,20 @@
1
+ import {
2
+ AlertEvent,
3
+ BaseEvent,
4
+ EventChannels,
5
+ OrderCreatedEvent,
6
+ OrderStatusChangedEvent,
7
+ StaffJoinedEvent,
8
+ SystemNotificationEvent,
9
+ TableStatusChangedEvent
10
+ } from "../chunk-DAZIRU7I.js";
11
+ export {
12
+ AlertEvent,
13
+ BaseEvent,
14
+ EventChannels,
15
+ OrderCreatedEvent,
16
+ OrderStatusChangedEvent,
17
+ StaffJoinedEvent,
18
+ SystemNotificationEvent,
19
+ TableStatusChangedEvent
20
+ };
@@ -0,0 +1 @@
1
+ export { AlertData, AlertEvent, BaseEvent, EventChannels, EventClass, EventContext, EventData, EventScope, OrderCreatedData, OrderCreatedEvent, OrderStatusChangedData, OrderStatusChangedEvent, SerializedEvent, StaffJoinedData, StaffJoinedEvent, SystemNotificationData, SystemNotificationEvent, TableStatusChangedData, TableStatusChangedEvent } from './events/index.js';
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ import {
2
+ AlertEvent,
3
+ BaseEvent,
4
+ EventChannels,
5
+ OrderCreatedEvent,
6
+ OrderStatusChangedEvent,
7
+ StaffJoinedEvent,
8
+ SystemNotificationEvent,
9
+ TableStatusChangedEvent
10
+ } from "./chunk-DAZIRU7I.js";
11
+ export {
12
+ AlertEvent,
13
+ BaseEvent,
14
+ EventChannels,
15
+ OrderCreatedEvent,
16
+ OrderStatusChangedEvent,
17
+ StaffJoinedEvent,
18
+ SystemNotificationEvent,
19
+ TableStatusChangedEvent
20
+ };
package/package.json CHANGED
@@ -1,22 +1,28 @@
1
1
  {
2
2
  "name": "@theelena/shared",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
- "main": "./index.js",
6
- "types": "./index.ts",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
7
  "exports": {
8
8
  ".": {
9
- "types": "./src/index.ts",
10
- "import": "./src/index.ts",
11
- "default": "./src/index.ts"
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
12
11
  },
13
12
  "./events": {
14
- "types": "./src/events/index.ts",
15
- "import": "./src/events/index.ts",
16
- "default": "./src/events/index.ts"
13
+ "types": "./dist/events/index.d.ts",
14
+ "import": "./dist/events/index.js"
17
15
  }
18
16
  },
19
17
  "files": [
20
18
  "dist"
21
- ]
19
+ ],
20
+ "scripts": {
21
+ "build": "tsup src/index.ts src/events/index.ts --format esm --dts --out-dir dist",
22
+ "build:watch": "tsup src/index.ts src/events/index.ts --format esm --dts --out-dir dist --watch"
23
+ },
24
+ "devDependencies": {
25
+ "tsup": "^8.0.2",
26
+ "typescript": "^5"
27
+ }
22
28
  }