// All methods/functions receive the sdk instance as the first parameter import isPlainObject from 'lodash/isPlainObject' import * as utils from './utils' import * as validate from './validate' import * as E from './customErrors' const { isString, isArray, isNumber } = utils export const params: any = {} params.activateProvider = (api: any, userId: string) => { const accountType = api.getCurrentAccountType() if (userId === undefined) { throw new E.MissingParametersError('userId is undefined') } } params.completeAppointment = (_: any, id: string) => { if (!id) { throw new E.InvalidParametersError( 'Cannot complete the appointment without a valid appointment id', ) } } params.confirmEmail = (_: any, key: any) => { if (!key) { throw new E.InvalidParametersError('The key is not a valid type') } } params.createAppointment = (_: any, params: any) => { if (!params.patient) { throw new E.InvalidParametersError( 'The "patient" parameter is required to create an appointment', ) } if (!params.provider) { throw new E.InvalidParametersError( 'The "provider" parameter is required to create an appointment', ) } if (!params.specialty) { throw new E.InvalidParametersError( 'The "specialty" parameter is required to create an appointment', ) } if (!params.time_slot) { throw new E.InvalidParametersError('The "time_slot" parameter is required') } if (!params.charge_method) { throw new E.InvalidParametersError( 'The "charge_method" parameter is required', ) } if (params.coupons !== undefined) { const isString = typeof params.coupons === 'string' if (!isString && !isArray(params.coupons)) { throw new TypeError(`The "coupons" parameter is not a string or array`) } } if (!params.payment_profile_id) { if (!params.card_number && !params.coupons) { throw new E.InvalidParametersError( 'The "card_number" or "coupons" parameter is required if "payment_profile_id" is not provided', ) } if (!params.expiration_date) { throw new E.InvalidParametersError( 'The "expiration_date" parameter is required if "payment_profile_id" is not provided', ) } if (!params.cvv_code) { throw new E.InvalidParametersError( 'The "cvv_code" parameter is required if "payment_profile_id" is not provided', ) } if (!params.billing_first_name) { throw new E.InvalidParametersError( 'The "billing_first_name" parameter is required if "payment_profile_id" is not provided', ) } if (!params.billing_last_name) { throw new E.InvalidParametersError( 'The "billing_last_name" parameter is required if "payment_profile_id" is not provided', ) } if (!params.billing_address) { throw new E.InvalidParametersError( 'The "billing_address" parameter is required if "payment_profile_id" is not provided', ) } if (!params.billing_state) { throw new E.InvalidParametersError( 'The "billing_state" parameter is required if "payment_profile_id" is not provided', ) } if (!params.billing_city) { throw new E.InvalidParametersError( 'The "billing_city" parameter is required if "payment_profile_id" is not provided', ) } if (!params.billing_zip_code) { throw new E.InvalidParametersError( 'The "billing_zip_code" parameter is required if "payment_profile_id" is not provided', ) } } } params.createAppointmentReview = (_: any, params: any) => { const { appointment, rating, comment } = params if (!isString(appointment)) { throw new TypeError('The "appointment" parameter is invalid') } if (!isString(rating) && !isNumber(rating)) { throw new TypeError('The "rating" parameter is invalid') } if (!isString(comment)) { throw new TypeError('The "comment" parameter is invalid') } } params.createPatientBlob = (client: any, blobInfo: any) => { if (!['patient', 'provider'].includes(client.getCurrentAccountType())) { throw new E.WrongAccountTypeError( 'Only patients and providers can use this URI', ) } if (blobInfo === undefined) { throw new E.MissingParametersError('Missing blob info parameter') } if (blobInfo && !isPlainObject(blobInfo)) { throw new TypeError( `The argument provided to createPatientBlob is not an object`, ) } if (blobInfo.tv_blob_ids == undefined) { throw new TypeError('tv_blob_ids is undefined') } if (blobInfo.filename == undefined) { throw new TypeError('The blob\'s "filename" cannot be undefined') } if (blobInfo.patient == undefined) { throw new TypeError( 'Missing the "patient" parameter. It needs to be a valid patient user_id', ) } } params.deletePatientBlob = (client: any, id: any) => { const accountType = client.getCurrentAccountType() if (accountType !== 'patient') { throw new E.WrongAccountTypeError('Only patients can use this API') } if (id === undefined) { throw new E.MissingParametersError('The blob id is undefined') } } params.fetchAppointmentReviews = (_: any, userId: any) => { if (!userId) { throw new E.InvalidParametersError( 'The provider user id is not a valid string', ) } if (!isString(userId)) { throw new E.InvalidParametersError('The "userId" parameter is not a string') } } params.fetchCoupons = (api: any) => { const accountType = api.getCurrentAccountType() if (accountType !== 'patient') { throw new E.WrongAccountTypeError('The accountType set is not a patient') } } params.fetchPatientDocument = (_: any, docId: string) => { if (!docId) { throw new E.InvalidParametersError('Invalid document id') } if (docId && !isString(docId)) { throw new TypeError('The document id passed in is not a string') } } params.fetchPatientDocuments = (_: any, docIds: string[]) => { if (!docIds) { throw new E.InvalidParametersError('docIds is not an array') } if (docIds && !isArray(docIds)) { throw new TypeError( 'The document ids parameter passed in is not an array type', ) } } params.fetchPatientMembers = (_: any, tvUids: any, { full }: any) => { if (typeof tvUids !== 'string' && !isArray(tvUids)) { throw new E.InvalidParametersError('tvUids is not a string or array') } } params.fetchProviders = (_: any, param: any) => { if (param.name) { if (isPlainObject(param.name)) { if ( param.name.firstName === undefined && param.name.middleName === undefined && param.name.lastName === undefined ) { throw new E.MissingParametersError( '"name" should include at least one of these properties: firstName, middleName, lastName', ) } } else if (isArray(param.name) && !param.name.length) { throw new Error('"name" cannot be empty') } else throw new TypeError('"name" is not an array or object') } } params.findProviders = (_: any, _params: any) => { if (!params) { throw new E.InvalidParametersError('The parameters are not a valid type') } if ( _params.time !== undefined && (typeof _params.time !== 'string' && typeof _params.time !== 'number') ) { throw new TypeError('The "time" type is invalid. Please use a string') } if (!_params.time) { throw new E.InvalidParametersError( 'The "time" parameter is required in order to find providers', ) } if (!_params.state) { throw new E.InvalidParametersError( 'The "state" parameter is required in order to find providers', ) } if (!_params.specialty) { throw new E.InvalidParametersError( 'The "specialty" parameter is required in order to find providers', ) } } params.staffingCompanies = (_: any, _params: any) => { if (!params) { throw new E.InvalidParametersError('The parameters are not a valid type') } if ( _params.time !== undefined && (typeof _params.time !== 'string' && typeof _params.time !== 'number') ) { throw new TypeError('The "time" type is invalid. Please use a string') } } params.findProviderAvailabilities = (_: any, params: any) => { if ( params.time !== undefined && (typeof params.time !== 'string' && typeof params.time !== 'number') ) { throw new TypeError('The "time" type is invalid. Please use a string') } if (!params.time) { throw new E.InvalidParametersError('The "time" parameter is required') } if (!params.state) { throw new E.InvalidParametersError('The "state" parameter is required') } } params.forgotPassword = (client: any, email: any, accountType: string) => { if (accountType === 'patient') { const username = email if (!username) { throw new E.InvalidParametersError('The username parameter is invalid') } if (!isString(username)) { throw new TypeError('The username parameter is not a string') } } else { if (!email) { throw new E.InvalidParametersError('The email parameter is invalid') } if (email && !isString(email)) { throw new TypeError('The email parameter is not a string') } } } params.login = (client: any, args: any) => { const accountTypes = [ 'admin', 'patient', 'provider', 'marketer', 'company', 'staffing_company', 'guest', ] const accountType = args[0] if (isString(accountType)) { if (!accountTypes.includes(accountType)) { throw new E.WrongAccountTypeError( 'The accountType is not a valid option. Valid options: admin, patient, ' + 'provider, marketer, company, staffing_company, guest', ) } if (accountType === 'admin') { const creds = args[1] if (!creds.hasOwnProperty('email')) { throw new E.MissingParametersError('Email is required') } if (!creds.hasOwnProperty('password')) { throw new E.MissingParametersError('Password is required') } } } else { throw new TypeError('The first argument must be a string (accountType)') } if (!params) { throw new E.InvalidParametersError('The parameters to login is invalid') } } params.loginAsPatient = (client: any, data: any) => { if (data === undefined) { throw new TypeError('data is undefined') } const keys = Object.keys(data || {}) if (!keys.includes('tv_uid')) { throw new E.MissingParametersError( 'You are missing the tv_uid in the parameters', ) } else if (!keys.includes('tv_token')) { throw new E.MissingParametersError( 'You are missing the tv_token in the parameters', ) } } params.loginWithJwtToken = (client: any, accountType: string, _params: any) => { if (!accountType) { throw new E.WrongAccountTypeError('accountType is invalid') } if (_params.userId === undefined && _params.user_id === undefined) { throw new TypeError('user_id or userId is undefined') } if (_params.token === undefined) { throw new TypeError('token is undefined') } } params.queryAppointments = (_: any, params: any) => { if (!isPlainObject(params)) { throw new E.InvalidParametersError('The parameters are invalid') } if (params.start_time && typeof params.start_time !== 'string') { throw new TypeError('Invalid type: "start_time" needs to be a string') } if (params.end_time && typeof params.end_time !== 'string') { throw new TypeError('Invalid type: "end_time" needs to be a string') } let hasValidKey = false const validKeys = [ 'patients', 'providers', 'status', 'start_time', 'end_time', ] const paramKeys = Object.keys(params) // Check if at least one param is valid for (const paramKey of paramKeys) { if (validKeys.includes(paramKey)) { hasValidKey = true break } } if (!hasValidKey) { throw new E.InvalidParametersError( 'None of the parameters are related to the search query. Please use one of: ' + '"patients", "providers", "status", "start_time", and "end_time"', ) } } params.updateAppointmentReview = (client: any, _params: any) => { const accountType = client.getCurrentAccountType() if (accountType !== 'patient') { throw new E.WrongAccountTypeError( 'Only patients can update appointment reviews', ) } if (!isPlainObject(_params)) { throw new E.InvalidParametersError( 'The parameters to update the appointment review is not a type of object', ) } const keysToLookFor = ['appointment', 'rating', 'comment'] const { validKeys } = validate.objectKeys(keysToLookFor, _params) if (!validKeys.length) { throw new E.InvalidParametersError( 'Please pass in any of these parameters: appointment, rating, comment', ) } } params.updateBackendPatient = (_: any, userId: string, changes: any) => { if (userId === undefined) { throw new E.MissingParametersError('"userId" is undefined') } if (changes && isPlainObject(changes)) { if (changes.languages) { if (!isPlainObject(changes.languages)) { throw new TypeError('"languages" is not an object') } } if (changes.profile) { if (!isPlainObject(changes.profile)) { throw new TypeError('"profile" is not an object') } } } } params.updatePatientDocument = (_: any, docId: string, changes: object) => { if (!docId) { throw new E.InvalidParametersError('The document ID is invalid') } if (!isPlainObject(changes) && !Array.isArray(changes)) { throw new TypeError('The "changes" parameter is not an object or array') } } params.updateProvider = (_: any, userId: string, changes: any) => { if (userId === undefined) { throw new E.MissingParametersError('"userId" is undefined') } if (changes && isPlainObject(changes)) { if (changes.languages) { if (!isPlainObject(changes.languages)) { throw new TypeError('"languages" is not an object') } } if (changes.specialties) { if ( typeof changes.specialties !== 'string' && !isArray(changes.specialties) ) { throw new TypeError('"specialties" is not a string or array') } } } }