All files / services records.service.ts

100% Statements 30/30
100% Branches 0/0
100% Functions 10/10
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141                                                      6x 45x 45x     44x           6x 4x 3x           6x 3x 2x           6x         3x 2x           6x         2x 2x           6x 3x           6x       3x       2x             6x       3x       2x             6x         3x       2x           6x       45x       40x    
/**
 * Records API service
 * Handles API calls for record CRUD operations
 */
 
import { apiClient } from '@/lib/api';
import type {
	RecordDetail,
	RecordData,
	RecordListResponse,
	GetRecordsParams,
	ListResponse,
	BatchUpdateItem,
	BatchCreateResponse,
	BatchUpdateResponse,
	BatchDeleteResponse,
	AggregationRequest,
	AggregationResponse,
} from '@/types/records.types';
 
// Re-export commonly used types
export type { RecordData, RecordListItem } from '@/types/records.types';
 
/**
 * Get list of records for a collection
 * Supports both offset-based pagination (paginated response) and cursor-based pagination (cursor response)
 */
export const getRecords = async (params: GetRecordsParams): Promise<ListResponse> => {
	const { collection, ...queryParams } = params;
	const response = await apiClient.get<ListResponse>(`/records/${collection}`, {
		params: queryParams,
	});
	return response.data;
};
 
/**
 * Get a single record by ID
 */
export const getRecordById = async (collection: string, recordId: string): Promise<RecordDetail> => {
	const response = await apiClient.get<RecordDetail>(`/records/${collection}/${recordId}`);
	return response.data;
};
 
/**
 * Create a new record
 */
export const createRecord = async (collection: string, data: RecordData): Promise<RecordDetail> => {
	const response = await apiClient.post<RecordDetail>(`/records/${collection}`, data);
	return response.data;
};
 
/**
 * Update a record (full replacement)
 */
export const updateRecord = async (
	collection: string,
	recordId: string,
	data: RecordData,
): Promise<RecordDetail> => {
	const response = await apiClient.put<RecordDetail>(`/records/${collection}/${recordId}`, data);
	return response.data;
};
 
/**
 * Partially update a record
 */
export const patchRecord = async (
	collection: string,
	recordId: string,
	data: Partial<RecordData>,
): Promise<RecordDetail> => {
	const response = await apiClient.patch<RecordDetail>(`/records/${collection}/${recordId}`, data);
	return response.data;
};
 
/**
 * Delete a record
 */
export const deleteRecord = async (collection: string, recordId: string): Promise<void> => {
	await apiClient.delete(`/records/${collection}/${recordId}`);
};
 
/**
 * Batch create records (all succeed or all fail atomically)
 */
export const batchCreateRecords = async (
	collection: string,
	records: RecordData[],
): Promise<BatchCreateResponse> => {
	const response = await apiClient.post<BatchCreateResponse>(
		`/records/${collection}/batch`,
		{ records },
	);
	return response.data;
};
 
/**
 * Batch patch records (all succeed or all fail atomically)
 * Returns 404 if any ID does not exist — entire batch fails.
 */
export const batchUpdateRecords = async (
	collection: string,
	updates: BatchUpdateItem[],
): Promise<BatchUpdateResponse> => {
	const response = await apiClient.patch<BatchUpdateResponse>(
		`/records/${collection}/batch`,
		{ records: updates },
	);
	return response.data;
};
 
/**
 * Batch delete records (all succeed or all fail atomically)
 * Returns 404 if any ID does not exist — entire batch fails.
 */
export const batchDeleteRecords = async (
	collection: string,
	ids: string[],
): Promise<BatchDeleteResponse> => {
	// Axios requires { data: body } to send a body with DELETE
	const response = await apiClient.delete<BatchDeleteResponse>(
		`/records/${collection}/batch`,
		{ data: { ids } },
	);
	return response.data;
};
 
/**
 * Run aggregation query on a collection
 */
export const aggregateRecords = async (
	collection: string,
	params: AggregationRequest,
): Promise<AggregationResponse> => {
	const response = await apiClient.get<AggregationResponse>(
		`/records/${collection}/aggregate`,
		{ params },
	);
	return response.data;
};