feat: import people

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-06-22 21:59:27 -04:00
parent cee6c2924c
commit 5fbc217edb
19 changed files with 1321 additions and 73 deletions
+18 -7
View File
@@ -22,8 +22,8 @@ import type {
EntityDeltaResponse,
EntityMoveRequest,
EntityMoveResponse,
EntityCopyRequest,
EntityCopyResponse,
EntityImportRequest,
EntityImportResponse,
EntityInterface,
} from '../types/entity';
import { useIntegrationStore } from '@KTXC/stores/integrationStore';
@@ -184,14 +184,25 @@ export const entityService = {
},
/**
* Copy entities to a target collection
* Import vCards into a collection, streaming progress as it arrives.
*
* @param request - copy request parameters
* @param request - import request (target collection, raw data, options)
* @param onObject - called synchronously for each per-contact result frame
* @param onDiscovered - called once with the discovered total (progress denominator)
*
* @returns Promise with copy results keyed by source entity identifier
* @returns Promise resolving to { total } (objects processed) when the stream completes
*/
async copy(request: EntityCopyRequest): Promise<EntityCopyResponse> {
return await transceivePost<EntityCopyRequest, EntityCopyResponse>('entity.copy', request);
async import(
request: EntityImportRequest,
onObject: (object: EntityImportResponse) => void,
onDiscovered: (expected: number) => void,
): Promise<{ total: number }> {
return await transceiveStream<EntityImportRequest, EntityImportResponse>(
'entity.import',
request,
onObject,
{ onStart: (expected) => { if (expected !== undefined) onDiscovered(expected); } },
);
},
};
+31 -34
View File
@@ -53,32 +53,55 @@ export async function transceivePost<TRequest, TResponse>(
* Stream an NDJSON API response, unwrapping data frames for the caller.
*
* The server emits one JSON object per line with a transport-level `type`
* discriminant. This helper consumes control and error frames, forwards only
* unwrapped `data` payloads to the caller, and returns the final stream total.
* discriminant. This consumes the chunked body, splits it into lines, forwards
* only unwrapped `data` payloads to the caller, and returns the final total.
*
* @param operation - Operation name, e.g. 'entity.listStream'
* @param operation - Operation name, e.g. 'entity.listStream', 'entity.import'
* @param data - Operation-specific request data
* @param onData - Synchronous callback invoked for every unwrapped data payload.
* May throw to abort the stream.
* @param user - Optional user identifier override
* @param options - Optional `user` override and an `onStart` hook, invoked once
* with the expected total (the progress denominator) when the
* server declares one on the start frame.
* @returns Promise resolving to the final stream total from the control/end frame
*/
export async function transceiveStream<TRequest, TData>(
operation: string,
data: TRequest,
onData: (data: TData) => void,
user?: string
options?: { user?: string; onStart?: (expected?: number) => void }
): Promise<{ total: number }> {
const request: ApiRequest<TRequest> = {
version: API_VERSION,
transaction: generateTransactionId(),
operation,
data,
user,
user: options?.user,
};
let total = 0;
// Interpret one NDJSON line: control frames carry start/end metadata, error
// frames abort, data frames are unwrapped to the caller.
const dispatch = (line: string): void => {
const message = JSON.parse(line) as ApiStreamResponse<TData>;
if (message.type === 'control') {
if (message.status === 'start') {
options?.onStart?.(message.total);
} else if (message.status === 'end') {
total = message.total;
}
return;
}
if (message.type === 'error') {
throw new Error(`[${operation}] ${message.message}`);
}
onData(message.data);
};
await fetchWrapper.post(API_URL, request, {
headers: { 'Accept': 'application/json' },
onStream: async (response: Response) => {
@@ -100,38 +123,12 @@ export async function transceiveStream<TRequest, TData>(
buffer = lines.pop()!; // retain any incomplete trailing chunk
for (const line of lines) {
if (!line.trim()) continue;
const message = JSON.parse(line) as ApiStreamResponse<TData>;
if (message.type === 'control') {
if (message.status === 'end') {
total = message.total;
}
continue;
}
if (message.type === 'error') {
throw new Error(`[${operation}] ${message.message}`);
}
onData(message.data);
if (line.trim()) dispatch(line);
}
}
// flush any remaining bytes still in the buffer
if (buffer.trim()) {
const message = JSON.parse(buffer) as ApiStreamResponse<TData>;
if (message.type === 'control') {
if (message.status === 'end') {
total = message.total;
}
} else if (message.type === 'error') {
throw new Error(`[${operation}] ${message.message}`);
} else {
onData(message.data);
}
}
if (buffer.trim()) dispatch(buffer);
} finally {
reader.releaseLock();
}