All files / src/lib/api/pro whatsapp-recipients.ts

100% Statements 22/22
100% Branches 10/10
100% Functions 6/6
100% Lines 17/17

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                                                                    94x   24x     2x 1x             2x       2x 1x               2x       2x 1x       1x                   2x       2x 1x             2x           2x 1x      
import { request } from "../base";
 
export type WhatsAppRecipient = {
	id: number;
	proId: string;
	label: string;
	number: string;
	isPrimary: boolean;
	notificationsEnabled: boolean;
	isVerified: boolean;
	verifiedAt: string | null;
	otpPendingUntil: string | null;
	otpLastSentAt: string | null;
	dateCreated: string;
	dateUpdated: string;
};
 
export type WhatsAppRecipientListResponse = {
	recipients: WhatsAppRecipient[];
	maxAllowed: number;
};
 
export type CreateWhatsAppRecipientInput = {
	label: string;
	number: string;
	isPrimary?: boolean;
};
 
export type UpdateWhatsAppRecipientInput = {
	label?: string;
	notificationsEnabled?: boolean;
	isPrimary?: true;
};
 
export const whatsappRecipientsApi = {
	async list(proId: string): Promise<WhatsAppRecipientListResponse> {
		const res = await request<WhatsAppRecipientListResponse>(
			`/api/pro/${proId}/whatsapp-recipients`,
		);
		if (!res.data) throw new Error("Empty response");
		return res.data;
	},
 
	async create(
		proId: string,
		input: CreateWhatsAppRecipientInput,
	): Promise<WhatsAppRecipient> {
		const res = await request<{ recipient: WhatsAppRecipient }>(
			`/api/pro/${proId}/whatsapp-recipients`,
			{ method: "POST", body: input },
		);
		if (!res.data) throw new Error("Empty response");
		return res.data.recipient;
	},
 
	async update(
		proId: string,
		id: number,
		patch: UpdateWhatsAppRecipientInput,
	): Promise<WhatsAppRecipient> {
		const res = await request<{ recipient: WhatsAppRecipient }>(
			`/api/pro/${proId}/whatsapp-recipients/${id}`,
			{ method: "PATCH", body: patch },
		);
		if (!res.data) throw new Error("Empty response");
		return res.data.recipient;
	},
 
	async remove(proId: string, id: number): Promise<void> {
		await request(`/api/pro/${proId}/whatsapp-recipients/${id}`, {
			method: "DELETE",
		});
	},
 
	async verify(
		proId: string,
		id: number,
		code: string,
	): Promise<WhatsAppRecipient> {
		const res = await request<{ recipient: WhatsAppRecipient }>(
			`/api/pro/${proId}/whatsapp-recipients/${id}/verify`,
			{ method: "POST", body: { code } },
		);
		if (!res.data) throw new Error("Empty response");
		return res.data.recipient;
	},
 
	async resendOtp(
		proId: string,
		id: number,
	): Promise<{ recipient: WhatsAppRecipient; cooldownSeconds: number }> {
		const res = await request<{
			recipient: WhatsAppRecipient;
			cooldownSeconds: number;
		}>(`/api/pro/${proId}/whatsapp-recipients/${id}/resend-otp`, {
			method: "POST",
		});
		if (!res.data) throw new Error("Empty response");
		return res.data;
	},
};