@memberjunction/communication-engine 6.3.0-edge.0 → 6.3.0-edge.2
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 +203 -0
- package/dist/CommsActivityWorker.d.ts +130 -0
- package/dist/CommsActivityWorker.d.ts.map +1 -0
- package/dist/CommsActivityWorker.js +1054 -0
- package/dist/CommsActivityWorker.js.map +1 -0
- package/dist/DefaultCommunicationPrincipal.d.ts +62 -0
- package/dist/DefaultCommunicationPrincipal.d.ts.map +1 -0
- package/dist/DefaultCommunicationPrincipal.js +382 -0
- package/dist/DefaultCommunicationPrincipal.js.map +1 -0
- package/dist/Engine.d.ts +54 -1
- package/dist/Engine.d.ts.map +1 -1
- package/dist/Engine.js +123 -2
- package/dist/Engine.js.map +1 -1
- package/dist/LocalJsonCommunicationProvider.d.ts +133 -0
- package/dist/LocalJsonCommunicationProvider.d.ts.map +1 -0
- package/dist/LocalJsonCommunicationProvider.js +1337 -0
- package/dist/LocalJsonCommunicationProvider.js.map +1 -0
- package/dist/V2SendService.d.ts +293 -0
- package/dist/V2SendService.d.ts.map +1 -0
- package/dist/V2SendService.js +2035 -0
- package/dist/V2SendService.js.map +1 -0
- package/dist/compliance/ComplianceGate.d.ts +56 -0
- package/dist/compliance/ComplianceGate.d.ts.map +1 -0
- package/dist/compliance/ComplianceGate.js +452 -0
- package/dist/compliance/ComplianceGate.js.map +1 -0
- package/dist/compliance/JurisdictionResolver.d.ts +82 -0
- package/dist/compliance/JurisdictionResolver.d.ts.map +1 -0
- package/dist/compliance/JurisdictionResolver.js +249 -0
- package/dist/compliance/JurisdictionResolver.js.map +1 -0
- package/dist/compliance/index.d.ts +4 -0
- package/dist/compliance/index.d.ts.map +1 -0
- package/dist/compliance/index.js +4 -0
- package/dist/compliance/index.js.map +1 -0
- package/dist/compliance/rules/ConsentEvidenceRuleDriver.d.ts +22 -0
- package/dist/compliance/rules/ConsentEvidenceRuleDriver.d.ts.map +1 -0
- package/dist/compliance/rules/ConsentEvidenceRuleDriver.js +76 -0
- package/dist/compliance/rules/ConsentEvidenceRuleDriver.js.map +1 -0
- package/dist/compliance/rules/ConsentExpiryRuleDriver.d.ts +20 -0
- package/dist/compliance/rules/ConsentExpiryRuleDriver.d.ts.map +1 -0
- package/dist/compliance/rules/ConsentExpiryRuleDriver.js +70 -0
- package/dist/compliance/rules/ConsentExpiryRuleDriver.js.map +1 -0
- package/dist/compliance/rules/ConsentModelRuleDriver.d.ts +22 -0
- package/dist/compliance/rules/ConsentModelRuleDriver.d.ts.map +1 -0
- package/dist/compliance/rules/ConsentModelRuleDriver.js +65 -0
- package/dist/compliance/rules/ConsentModelRuleDriver.js.map +1 -0
- package/dist/compliance/rules/QuietHoursRuleDriver.d.ts +37 -0
- package/dist/compliance/rules/QuietHoursRuleDriver.d.ts.map +1 -0
- package/dist/compliance/rules/QuietHoursRuleDriver.js +144 -0
- package/dist/compliance/rules/QuietHoursRuleDriver.js.map +1 -0
- package/dist/compliance/rules/RequireDoubleOptInRuleDriver.d.ts +16 -0
- package/dist/compliance/rules/RequireDoubleOptInRuleDriver.d.ts.map +1 -0
- package/dist/compliance/rules/RequireDoubleOptInRuleDriver.js +40 -0
- package/dist/compliance/rules/RequireDoubleOptInRuleDriver.js.map +1 -0
- package/dist/compliance/rules/RuleParamUtils.d.ts +13 -0
- package/dist/compliance/rules/RuleParamUtils.d.ts.map +1 -0
- package/dist/compliance/rules/RuleParamUtils.js +51 -0
- package/dist/compliance/rules/RuleParamUtils.js.map +1 -0
- package/dist/compliance/rules/SenderIdentificationRuleDriver.d.ts +17 -0
- package/dist/compliance/rules/SenderIdentificationRuleDriver.d.ts.map +1 -0
- package/dist/compliance/rules/SenderIdentificationRuleDriver.js +64 -0
- package/dist/compliance/rules/SenderIdentificationRuleDriver.js.map +1 -0
- package/dist/compliance/rules/TrackingConsentRuleDriver.d.ts +23 -0
- package/dist/compliance/rules/TrackingConsentRuleDriver.d.ts.map +1 -0
- package/dist/compliance/rules/TrackingConsentRuleDriver.js +81 -0
- package/dist/compliance/rules/TrackingConsentRuleDriver.js.map +1 -0
- package/dist/compliance/rules/UnsubscribeHonorWindowRuleDriver.d.ts +21 -0
- package/dist/compliance/rules/UnsubscribeHonorWindowRuleDriver.d.ts.map +1 -0
- package/dist/compliance/rules/UnsubscribeHonorWindowRuleDriver.js +33 -0
- package/dist/compliance/rules/UnsubscribeHonorWindowRuleDriver.js.map +1 -0
- package/dist/compliance/rules/UnsubscribeMechanismRuleDriver.d.ts +19 -0
- package/dist/compliance/rules/UnsubscribeMechanismRuleDriver.d.ts.map +1 -0
- package/dist/compliance/rules/UnsubscribeMechanismRuleDriver.js +73 -0
- package/dist/compliance/rules/UnsubscribeMechanismRuleDriver.js.map +1 -0
- package/dist/compliance/rules/index.d.ts +10 -0
- package/dist/compliance/rules/index.d.ts.map +1 -0
- package/dist/compliance/rules/index.js +10 -0
- package/dist/compliance/rules/index.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +9 -9
|
@@ -0,0 +1,1337 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import crypto from 'node:crypto';
|
|
4
|
+
import { MJGlobal, UUIDSet, UUIDsEqual } from '@memberjunction/global';
|
|
5
|
+
import { BaseCommunicationProvider, CalendarTargetError, CalendarUpdateError, } from '@memberjunction/communication-types';
|
|
6
|
+
/** Registered ClassFactory key and Communication Provider name for the local JSON mock driver. */
|
|
7
|
+
export const LOCAL_JSON_COMMUNICATION_PROVIDER_NAME = 'Local JSON Mock';
|
|
8
|
+
/** Environment variable controlling the root directory for per-mailbox JSON mock files. */
|
|
9
|
+
export const MJ_MOCK_COMM_DIR_ENV = 'MJ_MOCK_COMM_DIR';
|
|
10
|
+
/**
|
|
11
|
+
* Local filesystem JSON mock Email & Calendar communication provider for development,
|
|
12
|
+
* smoke testing, and automated CI suites.
|
|
13
|
+
*
|
|
14
|
+
* Persists per-mailbox state in `<MJ_MOCK_COMM_DIR>/<sanitized-email>.json` and auto-seeds
|
|
15
|
+
* a realistic executive inbox (4 threads) and calendar (4 events including a 2:00 PM conflict)
|
|
16
|
+
* on first access so Chief of Staff and user-scoped communication actions work out of the box
|
|
17
|
+
* without external OAuth credentials.
|
|
18
|
+
*/
|
|
19
|
+
export class LocalJsonCommunicationProvider extends BaseCommunicationProvider {
|
|
20
|
+
constructor(baseDir) {
|
|
21
|
+
super();
|
|
22
|
+
this._overrideDir = baseDir && baseDir.trim().length > 0 ? baseDir.trim() : null;
|
|
23
|
+
}
|
|
24
|
+
get ProviderName() {
|
|
25
|
+
return LOCAL_JSON_COMMUNICATION_PROVIDER_NAME;
|
|
26
|
+
}
|
|
27
|
+
get MessageRetrieval() {
|
|
28
|
+
return {
|
|
29
|
+
FilterByReceivedDate: true,
|
|
30
|
+
FilterByUnread: true,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
getSupportedOperations() {
|
|
34
|
+
return [
|
|
35
|
+
'SendSingleMessage',
|
|
36
|
+
'GetMessages',
|
|
37
|
+
'GetSingleMessage',
|
|
38
|
+
'DeleteMessage',
|
|
39
|
+
'MarkAsRead',
|
|
40
|
+
'ArchiveMessage',
|
|
41
|
+
'SearchMessages',
|
|
42
|
+
'ForwardMessage',
|
|
43
|
+
'ReplyToMessage',
|
|
44
|
+
'CreateDraft',
|
|
45
|
+
'GetEvents',
|
|
46
|
+
'CreateEvent',
|
|
47
|
+
'UpdateEvent',
|
|
48
|
+
'CancelEvent',
|
|
49
|
+
'GetEvent',
|
|
50
|
+
'SyncEvents',
|
|
51
|
+
'GetFreeBusy',
|
|
52
|
+
'FindMeetingTimes',
|
|
53
|
+
'CreateCalendarEvent',
|
|
54
|
+
'UpdateCalendarEvent',
|
|
55
|
+
'RespondToCalendarEvent',
|
|
56
|
+
'CancelCalendarEvent',
|
|
57
|
+
'VerifyMailboxAccess',
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Resolves the filesystem directory for JSON mailbox files, throwing null when unconfigured.
|
|
62
|
+
*/
|
|
63
|
+
ResolveDataDir(credentials) {
|
|
64
|
+
const credDir = credentials?.mockDir?.trim();
|
|
65
|
+
if (credDir) {
|
|
66
|
+
return credDir;
|
|
67
|
+
}
|
|
68
|
+
if (this._overrideDir) {
|
|
69
|
+
return this._overrideDir;
|
|
70
|
+
}
|
|
71
|
+
const envDir = process.env[MJ_MOCK_COMM_DIR_ENV]?.trim();
|
|
72
|
+
return envDir && envDir.length > 0 ? envDir : null;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Converts an email/identifier into a safe filename inside the mock directory.
|
|
76
|
+
*/
|
|
77
|
+
static SanitizeMailboxFilename(identifier) {
|
|
78
|
+
const normalized = identifier.trim().toLowerCase();
|
|
79
|
+
const safe = normalized.replace(/[^a-z0-9._@+-]/g, '_').replace(/^\.+/, '');
|
|
80
|
+
return `${safe || 'default'}.json`;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Returns the full JSON file path for a mailbox identifier.
|
|
84
|
+
*/
|
|
85
|
+
GetMailboxFilePath(identifier, credentials) {
|
|
86
|
+
const dir = this.ResolveDataDir(credentials);
|
|
87
|
+
if (!dir) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
return path.join(dir, LocalJsonCommunicationProvider.SanitizeMailboxFilename(identifier));
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Reads a mailbox JSON state file, auto-seeding realistic inbox + calendar data if missing.
|
|
94
|
+
*/
|
|
95
|
+
LoadOrSeedMailbox(identifier, credentials) {
|
|
96
|
+
const dir = this.ResolveDataDir(credentials);
|
|
97
|
+
if (!dir) {
|
|
98
|
+
throw new Error(`${LOCAL_JSON_COMMUNICATION_PROVIDER_NAME} requires ${MJ_MOCK_COMM_DIR_ENV} or a configured directory.`);
|
|
99
|
+
}
|
|
100
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
101
|
+
const normalizedEmail = identifier.trim().toLowerCase() || 'default@local.mock';
|
|
102
|
+
const filePath = path.join(dir, LocalJsonCommunicationProvider.SanitizeMailboxFilename(normalizedEmail));
|
|
103
|
+
if (fs.existsSync(filePath)) {
|
|
104
|
+
const raw = fs.readFileSync(filePath, 'utf8');
|
|
105
|
+
const parsed = JSON.parse(raw);
|
|
106
|
+
return {
|
|
107
|
+
Mailbox: parsed.Mailbox || normalizedEmail,
|
|
108
|
+
SeededAt: parsed.SeededAt || new Date().toISOString(),
|
|
109
|
+
Messages: Array.isArray(parsed.Messages) ? parsed.Messages : [],
|
|
110
|
+
Events: Array.isArray(parsed.Events) ? parsed.Events : [],
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const seeded = this.buildDefaultSeedState(normalizedEmail);
|
|
114
|
+
this.saveMailboxState(filePath, seeded);
|
|
115
|
+
return seeded;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Persists a mailbox JSON state file atomically to disk.
|
|
119
|
+
*/
|
|
120
|
+
SaveMailbox(state, credentials) {
|
|
121
|
+
const dir = this.ResolveDataDir(credentials);
|
|
122
|
+
if (!dir) {
|
|
123
|
+
throw new Error(`${LOCAL_JSON_COMMUNICATION_PROVIDER_NAME} requires ${MJ_MOCK_COMM_DIR_ENV} or a configured directory.`);
|
|
124
|
+
}
|
|
125
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
126
|
+
const filePath = path.join(dir, LocalJsonCommunicationProvider.SanitizeMailboxFilename(state.Mailbox));
|
|
127
|
+
this.saveMailboxState(filePath, state);
|
|
128
|
+
}
|
|
129
|
+
saveMailboxState(filePath, state) {
|
|
130
|
+
fs.writeFileSync(filePath, JSON.stringify(state, null, 2), 'utf8');
|
|
131
|
+
}
|
|
132
|
+
buildDefaultSeedState(mailbox) {
|
|
133
|
+
const now = new Date();
|
|
134
|
+
const nowIso = now.toISOString();
|
|
135
|
+
const hourMs = 3_600_000;
|
|
136
|
+
// Anchor the 2:00 PM conflict today when it is still before 1:00 PM local time,
|
|
137
|
+
// or tomorrow when running late in the day so default GetEvents(now .. +7d) always sees it.
|
|
138
|
+
const conflictDayOffset = now.getHours() < 13 ? 0 : 1;
|
|
139
|
+
const makeLocalSlot = (dayOffset, hour, minute) => {
|
|
140
|
+
const d = new Date(now.getFullYear(), now.getMonth(), now.getDate() + dayOffset, hour, minute, 0, 0);
|
|
141
|
+
return d.toISOString();
|
|
142
|
+
};
|
|
143
|
+
const messages = [
|
|
144
|
+
{
|
|
145
|
+
ID: 'mock-msg-001',
|
|
146
|
+
ThreadID: 'thread-board-deck',
|
|
147
|
+
Folder: 'Inbox',
|
|
148
|
+
Direction: 'Inbound',
|
|
149
|
+
From: 'sarah.chen@bluecypress.io',
|
|
150
|
+
To: mailbox,
|
|
151
|
+
ToRecipients: [mailbox],
|
|
152
|
+
CCRecipients: ['marcus.rivera@bluecypress.io'],
|
|
153
|
+
BCCRecipients: [],
|
|
154
|
+
Subject: 'Q4 Board Deck — Final Numbers Needed by 4 PM',
|
|
155
|
+
Body: 'Hi — can we confirm the Q4 enterprise ARR forecast and EMEA churn breakdown before we freeze the board deck at 4:00 PM? Also flagged a 2:00 PM calendar overlap with the customer escalation call.',
|
|
156
|
+
IsRead: false,
|
|
157
|
+
CreatedAt: new Date(now.getTime() - 2 * hourMs).toISOString(),
|
|
158
|
+
ReceivedAt: new Date(now.getTime() - 2 * hourMs).toISOString(),
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
ID: 'mock-msg-002',
|
|
162
|
+
ThreadID: 'thread-renewal-escalation',
|
|
163
|
+
Folder: 'Inbox',
|
|
164
|
+
Direction: 'Inbound',
|
|
165
|
+
From: 'marcus.rivera@bluecypress.io',
|
|
166
|
+
To: mailbox,
|
|
167
|
+
ToRecipients: [mailbox],
|
|
168
|
+
CCRecipients: [],
|
|
169
|
+
BCCRecipients: [],
|
|
170
|
+
Subject: 'Apex Health Enterprise Renewal — Scheduling Conflict at 2:00 PM',
|
|
171
|
+
Body: 'Apex Health requested a quick 45-minute executive alignment at 2:00 PM to review their 3-year expansion terms. Let me know if we should push this to 3:30 PM or tomorrow morning.',
|
|
172
|
+
IsRead: false,
|
|
173
|
+
CreatedAt: new Date(now.getTime() - 4 * hourMs).toISOString(),
|
|
174
|
+
ReceivedAt: new Date(now.getTime() - 4 * hourMs).toISOString(),
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
ID: 'mock-msg-003',
|
|
178
|
+
ThreadID: 'thread-cab-keynote',
|
|
179
|
+
Folder: 'Inbox',
|
|
180
|
+
Direction: 'Inbound',
|
|
181
|
+
From: 'elena.vance@association.org',
|
|
182
|
+
To: mailbox,
|
|
183
|
+
ToRecipients: [mailbox],
|
|
184
|
+
CCRecipients: ['events@association.org'],
|
|
185
|
+
BCCRecipients: [],
|
|
186
|
+
Subject: 'Invitation: Customer Advisory Board Keynote Outline',
|
|
187
|
+
Body: 'We would love to confirm your 20-minute opening keynote topic on autonomous enterprise agent orchestration. Please send a brief draft outline when convenient.',
|
|
188
|
+
IsRead: true,
|
|
189
|
+
CreatedAt: new Date(now.getTime() - 18 * hourMs).toISOString(),
|
|
190
|
+
ReceivedAt: new Date(now.getTime() - 18 * hourMs).toISOString(),
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
ID: 'mock-msg-004',
|
|
194
|
+
ThreadID: 'thread-eng-velocity',
|
|
195
|
+
Folder: 'Inbox',
|
|
196
|
+
Direction: 'Inbound',
|
|
197
|
+
From: 'eng-ops@bluecypress.io',
|
|
198
|
+
To: mailbox,
|
|
199
|
+
ToRecipients: [mailbox],
|
|
200
|
+
CCRecipients: [],
|
|
201
|
+
BCCRecipients: [],
|
|
202
|
+
Subject: 'Weekly Engineering Velocity & Release Readiness Digest',
|
|
203
|
+
Body: 'Agent Instances & Inheritance milestone is tracking green: 100% unit tests passing across core packages and zero P0 regressions.',
|
|
204
|
+
IsRead: true,
|
|
205
|
+
CreatedAt: new Date(now.getTime() - 28 * hourMs).toISOString(),
|
|
206
|
+
ReceivedAt: new Date(now.getTime() - 28 * hourMs).toISOString(),
|
|
207
|
+
},
|
|
208
|
+
];
|
|
209
|
+
const events = [
|
|
210
|
+
{
|
|
211
|
+
ID: 'mock-evt-001',
|
|
212
|
+
SeriesID: 'series-exec-standup',
|
|
213
|
+
ICalUID: 'ical-mock-evt-001@local.mock',
|
|
214
|
+
Subject: 'Executive Daily Standup',
|
|
215
|
+
Body: 'Daily leadership sync on top priorities and blockers.',
|
|
216
|
+
StartTime: makeLocalSlot(conflictDayOffset, 9, 30),
|
|
217
|
+
EndTime: makeLocalSlot(conflictDayOffset, 10, 0),
|
|
218
|
+
TimeZone: 'UTC',
|
|
219
|
+
IsAllDay: false,
|
|
220
|
+
Location: 'Zoom / Executive Room',
|
|
221
|
+
Organizer: mailbox,
|
|
222
|
+
Attendees: [
|
|
223
|
+
{ Email: 'sarah.chen@bluecypress.io', Name: 'Sarah Chen', Required: true, Response: 'Accepted' },
|
|
224
|
+
{ Email: 'marcus.rivera@bluecypress.io', Name: 'Marcus Rivera', Required: true, Response: 'Accepted' },
|
|
225
|
+
],
|
|
226
|
+
IsCancelled: false,
|
|
227
|
+
OnlineMeetingUrl: 'https://meet.local.mock/exec-standup',
|
|
228
|
+
ShowAs: 'Busy',
|
|
229
|
+
LastModified: nowIso,
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
ID: 'mock-evt-002',
|
|
233
|
+
SeriesID: null,
|
|
234
|
+
ICalUID: 'ical-mock-evt-002@local.mock',
|
|
235
|
+
Subject: 'Q4 Board Deck Review',
|
|
236
|
+
Body: 'Final walkthrough of Q4 board slides, ARR metrics, and strategic priorities.',
|
|
237
|
+
StartTime: makeLocalSlot(conflictDayOffset, 14, 0),
|
|
238
|
+
EndTime: makeLocalSlot(conflictDayOffset, 15, 0),
|
|
239
|
+
TimeZone: 'UTC',
|
|
240
|
+
IsAllDay: false,
|
|
241
|
+
Location: 'Boardroom A / Teams',
|
|
242
|
+
Organizer: 'sarah.chen@bluecypress.io',
|
|
243
|
+
Attendees: [
|
|
244
|
+
{ Email: mailbox, Required: true, Response: 'Accepted' },
|
|
245
|
+
{ Email: 'sarah.chen@bluecypress.io', Name: 'Sarah Chen', Required: true, Response: 'Accepted' },
|
|
246
|
+
],
|
|
247
|
+
IsCancelled: false,
|
|
248
|
+
OnlineMeetingUrl: 'https://meet.local.mock/q4-board-review',
|
|
249
|
+
ShowAs: 'Busy',
|
|
250
|
+
LastModified: nowIso,
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
ID: 'mock-evt-003',
|
|
254
|
+
SeriesID: null,
|
|
255
|
+
ICalUID: 'ical-mock-evt-003@local.mock',
|
|
256
|
+
Subject: 'Apex Health Enterprise Renewal Check-in (2:00 PM Conflict)',
|
|
257
|
+
Body: 'Double-booked at 2:00 PM against Q4 Board Deck Review — review 3-year expansion terms.',
|
|
258
|
+
StartTime: makeLocalSlot(conflictDayOffset, 14, 0),
|
|
259
|
+
EndTime: makeLocalSlot(conflictDayOffset, 14, 45),
|
|
260
|
+
TimeZone: 'UTC',
|
|
261
|
+
IsAllDay: false,
|
|
262
|
+
Location: 'Teams Meeting',
|
|
263
|
+
Organizer: 'marcus.rivera@bluecypress.io',
|
|
264
|
+
Attendees: [
|
|
265
|
+
{ Email: mailbox, Required: true, Response: 'Tentative' },
|
|
266
|
+
{ Email: 'marcus.rivera@bluecypress.io', Name: 'Marcus Rivera', Required: true, Response: 'Accepted' },
|
|
267
|
+
],
|
|
268
|
+
IsCancelled: false,
|
|
269
|
+
OnlineMeetingUrl: 'https://meet.local.mock/apex-renewal',
|
|
270
|
+
ShowAs: 'Busy',
|
|
271
|
+
LastModified: nowIso,
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
ID: 'mock-evt-004',
|
|
275
|
+
SeriesID: null,
|
|
276
|
+
ICalUID: 'ical-mock-evt-004@local.mock',
|
|
277
|
+
Subject: 'Product Strategy & AI Roadmap Sync',
|
|
278
|
+
Body: 'Review Agent Instances UX, Smart @Mentions, and multi-agent roadmap.',
|
|
279
|
+
StartTime: makeLocalSlot(conflictDayOffset + 1, 11, 0),
|
|
280
|
+
EndTime: makeLocalSlot(conflictDayOffset + 1, 12, 0),
|
|
281
|
+
TimeZone: 'UTC',
|
|
282
|
+
IsAllDay: false,
|
|
283
|
+
Location: 'Teams Meeting',
|
|
284
|
+
Organizer: mailbox,
|
|
285
|
+
Attendees: [
|
|
286
|
+
{ Email: 'sarah.chen@bluecypress.io', Name: 'Sarah Chen', Required: true, Response: 'Accepted' },
|
|
287
|
+
{ Email: 'eng-ops@bluecypress.io', Name: 'Engineering Ops', Required: false, Response: 'None' },
|
|
288
|
+
],
|
|
289
|
+
IsCancelled: false,
|
|
290
|
+
OnlineMeetingUrl: 'https://meet.local.mock/ai-roadmap',
|
|
291
|
+
ShowAs: 'Busy',
|
|
292
|
+
LastModified: nowIso,
|
|
293
|
+
},
|
|
294
|
+
];
|
|
295
|
+
return {
|
|
296
|
+
Mailbox: mailbox,
|
|
297
|
+
SeededAt: nowIso,
|
|
298
|
+
Messages: messages,
|
|
299
|
+
Events: events,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
resolveMailboxIdentifier(explicitIdentifier, contextData, fallbackEmail) {
|
|
303
|
+
const candidate = explicitIdentifier?.trim() ||
|
|
304
|
+
contextData?.Email?.trim() ||
|
|
305
|
+
contextData?.Mailbox?.trim() ||
|
|
306
|
+
contextData?.Identifier?.trim() ||
|
|
307
|
+
fallbackEmail?.trim() ||
|
|
308
|
+
'default@local.mock';
|
|
309
|
+
return candidate.toLowerCase();
|
|
310
|
+
}
|
|
311
|
+
toGetMessageMessage(stored) {
|
|
312
|
+
return {
|
|
313
|
+
From: stored.From,
|
|
314
|
+
To: stored.To,
|
|
315
|
+
ToRecipients: [...stored.ToRecipients],
|
|
316
|
+
CCRecipients: [...stored.CCRecipients],
|
|
317
|
+
BCCRecipients: [...stored.BCCRecipients],
|
|
318
|
+
Direction: stored.Direction,
|
|
319
|
+
Body: stored.Body,
|
|
320
|
+
Subject: stored.Subject,
|
|
321
|
+
ExternalSystemRecordID: stored.ID,
|
|
322
|
+
ThreadID: stored.ThreadID,
|
|
323
|
+
CreatedAt: new Date(stored.CreatedAt),
|
|
324
|
+
LastModifiedAt: new Date(stored.CreatedAt),
|
|
325
|
+
ReceivedAt: stored.ReceivedAt ? new Date(stored.ReceivedAt) : undefined,
|
|
326
|
+
SentAt: stored.SentAt ? new Date(stored.SentAt) : undefined,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
toGetEventsEvent(stored) {
|
|
330
|
+
const organizerLower = stored.Organizer.trim().toLowerCase();
|
|
331
|
+
return {
|
|
332
|
+
ExternalSystemRecordID: stored.ID,
|
|
333
|
+
SeriesID: stored.SeriesID,
|
|
334
|
+
Subject: stored.Subject,
|
|
335
|
+
Body: stored.Body,
|
|
336
|
+
StartTime: new Date(stored.StartTime),
|
|
337
|
+
EndTime: new Date(stored.EndTime),
|
|
338
|
+
Location: stored.Location,
|
|
339
|
+
Organizer: stored.Organizer,
|
|
340
|
+
Attendees: stored.Attendees
|
|
341
|
+
.map((a) => a.Email)
|
|
342
|
+
.filter((email) => email.trim().toLowerCase() !== organizerLower),
|
|
343
|
+
IsCancelled: stored.IsCancelled,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
toCalendarEvent(stored) {
|
|
347
|
+
const attendees = stored.Attendees.map((a) => ({
|
|
348
|
+
Email: a.Email,
|
|
349
|
+
Name: a.Name,
|
|
350
|
+
Type: a.Required ? 'Required' : 'Optional',
|
|
351
|
+
Response: a.Response,
|
|
352
|
+
}));
|
|
353
|
+
return {
|
|
354
|
+
ExternalEventID: stored.ID,
|
|
355
|
+
SeriesID: stored.SeriesID,
|
|
356
|
+
ICalUID: stored.ICalUID,
|
|
357
|
+
Subject: stored.Subject,
|
|
358
|
+
Body: stored.Body,
|
|
359
|
+
Start: new Date(stored.StartTime),
|
|
360
|
+
End: new Date(stored.EndTime),
|
|
361
|
+
IsAllDay: stored.IsAllDay,
|
|
362
|
+
Location: stored.Location,
|
|
363
|
+
Organizer: { Email: stored.Organizer },
|
|
364
|
+
Attendees: attendees,
|
|
365
|
+
IsCancelled: stored.IsCancelled,
|
|
366
|
+
OnlineMeetingUrl: stored.OnlineMeetingUrl,
|
|
367
|
+
LastModified: new Date(stored.LastModified),
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
// =========================================================================
|
|
371
|
+
// MAIL OPERATIONS
|
|
372
|
+
// =========================================================================
|
|
373
|
+
async SendSingleMessage(message, credentials) {
|
|
374
|
+
try {
|
|
375
|
+
const dir = this.ResolveDataDir(credentials);
|
|
376
|
+
if (!dir) {
|
|
377
|
+
return {
|
|
378
|
+
Message: message,
|
|
379
|
+
Success: false,
|
|
380
|
+
Error: `${LOCAL_JSON_COMMUNICATION_PROVIDER_NAME} requires ${MJ_MOCK_COMM_DIR_ENV}.`,
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
if (message.DryRun) {
|
|
384
|
+
return {
|
|
385
|
+
Message: message,
|
|
386
|
+
Success: true,
|
|
387
|
+
Error: '',
|
|
388
|
+
DryRun: true,
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
const mailbox = this.resolveMailboxIdentifier(null, message.ContextData, message.From);
|
|
392
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
393
|
+
const nowIso = new Date().toISOString();
|
|
394
|
+
const id = `mock-sent-${crypto.randomUUID()}`;
|
|
395
|
+
const threadId = `thread-${crypto.randomUUID().slice(0, 8)}`;
|
|
396
|
+
const toList = message.To ? [message.To] : [];
|
|
397
|
+
const sentRecord = {
|
|
398
|
+
ID: id,
|
|
399
|
+
ThreadID: threadId,
|
|
400
|
+
Folder: 'Sent',
|
|
401
|
+
Direction: 'Outbound',
|
|
402
|
+
From: message.From || mailbox,
|
|
403
|
+
To: message.To || '',
|
|
404
|
+
ToRecipients: toList,
|
|
405
|
+
CCRecipients: message.CCRecipients ? [...message.CCRecipients] : [],
|
|
406
|
+
BCCRecipients: message.BCCRecipients ? [...message.BCCRecipients] : [],
|
|
407
|
+
Subject: message.ProcessedSubject || message.Subject || '',
|
|
408
|
+
Body: message.ProcessedBody || message.Body || '',
|
|
409
|
+
HTMLBody: message.ProcessedHTMLBody || message.HTMLBody || undefined,
|
|
410
|
+
IsRead: true,
|
|
411
|
+
CreatedAt: nowIso,
|
|
412
|
+
SentAt: nowIso,
|
|
413
|
+
};
|
|
414
|
+
state.Messages.unshift(sentRecord);
|
|
415
|
+
this.SaveMailbox(state, credentials);
|
|
416
|
+
return {
|
|
417
|
+
Message: message,
|
|
418
|
+
Success: true,
|
|
419
|
+
Error: '',
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
catch (error) {
|
|
423
|
+
return {
|
|
424
|
+
Message: message,
|
|
425
|
+
Success: false,
|
|
426
|
+
Error: error instanceof Error ? error.message : String(error),
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
async GetMessages(params, credentials) {
|
|
431
|
+
try {
|
|
432
|
+
const mailbox = this.resolveMailboxIdentifier(params.Identifier, params.ContextData);
|
|
433
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
434
|
+
let filtered = state.Messages.filter((m) => m.Folder !== 'Drafts');
|
|
435
|
+
if (params.UnreadOnly) {
|
|
436
|
+
filtered = filtered.filter((m) => !m.IsRead);
|
|
437
|
+
}
|
|
438
|
+
if (params.ReceivedAfter) {
|
|
439
|
+
const afterMs = params.ReceivedAfter.getTime();
|
|
440
|
+
filtered = filtered.filter((m) => Date.parse(m.ReceivedAt || m.CreatedAt) >= afterMs);
|
|
441
|
+
}
|
|
442
|
+
if (params.ReceivedBefore) {
|
|
443
|
+
const beforeMs = params.ReceivedBefore.getTime();
|
|
444
|
+
filtered = filtered.filter((m) => Date.parse(m.ReceivedAt || m.CreatedAt) <= beforeMs);
|
|
445
|
+
}
|
|
446
|
+
filtered.sort((a, b) => Date.parse(b.ReceivedAt || b.CreatedAt) - Date.parse(a.ReceivedAt || a.CreatedAt));
|
|
447
|
+
const cap = params.NumMessages > 0 ? params.NumMessages : 20;
|
|
448
|
+
const sliced = filtered.slice(0, cap);
|
|
449
|
+
return {
|
|
450
|
+
Success: true,
|
|
451
|
+
Messages: sliced.map((m) => this.toGetMessageMessage(m)),
|
|
452
|
+
SourceData: sliced,
|
|
453
|
+
AppliedFilters: {
|
|
454
|
+
ReceivedAfter: Boolean(params.ReceivedAfter),
|
|
455
|
+
ReceivedBefore: Boolean(params.ReceivedBefore),
|
|
456
|
+
UnreadOnly: Boolean(params.UnreadOnly),
|
|
457
|
+
},
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
catch (error) {
|
|
461
|
+
return {
|
|
462
|
+
Success: false,
|
|
463
|
+
Messages: [],
|
|
464
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
async GetSingleMessage(params, credentials) {
|
|
469
|
+
try {
|
|
470
|
+
const mailbox = this.resolveMailboxIdentifier(null, params.ContextData);
|
|
471
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
472
|
+
const found = state.Messages.find((m) => UUIDsEqual(m.ID, params.MessageID));
|
|
473
|
+
if (!found) {
|
|
474
|
+
return {
|
|
475
|
+
Success: false,
|
|
476
|
+
ErrorMessage: `Message '${params.MessageID}' not found in mailbox '${mailbox}'.`,
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
return {
|
|
480
|
+
Success: true,
|
|
481
|
+
Message: this.toGetMessageMessage(found),
|
|
482
|
+
SourceData: found,
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
catch (error) {
|
|
486
|
+
return {
|
|
487
|
+
Success: false,
|
|
488
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
async SearchMessages(params, credentials) {
|
|
493
|
+
try {
|
|
494
|
+
const mailbox = this.resolveMailboxIdentifier(null, params.ContextData);
|
|
495
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
496
|
+
const query = params.Query.trim().toLowerCase();
|
|
497
|
+
const tokens = query.split(/\s+/).filter(Boolean);
|
|
498
|
+
const searchIn = params.SearchIn && params.SearchIn.length > 0 ? params.SearchIn : ['all'];
|
|
499
|
+
let matches = state.Messages.filter((m) => {
|
|
500
|
+
if (params.FolderID && m.Folder.toLowerCase() !== params.FolderID.toLowerCase()) {
|
|
501
|
+
return false;
|
|
502
|
+
}
|
|
503
|
+
const ts = Date.parse(m.ReceivedAt || m.CreatedAt);
|
|
504
|
+
if (params.FromDate && ts < params.FromDate.getTime()) {
|
|
505
|
+
return false;
|
|
506
|
+
}
|
|
507
|
+
if (params.ToDate && ts > params.ToDate.getTime()) {
|
|
508
|
+
return false;
|
|
509
|
+
}
|
|
510
|
+
if (tokens.length === 0) {
|
|
511
|
+
return true;
|
|
512
|
+
}
|
|
513
|
+
const parts = [];
|
|
514
|
+
if (searchIn.includes('all') || searchIn.includes('subject'))
|
|
515
|
+
parts.push(m.Subject);
|
|
516
|
+
if (searchIn.includes('all') || searchIn.includes('body'))
|
|
517
|
+
parts.push(m.Body);
|
|
518
|
+
if (searchIn.includes('all') || searchIn.includes('from'))
|
|
519
|
+
parts.push(m.From);
|
|
520
|
+
if (searchIn.includes('all') || searchIn.includes('to'))
|
|
521
|
+
parts.push(m.To, ...m.ToRecipients);
|
|
522
|
+
const haystack = parts.join(' ').toLowerCase();
|
|
523
|
+
return tokens.every((t) => haystack.includes(t));
|
|
524
|
+
});
|
|
525
|
+
matches.sort((a, b) => Date.parse(b.ReceivedAt || b.CreatedAt) - Date.parse(a.ReceivedAt || a.CreatedAt));
|
|
526
|
+
const totalCount = matches.length;
|
|
527
|
+
const max = params.MaxResults && params.MaxResults > 0 ? params.MaxResults : 20;
|
|
528
|
+
matches = matches.slice(0, max);
|
|
529
|
+
return {
|
|
530
|
+
Success: true,
|
|
531
|
+
Messages: matches.map((m) => this.toGetMessageMessage(m)),
|
|
532
|
+
TotalCount: totalCount,
|
|
533
|
+
SourceData: matches,
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
catch (error) {
|
|
537
|
+
return {
|
|
538
|
+
Success: false,
|
|
539
|
+
Messages: [],
|
|
540
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
async ForwardMessage(params, credentials) {
|
|
545
|
+
try {
|
|
546
|
+
const mailbox = this.resolveMailboxIdentifier(null, params.ContextData);
|
|
547
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
548
|
+
const original = state.Messages.find((m) => UUIDsEqual(m.ID, params.MessageID));
|
|
549
|
+
if (!original) {
|
|
550
|
+
return {
|
|
551
|
+
Success: false,
|
|
552
|
+
ErrorMessage: `Message '${params.MessageID}' not found in mailbox '${mailbox}'.`,
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
const nowIso = new Date().toISOString();
|
|
556
|
+
const forwardedId = `mock-fwd-${crypto.randomUUID()}`;
|
|
557
|
+
const forwarded = {
|
|
558
|
+
ID: forwardedId,
|
|
559
|
+
ThreadID: original.ThreadID,
|
|
560
|
+
Folder: 'Sent',
|
|
561
|
+
Direction: 'Outbound',
|
|
562
|
+
From: mailbox,
|
|
563
|
+
To: params.ToRecipients[0] || '',
|
|
564
|
+
ToRecipients: [...params.ToRecipients],
|
|
565
|
+
CCRecipients: params.CCRecipients ? [...params.CCRecipients] : [],
|
|
566
|
+
BCCRecipients: params.BCCRecipients ? [...params.BCCRecipients] : [],
|
|
567
|
+
Subject: original.Subject.startsWith('Fwd:') ? original.Subject : `Fwd: ${original.Subject}`,
|
|
568
|
+
Body: `${params.Message ? `${params.Message}\n\n` : ''}--- Forwarded message ---\n${original.Body}`,
|
|
569
|
+
IsRead: true,
|
|
570
|
+
CreatedAt: nowIso,
|
|
571
|
+
SentAt: nowIso,
|
|
572
|
+
};
|
|
573
|
+
state.Messages.unshift(forwarded);
|
|
574
|
+
this.SaveMailbox(state, credentials);
|
|
575
|
+
return {
|
|
576
|
+
Success: true,
|
|
577
|
+
Result: { MessageID: forwardedId },
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
catch (error) {
|
|
581
|
+
return {
|
|
582
|
+
Success: false,
|
|
583
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
async ReplyToMessage(params, credentials) {
|
|
588
|
+
try {
|
|
589
|
+
const mailbox = this.resolveMailboxIdentifier(null, params.ContextData, params.Message?.From);
|
|
590
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
591
|
+
const original = state.Messages.find((m) => UUIDsEqual(m.ID, params.MessageID));
|
|
592
|
+
if (!original) {
|
|
593
|
+
return {
|
|
594
|
+
Success: false,
|
|
595
|
+
ErrorMessage: `Message '${params.MessageID}' not found in mailbox '${mailbox}'.`,
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
const nowIso = new Date().toISOString();
|
|
599
|
+
const replyId = `mock-reply-${crypto.randomUUID()}`;
|
|
600
|
+
const recipient = params.Message.To || original.From;
|
|
601
|
+
const subject = params.Message.ProcessedSubject ||
|
|
602
|
+
params.Message.Subject ||
|
|
603
|
+
(original.Subject.startsWith('Re:') ? original.Subject : `Re: ${original.Subject}`);
|
|
604
|
+
const reply = {
|
|
605
|
+
ID: replyId,
|
|
606
|
+
ThreadID: original.ThreadID,
|
|
607
|
+
Folder: 'Sent',
|
|
608
|
+
Direction: 'Outbound',
|
|
609
|
+
From: params.Message.From || mailbox,
|
|
610
|
+
To: recipient,
|
|
611
|
+
ToRecipients: [recipient],
|
|
612
|
+
CCRecipients: params.Message.CCRecipients ? [...params.Message.CCRecipients] : [],
|
|
613
|
+
BCCRecipients: params.Message.BCCRecipients ? [...params.Message.BCCRecipients] : [],
|
|
614
|
+
Subject: subject,
|
|
615
|
+
Body: params.Message.ProcessedBody || params.Message.Body || '',
|
|
616
|
+
HTMLBody: params.Message.ProcessedHTMLBody || params.Message.HTMLBody || undefined,
|
|
617
|
+
IsRead: true,
|
|
618
|
+
CreatedAt: nowIso,
|
|
619
|
+
SentAt: nowIso,
|
|
620
|
+
};
|
|
621
|
+
state.Messages.unshift(reply);
|
|
622
|
+
this.SaveMailbox(state, credentials);
|
|
623
|
+
return {
|
|
624
|
+
Success: true,
|
|
625
|
+
Result: { MessageID: replyId, ThreadID: original.ThreadID },
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
catch (error) {
|
|
629
|
+
return {
|
|
630
|
+
Success: false,
|
|
631
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
async CreateDraft(params, credentials) {
|
|
636
|
+
try {
|
|
637
|
+
const mailbox = this.resolveMailboxIdentifier(null, params.ContextData, params.Message?.From);
|
|
638
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
639
|
+
const nowIso = new Date().toISOString();
|
|
640
|
+
const draftId = `mock-draft-${crypto.randomUUID()}`;
|
|
641
|
+
const draft = {
|
|
642
|
+
ID: draftId,
|
|
643
|
+
ThreadID: `thread-${crypto.randomUUID().slice(0, 8)}`,
|
|
644
|
+
Folder: 'Drafts',
|
|
645
|
+
Direction: 'Outbound',
|
|
646
|
+
From: params.Message.From || mailbox,
|
|
647
|
+
To: params.Message.To || '',
|
|
648
|
+
ToRecipients: params.Message.To ? [params.Message.To] : [],
|
|
649
|
+
CCRecipients: params.Message.CCRecipients ? [...params.Message.CCRecipients] : [],
|
|
650
|
+
BCCRecipients: params.Message.BCCRecipients ? [...params.Message.BCCRecipients] : [],
|
|
651
|
+
Subject: params.Message.ProcessedSubject || params.Message.Subject || '',
|
|
652
|
+
Body: params.Message.ProcessedBody || params.Message.Body || '',
|
|
653
|
+
HTMLBody: params.Message.ProcessedHTMLBody || params.Message.HTMLBody || undefined,
|
|
654
|
+
IsRead: true,
|
|
655
|
+
CreatedAt: nowIso,
|
|
656
|
+
};
|
|
657
|
+
state.Messages.unshift(draft);
|
|
658
|
+
this.SaveMailbox(state, credentials);
|
|
659
|
+
return {
|
|
660
|
+
Success: true,
|
|
661
|
+
DraftID: draftId,
|
|
662
|
+
Result: draft,
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
catch (error) {
|
|
666
|
+
return {
|
|
667
|
+
Success: false,
|
|
668
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
async DeleteMessage(params, credentials) {
|
|
673
|
+
try {
|
|
674
|
+
const mailbox = this.resolveMailboxIdentifier(null, params.ContextData);
|
|
675
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
676
|
+
const idx = state.Messages.findIndex((m) => UUIDsEqual(m.ID, params.MessageID));
|
|
677
|
+
if (idx < 0) {
|
|
678
|
+
return { Success: false, ErrorMessage: `Message '${params.MessageID}' not found.` };
|
|
679
|
+
}
|
|
680
|
+
state.Messages.splice(idx, 1);
|
|
681
|
+
this.SaveMailbox(state, credentials);
|
|
682
|
+
return { Success: true };
|
|
683
|
+
}
|
|
684
|
+
catch (error) {
|
|
685
|
+
return {
|
|
686
|
+
Success: false,
|
|
687
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
async MarkAsRead(params, credentials) {
|
|
692
|
+
try {
|
|
693
|
+
const mailbox = this.resolveMailboxIdentifier(null, params.ContextData);
|
|
694
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
695
|
+
const ids = new UUIDSet(params.MessageIDs);
|
|
696
|
+
for (const msg of state.Messages) {
|
|
697
|
+
if (ids.Has(msg.ID)) {
|
|
698
|
+
msg.IsRead = params.IsRead;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
this.SaveMailbox(state, credentials);
|
|
702
|
+
return { Success: true };
|
|
703
|
+
}
|
|
704
|
+
catch (error) {
|
|
705
|
+
return {
|
|
706
|
+
Success: false,
|
|
707
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
async ArchiveMessage(params, credentials) {
|
|
712
|
+
try {
|
|
713
|
+
const mailbox = this.resolveMailboxIdentifier(null, params.ContextData);
|
|
714
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
715
|
+
const msg = state.Messages.find((m) => UUIDsEqual(m.ID, params.MessageID));
|
|
716
|
+
if (!msg) {
|
|
717
|
+
return { Success: false, ErrorMessage: `Message '${params.MessageID}' not found.` };
|
|
718
|
+
}
|
|
719
|
+
msg.Folder = 'Archive';
|
|
720
|
+
this.SaveMailbox(state, credentials);
|
|
721
|
+
return { Success: true };
|
|
722
|
+
}
|
|
723
|
+
catch (error) {
|
|
724
|
+
return {
|
|
725
|
+
Success: false,
|
|
726
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
// =========================================================================
|
|
731
|
+
// CALENDAR OPERATIONS
|
|
732
|
+
// =========================================================================
|
|
733
|
+
async GetEvents(params, credentials) {
|
|
734
|
+
try {
|
|
735
|
+
const mailbox = this.resolveMailboxIdentifier(params.Identifier, params.ContextData);
|
|
736
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
737
|
+
let events = state.Events.filter((evt) => {
|
|
738
|
+
if (!params.IncludeCancelled && evt.IsCancelled) {
|
|
739
|
+
return false;
|
|
740
|
+
}
|
|
741
|
+
const startMs = Date.parse(evt.StartTime);
|
|
742
|
+
const endMs = Date.parse(evt.EndTime);
|
|
743
|
+
if (params.StartDateTime && endMs <= params.StartDateTime.getTime()) {
|
|
744
|
+
return false;
|
|
745
|
+
}
|
|
746
|
+
if (params.EndDateTime && startMs >= params.EndDateTime.getTime()) {
|
|
747
|
+
return false;
|
|
748
|
+
}
|
|
749
|
+
return true;
|
|
750
|
+
});
|
|
751
|
+
events.sort((a, b) => Date.parse(a.StartTime) - Date.parse(b.StartTime));
|
|
752
|
+
const cap = params.NumEvents > 0 ? params.NumEvents : 25;
|
|
753
|
+
events = events.slice(0, cap);
|
|
754
|
+
return {
|
|
755
|
+
Success: true,
|
|
756
|
+
Events: events.map((e) => this.toGetEventsEvent(e)),
|
|
757
|
+
SourceData: events,
|
|
758
|
+
RecurrenceExpanded: false,
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
catch (error) {
|
|
762
|
+
return {
|
|
763
|
+
Success: false,
|
|
764
|
+
Events: [],
|
|
765
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
/** Convenience alias for {@link GetEvents}. */
|
|
770
|
+
async ListCalendarEvents(params, credentials) {
|
|
771
|
+
return this.GetEvents(params, credentials);
|
|
772
|
+
}
|
|
773
|
+
async CreateCalendarEvent(params, credentials) {
|
|
774
|
+
try {
|
|
775
|
+
const mailbox = this.resolveMailboxIdentifier(params.Identifier, params.ContextData);
|
|
776
|
+
const nowIso = new Date().toISOString();
|
|
777
|
+
const eventId = params.DryRun ? 'DRY_RUN_EVENT_ID' : `mock-evt-${crypto.randomUUID()}`;
|
|
778
|
+
const onlineUrl = params.IsOnlineMeeting
|
|
779
|
+
? `https://meet.local.mock/${eventId.slice(-8)}`
|
|
780
|
+
: null;
|
|
781
|
+
const stored = {
|
|
782
|
+
ID: eventId,
|
|
783
|
+
SeriesID: null,
|
|
784
|
+
ICalUID: `ical-${eventId}@local.mock`,
|
|
785
|
+
Subject: params.Subject,
|
|
786
|
+
Body: params.Body ?? '',
|
|
787
|
+
StartTime: params.StartTime.toISOString(),
|
|
788
|
+
EndTime: params.EndTime.toISOString(),
|
|
789
|
+
TimeZone: params.TimeZone ?? 'UTC',
|
|
790
|
+
IsAllDay: false,
|
|
791
|
+
Location: params.Location ?? null,
|
|
792
|
+
Organizer: mailbox,
|
|
793
|
+
Attendees: (params.Attendees ?? []).map((a) => ({
|
|
794
|
+
Email: a.Email,
|
|
795
|
+
Name: a.Name,
|
|
796
|
+
Required: a.Required ?? true,
|
|
797
|
+
Response: 'None',
|
|
798
|
+
})),
|
|
799
|
+
IsCancelled: false,
|
|
800
|
+
OnlineMeetingUrl: onlineUrl,
|
|
801
|
+
ShowAs: 'Busy',
|
|
802
|
+
LastModified: nowIso,
|
|
803
|
+
};
|
|
804
|
+
if (params.DryRun) {
|
|
805
|
+
return {
|
|
806
|
+
Success: true,
|
|
807
|
+
DryRun: true,
|
|
808
|
+
Event: this.toGetEventsEvent(stored),
|
|
809
|
+
OnlineMeetingUrl: onlineUrl,
|
|
810
|
+
SourceData: { Preview: stored },
|
|
811
|
+
};
|
|
812
|
+
}
|
|
813
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
814
|
+
state.Events.push(stored);
|
|
815
|
+
this.SaveMailbox(state, credentials);
|
|
816
|
+
return {
|
|
817
|
+
Success: true,
|
|
818
|
+
DryRun: false,
|
|
819
|
+
Event: this.toGetEventsEvent(stored),
|
|
820
|
+
OnlineMeetingUrl: onlineUrl,
|
|
821
|
+
SourceData: { Stored: stored },
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
catch (error) {
|
|
825
|
+
return {
|
|
826
|
+
Success: false,
|
|
827
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
828
|
+
};
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
async UpdateCalendarEvent(params, credentials) {
|
|
832
|
+
try {
|
|
833
|
+
const mailbox = this.resolveMailboxIdentifier(params.Identifier, params.ContextData);
|
|
834
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
835
|
+
const existing = state.Events.find((e) => UUIDsEqual(e.ID, params.EventID));
|
|
836
|
+
if (!existing) {
|
|
837
|
+
return {
|
|
838
|
+
Success: false,
|
|
839
|
+
ErrorMessage: `Calendar event '${params.EventID}' not found in mailbox '${mailbox}'.`,
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
const updated = {
|
|
843
|
+
...existing,
|
|
844
|
+
Subject: params.Subject ?? existing.Subject,
|
|
845
|
+
Body: params.Body ?? existing.Body,
|
|
846
|
+
StartTime: params.StartTime ? params.StartTime.toISOString() : existing.StartTime,
|
|
847
|
+
EndTime: params.EndTime ? params.EndTime.toISOString() : existing.EndTime,
|
|
848
|
+
TimeZone: params.TimeZone ?? existing.TimeZone,
|
|
849
|
+
Location: params.Location !== undefined ? params.Location : existing.Location,
|
|
850
|
+
Attendees: params.Attendees
|
|
851
|
+
? params.Attendees.map((a) => ({
|
|
852
|
+
Email: a.Email,
|
|
853
|
+
Name: a.Name,
|
|
854
|
+
Required: a.Required ?? true,
|
|
855
|
+
Response: 'None',
|
|
856
|
+
}))
|
|
857
|
+
: existing.Attendees,
|
|
858
|
+
LastModified: new Date().toISOString(),
|
|
859
|
+
};
|
|
860
|
+
if (params.DryRun) {
|
|
861
|
+
return {
|
|
862
|
+
Success: true,
|
|
863
|
+
DryRun: true,
|
|
864
|
+
Event: this.toGetEventsEvent(updated),
|
|
865
|
+
SourceData: { Preview: updated },
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
const idx = state.Events.findIndex((e) => UUIDsEqual(e.ID, params.EventID));
|
|
869
|
+
state.Events[idx] = updated;
|
|
870
|
+
this.SaveMailbox(state, credentials);
|
|
871
|
+
return {
|
|
872
|
+
Success: true,
|
|
873
|
+
DryRun: false,
|
|
874
|
+
Event: this.toGetEventsEvent(updated),
|
|
875
|
+
SourceData: { Stored: updated },
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
catch (error) {
|
|
879
|
+
return {
|
|
880
|
+
Success: false,
|
|
881
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
async CancelCalendarEvent(params, credentials) {
|
|
886
|
+
try {
|
|
887
|
+
const mailbox = this.resolveMailboxIdentifier(params.Identifier, params.ContextData);
|
|
888
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
889
|
+
const existing = state.Events.find((e) => UUIDsEqual(e.ID, params.EventID));
|
|
890
|
+
if (!existing) {
|
|
891
|
+
return {
|
|
892
|
+
Success: false,
|
|
893
|
+
ErrorMessage: `Calendar event '${params.EventID}' not found in mailbox '${mailbox}'.`,
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
if (params.DryRun) {
|
|
897
|
+
return { Success: true, DryRun: true };
|
|
898
|
+
}
|
|
899
|
+
existing.IsCancelled = true;
|
|
900
|
+
existing.LastModified = new Date().toISOString();
|
|
901
|
+
this.SaveMailbox(state, credentials);
|
|
902
|
+
return { Success: true, DryRun: false };
|
|
903
|
+
}
|
|
904
|
+
catch (error) {
|
|
905
|
+
return {
|
|
906
|
+
Success: false,
|
|
907
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
/** Convenience alias for {@link CancelCalendarEvent}. */
|
|
912
|
+
async DeleteCalendarEvent(params, credentials) {
|
|
913
|
+
return this.CancelCalendarEvent(params, credentials);
|
|
914
|
+
}
|
|
915
|
+
async RespondToCalendarEvent(params, credentials) {
|
|
916
|
+
try {
|
|
917
|
+
const mailbox = this.resolveMailboxIdentifier(params.Identifier, params.ContextData);
|
|
918
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
919
|
+
const existing = state.Events.find((e) => UUIDsEqual(e.ID, params.EventID));
|
|
920
|
+
if (!existing) {
|
|
921
|
+
return {
|
|
922
|
+
Success: false,
|
|
923
|
+
ErrorMessage: `Calendar event '${params.EventID}' not found in mailbox '${mailbox}'.`,
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
if (params.DryRun) {
|
|
927
|
+
return { Success: true, DryRun: true };
|
|
928
|
+
}
|
|
929
|
+
const mappedResponse = params.Response === 'Accept'
|
|
930
|
+
? 'Accepted'
|
|
931
|
+
: params.Response === 'TentativelyAccept'
|
|
932
|
+
? 'Tentative'
|
|
933
|
+
: 'Declined';
|
|
934
|
+
const attendee = existing.Attendees.find((a) => a.Email.trim().toLowerCase() === mailbox);
|
|
935
|
+
if (attendee) {
|
|
936
|
+
attendee.Response = mappedResponse;
|
|
937
|
+
}
|
|
938
|
+
else {
|
|
939
|
+
existing.Attendees.push({
|
|
940
|
+
Email: mailbox,
|
|
941
|
+
Required: true,
|
|
942
|
+
Response: mappedResponse,
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
if (mappedResponse === 'Declined') {
|
|
946
|
+
existing.ShowAs = 'Free';
|
|
947
|
+
}
|
|
948
|
+
existing.LastModified = new Date().toISOString();
|
|
949
|
+
this.SaveMailbox(state, credentials);
|
|
950
|
+
return { Success: true, DryRun: false };
|
|
951
|
+
}
|
|
952
|
+
catch (error) {
|
|
953
|
+
return {
|
|
954
|
+
Success: false,
|
|
955
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
async GetFreeBusy(params, credentials) {
|
|
960
|
+
try {
|
|
961
|
+
const startMs = params.StartDateTime.getTime();
|
|
962
|
+
const endMs = params.EndDateTime.getTime();
|
|
963
|
+
const intervalMinutes = Math.max(5, params.AvailabilityViewIntervalMinutes ?? 30);
|
|
964
|
+
const intervalMs = intervalMinutes * 60_000;
|
|
965
|
+
const schedules = params.Schedules.map((scheduleId) => {
|
|
966
|
+
const state = this.LoadOrSeedMailbox(scheduleId, credentials);
|
|
967
|
+
const overlapping = state.Events
|
|
968
|
+
.filter((evt) => {
|
|
969
|
+
if (evt.IsCancelled)
|
|
970
|
+
return false;
|
|
971
|
+
const s = Date.parse(evt.StartTime);
|
|
972
|
+
const e = Date.parse(evt.EndTime);
|
|
973
|
+
return e > startMs && s < endMs;
|
|
974
|
+
})
|
|
975
|
+
.sort((a, b) => Date.parse(a.StartTime) - Date.parse(b.StartTime));
|
|
976
|
+
const slots = overlapping.map((evt) => ({
|
|
977
|
+
StartTime: new Date(evt.StartTime),
|
|
978
|
+
EndTime: new Date(evt.EndTime),
|
|
979
|
+
Status: evt.ShowAs,
|
|
980
|
+
Subject: evt.Subject,
|
|
981
|
+
}));
|
|
982
|
+
// Build compact Graph-style availability view string ('0'=Free,'1'=Tentative,'2'=Busy,'3'=OOF)
|
|
983
|
+
const chars = [];
|
|
984
|
+
for (let cursor = startMs; cursor < endMs && chars.length < 480; cursor += intervalMs) {
|
|
985
|
+
const bucketEnd = Math.min(cursor + intervalMs, endMs);
|
|
986
|
+
let code = '0';
|
|
987
|
+
for (const evt of overlapping) {
|
|
988
|
+
const s = Date.parse(evt.StartTime);
|
|
989
|
+
const e = Date.parse(evt.EndTime);
|
|
990
|
+
if (e > cursor && s < bucketEnd) {
|
|
991
|
+
if (evt.ShowAs === 'OutOfOffice') {
|
|
992
|
+
code = '3';
|
|
993
|
+
break;
|
|
994
|
+
}
|
|
995
|
+
if (evt.ShowAs === 'Busy' || evt.ShowAs === 'WorkingElsewhere') {
|
|
996
|
+
code = '2';
|
|
997
|
+
}
|
|
998
|
+
else if (evt.ShowAs === 'Tentative' && code === '0') {
|
|
999
|
+
code = '1';
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
chars.push(code);
|
|
1004
|
+
}
|
|
1005
|
+
return {
|
|
1006
|
+
ScheduleID: scheduleId,
|
|
1007
|
+
AvailabilityView: chars.join(''),
|
|
1008
|
+
Slots: slots,
|
|
1009
|
+
};
|
|
1010
|
+
});
|
|
1011
|
+
return {
|
|
1012
|
+
Success: true,
|
|
1013
|
+
Schedules: schedules,
|
|
1014
|
+
};
|
|
1015
|
+
}
|
|
1016
|
+
catch (error) {
|
|
1017
|
+
return {
|
|
1018
|
+
Success: false,
|
|
1019
|
+
Schedules: [],
|
|
1020
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
1021
|
+
};
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
async FindMeetingTimes(params, credentials) {
|
|
1025
|
+
try {
|
|
1026
|
+
const organizer = this.resolveMailboxIdentifier(params.Identifier, params.ContextData);
|
|
1027
|
+
const attendeeEmails = params.Attendees.map((a) => a.Email.trim().toLowerCase());
|
|
1028
|
+
const allSchedules = Array.from(new Set([organizer, ...attendeeEmails]));
|
|
1029
|
+
const busyBySchedule = new Map();
|
|
1030
|
+
for (const email of allSchedules) {
|
|
1031
|
+
const state = this.LoadOrSeedMailbox(email, credentials);
|
|
1032
|
+
busyBySchedule.set(email, state.Events
|
|
1033
|
+
.filter((e) => !e.IsCancelled && e.ShowAs !== 'Free')
|
|
1034
|
+
.map((e) => ({ start: Date.parse(e.StartTime), end: Date.parse(e.EndTime) })));
|
|
1035
|
+
}
|
|
1036
|
+
const durationMs = Math.max(15, params.DurationMinutes) * 60_000;
|
|
1037
|
+
const stepMs = 30 * 60_000;
|
|
1038
|
+
const maxCandidates = Math.max(1, params.MaxCandidates ?? 5);
|
|
1039
|
+
const startMs = params.StartDateTime.getTime();
|
|
1040
|
+
const endMs = params.EndDateTime.getTime();
|
|
1041
|
+
const spanHours = (endMs - startMs) / 3_600_000;
|
|
1042
|
+
const suggestions = [];
|
|
1043
|
+
// Snap cursor up to nearest 30-minute boundary
|
|
1044
|
+
let cursor = Math.ceil(startMs / stepMs) * stepMs;
|
|
1045
|
+
while (cursor + durationMs <= endMs && suggestions.length < maxCandidates) {
|
|
1046
|
+
const candidateEnd = cursor + durationMs;
|
|
1047
|
+
const candidateDate = new Date(cursor);
|
|
1048
|
+
const hour = candidateDate.getHours();
|
|
1049
|
+
// Prefer business hours (08:00-18:00 local) when searching multi-day windows
|
|
1050
|
+
const withinBusinessHours = spanHours <= 12 || (hour >= 8 && hour < 18);
|
|
1051
|
+
if (withinBusinessHours) {
|
|
1052
|
+
const isFree = (email) => {
|
|
1053
|
+
const blocks = busyBySchedule.get(email) ?? [];
|
|
1054
|
+
return !blocks.some((b) => b.end > cursor && b.start < candidateEnd);
|
|
1055
|
+
};
|
|
1056
|
+
const organizerFree = isFree(organizer);
|
|
1057
|
+
const attendeeStatuses = attendeeEmails.map((email) => ({
|
|
1058
|
+
Email: email,
|
|
1059
|
+
Availability: isFree(email) ? 'free' : 'busy',
|
|
1060
|
+
}));
|
|
1061
|
+
const allFree = organizerFree && attendeeStatuses.every((a) => a.Availability === 'free');
|
|
1062
|
+
if (allFree) {
|
|
1063
|
+
suggestions.push({
|
|
1064
|
+
StartTime: new Date(cursor),
|
|
1065
|
+
EndTime: new Date(candidateEnd),
|
|
1066
|
+
Confidence: 100,
|
|
1067
|
+
OrganizerAvailability: 'free',
|
|
1068
|
+
AttendeeAvailability: attendeeStatuses,
|
|
1069
|
+
SuggestionReason: 'All attendees are available in local JSON calendar.',
|
|
1070
|
+
});
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
cursor += stepMs;
|
|
1074
|
+
}
|
|
1075
|
+
return {
|
|
1076
|
+
Success: true,
|
|
1077
|
+
Suggestions: suggestions,
|
|
1078
|
+
EmptySuggestionsReason: suggestions.length === 0 ? 'No mutually free meeting slots found in window.' : undefined,
|
|
1079
|
+
};
|
|
1080
|
+
}
|
|
1081
|
+
catch (error) {
|
|
1082
|
+
return {
|
|
1083
|
+
Success: false,
|
|
1084
|
+
Suggestions: [],
|
|
1085
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
1086
|
+
};
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
async VerifyMailboxAccess(params, credentials) {
|
|
1090
|
+
const dir = this.ResolveDataDir(credentials);
|
|
1091
|
+
if (!dir) {
|
|
1092
|
+
return {
|
|
1093
|
+
Success: false,
|
|
1094
|
+
Identifier: params.Identifier,
|
|
1095
|
+
CanReadMail: false,
|
|
1096
|
+
CanReadCalendar: false,
|
|
1097
|
+
ErrorMessage: `${LOCAL_JSON_COMMUNICATION_PROVIDER_NAME} requires ${MJ_MOCK_COMM_DIR_ENV}.`,
|
|
1098
|
+
};
|
|
1099
|
+
}
|
|
1100
|
+
if (!params.Identifier || params.Identifier.trim().length === 0) {
|
|
1101
|
+
return {
|
|
1102
|
+
Success: false,
|
|
1103
|
+
Identifier: '',
|
|
1104
|
+
CanReadMail: false,
|
|
1105
|
+
CanReadCalendar: false,
|
|
1106
|
+
ErrorMessage: 'Mailbox Identifier is required.',
|
|
1107
|
+
};
|
|
1108
|
+
}
|
|
1109
|
+
try {
|
|
1110
|
+
this.LoadOrSeedMailbox(params.Identifier, credentials);
|
|
1111
|
+
return {
|
|
1112
|
+
Success: true,
|
|
1113
|
+
Identifier: params.Identifier,
|
|
1114
|
+
CanReadMail: true,
|
|
1115
|
+
CanReadCalendar: true,
|
|
1116
|
+
};
|
|
1117
|
+
}
|
|
1118
|
+
catch (error) {
|
|
1119
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
1120
|
+
return {
|
|
1121
|
+
Success: false,
|
|
1122
|
+
Identifier: params.Identifier,
|
|
1123
|
+
CanReadMail: false,
|
|
1124
|
+
CanReadCalendar: false,
|
|
1125
|
+
ErrorMessage: msg,
|
|
1126
|
+
MailErrorMessage: msg,
|
|
1127
|
+
CalendarErrorMessage: msg,
|
|
1128
|
+
};
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
// =========================================================================
|
|
1132
|
+
// STANDARD CALENDAR CRUD OPERATIONS (Calendar.ts contract)
|
|
1133
|
+
// =========================================================================
|
|
1134
|
+
async CreateEvent(params, credentials) {
|
|
1135
|
+
const targetErr = CalendarTargetError('CreateEvent', params.Target);
|
|
1136
|
+
if (targetErr) {
|
|
1137
|
+
return { Success: false, ErrorMessage: targetErr };
|
|
1138
|
+
}
|
|
1139
|
+
try {
|
|
1140
|
+
const mailbox = params.Target.Identifier.trim().toLowerCase();
|
|
1141
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
1142
|
+
const nowIso = new Date().toISOString();
|
|
1143
|
+
const eventId = `mock-evt-${crypto.randomUUID()}`;
|
|
1144
|
+
const stored = {
|
|
1145
|
+
ID: eventId,
|
|
1146
|
+
SeriesID: null,
|
|
1147
|
+
ICalUID: `ical-${eventId}@local.mock`,
|
|
1148
|
+
Subject: params.Event.Subject,
|
|
1149
|
+
Body: params.Event.Body ?? '',
|
|
1150
|
+
StartTime: params.Event.Start.toISOString(),
|
|
1151
|
+
EndTime: params.Event.End.toISOString(),
|
|
1152
|
+
TimeZone: params.Event.TimeZone ?? 'UTC',
|
|
1153
|
+
IsAllDay: params.Event.IsAllDay ?? false,
|
|
1154
|
+
Location: params.Event.Location ?? null,
|
|
1155
|
+
Organizer: mailbox,
|
|
1156
|
+
Attendees: (params.Event.Attendees ?? []).map((a) => ({
|
|
1157
|
+
Email: a.Email,
|
|
1158
|
+
Name: a.Name,
|
|
1159
|
+
Required: a.Type === 'Required',
|
|
1160
|
+
Response: 'None',
|
|
1161
|
+
})),
|
|
1162
|
+
IsCancelled: false,
|
|
1163
|
+
OnlineMeetingUrl: params.Event.IsOnlineMeeting
|
|
1164
|
+
? `https://meet.local.mock/${eventId.slice(-8)}`
|
|
1165
|
+
: null,
|
|
1166
|
+
ShowAs: 'Busy',
|
|
1167
|
+
LastModified: nowIso,
|
|
1168
|
+
};
|
|
1169
|
+
state.Events.push(stored);
|
|
1170
|
+
this.SaveMailbox(state, credentials);
|
|
1171
|
+
return { Success: true, Event: this.toCalendarEvent(stored) };
|
|
1172
|
+
}
|
|
1173
|
+
catch (error) {
|
|
1174
|
+
return {
|
|
1175
|
+
Success: false,
|
|
1176
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
1177
|
+
};
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
async UpdateEvent(params, credentials) {
|
|
1181
|
+
const targetErr = CalendarTargetError('UpdateEvent', params.Target);
|
|
1182
|
+
if (targetErr) {
|
|
1183
|
+
return { Success: false, ErrorMessage: targetErr };
|
|
1184
|
+
}
|
|
1185
|
+
const updateErr = CalendarUpdateError(params.Changes);
|
|
1186
|
+
if (updateErr) {
|
|
1187
|
+
return { Success: false, ErrorMessage: updateErr };
|
|
1188
|
+
}
|
|
1189
|
+
try {
|
|
1190
|
+
const mailbox = params.Target.Identifier.trim().toLowerCase();
|
|
1191
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
1192
|
+
const idx = state.Events.findIndex((e) => UUIDsEqual(e.ID, params.ExternalEventID));
|
|
1193
|
+
if (idx < 0) {
|
|
1194
|
+
return {
|
|
1195
|
+
Success: false,
|
|
1196
|
+
ErrorMessage: `Calendar event '${params.ExternalEventID}' not found.`,
|
|
1197
|
+
};
|
|
1198
|
+
}
|
|
1199
|
+
const existing = state.Events[idx];
|
|
1200
|
+
const updated = {
|
|
1201
|
+
...existing,
|
|
1202
|
+
Subject: params.Changes.Subject ?? existing.Subject,
|
|
1203
|
+
Body: params.Changes.Body ?? existing.Body,
|
|
1204
|
+
StartTime: params.Changes.Start ? params.Changes.Start.toISOString() : existing.StartTime,
|
|
1205
|
+
EndTime: params.Changes.End ? params.Changes.End.toISOString() : existing.EndTime,
|
|
1206
|
+
TimeZone: params.Changes.TimeZone ?? existing.TimeZone,
|
|
1207
|
+
IsAllDay: params.Changes.IsAllDay ?? existing.IsAllDay,
|
|
1208
|
+
Location: params.Changes.Location !== undefined ? params.Changes.Location : existing.Location,
|
|
1209
|
+
Attendees: params.Changes.Attendees
|
|
1210
|
+
? params.Changes.Attendees.map((a) => ({
|
|
1211
|
+
Email: a.Email,
|
|
1212
|
+
Name: a.Name,
|
|
1213
|
+
Required: a.Type === 'Required',
|
|
1214
|
+
Response: 'None',
|
|
1215
|
+
}))
|
|
1216
|
+
: existing.Attendees,
|
|
1217
|
+
LastModified: new Date().toISOString(),
|
|
1218
|
+
};
|
|
1219
|
+
state.Events[idx] = updated;
|
|
1220
|
+
this.SaveMailbox(state, credentials);
|
|
1221
|
+
return { Success: true, Event: this.toCalendarEvent(updated) };
|
|
1222
|
+
}
|
|
1223
|
+
catch (error) {
|
|
1224
|
+
return {
|
|
1225
|
+
Success: false,
|
|
1226
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
1227
|
+
};
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
async CancelEvent(params, credentials) {
|
|
1231
|
+
const targetErr = CalendarTargetError('CancelEvent', params.Target);
|
|
1232
|
+
if (targetErr) {
|
|
1233
|
+
return { Success: false, ErrorMessage: targetErr };
|
|
1234
|
+
}
|
|
1235
|
+
try {
|
|
1236
|
+
const mailbox = params.Target.Identifier.trim().toLowerCase();
|
|
1237
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
1238
|
+
const existing = state.Events.find((e) => UUIDsEqual(e.ID, params.ExternalEventID));
|
|
1239
|
+
if (existing) {
|
|
1240
|
+
existing.IsCancelled = true;
|
|
1241
|
+
existing.LastModified = new Date().toISOString();
|
|
1242
|
+
this.SaveMailbox(state, credentials);
|
|
1243
|
+
}
|
|
1244
|
+
return { Success: true };
|
|
1245
|
+
}
|
|
1246
|
+
catch (error) {
|
|
1247
|
+
return {
|
|
1248
|
+
Success: false,
|
|
1249
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
1250
|
+
};
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
async GetEvent(params, credentials) {
|
|
1254
|
+
const targetErr = CalendarTargetError('GetEvent', params.Target);
|
|
1255
|
+
if (targetErr) {
|
|
1256
|
+
return { Success: false, ErrorMessage: targetErr };
|
|
1257
|
+
}
|
|
1258
|
+
try {
|
|
1259
|
+
const mailbox = params.Target.Identifier.trim().toLowerCase();
|
|
1260
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
1261
|
+
const existing = state.Events.find((e) => UUIDsEqual(e.ID, params.ExternalEventID));
|
|
1262
|
+
if (!existing) {
|
|
1263
|
+
return {
|
|
1264
|
+
Success: false,
|
|
1265
|
+
ErrorMessage: `Calendar event '${params.ExternalEventID}' not found.`,
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
return { Success: true, Event: this.toCalendarEvent(existing) };
|
|
1269
|
+
}
|
|
1270
|
+
catch (error) {
|
|
1271
|
+
return {
|
|
1272
|
+
Success: false,
|
|
1273
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
1274
|
+
};
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
async SyncEvents(params, credentials) {
|
|
1278
|
+
const targetErr = CalendarTargetError('SyncEvents', params.Target);
|
|
1279
|
+
if (targetErr) {
|
|
1280
|
+
return { Success: false, Changes: [], ErrorMessage: targetErr };
|
|
1281
|
+
}
|
|
1282
|
+
try {
|
|
1283
|
+
const mailbox = params.Target.Identifier.trim().toLowerCase();
|
|
1284
|
+
const state = this.LoadOrSeedMailbox(mailbox, credentials);
|
|
1285
|
+
const sinceMs = params.SyncToken ? Date.parse(params.SyncToken) : NaN;
|
|
1286
|
+
const matching = state.Events.filter((e) => {
|
|
1287
|
+
if (Number.isFinite(sinceMs)) {
|
|
1288
|
+
return Date.parse(e.LastModified) > sinceMs;
|
|
1289
|
+
}
|
|
1290
|
+
const s = Date.parse(e.StartTime);
|
|
1291
|
+
const end = Date.parse(e.EndTime);
|
|
1292
|
+
if (params.WindowStart && end <= params.WindowStart.getTime())
|
|
1293
|
+
return false;
|
|
1294
|
+
if (params.WindowEnd && s >= params.WindowEnd.getTime())
|
|
1295
|
+
return false;
|
|
1296
|
+
return true;
|
|
1297
|
+
});
|
|
1298
|
+
return {
|
|
1299
|
+
Success: true,
|
|
1300
|
+
Changes: matching.map((e) => e.IsCancelled
|
|
1301
|
+
? { Kind: 'Removed', ExternalEventID: e.ID }
|
|
1302
|
+
: { Kind: 'Upserted', ExternalEventID: e.ID, Event: this.toCalendarEvent(e) }),
|
|
1303
|
+
NextSyncToken: new Date().toISOString(),
|
|
1304
|
+
};
|
|
1305
|
+
}
|
|
1306
|
+
catch (error) {
|
|
1307
|
+
return {
|
|
1308
|
+
Success: false,
|
|
1309
|
+
Changes: [],
|
|
1310
|
+
ErrorMessage: error instanceof Error ? error.message : String(error),
|
|
1311
|
+
};
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
let _localJsonProviderRegistered = false;
|
|
1316
|
+
/**
|
|
1317
|
+
* Programmatically registers {@link LocalJsonCommunicationProvider} with `MJGlobal.ClassFactory`
|
|
1318
|
+
* when `@memberjunction/communication-engine` is loaded (avoiding static manifest coupling in
|
|
1319
|
+
* `ServerBootstrap` while keeping `ClassFactory.CreateInstance(BaseCommunicationProvider, 'Local JSON Mock')` active).
|
|
1320
|
+
*/
|
|
1321
|
+
export function LoadLocalJsonCommunicationProvider() {
|
|
1322
|
+
if (_localJsonProviderRegistered) {
|
|
1323
|
+
return;
|
|
1324
|
+
}
|
|
1325
|
+
const cf = MJGlobal.Instance?.ClassFactory;
|
|
1326
|
+
if (cf && typeof cf.Register === 'function') {
|
|
1327
|
+
const existing = typeof cf.GetRegistration === 'function'
|
|
1328
|
+
? cf.GetRegistration(BaseCommunicationProvider, LOCAL_JSON_COMMUNICATION_PROVIDER_NAME)
|
|
1329
|
+
: undefined;
|
|
1330
|
+
if (!existing) {
|
|
1331
|
+
cf.Register(BaseCommunicationProvider, LocalJsonCommunicationProvider, LOCAL_JSON_COMMUNICATION_PROVIDER_NAME);
|
|
1332
|
+
}
|
|
1333
|
+
_localJsonProviderRegistered = true;
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
LoadLocalJsonCommunicationProvider();
|
|
1337
|
+
//# sourceMappingURL=LocalJsonCommunicationProvider.js.map
|