|
| 1 | +// -------------------------------------------------------- |
| 2 | +// Effect Functions |
| 3 | +// -------------------------------------------------------- |
| 4 | + |
| 5 | +import type { AuxiaRouterConfig } from '../api/auxiaProxyRouter'; |
| 6 | +import { |
| 7 | + buildGetTreatmentsRequestPayload, |
| 8 | + buildLogTreatmentInteractionRequestPayload, |
| 9 | + guDismissibleUserTreatment, |
| 10 | + guMandatoryUserTreatment, |
| 11 | +} from './libPure'; |
| 12 | +import type { GetTreatmentsRequestPayload, UserTreatment, UserTreatmentsEnvelop } from './types'; |
| 13 | +import { GateType } from './types'; |
| 14 | + |
| 15 | +export const callAuxiaGetTreatments = async ( |
| 16 | + apiKey: string, |
| 17 | + projectId: string, |
| 18 | + browserId: string | undefined, |
| 19 | + isSupporter: boolean, |
| 20 | + dailyArticleCount: number, |
| 21 | + articleIdentifier: string, |
| 22 | + editionId: string, |
| 23 | + countryCode: string, |
| 24 | + hasConsented: boolean, |
| 25 | + shouldServeDismissible: boolean, |
| 26 | +): Promise<UserTreatmentsEnvelop | undefined> => { |
| 27 | + // We now have clearance to call the Auxia API. |
| 28 | + |
| 29 | + // If the browser id could not be recovered client side, then we do not call auxia |
| 30 | + if (browserId === undefined) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + const url = 'https://apis.auxia.io/v1/GetTreatments'; |
| 35 | + |
| 36 | + const headers = { |
| 37 | + 'Content-Type': 'application/json', |
| 38 | + 'x-api-key': apiKey, |
| 39 | + }; |
| 40 | + |
| 41 | + const payload = buildGetTreatmentsRequestPayload( |
| 42 | + projectId, |
| 43 | + browserId, |
| 44 | + isSupporter, |
| 45 | + dailyArticleCount, |
| 46 | + articleIdentifier, |
| 47 | + editionId, |
| 48 | + countryCode, |
| 49 | + hasConsented, |
| 50 | + shouldServeDismissible, |
| 51 | + ); |
| 52 | + |
| 53 | + const params = { |
| 54 | + method: 'POST', |
| 55 | + headers: headers, |
| 56 | + body: JSON.stringify(payload), |
| 57 | + }; |
| 58 | + |
| 59 | + try { |
| 60 | + const response = await fetch(url, params); |
| 61 | + const responseBody = (await response.json()) as { |
| 62 | + userTreatments: UserTreatment[] | undefined; |
| 63 | + }; |
| 64 | + |
| 65 | + // nb: In some circumstances, for instance if the payload although having the right |
| 66 | + // schema, is going to fail Auxia's validation then the response body may not contain |
| 67 | + // the userTreatments field. In this case we return undefined. |
| 68 | + if (responseBody['userTreatments'] === undefined) { |
| 69 | + return Promise.resolve(undefined); |
| 70 | + } |
| 71 | + const data = responseBody as UserTreatmentsEnvelop; |
| 72 | + return Promise.resolve(data); |
| 73 | + } catch (error) { |
| 74 | + return Promise.resolve(undefined); |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +export const gateTypeToUserTreatmentsEnvelop = async ( |
| 79 | + config: AuxiaRouterConfig, |
| 80 | + gateType: GateType, |
| 81 | + getTreatmentsRequestPayload: GetTreatmentsRequestPayload, |
| 82 | +): Promise<UserTreatmentsEnvelop | undefined> => { |
| 83 | + switch (gateType) { |
| 84 | + case GateType.None: |
| 85 | + return Promise.resolve(undefined); |
| 86 | + case GateType.GuDismissible: |
| 87 | + return { |
| 88 | + responseId: '', |
| 89 | + userTreatments: [guDismissibleUserTreatment()], |
| 90 | + }; |
| 91 | + case GateType.GuMandatory: |
| 92 | + return { |
| 93 | + responseId: '', |
| 94 | + userTreatments: [guDismissibleUserTreatment()], |
| 95 | + }; |
| 96 | + case GateType.Auxia: |
| 97 | + return await callAuxiaGetTreatments( |
| 98 | + config.apiKey, |
| 99 | + config.projectId, |
| 100 | + getTreatmentsRequestPayload.browserId, |
| 101 | + getTreatmentsRequestPayload.isSupporter, |
| 102 | + getTreatmentsRequestPayload.dailyArticleCount, |
| 103 | + getTreatmentsRequestPayload.articleIdentifier, |
| 104 | + getTreatmentsRequestPayload.editionId, |
| 105 | + getTreatmentsRequestPayload.countryCode, |
| 106 | + getTreatmentsRequestPayload.hasConsented, |
| 107 | + getTreatmentsRequestPayload.shouldServeDismissible, |
| 108 | + ); |
| 109 | + case GateType.AuxiaAnalyticThenNone: |
| 110 | + await callAuxiaGetTreatments( |
| 111 | + config.apiKey, |
| 112 | + config.projectId, |
| 113 | + getTreatmentsRequestPayload.browserId, |
| 114 | + getTreatmentsRequestPayload.isSupporter, |
| 115 | + getTreatmentsRequestPayload.dailyArticleCount, |
| 116 | + getTreatmentsRequestPayload.articleIdentifier, |
| 117 | + getTreatmentsRequestPayload.editionId, |
| 118 | + getTreatmentsRequestPayload.countryCode, |
| 119 | + getTreatmentsRequestPayload.hasConsented, |
| 120 | + getTreatmentsRequestPayload.shouldServeDismissible, |
| 121 | + ); |
| 122 | + return Promise.resolve(undefined); |
| 123 | + case GateType.AuxiaAnalyticThenGuDismissible: |
| 124 | + await callAuxiaGetTreatments( |
| 125 | + config.apiKey, |
| 126 | + config.projectId, |
| 127 | + getTreatmentsRequestPayload.browserId, |
| 128 | + getTreatmentsRequestPayload.isSupporter, |
| 129 | + getTreatmentsRequestPayload.dailyArticleCount, |
| 130 | + getTreatmentsRequestPayload.articleIdentifier, |
| 131 | + getTreatmentsRequestPayload.editionId, |
| 132 | + getTreatmentsRequestPayload.countryCode, |
| 133 | + getTreatmentsRequestPayload.hasConsented, |
| 134 | + getTreatmentsRequestPayload.shouldServeDismissible, |
| 135 | + ); |
| 136 | + return { |
| 137 | + responseId: '', |
| 138 | + userTreatments: [guDismissibleUserTreatment()], |
| 139 | + }; |
| 140 | + case GateType.AuxiaAnalyticThenGuMandatory: |
| 141 | + await callAuxiaGetTreatments( |
| 142 | + config.apiKey, |
| 143 | + config.projectId, |
| 144 | + getTreatmentsRequestPayload.browserId, |
| 145 | + getTreatmentsRequestPayload.isSupporter, |
| 146 | + getTreatmentsRequestPayload.dailyArticleCount, |
| 147 | + getTreatmentsRequestPayload.articleIdentifier, |
| 148 | + getTreatmentsRequestPayload.editionId, |
| 149 | + getTreatmentsRequestPayload.countryCode, |
| 150 | + getTreatmentsRequestPayload.hasConsented, |
| 151 | + getTreatmentsRequestPayload.shouldServeDismissible, |
| 152 | + ); |
| 153 | + return { |
| 154 | + responseId: '', |
| 155 | + userTreatments: [guMandatoryUserTreatment()], |
| 156 | + }; |
| 157 | + default: |
| 158 | + console.error('Unknown direction'); |
| 159 | + } |
| 160 | +}; |
| 161 | + |
| 162 | +export const callAuxiaLogTreatmentInteration = async ( |
| 163 | + apiKey: string, |
| 164 | + projectId: string, |
| 165 | + browserId: string | undefined, |
| 166 | + treatmentTrackingId: string, |
| 167 | + treatmentId: string, |
| 168 | + surface: string, |
| 169 | + interactionType: string, |
| 170 | + interactionTimeMicros: number, |
| 171 | + actionName: string, |
| 172 | +): Promise<void> => { |
| 173 | + // If the browser id could not be recovered client side, then we do not call auxia |
| 174 | + if (browserId === undefined) { |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + const url = 'https://apis.auxia.io/v1/LogTreatmentInteraction'; |
| 179 | + |
| 180 | + const headers = { |
| 181 | + 'Content-Type': 'application/json', |
| 182 | + 'x-api-key': apiKey, |
| 183 | + }; |
| 184 | + |
| 185 | + const payload = buildLogTreatmentInteractionRequestPayload( |
| 186 | + projectId, |
| 187 | + browserId, |
| 188 | + treatmentTrackingId, |
| 189 | + treatmentId, |
| 190 | + surface, |
| 191 | + interactionType, |
| 192 | + interactionTimeMicros, |
| 193 | + actionName, |
| 194 | + ); |
| 195 | + |
| 196 | + const params = { |
| 197 | + method: 'POST', |
| 198 | + headers: headers, |
| 199 | + body: JSON.stringify(payload), |
| 200 | + }; |
| 201 | + |
| 202 | + await fetch(url, params); |
| 203 | + |
| 204 | + // We are not consuming an answer from the server, so we are not returning anything. |
| 205 | +}; |
0 commit comments