import BaseService from '..'; import type { ICreatePlatformConnectLinkPayload, ICreatePlatformConnectLinkResponse, IPlatformConnectLinkStatusResponse, IValidationDetail, } from './platform-connect.interface'; import { CREATE_PLATFORM_CONNECT_LINK, platformConnectLinkStatus, } from './platform-connect.routes'; import { CONNECT_LINK_FALLBACK_ERROR } from './platform-connect.constants'; /** * Ask dashboard-api for a one-shot link to the connect page, where the * merchant sets the email + password this account signs in with on * custom.recomaze.ai. The plugin keeps authenticating with its own secret. * * @param {ICreatePlatformConnectLinkPayload} payload * @return {Promise} */ export const createPlatformConnectLink = async ( payload: ICreatePlatformConnectLinkPayload ): Promise => await BaseService.post(CREATE_PLATFORM_CONNECT_LINK, payload); /** * Whether the link can still be opened. dashboard-api answers 404 once the * token was consumed by the connect page or ran past its 30 minutes, which * is the plugin's cue that the other tab is done. * * @param {string} token * @return {Promise} */ export const isPlatformConnectLinkUsable = async ( token: string ): Promise => { const response: IPlatformConnectLinkStatusResponse = await BaseService.get( platformConnectLinkStatus(token) ); return response?.success === true; }; /** * The one message worth showing for a failed link request. dashboard-api * answers a Joi failure with a details array and a business failure with a * plain message; handleError parks either under `errors`. * * @param {ICreatePlatformConnectLinkResponse} response * @return {string} */ export const getPlatformConnectError = ( response: ICreatePlatformConnectLinkResponse ): string => { if (typeof response?.error === 'string' && response.error) { return response.error; } if (typeof response?.errors === 'string' && response.errors) { return response.errors; } if (Array.isArray(response?.errors)) { const firstDetail: IValidationDetail | undefined = response.errors[0]; if (firstDetail?.message) return firstDetail.message; } if (typeof response?.message === 'string' && response.message) { return response.message; } return CONNECT_LINK_FALLBACK_ERROR; };