npm package: @schedule-tribe/pd-sdk
Description: This package provides TypeScript type definitions for handling Digital Platform in Domain-Driven Design contexts
Author: Alessandro Dev
Latest version: 0.0.4

### Schedule SDK Documentation

Welcome to the Schedule SDK documentation. This guide provides an overview of the SDK's functionalities and how to use them to manage medical appointments effectively.

## Table of Contents

- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
  - [Book a Schedule](#book-a-schedule)
  - [Cancel a Schedule](#cancel-a-schedule)
  - [List Schedules](#list-schedules)
- [Types](#types)
- [Events](#events)
- [Error Handling](#error-handling)
- [License](#license)

## Installation

To install the Schedule SDK, use npm or yarn:

```bash

npm install @schedule-tribe/schedule rich-domain curl2axios axios

```

## Configuration

To use the SDK, you need to configure it with your credentials, tenant, and API URL.

### Example Configuration

```typescript

import Platform, { Config, Credentials, Tenant } from "@schedule-tribe/schedule";

const credentials: Credentials = {
    username: '...',
    password: '...'
};

const config: Config = {
    url: new URL('https://api.example.com'),
    tenant: 'SDK1',
    credentials: credentials,
    emitEvents: true,
    mode: 'debug'
};

const platform = new Platform(config);

```

## Usage

### Book a Schedule

To book a schedule, use the `book` method:

```typescript

import Schedule from "@schedule-tribe/schedule";

const crm = CRM.init(req.body.crm);
const doctorName = Name.init(req.body.doctorName);
const patientName = Name.init(req.body.patientName);
const doctor = Doctor.init({ crm, name: doctorName });
const document = CPF.init(req.body.cpf);
const patient = Patient.init({ name: patientName, document });
const procedure = Procedure.init({ type: req.body.procedure });
const date = Dates.init(req.body.date);
const duration = Duration.init(req.body.duration);

const schedule = Schedule.init({ doctor, date, duration, patient, procedure });

platform.book(schedule).then(result => {
    if (result.isOk()) {
        console.log('Schedule booked successfully');
    } else {
        console.error('Failed to book schedule:', result.error());
    }
});

```

### Cancel a Schedule

To cancel a schedule, use the `cancel` method:

```typescript

import { Id } from "rich-domain";

const scheduleId = Id('1');

platform.cancel(scheduleId).then(result => {
    if (result.isOk()) {
        console.log('Schedule cancelled successfully');
    } else {
        console.error('Failed to cancel schedule:', result.error());
    }
});

```

### List Schedules

To list all schedules, use the `list` method:

```typescript

platform.list().then(result => {
    if (result.isOk()) {
        console.log('Schedules:', result.value());
    } else {
        console.error('Failed to list schedules:', result.error());
    }
});

```

## Types

### Tenant

The `Tenant` type defines the possible tenants:

```typescript

export type Tenant = 'SDK1' | 'SDK2';

```

### ScheduleModel

The `ScheduleModel` type represents the structure of a schedule:

```typescript

export type ScheduleModel = {
    id: string;
    date: Date;
    duration: number;
    doctor: {
        id: string;
        name: string;
        crm: string;
        createdAt: Date;
        updatedAt: Date;
    };
    patient: {
        id: string;
        name: string;
        document: string;
        createdAt: Date;
        updatedAt: Date;
    };
    procedure: {
        id: string;
        type: string;
        createdAt: Date;
        updatedAt: Date;
    };
    status: string;
    createdAt: Date;
    updatedAt: Date;
}

```

### Credentials

The `Credentials` type defines the authentication credentials:

```typescript

export type Credentials = {
    username: string;
    password: string;
}

```

### Config

The `Config` type defines the configuration for the SDK:

```typescript

export type Config = {
    url: URL;
    tenant: Tenant;
    credentials: Credentials;
    emitEvents?: boolean;
    mode?: 'debug';
}

```

## Events

The SDK can emit events for schedule actions if `emitEvents` is enabled in the configuration.

### Schedule Canceled Event

```typescript

contexts.dispatchEvent('schedule:canceled', { id });

```

### Schedule Booked Event

```typescript

contexts.dispatchEvent('schedule:booked', { schedule });

```

## Error Handling

All methods return a `Result` object, which can be either `Ok` or `Fail`. Use the `isOk()` method to check if the operation was successful and handle errors accordingly.

## License

This project is licensed under the MIT License.

---

This documentation provides a comprehensive guide to using the Schedule SDK for managing medical appointments. For further details or questions, please refer to the official repository or contact the support team.