# Backend Handler

## Table of Contents
- [Overview](#overview)
- [Usage](#usage)
- [Architecture](#architecture)
  - [Proposed Data Models](#proposed-data-models)
- [Repository Tree](#repository-tree)
- [License](#license)

## Overview

The backendHandler is responsible for mediating communication between the Aurora WUI and backend APIs. It listens for frontend events (e.g., updateUser), translates them into API calls, and emits an event back into the UI via the WUIBridge with the corresponding data. 

This ensures a decoupled, event-driven architecture where WUI components remain agnostic of backend details.

## Usage

To install this library ensure you have the following prerequisites installed:
- [Node.js](https://nodejs.org) (v16 or later recommended)
- [npm](https://www.npmjs.com) or [Yarn](https://yarnpkg.com)

Then you can install the package with the following command:

```bash
npm install @aurora-backend-handler/backend-handler@latest
```

This library gives acces to the backendHandler which recives and produces events involving the API.

### Test

#### Installation

1. Install dependencies:

```bash
npm install
```

#### Running Test

The project uses Jest as the testing framework with TypeScript support via ts-jest. To execute the tests, simply run:

```bash
npm run test
```

#### Coverage

The coverage of the test will be shown on the console where the test have been executed but also in a coverage folder on the repository directory.

## Architecture

### Proposed Data Models

```typescript
export class User {
    id: string;
	name: string;
	surname: string;
	email: string;
	password: string;
	notificationsEnabled: string;
	organizations: Organization[];
	tokens: Token[];
	tenants: Tenant[];
	axebowPlan: "freemium" | "premium";
	companyName: string;
	rol: string;
}

interface Organization {
    id: string,
    name: string,
    usersIds: string[],
    tenantsIds: string[],
    billingInformation: {
        legalName: string,
        CIFNIF: string,
        email: string,
        language: string,
        country: string,
        region: string,
        city: string,
        address: string,
        zipcode: string,
    }
    invoices: {
        id: string,
        name: string,
        customer: string,
        customerCIF: string,
        cost: number,
        date: string,
        invoiceFile: string,
    }[],
    status?: string,
}

interface Token {
    name: string;
    tenant: string;
    desciprion: string;
    lastUsed: string;
    expiration: string;
    token: string;
}

interface Tenant {
    id: string,
    name: string,
    organizationsIds: string[],
    services: Service[],
    accounts: Account[],
    environments: Environment[],
    marketplaceItems: MarketplaceItem[],
    resources: Resource[],
    role: string,
    status: string,
    users: number;
}

interface Service {
    id: string;
	tenant: string;
	account: string;
	environment: string;
	name: string;
	logo: string;
	description: string;
	revisions: string[];
	status: string;
	role: { name: string; instances: Instance[]; logo?: string, category?: string, version?: string, description?: string, resource?: Resource[] }[];
	links: Link[];
	resources: Resource[];
	usage: Usage;
	minReplicas?: number;
	maxReplicas?: number;
	lastDeployed?: string;
	project: string;
	registry: string;
	imageName: string;
	entrypoint: string;
	cmd: string;
	serverChannels: Channel[];
	clientChannels: Channel[];
	duplexChannels: Channel[];
	cloudProvider: string;
	currentRevision?: string;
	startedAt?: string;
}

interface Instance {
    id: string;
    name: string;
    status: string;
    usage: Usage;
    logs: string[];
    conatiners: Container[];
}

interface Container {
    name: string;
    ready: boolean;
    reestartCount: number;
    metrics: {
        cpu: number;
        memory: number;
    }
    states: any;
}

interface Channel {
    name: string;
    from: string;
    to: string;
    protocol?: "http" | "tcp" | "https";
    port?: number;
    portNum?: number;
}

interface Account {
    id: string;
	name: string;
	tenant: string;
	cloudProvider: {
		name: string;
		region?: string;
		interface?: string;
		apiVersion?: string;
		authType?: string;
		authUrl?: string;
		credentialId?: string;
		credentialSecret?: string;
	};
	logo: string;
	environments: string[];
	services: string[];
	domains: string[];
	status: string;
	usage: Usage;
	flavors?: {
		small: string;
		medium: string;
		large: string;
		volatile: string;
		nonReplicated: string;
		persistent: string;
	};
	organization?: string;
}

interface Usage {
    current: {
        cpu: number;
        memory: number;
        storage: number;
        volatileStorage: number;
        nonReplicatedStorage: number;
        persistentStorage: number;
    };
    limit: {
        cpu: {
            max: number;
            min: number;
        }
        memory: {
            max: number;
            min: number;
        }
        storage: {
            max: number;
            min: number;
        }
        volatileStorage: {
            max: number;
            min: number;
        }
        nonReplicatedStorage: {
            max: number;
            min: number;
        }
        persistentStorage:{
            max: number;
            min: number;
        }
    };
    cost: number;
}

interface Environment {
    id: string;
    name: string;
    account: string;
    tenant: string;
    logo: string;
    services: string[];
    domains: string[];
    status: string;
    usage: Usage;
    organization?: string;
    cloudProvider?: string;
    labels?: string[];
}

interface MarketplaceItem {
    tenant: string,
    name: string,
    logo: string,
    description: string,
    version: string,
    requirements: {
        cpu: number,
        memory: number,
    }
    status: string,
    instances: Instance[],
    links: Link[],
    resources: Resource[],
    domain?: string,
    type?: string,
}

interface Resource {
    type: 'clientChannel' | 'secret' | 'volume' | 'file' | 'string' | 'number' | 'boolean',
    name: string,
    value: string,
    kind?: 'volatile' | 'nonReplicated' | 'persistent',
    maxItems?: number,
}

interface Link {
    name: string,
    origin: string,
    target: string,
}

```

## Repository Tree

```
.
├── README.md
├── backend-handler.ts
├── environment.ts
├── event-helper.ts
├── event-names.ts 
├── jest.config.ts
├── package.lock.json
├── package.json
├── tsconfig.json
├── interfaces
│   ├── account-interface.ts
│   ├── channel-interface.ts
│   ├── container-interface.ts
│   ├── environment-interface.ts
│   ├── instance-interface.ts
│   ├── link-interface.ts
│   ├── marketplaceItem-interface.ts
│   ├── organization-interface.ts
│   ├── resource-interface.ts
│   ├── service-interface.ts
│   ├── tenant-interface.ts
│   ├── token-interface.ts
│   ├── usage-interface.ts
│   └── user-interface.ts
│
├── api
│   ├── account-api-service.ts
│   ├── deploy-service-helper.ts
│   ├── environment-api-service.ts
│   ├── marketplace-api-service.ts
│   ├── organizations-api-service.ts
│   ├── resources-api-service.ts
│   ├── service-api-service.ts
│   ├── tenant-api-service.ts
│   └── user-api-service.ts
│
├── test
│   ├── backend-handler.test.ts
│   ├── deploy-service-helper.ts
│   └── event-helper.test.ts
│
├── coverage
│   └── coverage data...
│
└── LICENSE

```
## License
