amplitudeRepo.ts
3 months ago
formsRepo.ts
6 months ago
hostingRepo.ts
3 months ago
index.ts
3 months ago
pagesRepo.ts
10 months ago
reachRepo.ts
1 month ago
formsRepo.ts
149 lines
| 1 | import { useGeneralDataStore } from '@/stores/generalDataStore'; |
| 2 | import { Header } from '@/types/enums'; |
| 3 | import type { AuthorizeRequestHeaders, ContactList, Form, FormsFilter, IntegrationsResponse } from '@/types/models'; |
| 4 | import { generateCorrelationId } from '@/utils/helpers'; |
| 5 | import httpService from '@/utils/services/httpService'; |
| 6 | |
| 7 | const URL = `${hostinger_reach_reach_data.rest_base_url}hostinger-reach/v1`; |
| 8 | |
| 9 | export const formsRepo = { |
| 10 | getForms: (filters: FormsFilter = {}, headers?: AuthorizeRequestHeaders) => { |
| 11 | const { nonce } = useGeneralDataStore(); |
| 12 | const params = new URLSearchParams(); |
| 13 | |
| 14 | if (filters.contactListId) { |
| 15 | params.append('contact_list_id', filters.contactListId.toString()); |
| 16 | } |
| 17 | |
| 18 | if (filters.type) { |
| 19 | params.append('type', filters.type); |
| 20 | } |
| 21 | |
| 22 | if (filters.limit) { |
| 23 | params.append('limit', filters.limit.toString()); |
| 24 | } |
| 25 | |
| 26 | if (filters.offset) { |
| 27 | params.append('offset', filters.offset.toString()); |
| 28 | } |
| 29 | |
| 30 | const queryString = params.toString() ? `?${params.toString()}` : ''; |
| 31 | const config = { |
| 32 | headers: { |
| 33 | [Header.CORRELATION_ID]: headers?.[Header.CORRELATION_ID] || generateCorrelationId(), |
| 34 | [Header.WP_NONCE]: nonce |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | return httpService.get<Form[]>(`${URL}/forms${queryString}`, config); |
| 39 | }, |
| 40 | |
| 41 | getContactLists: (headers?: AuthorizeRequestHeaders) => { |
| 42 | const { nonce } = useGeneralDataStore(); |
| 43 | |
| 44 | const config = { |
| 45 | headers: { |
| 46 | [Header.CORRELATION_ID]: headers?.[Header.CORRELATION_ID] || generateCorrelationId(), |
| 47 | [Header.WP_NONCE]: nonce |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | return httpService.get<ContactList[]>(`${URL}/contact-lists`, config); |
| 52 | }, |
| 53 | |
| 54 | toggleFormStatus: (formId: string, isActive: boolean, type: string, headers?: AuthorizeRequestHeaders) => { |
| 55 | const { nonce } = useGeneralDataStore(); |
| 56 | |
| 57 | const config = { |
| 58 | headers: { |
| 59 | [Header.CORRELATION_ID]: headers?.[Header.CORRELATION_ID] || generateCorrelationId(), |
| 60 | [Header.WP_NONCE]: nonce |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | const data = { |
| 65 | formId, |
| 66 | isActive, |
| 67 | type |
| 68 | }; |
| 69 | |
| 70 | return httpService.post<{ success: boolean }>(`${URL}/forms`, data, config); |
| 71 | }, |
| 72 | |
| 73 | getIntegrations: (headers?: AuthorizeRequestHeaders) => { |
| 74 | const { nonce } = useGeneralDataStore(); |
| 75 | |
| 76 | const config = { |
| 77 | headers: { |
| 78 | [Header.CORRELATION_ID]: headers?.[Header.CORRELATION_ID] || generateCorrelationId(), |
| 79 | [Header.WP_NONCE]: nonce |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | return httpService.get<IntegrationsResponse>(`${URL}/integrations`, config); |
| 84 | }, |
| 85 | |
| 86 | getIntegrationForms: (integrationType: string, headers?: AuthorizeRequestHeaders) => { |
| 87 | const { nonce } = useGeneralDataStore(); |
| 88 | const params = new URLSearchParams(); |
| 89 | params.append('type', integrationType); |
| 90 | |
| 91 | const config = { |
| 92 | plain: true, |
| 93 | headers: { |
| 94 | [Header.CORRELATION_ID]: headers?.[Header.CORRELATION_ID] || generateCorrelationId(), |
| 95 | [Header.WP_NONCE]: nonce |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | return httpService.get<Form[]>(`${URL}/forms?${params.toString()}`, config); |
| 100 | }, |
| 101 | |
| 102 | toggleIntegrationStatus: (integrationId: string, isActive: boolean, headers?: AuthorizeRequestHeaders) => { |
| 103 | const { nonce } = useGeneralDataStore(); |
| 104 | |
| 105 | const config = { |
| 106 | headers: { |
| 107 | [Header.CORRELATION_ID]: headers?.[Header.CORRELATION_ID] || generateCorrelationId(), |
| 108 | [Header.WP_NONCE]: nonce |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | const data = { |
| 113 | integration: integrationId, |
| 114 | isActive |
| 115 | }; |
| 116 | |
| 117 | return httpService.post<{ success: boolean }>(`${URL}/integrations`, data, config); |
| 118 | }, |
| 119 | sync: (importRequest: Record<string, Set<string>>, headers?: AuthorizeRequestHeaders) => { |
| 120 | const { nonce } = useGeneralDataStore(); |
| 121 | |
| 122 | const config = { |
| 123 | headers: { |
| 124 | [Header.CORRELATION_ID]: headers?.[Header.CORRELATION_ID] || generateCorrelationId(), |
| 125 | [Header.WP_NONCE]: nonce |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | const integrations = {}; |
| 130 | Object.entries(importRequest).forEach(([integrationId, formIds]) => { |
| 131 | integrations[integrationId] = Array.from(formIds); |
| 132 | }); |
| 133 | |
| 134 | return httpService.post<{ success: boolean }>(`${URL}/import`, { integrations }, config); |
| 135 | }, |
| 136 | getImportStatus: (integrationId: string, headers?: AuthorizeRequestHeaders) => { |
| 137 | const { nonce } = useGeneralDataStore(); |
| 138 | |
| 139 | const config = { |
| 140 | headers: { |
| 141 | [Header.CORRELATION_ID]: headers?.[Header.CORRELATION_ID] || generateCorrelationId(), |
| 142 | [Header.WP_NONCE]: nonce |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | return httpService.get(`${URL}/import/${integrationId}`, config); |
| 147 | } |
| 148 | }; |
| 149 |