@acms-subir/core 0.3.58 → 0.3.60

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/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # @acms-subir/core
2
2
 
3
+ ## 0.3.60
4
+
5
+ ### Patch Changes
6
+
7
+ - 9e2d741: A follow-up for forms on sites in more than one language, and for staff reading time answers.
8
+
9
+ - **Times read the way your site writes them.** A time answer, stored as 24-hour `18:30`, now shows the way your site's locale writes times (Settings → Timezone & language): "6:30 PM" for en-US, "6:30 p.m." for es-MX, "18:30" for es-ES. You'll see this in Forms → Responses, the editor app's responses and the staff alert emails. For event registrations, it's in the hosts' "New RSVP" email and the registration's details in Bookings. CSV exports keep the stored `HH:mm`, just as they keep dates as `YYYY-MM-DD`, because spreadsheets sort and format that reliably. Nothing stored changes, and searching Responses still matches the stored text ("18:30").
10
+ - **Error messages in the visitor's language.** On `/es/forms/<id>`, and in Form blocks on Spanish pages, a missing or invalid answer now gets a Spanish message that names the question by the Spanish label the visitor saw ("Responde “Hora”."). The form's other refusals are in Spanish too: a form that changed, a post that's too large, too many submissions, "reload the form". Event pages word their RSVP form's errors the same way. Apps get these messages in the `locale` they send to `POST /api/v1/forms/{id}/responses` (also the 404 and 410 from `GET /api/v1/forms/{id}`), and for RSVP answers sent to `POST /api/v1/bookings` with a `locale`. Error codes and statuses don't change, so apps that check them keep working. Messages come in English, Spanish, French, German and Portuguese; other languages get English, and pages in the main language read as before.
11
+ - **Forms stored with their keys in another order take responses again.** Sometimes a form definition is written straight into the database or imported, with its JSON keys in a different order from the one the site uses. Every response to such a form failed with "This form changed. Reload and try again." Responses are now checked against the form by what it asks, not by how its JSON is written, so they go through. A form that's unpublished, archived or really changed between loading and sending still refuses the response.
12
+
13
+ There's no migration, and nothing to change after updating.
14
+
15
+ ## 0.3.59
16
+
17
+ ### Patch Changes
18
+
19
+ - fa476b6: The Google Calendar sync now maps Google's "this and following" to "from this date on" (C4), both ways.
20
+
21
+ - A split in Google changes the site event from that date on (`changeFromDate` as `GOOGLE_SYNC`). It stays one event, with the same page, hosts and RSVPs, and sign-ups move to the nearest new date with the "new time" email. The new Google series becomes that later part's pair. Its later time, rule or place edits change the part again, and its single dates are cancelled and moved like any date.
22
+ - A change from a date on, made on the site, gives the later part its own Google series. The first series gets an UNTIL before that day, as in the site's own feeds. Undo removes the later series again.
23
+ - Deleting the later series in Google, or ending a series sooner, cancels those dates on the site from the first one on (`cancelFromDate`). They're struck through, their rooms are freed, and they can be restored. Restoring them on either side brings them back on the other.
24
+ - At the first match, a split Google already has is recorded as it is (Q18). Matching now pairs a Google series that was split with its site twin.
25
+ - Before this, splits were only listed for staff, and both Google series stayed linked to the one site event. Those pairs carry on: the next change on either side applies the new way.
26
+
27
+ No migration. Nothing to do after updating.
28
+
3
29
  ## 0.3.58
4
30
 
5
31
  ### Patch Changes
package/docs/calendar.md CHANGED
@@ -440,8 +440,11 @@ calendars in step, both ways, within a few minutes. The full guide is
440
440
  - **Booked appointments** from Google booking pages come in as "Booked" with the
441
441
  internal tag `#google-booking` (`GOOGLE_BOOKING_TAG`), holding the link's room. Treat
442
442
  them as bookings: the tag never shows publicly.
443
- - **"This and following"** in Google is listed for staff, and the site series stays whole
444
- until "from this date on" exists.
443
+ - **"This and following"** maps to "from this date on", both ways:
444
+ - A Google split changes the event from that date on (`changeFromDate` as `GOOGLE_SYNC`).
445
+ - A change from a date on gives the later part its own Google series.
446
+ - Deleting the later series in Google, or ending a series sooner, cancels those dates
447
+ (`cancelFromDate`), so they stay restorable.
445
448
  - Site code can read an event's Google copies with `googleCopies(repo, eventId)`, and
446
449
  Recent changes with `googleSyncHistory(repo)`.
447
450
 
@@ -185,12 +185,28 @@ A series is one event with a rule on both sides. Single dates:
185
185
  taken room only warns. A date moved on the site moves that Google instance.
186
186
  - **Per-date titles or descriptions** from Google aren't applied, because the
187
187
  site keeps one title for every date. They're listed under Recent changes.
188
- - **"This and following"** in Google splits the series in two. Until the site
189
- has "from this date on", the new Google series is linked to the same site
190
- event as a later part. The split is listed for staff, and the site series
191
- stays whole: its time and rule don't change from either side. Likewise, a
192
- series Google ends sooner keeps its later dates on the site, listed for
193
- staff to end or cancel.
188
+ - **"This and following"** maps to "from this date on", both ways.
189
+ - **In Google:** a split makes a second Google series. The site changes the
190
+ same event from that date on (`changeFromDate`, as `GOOGLE_SYNC`), so its
191
+ page, hosts and RSVPs stay. Sign-ups move to the nearest new date (with the
192
+ "new time" email). The date is the one Google split at: its id ends in
193
+ `_R` and that date's original start.
194
+ - **Pairs:** the new Google series becomes the later part's pair. Its later
195
+ time, rule or place edits change that part again. Its single dates are
196
+ cancelled and moved like any date. Its own title or description isn't kept,
197
+ because the site keeps one for the whole series.
198
+ - **On the site:** a change from a date on gives the later part its own
199
+ Google series (its hidden link names the part), and the first series gets
200
+ an UNTIL before that day, as in the site's feeds. **Undo** removes the
201
+ later series again.
202
+ - **Deleting:** deleting the later series in Google, or ending a series
203
+ sooner, cancels those dates on the site from the first one on
204
+ (`cancelFromDate`): struck through, rooms freed, restorable (Q6).
205
+ Restoring them, in Google or on the site, brings them back on the other
206
+ side. Dates cancelled from a date on, on the site, are removed from
207
+ Google, like a cancelled date's instance.
208
+ - **First match:** a split Google already has is recorded as it is. Neither
209
+ side changes until one of them does (Q18).
194
210
 
195
211
  ## Clashes
196
212
 
@@ -284,9 +300,6 @@ insert, update and delete of `events`) and `gcal_log`.
284
300
 
285
301
  - **Google push notifications** (`events.watch`) for near-instant updates.
286
302
  Reading every 2 minutes is the mechanism.
287
- - **"This and following"** as a real change on the site, once "from this date
288
- on" exists. Until then, splits are listed and both Google series stay linked
289
- to the one site event.
290
303
 
291
304
  ## Troubleshooting
292
305
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acms-subir/core",
3
- "version": "0.3.58",
3
+ "version": "0.3.60",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -1,4 +1,4 @@
1
- import { collectResponse, displayAnswers } from '../forms/service';
1
+ import { collectResponse, displayAnswers, staffAnswers } from '../forms/service';
2
2
  // Booking workflows shared by the public booking pages, the admin, the REST
3
3
  // API and agent tools, so every path validates, notifies and fires webhooks
4
4
  // the same way. Auto-confirm links confirm on the spot (and show on the
@@ -158,7 +158,8 @@ export async function bookingMail(repo: ContentRepository, b: Booking, kind: Boo
158
158
  position: await waitlistPosition(repo, b), hours: WAITLIST_HOLD / 3_600_000,
159
159
  until: b.holdUntil === null ? '' : untilText(b.holdUntil, d.tz, settings),
160
160
  } : undefined;
161
- return bookingEmail(kind, { booking:b,resource:d.resource,link:d.link,title:d.title,location:d.location,when:d.when,
161
+ // staff read an RSVP's time answers as the site writes them ("6:30 PM"); Booking.answers keeps them as stored
162
+ return bookingEmail(kind, { booking:staff ? { ...b, answers: staffAnswers(b, siteLocale(settings)) } : b,resource:d.resource,link:d.link,title:d.title,location:d.location,when:d.when,
162
163
  manageUrl:mine ? origin + mine : staff ? adminBookingUrl(origin, b, kind) : origin + bookingManagePath({ manageToken: b.manageToken, locale: language.locale }),
163
164
  mineLink: !!mine,
164
165
  siteTitle:settings.siteTitle,host:new URL(origin).host,organizer:parseFrom(from)?.email,message:snapshot?.message,
@@ -18,20 +18,25 @@
18
18
  // - deleted: cancelled on the site (struck through, room freed, restorable,
19
19
  // Q6), or, when the event has another linked copy, just this link's room
20
20
  // and calendar taken off it
21
- // - "this and following": the new Google series is linked to the same site
22
- // event as a later part (kind segment) and listed for staff; C4 ("from this
23
- // date on") will apply those later
21
+ // - "this and following": C4's changeFromDate from the date Google split at,
22
+ // as GOOGLE_SYNC (RSVPs and dates follow; quiet in full reads); the new
23
+ // Google series is that later part's pair (kind segment). Its later edits
24
+ // change the part again, its single dates go through C1 like any date, and
25
+ // deleting it, or a series Google ends sooner, cancels those dates
26
+ // (cancelFromDate: struck through, restorable, Q6)
24
27
  import { EventLocation, RoomHold, type CalEvent } from '../../schema';
25
28
  import { eventSideEffects } from '../../content-service';
26
- import { expandEvents } from '../events';
29
+ import { eventTimeZone, expandEvents, findOccurrence, locationLine, seriesParts, type SeriesPart } from '../events';
27
30
  import { rruleMap } from '../rrule';
31
+ import { cancelFromDate, changeFromDate, restoreFromDate } from '../segments';
28
32
  import { cancelOccurrence, restoreOccurrence, uniqueSlug } from '../schedule';
29
33
  import { GOOGLE_SYNC, moveOccurrence, moveOccurrenceBack } from '../moves';
30
34
  import { clashNote, entry, type SyncContext } from './context';
31
35
  import { GoogleError } from './client';
32
36
  import {
33
37
  BOOKING_TAG, NONE, asSiteEvent, changed, dateHash, describe, fieldHash, fieldHashes, fieldWord, googleFields, hiddenLink, inWindow, instanceKey,
34
- instanceState, isBooking, keyStart, linkCarries, notAnEvent, paragraphsHtml, sameValue, siteFields, siteUpdate, splitFrom, windowStart, type DateState, type Fields,
38
+ instanceState, isBooking, keyStart, linkCarries, notAnEvent, paragraphsHtml, sameButEnd, sameValue, siteFields, siteUpdate, splitFrom, splitKey, windowStart, withEndOf,
39
+ type DateState, type Fields,
35
40
  } from './map';
36
41
  import type { GcalItem, GcalLink, GoogleEvent, Hashes, NewLogEntry } from './types';
37
42
 
@@ -139,7 +144,11 @@ async function handleEvent(ctx: SyncContext, link: GcalLink, g: GoogleEvent, opt
139
144
  const item = await ctx.repo.gcal.getItem(link.id, g.id);
140
145
  if (g.status === 'cancelled') { if (item && !item.gone) await googleDeleted(ctx, link, item, opts); return; }
141
146
  if (item) {
142
- if (item.kind === 'segment') return;
147
+ if (item.kind === 'segment') {
148
+ if (item.gone) await segmentBack(ctx, link, item, opts);
149
+ await applySegmentChange(ctx, link, { ...item, gone: false }, g, opts);
150
+ return;
151
+ }
143
152
  if (item.gone) await backInGoogle(ctx, link, item, opts);
144
153
  await applyGoogleChange(ctx, link, { ...item, gone: false }, g, opts);
145
154
  return;
@@ -148,9 +157,18 @@ async function handleEvent(ctx: SyncContext, link: GcalLink, g: GoogleEvent, opt
148
157
  const base = splitFrom(g.id);
149
158
  if (base) {
150
159
  const from = await ctx.repo.gcal.getItem(link.id, base);
151
- if (from && from.kind !== 'segment') return void await recordSplit(ctx, link, from, g);
160
+ if (from && from.kind !== 'segment') return void await applySplit(ctx, link, from, g, opts);
152
161
  }
153
162
  const hidden = g.extendedProperties?.private;
163
+ if (hidden?.acmsEventId && hidden.acmsSite === ctx.origin && hidden.acmsSegment) {
164
+ // a later part's own series, copied there by the site: paired again (a resync, a new link)
165
+ const e = await ctx.repo.getEvent(hidden.acmsEventId);
166
+ const part = e ? seriesParts(e).find((p) => p.from === hidden.acmsSegment) : undefined;
167
+ const paired = e && (await ctx.repo.gcal.itemsForEvent(e.id)).some((i) => i.linkId === link.id && i.kind === 'segment' && i.segmentFrom === hidden.acmsSegment);
168
+ if (e && part && !paired && linkCarries(e, link)) return void await pairSegment(ctx, link, e, part, g);
169
+ // the site doesn't have that part any more: left as it is rather than made a second event
170
+ if (e) return;
171
+ }
154
172
  if (hidden?.acmsEventId && hidden.acmsSite === ctx.origin) {
155
173
  const e = await ctx.repo.getEvent(hidden.acmsEventId);
156
174
  const paired = e && (await ctx.repo.gcal.itemsForEvent(e.id)).some((i) => i.linkId === link.id && i.kind !== 'segment');
@@ -184,6 +202,17 @@ export async function pairExisting(ctx: SyncContext, link: GcalLink, e: CalEvent
184
202
  await ctx.repo.gcal.log([entry(link, item, e.title, { direction: 'info', action: 'paired', summary: `Paired with “${g.summary ?? ''}” in ${link.googleName} by ${why}. Both keep their titles until one is edited.` })], ctx.now);
185
203
  }
186
204
 
205
+ /** A later part's Google series paired as it is (a resync, a new link): both sides' fields become the agreed starting point. */
206
+ async function pairSegment(ctx: SyncContext, link: GcalLink, e: CalEvent, part: SeriesPart, g: GoogleEvent) {
207
+ const gf = googleFields(g, link, ctx.settings);
208
+ const item: GcalItem = {
209
+ linkId: link.id, googleId: g.id, eventId: e.id, icalUid: g.iCalUID ?? '', kind: 'segment', segmentFrom: part.from, etag: g.etag ?? '', htmlLink: g.htmlLink ?? '',
210
+ site: fieldHashes(siteFields(e, link, ctx.map, part)), google: gf ? fieldHashes(gf) : {}, descGoogle: false, booking: false, gone: false, leftOut: false, conflict: null, syncedAt: ctx.now, createdAt: ctx.now,
211
+ };
212
+ await ctx.repo.gcal.putItem(item);
213
+ await ctx.repo.gcal.log([entry(link, item, e.title, { direction: 'info', action: 'paired', summary: `Paired its dates from ${part.from.replace('T', ' ')} on with their series in ${link.googleName}, by its hidden link.` })], ctx.now);
214
+ }
215
+
187
216
  /** A new Google event: published on the site at once (Q5), in the link's calendar, holding its rooms. */
188
217
  async function createSiteEvent(ctx: SyncContext, link: GcalLink, g: GoogleEvent, opts: InboundOptions) {
189
218
  const gf = googleFields(g, link, ctx.settings);
@@ -233,17 +262,50 @@ async function backInGoogle(ctx: SyncContext, link: GcalLink, item: GcalItem, op
233
262
  await ctx.repo.gcal.log([entry(link, item, e?.title ?? '', { direction: 'in', action: 'restored', summary: `Back in ${link.googleName}: no longer cancelled.` })], ctx.now);
234
263
  }
235
264
 
236
- /** True when Google's rule is the site's with an earlier end (an end added or moved sooner): Google dropped dates the site still has. */
237
- function endsSooner(siteRule: string, googleRule: string): boolean {
238
- if (!siteRule || !googleRule) return false;
239
- const a = rruleMap(siteRule), b = rruleMap(googleRule);
240
- if (!b.UNTIL && !b.COUNT) return false;
241
- const rest = (m: Record<string, string>) => JSON.stringify(Object.entries(m).filter(([k]) => k !== 'UNTIL' && k !== 'COUNT').sort());
242
- if (rest(a) !== rest(b)) return false;
243
- if (!a.UNTIL && !a.COUNT) return true;
244
- if (a.COUNT && b.COUNT) return Number(b.COUNT) < Number(a.COUNT);
245
- if (a.UNTIL && b.UNTIL) return b.UNTIL.slice(0, 8) < a.UNTIL.slice(0, 8);
246
- return true;
265
+ const DAY = 86_400_000;
266
+ /** The site event's first date (any part, moves aside) starting after `ms`, as its key. */
267
+ function firstDateAfter(ctx: SyncContext, e: CalEvent, ms: number): string | null {
268
+ return expandEvents([{ ...e, movedDates: {} }], ctx.settings, { displayTz: ctx.settings.timezone || 'UTC', now: ms, windowStartMs: ms + 1, windowEndMs: ms + 800 * DAY })
269
+ .find((o) => o.start > ms)?.key ?? null;
270
+ }
271
+ /** When Google's series has its last date (UTC ms), or null when it goes on. */
272
+ function googleEnd(ctx: SyncContext, gf: Fields): number | null {
273
+ const parts = rruleMap(gf.repeat.rrule);
274
+ if (!gf.repeat.rrule || (!parts.UNTIL && !parts.COUNT)) return null;
275
+ const shape = asSiteEvent(gf, ctx.settings);
276
+ const dates = expandEvents([shape], ctx.settings, { displayTz: ctx.settings.timezone || 'UTC', now: shape.startAt, windowStartMs: shape.startAt, windowEndMs: shape.startAt + 20 * 366 * DAY });
277
+ return dates.length ? dates[dates.length - 1].start : shape.startAt;
278
+ }
279
+ /** A place for C4 from Google's: null when it's the event's own. */
280
+ const placeInput = (place: string, e: CalEvent) =>
281
+ place === locationLine(e.location) ? null : { name: place.slice(0, 300), address: '', placeId: '', lat: null, lng: null, mapImage: '', mapImageDark: '' };
282
+
283
+ /**
284
+ * A change to the end of the first part's rule in Google (its other parts
285
+ * alike). Google ending it where the site's next part takes over is the same
286
+ * cut ("this and following"): nothing to do. Ending it sooner dropped dates
287
+ * the site still has: they're cancelled from the first one on
288
+ * (cancelFromDate, Q6), and Google going on again past a cancelled part
289
+ * restores it. Returns what it did, or null when it's an ordinary rule change.
290
+ */
291
+ async function seriesEnd(ctx: SyncContext, link: GcalLink, e: CalEvent, gf: Fields, opts: InboundOptions): Promise<{ words: string } | null> {
292
+ if (!e.rrule || !sameButEnd(e.rrule, gf.repeat.rrule)) return null;
293
+ const parts = seriesParts(e);
294
+ const next = parts[1];
295
+ const end = googleEnd(ctx, gf);
296
+ const firstGone = end === null ? null : firstDateAfter(ctx, { ...e, segments: [] }, end);
297
+ const cut = next ? next.from.slice(0, 10) : null;
298
+ // Google's dates run to the site's next part: the same series
299
+ if (cut && firstGone && firstGone.slice(0, 10) >= cut) return { words: '' };
300
+ if (cut && end === null && next.cancelled) {
301
+ if (link.direction === 'out') return { words: '' };
302
+ await restoreFromDate(ctx.repo, GOOGLE_SYNC, e.id, next.from, ctx.hooks, { quiet: opts.quiet, now: ctx.now });
303
+ return { words: `Google’s series goes on again: the dates from ${next.from.replace('T', ' ')} on are back.` };
304
+ }
305
+ if (!firstGone) return null;
306
+ if (link.direction === 'out') return { words: '' };
307
+ await cancelFromDate(ctx.repo, GOOGLE_SYNC, e.id, firstGone, ctx.hooks, { quiet: opts.quiet, now: ctx.now });
308
+ return { words: `Google ended this series sooner: the dates from ${firstGone.replace('T', ' ')} on are cancelled here (restorable).` };
247
309
  }
248
310
 
249
311
  /** The value of one field from Google, for "Use Google's version" later. */
@@ -272,8 +334,8 @@ export async function applyGoogleChange(ctx: SyncContext, link: GcalLink, item:
272
334
  return;
273
335
  }
274
336
  const sf = siteFields(e, link, ctx.map), sh = fieldHashes(sf);
275
- const split = (await ctx.repo.gcal.itemsForEvent(e.id)).some((i) => i.linkId === link.id && i.kind === 'segment');
276
337
  const google: Hashes = { ...item.google }, site: Hashes = { ...item.site };
338
+ let reparted = false;
277
339
  const notes: NewLogEntry[] = [];
278
340
  const apply: string[] = [];
279
341
  let conflict = item.conflict, sendBack = false;
@@ -290,13 +352,19 @@ export async function applyGoogleChange(ctx: SyncContext, link: GcalLink, item:
290
352
  sendBack = true;
291
353
  continue;
292
354
  }
293
- if (split && (n === 'when' || n === 'repeat')) {
294
- notes.push(entry(link, item, e.title, { direction: 'info', action: 'split', summary: `${fieldWord(n)} changed when Google split this series (“this and following”). The site keeps one event as it was for now: change the later dates on the site by hand if needed.` }));
295
- continue;
296
- }
297
- if (n === 'repeat' && endsSooner(sf.repeat.rrule, gf.repeat.rrule)) {
298
- notes.push(entry(link, item, e.title, { direction: 'info', action: 'ended', summary: `Google ended this series sooner (${describe(n, gf)}). The site keeps its later dates: end the series or cancel those dates on the site if that’s right.` }));
299
- continue;
355
+ if (n === 'repeat' && !item.booking) {
356
+ try {
357
+ const done = await seriesEnd(ctx, link, e, gf, opts);
358
+ if (done) {
359
+ reparted = true;
360
+ if (done.words) notes.push(entry(link, item, e.title, { direction: 'in', action: 'ended', summary: done.words }));
361
+ continue;
362
+ }
363
+ } catch (err) {
364
+ rethrowGoogle(err);
365
+ notes.push(entry(link, item, e.title, { direction: 'info', action: 'ended', summary: `Google changed when this series ends (${describe(n, gf)}), but the site couldn’t follow: ${errorText(err)}` }));
366
+ continue;
367
+ }
300
368
  }
301
369
  // a Google → site link mirrors Google; otherwise a field both sides changed keeps the site's value (Q4)
302
370
  const siteChanged = (sh[n] ?? '') !== (item.site[n] ?? '');
@@ -311,7 +379,9 @@ export async function applyGoogleChange(ctx: SyncContext, link: GcalLink, item:
311
379
  apply.push(n);
312
380
  }
313
381
  if (apply.length) {
314
- const update = siteUpdate(e, gf, apply, { rules: link.rules, descGoogle: item.descGoogle, settings: ctx.settings });
382
+ // a series changed from a date on keeps its parts: the first part's own rule changes, its end stays where the next takes over
383
+ const parted = seriesParts(e).length > 1 ? { ...gf, repeat: { ...gf.repeat, rrule: withEndOf(gf.repeat.rrule, e.rrule) } } : gf;
384
+ const update = siteUpdate(e, parted, apply, { rules: link.rules, descGoogle: item.descGoogle, settings: ctx.settings });
315
385
  if (update) {
316
386
  const after = await writeSite(ctx, e, update, opts.quiet);
317
387
  if (update.tags) await ctx.repo.ensureTags(update.tags);
@@ -329,6 +399,13 @@ export async function applyGoogleChange(ctx: SyncContext, link: GcalLink, item:
329
399
  if (conflict && apply.includes(conflict.field)) conflict = null;
330
400
  } else for (const n of apply) setHash(site, n, sh[n] ?? '');
331
401
  }
402
+ if (reparted) {
403
+ // the parts moved (a cut, cancelled or restored dates): the first part's rule and dates as the site has them now
404
+ const now = fieldHashes(siteFields((await ctx.repo.getEvent(e.id)) ?? e, link, ctx.map));
405
+ setHash(site, 'repeat', now.repeat);
406
+ for (const k of Object.keys(site)) if (k.startsWith('date:')) delete site[k];
407
+ for (const [k, v] of Object.entries(now)) if (k.startsWith('date:')) site[k] = v;
408
+ }
332
409
  await ctx.repo.gcal.patchItem(link.id, item.googleId, { site, google, conflict, etag: g.etag ?? item.etag, htmlLink: g.htmlLink ?? item.htmlLink, kind: gf.repeat.rrule ? 'series' : 'single', syncedAt: ctx.now });
333
410
  await ctx.repo.gcal.log(notes, ctx.now);
334
411
  if (sendBack) await ctx.repo.gcal.enqueue([e.id], ctx.now);
@@ -337,12 +414,16 @@ export async function applyGoogleChange(ctx: SyncContext, link: GcalLink, item:
337
414
  /** Single dates of a series that changed in Google: cancelled, moved, or back to normal. */
338
415
  async function handleDates(ctx: SyncContext, link: GcalLink, masterId: string, list: GoogleEvent[], master: GoogleEvent | undefined, opts: InboundOptions) {
339
416
  const item = await ctx.repo.gcal.getItem(link.id, masterId);
340
- // dates of a later part (Google moved them to the split series), of a booking, or of a series no longer paired
341
- if (!item || item.kind === 'segment' || item.booking || item.gone) return;
417
+ // dates of a booking, or of a series no longer paired
418
+ if (!item || item.booking || item.gone) return;
342
419
  let e = await ctx.repo.getEvent(item.eventId);
343
420
  if (!e) return;
421
+ // a later part's dates (its own Google series): only while the site has that part
422
+ const partOfItem = (ev: CalEvent) => (item.kind === 'segment' ? seriesParts(ev).find((p) => p.from === item.segmentFrom) : undefined);
423
+ let part = partOfItem(e);
424
+ if (item.kind === 'segment' && (!part || part.cancelled)) return;
344
425
  const m = master ?? await ctx.client.getEvent(link.googleId, masterId).catch((err) => { if (err instanceof GoogleError && (err.code === 'notfound' || err.code === 'gone')) return null; throw err; });
345
- let sf = siteFields(e, link, ctx.map);
426
+ let sf = siteFields(e, link, ctx.map, part);
346
427
  // Google's dates read in the site event's zone, where its date keys are
347
428
  const gf = m && m.status !== 'cancelled' ? googleFields(m, link, ctx.settings, sf.when.tz) : null;
348
429
  if (!gf || !gf.repeat.rrule) return;
@@ -375,7 +456,8 @@ async function handleDates(ctx: SyncContext, link: GcalLink, masterId: string, l
375
456
  try {
376
457
  const words = await applyDate(ctx, e, key, state, mine, sf, opts);
377
458
  e = (await ctx.repo.getEvent(e.id))!;
378
- sf = siteFields(e, link, ctx.map);
459
+ part = partOfItem(e);
460
+ sf = siteFields(e, link, ctx.map, part);
379
461
  setHash(site, n, dateHash(sf.dates[key] ?? 'none'));
380
462
  notes.push(entry(link, item, e.title, { direction: 'in', action: 'date', summary: `${key.replace('T', ' ')}: ${words}` }));
381
463
  } catch (err) {
@@ -406,8 +488,17 @@ async function applyDate(ctx: SyncContext, e: CalEvent, key: string, state: Date
406
488
  async function googleDeleted(ctx: SyncContext, link: GcalLink, item: GcalItem, opts: InboundOptions) {
407
489
  const e = await ctx.repo.getEvent(item.eventId);
408
490
  if (item.kind === 'segment') {
409
- await ctx.repo.gcal.deleteItem(link.id, item.googleId);
410
- await ctx.repo.gcal.log([entry(link, item, e?.title ?? '', { direction: 'info', action: 'split', summary: `Google deleted the later part of this series${item.segmentFrom ? ` (from ${item.segmentFrom.replace('T', ' ')})` : ''}. The site keeps those dates: cancel them on the site if that’s right.` })], ctx.now);
491
+ const part = e ? seriesParts(e).find((p) => p.from === item.segmentFrom) : undefined;
492
+ // a split Google had at the first match, never on the site: nothing to cancel
493
+ if (!e || !part) { await ctx.repo.gcal.deleteItem(link.id, item.googleId); return; }
494
+ let words = `Google deleted the dates from ${item.segmentFrom.replace('T', ' ')} on: they’re cancelled here (restorable).`;
495
+ if (!part.cancelled && link.direction !== 'out') {
496
+ try { await cancelFromDate(ctx.repo, GOOGLE_SYNC, e.id, item.segmentFrom, ctx.hooks, { quiet: opts.quiet, now: ctx.now }); }
497
+ catch (err) { rethrowGoogle(err); words = `Google deleted the dates from ${item.segmentFrom.replace('T', ' ')} on, but the site couldn’t cancel them: ${errorText(err)}`; }
498
+ }
499
+ // kept as the pair: restoring the dates on the site (or in Google) makes the series again
500
+ await ctx.repo.gcal.patchItem(link.id, item.googleId, { gone: true, leftOut: false, syncedAt: ctx.now });
501
+ await ctx.repo.gcal.log([entry(link, item, e.title, { direction: 'in', action: 'cancelled', summary: words })], ctx.now);
411
502
  return;
412
503
  }
413
504
  if (!e) { await ctx.repo.gcal.deleteItem(link.id, item.googleId); return; }
@@ -437,23 +528,132 @@ async function googleDeleted(ctx: SyncContext, link: GcalLink, item: GcalItem, o
437
528
  await ctx.repo.gcal.log([entry(link, item, e.title, { direction: 'in', action: 'cancelled', summary: `Deleted in ${link.googleName}: cancelled on the site. Restore it there to bring it back in Google.` })], ctx.now);
438
529
  }
439
530
 
440
- /** Google split a series ("this and following"): the new series is linked to the same site event as a later part, listed for staff (C4 will apply these). */
441
- async function recordSplit(ctx: SyncContext, link: GcalLink, base: GcalItem, g: GoogleEvent) {
531
+ /**
532
+ * Google split a series ("this and following"): the site changes it from
533
+ * that date on (C4's changeFromDate, as GOOGLE_SYNC), and the new Google
534
+ * series becomes that later part's pair. The date is the one the split's id
535
+ * names, else the first date from the new part's first day. At the first
536
+ * match it's only recorded (both sides as they are, Q18).
537
+ */
538
+ async function applySplit(ctx: SyncContext, link: GcalLink, base: GcalItem, g: GoogleEvent, opts: InboundOptions) {
442
539
  const e = await ctx.repo.getEvent(base.eventId);
443
540
  const gf = googleFields(g, link, ctx.settings);
444
- // the first date of the site's series from the new part's first day on
541
+ const tz = e ? eventTimeZone(e, ctx.settings) : '';
542
+ const inZone = e ? googleFields(g, link, ctx.settings, e.allDay ? undefined : tz) : null;
445
543
  let from = '';
446
- if (e && gf) {
447
- const day = asSiteEvent({ ...gf, when: { ...gf.when, start: gf.when.start.slice(0, 10), end: gf.when.start.slice(0, 10), allDay: true } }, ctx.settings).startAt;
448
- from = expandEvents([{ ...e, movedDates: {} }], ctx.settings, { displayTz: ctx.settings.timezone || 'UTC', now: day, windowStartMs: day, windowEndMs: day + 400 * 86_400_000 })
449
- .find((o) => o.start >= day)?.key ?? '';
544
+ if (e && inZone) {
545
+ const key = splitKey(g.id, { allDay: e.allDay, tz });
546
+ from = key && findOccurrence({ ...e, movedDates: {} }, key, ctx.settings) ? key : '';
547
+ if (!from) {
548
+ const day = asSiteEvent({ ...inZone, when: { ...inZone.when, start: inZone.when.start.slice(0, 10), end: inZone.when.start.slice(0, 10), allDay: true } }, ctx.settings).startAt;
549
+ from = firstDateAfter(ctx, e, day - 1) ?? '';
550
+ }
450
551
  }
451
- await ctx.repo.gcal.putItem({
452
- linkId: link.id, googleId: g.id, eventId: base.eventId, icalUid: g.iCalUID ?? '', kind: 'segment', segmentFrom: from, etag: g.etag ?? '', htmlLink: g.htmlLink ?? '',
453
- site: {}, google: gf ? fieldHashes(gf) : {}, descGoogle: false, booking: false, gone: false, leftOut: false, conflict: null, syncedAt: ctx.now, createdAt: ctx.now,
552
+ const record = (site: GcalItem['site'], segmentFrom = from) => ctx.repo.gcal.putItem({
553
+ linkId: link.id, googleId: g.id, eventId: base.eventId, icalUid: g.iCalUID ?? '', kind: 'segment', segmentFrom, etag: g.etag ?? '', htmlLink: g.htmlLink ?? '',
554
+ site, google: gf ? fieldHashes(gf) : {}, descGoogle: false, booking: false, gone: false, leftOut: false, conflict: null, syncedAt: ctx.now, createdAt: ctx.now,
454
555
  });
455
- await ctx.repo.gcal.log([entry(link, base, e?.title ?? g.summary ?? '', { direction: 'info', action: 'split',
456
- summary: `Google changed this series from ${from ? from.replace('T', ' ') : 'a later date'} on (“this and following”)${gf ? `: ${describe('when', gf)}, ${describe('repeat', gf)}` : ''}. The site keeps one event as it was for now; change the later dates on the site by hand if needed.` })], ctx.now);
556
+ const date = from ? from.replace('T', ' ') : 'a later date';
557
+ const what = gf ? `: ${describe('when', gf)}, ${describe('repeat', gf)}` : '';
558
+ if (!e || !inZone || !from || opts.adopt?.has(base.googleId) || link.direction === 'out') {
559
+ await record({});
560
+ await ctx.repo.gcal.log([entry(link, base, e?.title ?? g.summary ?? '', { direction: 'info', action: 'split',
561
+ summary: `Google has this series changed from ${date} on (“this and following”)${what}. Both stay as they are until one of them changes.` })], ctx.now);
562
+ return;
563
+ }
564
+ try {
565
+ const r = await changeFromDate(ctx.repo, GOOGLE_SYNC, e.id, from, { start: inZone.when.start, end: inZone.when.end, rrule: inZone.repeat.rrule, location: placeInput(inZone.place, e) }, ctx.hooks, { quiet: opts.quiet, now: ctx.now });
566
+ const after = r.event;
567
+ const part = seriesParts(after).filter((p) => p.from && p.from <= from).pop();
568
+ await record(part ? fieldHashes(siteFields(after, link, ctx.map, part)) : {}, part?.from ?? from);
569
+ // the first part's rule now stops there: agreed for the base series too (Google's own cut is the same change)
570
+ const base2 = await ctx.repo.gcal.getItem(link.id, base.googleId);
571
+ if (base2) {
572
+ const now = fieldHashes(siteFields(after, link, ctx.map));
573
+ const site = { ...base2.site, repeat: now.repeat };
574
+ for (const k of Object.keys(site)) if (k.startsWith('date:')) delete site[k];
575
+ for (const [k, v] of Object.entries(now)) if (k.startsWith('date:')) site[k] = v;
576
+ await ctx.repo.gcal.patchItem(link.id, base.googleId, { site });
577
+ }
578
+ const rsvps = r.rsvps.moved || r.rsvps.cancelled ? ` Sign-ups: ${r.rsvps.moved} moved, ${r.rsvps.cancelled} cancelled.` : '';
579
+ const dropped = r.dropped.length ? ` Dates with no match: ${r.dropped.map((k) => k.replace('T', ' ')).join(', ')}.` : '';
580
+ await ctx.repo.gcal.log([entry(link, base, after.title, { direction: 'in', action: 'split',
581
+ summary: `Google changed this series from ${date} on (“this and following”)${what}. ${r.changed ? 'The site changed it from that date on too.' : 'The site already had it so.'}${rsvps}${dropped}` })], ctx.now);
582
+ const clash = r.clashes.length ? await clashNote(ctx, link, base, after) : null;
583
+ if (clash) await ctx.repo.gcal.log([clash], ctx.now);
584
+ } catch (err) {
585
+ rethrowGoogle(err);
586
+ await record({});
587
+ await ctx.repo.gcal.log([entry(link, base, e.title, { direction: 'info', action: 'split',
588
+ summary: `Google changed this series from ${date} on (“this and following”)${what}, but the site couldn’t follow: ${errorText(err)}` })], ctx.now);
589
+ }
590
+ }
591
+
592
+ /**
593
+ * A later part's own Google series changed: its time, rule or place change
594
+ * that part on the site (changeFromDate at the same date, replacing it); a
595
+ * part's own title or description isn't kept (one for the whole series). The
596
+ * site wins where both changed (Q4).
597
+ */
598
+ export async function applySegmentChange(ctx: SyncContext, link: GcalLink, item: GcalItem, g: GoogleEvent, opts: InboundOptions = { quiet: false }) {
599
+ const e = await ctx.repo.getEvent(item.eventId);
600
+ const gf = googleFields(g, link, ctx.settings);
601
+ if (!e || !gf) return;
602
+ const gh = fieldHashes(gf);
603
+ const chg = changed(masterHashes(gh), masterHashes(item.google));
604
+ if (!chg.length) {
605
+ if (g.etag && g.etag !== item.etag) await ctx.repo.gcal.patchItem(link.id, item.googleId, { etag: g.etag, htmlLink: g.htmlLink ?? item.htmlLink });
606
+ return;
607
+ }
608
+ const inZone = googleFields(g, link, ctx.settings, e.allDay ? undefined : eventTimeZone(e, ctx.settings))!;
609
+ const part = seriesParts(e).find((p) => p.from === item.segmentFrom);
610
+ const sf = part ? siteFields(e, link, ctx.map, part) : null, sh = sf ? fieldHashes(sf) : {};
611
+ const google: Hashes = { ...item.google }, site: Hashes = { ...item.site };
612
+ for (const n of chg) setHash(google, n, gh[n] ?? '');
613
+ const notes: NewLogEntry[] = [];
614
+ const date = item.segmentFrom.replace('T', ' ');
615
+ const own = chg.filter((n) => n === 'title' || n === 'desc' || n.startsWith('rule:'));
616
+ if (own.length) notes.push(entry(link, item, e.title, { direction: 'info', action: 'split', summary: `The dates from ${date} on have their own ${own.includes('desc') ? 'description' : 'title'} in Google. The site keeps one for the whole series, so that edit stays in Google only.` }));
617
+ let apply = chg.filter((n) => n === 'when' || n === 'repeat' || n === 'place');
618
+ let sendBack = false;
619
+ if (link.direction === 'out') apply = [];
620
+ else if (part && sf && link.direction !== 'in' && Object.keys(item.site).length) {
621
+ const both = apply.filter((n) => (sh[n] ?? '') !== (item.site[n] ?? '') && !sameValue(n, sf, inZone, link.rules));
622
+ if (both.length) {
623
+ notes.push(entry(link, item, e.title, { direction: 'clash', action: 'split', summary: `From ${date} on, ${both.map((n) => fieldWord(n).toLowerCase()).join(' and ')} changed on both sides: kept the site’s.` }));
624
+ apply = apply.filter((n) => !both.includes(n));
625
+ sendBack = true;
626
+ }
627
+ }
628
+ if (apply.length) {
629
+ try {
630
+ const r = await changeFromDate(ctx.repo, GOOGLE_SYNC, e.id, item.segmentFrom, { start: inZone.when.start, end: inZone.when.end, rrule: inZone.repeat.rrule, location: placeInput(inZone.place, e) }, ctx.hooks, { quiet: opts.quiet, now: ctx.now });
631
+ const next = seriesParts(r.event).find((p) => p.from === item.segmentFrom);
632
+ const sh2 = next ? fieldHashes(siteFields(r.event, link, ctx.map, next)) : {};
633
+ for (const n of apply) setHash(site, n, sh2[n] ?? '');
634
+ for (const k of Object.keys(site)) if (k.startsWith('date:')) delete site[k];
635
+ for (const [k, v] of Object.entries(sh2)) if (k.startsWith('date:')) site[k] = v;
636
+ if (r.changed) notes.unshift(entry(link, item, r.event.title, { direction: 'in', action: 'updated', summary: `From ${date} on: ${describe('when', inZone)}, ${describe('repeat', inZone)}${inZone.place ? `, at ${inZone.place}` : ''}.` }));
637
+ } catch (err) {
638
+ rethrowGoogle(err);
639
+ notes.push(entry(link, item, e.title, { direction: 'info', action: 'split', summary: `The dates from ${date} on changed in Google, but the site couldn’t follow: ${errorText(err)}` }));
640
+ }
641
+ }
642
+ await ctx.repo.gcal.patchItem(link.id, item.googleId, { site, google, etag: g.etag ?? item.etag, htmlLink: g.htmlLink ?? item.htmlLink, syncedAt: ctx.now });
643
+ await ctx.repo.gcal.log(notes, ctx.now);
644
+ if (sendBack) await ctx.repo.gcal.enqueue([e.id], ctx.now);
645
+ }
646
+
647
+ /** A later part's series Google brought back (its trash): the dates are restored on the site. */
648
+ async function segmentBack(ctx: SyncContext, link: GcalLink, item: GcalItem, opts: InboundOptions) {
649
+ const e = await ctx.repo.getEvent(item.eventId);
650
+ const part = e ? seriesParts(e).find((p) => p.from === item.segmentFrom) : undefined;
651
+ if (e && part?.cancelled && link.direction !== 'out') {
652
+ try { await restoreFromDate(ctx.repo, GOOGLE_SYNC, e.id, item.segmentFrom, ctx.hooks, { quiet: opts.quiet, now: ctx.now }); }
653
+ catch (err) { rethrowGoogle(err); }
654
+ }
655
+ await ctx.repo.gcal.patchItem(link.id, item.googleId, { gone: false, leftOut: false });
656
+ await ctx.repo.gcal.log([entry(link, item, e?.title ?? '', { direction: 'in', action: 'restored', summary: `The dates from ${item.segmentFrom.replace('T', ' ')} on are back in ${link.googleName}, and on the site.` })], ctx.now);
457
657
  }
458
658
 
459
659
  /** After a full read: pairs Google didn't list (deleted long ago, or a list that skipped them) are checked one by one. */