All files / services consentsApi.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 5/5
100% Lines 7/7

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                                                                                1x 1x   3x 3x         1x         1x                 1x  
import {api} from './api.ts';
 
// ============================================================================
// Types
// ============================================================================
 
export type ConsentType = 'transactional' | 'sms' | 'automated_calls';
 
/**
 * One row of the caller's current TCPA consent state (FSC-87).
 * `required` consents (transactional) are shown read-only in the UI.
 */
export interface ConsentState {
	consentType: ConsentType;
	granted: boolean;
	required: boolean;
	version: string;
	updatedAt: string | null;
}
 
/**
 * A consent change submitted to PUT /api/me/consents. Only optional consents
 * may be changed here; the backend rejects the required transactional type.
 */
export interface ConsentUpdate {
	consentType: ConsentType;
	granted: boolean;
}
 
interface ConsentsResponse {
	success: boolean;
	data: {
		consents: ConsentState[];
	};
}
 
// ============================================================================
// API Slice
// ============================================================================
 
export const consentsApi = api.injectEndpoints({
	endpoints: (builder) => ({
		getMyConsents: builder.query<ConsentState[], void>({
			query: () => '/me/consents',
			transformResponse: (response: ConsentsResponse) => response.data.consents,
			providesTags: ['Consents']
		}),
 
		updateMyConsents: builder.mutation<ConsentState[], ConsentUpdate[]>({
			query: (consents) => ({
				url: '/me/consents',
				method: 'PUT',
				body: {consents}
			}),
			transformResponse: (response: ConsentsResponse) => response.data.consents,
			invalidatesTags: ['Consents']
		})
	})
});
 
export const {
	useGetMyConsentsQuery,
	useUpdateMyConsentsMutation
} = consentsApi;