fix: sync delivery routing context

Co-authored-by: adam91holt <adam91holt@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-01-17 06:01:30 +00:00
parent e59d8c5436
commit 285ed8bac3
10 changed files with 208 additions and 51 deletions

View File

@@ -5,6 +5,7 @@ import {
deliveryContextFromSession,
mergeDeliveryContext,
normalizeDeliveryContext,
normalizeSessionDeliveryFields,
} from "./delivery-context.js";
describe("delivery context helpers", () => {
@@ -70,4 +71,21 @@ describe("delivery context helpers", () => {
accountId: undefined,
});
});
it("normalizes delivery fields and mirrors them on session entries", () => {
const normalized = normalizeSessionDeliveryFields({
deliveryContext: { channel: " Slack ", to: " channel:1 ", accountId: " acct-2 " },
lastChannel: " whatsapp ",
lastTo: " +1555 ",
});
expect(normalized.deliveryContext).toEqual({
channel: "whatsapp",
to: "+1555",
accountId: "acct-2",
});
expect(normalized.lastChannel).toBe("whatsapp");
expect(normalized.lastTo).toBe("+1555");
expect(normalized.lastAccountId).toBe("acct-2");
});
});

View File

@@ -1,4 +1,5 @@
import { normalizeAccountId } from "./account-id.js";
import { normalizeMessageChannel } from "./message-channel.js";
export type DeliveryContext = {
channel?: string;
@@ -6,16 +7,20 @@ export type DeliveryContext = {
accountId?: string;
};
type DeliveryContextSessionSource = {
export type DeliveryContextSessionSource = {
channel?: string;
lastChannel?: string;
lastTo?: string;
lastAccountId?: string;
deliveryContext?: DeliveryContext;
};
export function normalizeDeliveryContext(context?: DeliveryContext): DeliveryContext | undefined {
if (!context) return undefined;
const channel = typeof context.channel === "string" ? context.channel.trim() : undefined;
const channel =
typeof context.channel === "string"
? normalizeMessageChannel(context.channel) ?? context.channel.trim()
: undefined;
const to = typeof context.to === "string" ? context.to.trim() : undefined;
const accountId = normalizeAccountId(context.accountId);
if (!channel && !to && !accountId) return undefined;
@@ -26,15 +31,54 @@ export function normalizeDeliveryContext(context?: DeliveryContext): DeliveryCon
};
}
export function normalizeSessionDeliveryFields(
source?: DeliveryContextSessionSource,
): {
deliveryContext?: DeliveryContext;
lastChannel?: string;
lastTo?: string;
lastAccountId?: string;
} {
if (!source) {
return {
deliveryContext: undefined,
lastChannel: undefined,
lastTo: undefined,
lastAccountId: undefined,
};
}
const merged = mergeDeliveryContext(
normalizeDeliveryContext({
channel: source.lastChannel ?? source.channel,
to: source.lastTo,
accountId: source.lastAccountId,
}),
normalizeDeliveryContext(source.deliveryContext),
);
if (!merged) {
return {
deliveryContext: undefined,
lastChannel: undefined,
lastTo: undefined,
lastAccountId: undefined,
};
}
return {
deliveryContext: merged,
lastChannel: merged.channel,
lastTo: merged.to,
lastAccountId: merged.accountId,
};
}
export function deliveryContextFromSession(
entry?: DeliveryContextSessionSource,
): DeliveryContext | undefined {
if (!entry) return undefined;
return normalizeDeliveryContext({
channel: entry.lastChannel ?? entry.channel,
to: entry.lastTo,
accountId: entry.lastAccountId,
});
return normalizeSessionDeliveryFields(entry).deliveryContext;
}
export function mergeDeliveryContext(