import { hashToBase64String } from '../helpers' import { PaymentStatus, PracticeAccount, Uuid } from '../models' import { Assignment, AssignmentRequest, PaymentIntentRequestMetadata, PlanType, Practice, PracticeConfigKind, PracticeConfigs, PracticeInvoice, PracticePayment, PracticePaymentIntent, PracticePlan, PracticePlanPrices, PracticeWorkflow, PracticeWorkflowWithTagSpecialty, Practitioner, PractitionerLicense, PractitionerPreference, PractitionerQuota, PractitionerRole, WorkflowType, } from '../models/practice' import { APIService } from './api' export class PracticeService { constructor(private api: APIService, private baseURL: string) {} /** * This function will only work if the service is initialized with * an M2M with the scope `practice.practices.get` * @returns an array of practices */ public practiceGetAll(): Promise { return this.api.get(`${this.baseURL}/v1/practices`) } /** * This function get the practice from the URL of a practice * It is the entry point of our web apps * @param practiceURL URL of the practice to search * @param hydratePracticeConfigs (optional) if set true it the Practice field configs will be set * @param accounts (optional) if set true it the Practice field accounts will be set * @returns the found practice or undefined */ public practiceGetFromURL( practiceURL: string, params?: { hydratePracticeConfigs?: boolean accounts?: boolean } ): Promise { return this.api.get(`${this.baseURL}/v1/practices`, { params: { url_practice: practiceURL, ...params, }, }) } public practiceGetFromUuid(practiceUuid: Uuid, locale?: string, withAccounts?: boolean): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}`, { params: { locale, accounts: withAccounts }, }) } /// Practice Configs /** * This function retrieves all configs of a specific practice * @param practiceUuid uuid of the practice * @returns the practice configs */ public practiceConfigGetFromPracticeUuid(practiceUuid: Uuid): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/configs`) } /** * This function retrieves a specific config of a practice * @param practiceUuid uuid of the practice * @param kind of the config * @returns the practice config */ public practiceConfigGetByKindForPracticeUuid( practiceUuid: Uuid, kind: PracticeConfigKind ): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/configs/${kind}`) } /** * This function creates a config for a specific practice * @param practiceUuid uuid of the practice * @param config the config to add to the practice * @returns the created practice config */ public practiceConfigCreateForPracticeUuid(practiceUuid: Uuid, config: PracticeConfigs): Promise { return this.api.post(`${this.baseURL}/v1/practices/${practiceUuid}/configs`, config) } /** * This function updates a specific config of a practice * @param practiceUuid uuid of the practice * @param config the config to update * @returns the practice config */ public practiceConfigUpdate(config: PracticeConfigs): Promise { return this.api.put( `${this.baseURL}/v1/practices/${config.uuidPractice}/configs/${config.kind}`, config ) } /// Accounts public practiceGetAccounts(practiceUuid: Uuid): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/accounts`) } public practiceGetAccount(practiceUuid: Uuid, accountUuid: Uuid): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/accounts/${accountUuid}`) } /** * Get the PracticeWorkflows of a specific practice * @param practiceUuid the uuid of the practice * @param kind (optional) the kind of WorkflowType to filter in * @returns a list of PracticeWorkflow */ public practiceGetWorkflows(practiceUuid: Uuid, kind?: WorkflowType): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/workflows`, { params: { kind }, }) } public practiceGetWorkflow( practiceUuid: Uuid, workflowType: WorkflowType ): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/workflows/${workflowType}` ) } /// Plans public practiceGetPlans(practiceUuid: Uuid, planType?: PlanType): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/plans`, { params: { kind: planType }, }) } public practiceGetPlan(practiceUuid: Uuid, planId: number): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/plans/${planId}`) } public practiceGetPlanPrices(practiceUuid: Uuid, planId: number): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/plans/${planId}/prices`) } // Payments public practiceGetPayments( practiceUuid: Uuid, statusPayment?: PaymentStatus, withConsultUUIDNULL?: boolean, perPage?: number, indexPage?: number ): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/payments`, { params: { status: statusPayment, withConsultUUIDNULL, perPage, indexPage, }, }) } public practiceGetPayment(practiceUuid: Uuid, idStripeInvoiceOrPaymentIntent: string): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/payments/${idStripeInvoiceOrPaymentIntent}` ) } public practiceGetPaymentForStripePaymentIntentWithID( practiceUuid: Uuid, stripePaymentIntentId: number ): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/payments/${stripePaymentIntentId}` ) } // Payments Intent public practiceGetPaymentsIntents(practiceUuid: Uuid, planType?: PlanType): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/payments/intents`, { params: { kind: planType }, }) } /** * This function return the user hased email to be use for creating payment intent * @param email the email to hash * @returns a hashed email */ public getPaymentIntentHashedEmail(email: string): string { return hashToBase64String(email.toLowerCase()) } /** * Creates a PracticePaymentIntent * @param practiceUuid the uuid of the practice * @param planId the plan id to use * @param userEmail the email address of the user * @param isoLocality (optional) the desired locality * @param url_subdomain (optional) the url of the sub domain (@bruno-morel need you to document that) * @param promotionCode (optional) promotion code to apply * @param requestMetadata (optional) the request metadata to use. If defined, when payment service call our hooks in practice, it will use it to do required action (create a consult, refill a consult, etc.). * @returns */ public practiceCreatePaymentsIntent( practiceUuid: Uuid, planId: number, userEmail: string, isoLocality?: string, url_subdomain?: string, requestMetadata?: PaymentIntentRequestMetadata ): Promise { return this.api.post( `${this.baseURL}/v1/practices/${practiceUuid}/payments/intents/`, { idPlan: planId, hashUserEmail: userEmail ? this.getPaymentIntentHashedEmail(userEmail) : undefined, isoLocality, requestMetadata, }, { params: { url_subdomain } } ) } public practiceGetPaymentsIntent(practiceUuid: Uuid, paymentIntentId: number): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/payments/intents/${paymentIntentId}` ) } /** * Updates a PracticePaymentIntent * @param practiceUuid the practice uuid * @param idPraticePaymentIntent the id of the PracticePaymentIntent to update * @param practicePaymentIntent the desired PracticePaymentIntent * @param userEmail the email of the user * @param promotionCode (optional) promotional code to apply * @param finalize (optional) if true will finalize the PracticePaymentIntent and related Stripe.Invoice. Once, finalized you cannot modify the PracticePaymentIntent anymore. * @returns the updated PracticePaymentIntent */ public practiceUpdatePaymentsIntent( practiceUuid: string, idPraticePaymentIntent: number, practicePaymentIntent: PracticePaymentIntent, userEmail: string, promotionCode?: string, finalize?: boolean ) { return this.api.put( `${this.baseURL}/v1/practices/${practiceUuid}/payments/intents/${idPraticePaymentIntent}`, { ...practicePaymentIntent, hashUserEmail: userEmail ? this.getPaymentIntentHashedEmail(userEmail) : undefined, }, { params: { promotionCode, finalize } } ) } /** * Invoice * @param practiceUuid UUID of the practice to get the invoice from * @param invoiceId ID of the invoice in stripe */ public getInvoice(practiceUuid: Uuid, invoiceId: string): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/payments/invoices/${invoiceId}` ) } // Practitioner public practiceGetPractitioners(practiceUuid: Uuid): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/practitioners`) } public practiceUpdatePractitioner( practiceUuid: Uuid, practitionerUuid: Uuid, requestBody: Practitioner ): Promise { return this.api.put( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}`, requestBody ) } public practiceGetPractitioner(practiceUuid: Uuid, practitionerUuid: Uuid): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}` ) } // Practitioner Licenses public practiceGetPractitionerLicenses(practiceUuid: Uuid, practitionerUuid: Uuid): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/licenses` ) } public practiceCreatePractitionerLicense( practiceUuid: Uuid, practitionerUuid: Uuid, requestBody: PractitionerLicense ): Promise { return this.api.post( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/licenses`, requestBody ) } public practiceUpdatePractitionerLicense( practiceUuid: Uuid, practitionerUuid: Uuid, licenseId: number, requestBody: PractitionerLicense ): Promise { return this.api.put( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/licenses/${licenseId}`, requestBody ) } public practiceGetPractitionerLicense( practiceUuid: Uuid, practitionerUuid: Uuid, licenseId: number ): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/licenses/${licenseId}` ) } // Practitioner Preferences public practiceGetPractitionerPreferences( practiceUuid: Uuid, practitionerUuid: Uuid ): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/preferences` ) } public practiceCreatePractitionerPreference( practiceUuid: Uuid, practitionerUuid: Uuid, requestBody: PractitionerPreference ): Promise { return this.api.post( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/preferences`, requestBody ) } public practiceUpdatePractitionerPreference( practiceUuid: Uuid, practitionerUuid: Uuid, preferenceId: number, requestBody: PractitionerPreference ): Promise { return this.api.put( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/preferences/${preferenceId}`, requestBody ) } public practiceGetPractitionerPreference( practiceUuid: Uuid, practitionerUuid: Uuid, preferenceId: number ): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/preferences/${preferenceId}` ) } // Practitioner Roles public practiceGetPractitionerRoles(practiceUuid: Uuid, practitionerUuid: Uuid): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/roles` ) } public practiceCreatePractitionerRole( practiceUuid: Uuid, practitionerUuid: Uuid, requestBody: PractitionerRole ): Promise { return this.api.post( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/roles`, requestBody ) } public practiceDeletePractitionerRoles(practiceUuid: Uuid, practitionerUuid: Uuid): Promise { return this.api.deleteRequest( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/roles` ) } public practiceUpdatePractitionerRole( practiceUuid: Uuid, practitionerUuid: Uuid, roleId: number, requestBody: PractitionerRole ): Promise { return this.api.put( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/roles/${roleId}`, requestBody ) } public practiceGetPractitionerRole( practiceUuid: Uuid, practitionerUuid: Uuid, roleId: number ): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/roles/${roleId}` ) } public practiceDeletePractitionerRole( practiceUuid: Uuid, practitionerUuid: Uuid, roleId: number ): Promise { return this.api.deleteRequest( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/roles/${roleId}` ) } // Practitioner signature /** * This function returns the practitioner's signature as a Blob * @param practiceUuid the practice uuid of the practitioner * @param practitionerUuid the practitioner uuid * @returns a blob representing the signature */ public practiceGetPractitionerSignature(practiceUuid: Uuid, practitionerUuid: Uuid): Promise { return this.api.get( `${this.baseURL}/v1/practices/${practiceUuid}/practitioners/${practitionerUuid}/signature`, { responseType: 'blob' } ) } // Assignments public practiceGetAssignments(practiceUuid: Uuid): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/assignments`) } public practiceCreateAssignment(practiceUuid: Uuid, requestBody: AssignmentRequest): Promise { return this.api.post(`${this.baseURL}/v1/practices/${practiceUuid}/assignments`, requestBody) } public practiceUpdateAssignment( practiceUuid: Uuid, assignmentId: number, requestBody: Assignment ): Promise { return this.api.put( `${this.baseURL}/v1/practices/${practiceUuid}/assignments/${assignmentId}`, requestBody ) } public practiceGetAssignment(practiceUuid: Uuid, assignmentId: number): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/assignments/${assignmentId}`) } // Quotas public practiceGetQuotas(practiceUuid: Uuid): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/quotas`) } public practiceGetQuota(practiceUuid: Uuid, quotaId: number): Promise { return this.api.get(`${this.baseURL}/v1/practices/${practiceUuid}/quotas/${quotaId}`) } }