refactor(channels): share allowlist user resolve helpers

This commit is contained in:
Peter Steinberger
2026-02-15 05:28:38 +00:00
parent 164c1a3b5c
commit f41f6d3243
3 changed files with 61 additions and 40 deletions

View File

@@ -1,5 +1,11 @@
import type { RuntimeEnv } from "../../runtime.js";
export type AllowlistUserResolutionLike = {
input: string;
resolved: boolean;
id?: string;
};
export function mergeAllowlist(params: {
existing?: Array<string | number>;
additions: string[];
@@ -27,6 +33,36 @@ export function mergeAllowlist(params: {
return merged;
}
export function buildAllowlistResolutionSummary<T extends AllowlistUserResolutionLike>(
resolvedUsers: T[],
): {
resolvedMap: Map<string, T>;
mapping: string[];
unresolved: string[];
} {
const resolvedMap = new Map(resolvedUsers.map((entry) => [entry.input, entry]));
const mapping = resolvedUsers
.filter((entry) => entry.resolved && entry.id)
.map((entry) => `${entry.input}${entry.id}`);
const unresolved = resolvedUsers.filter((entry) => !entry.resolved).map((entry) => entry.input);
return { resolvedMap, mapping, unresolved };
}
export function resolveAllowlistIdAdditions<T extends AllowlistUserResolutionLike>(params: {
existing: Array<string | number>;
resolvedMap: Map<string, T>;
}): string[] {
const additions: string[] = [];
for (const entry of params.existing) {
const trimmed = String(entry).trim();
const resolved = params.resolvedMap.get(trimmed);
if (resolved?.resolved && resolved.id) {
additions.push(resolved.id);
}
}
return additions;
}
export function summarizeMapping(
label: string,
mapping: string[],