[![Version](https://img.shields.io/npm/v/@mybus/ticketing?style=flat&colorA=FFA500&colorB=FFA500)](https://www.npmjs.com/package/@mybus/ticketing)

`@mybus/ticketing` provides several utilities for managing tickets for a user of a Monkey Factory application, including:

List of ticketing interfaces.
Ticket validation rules
Conversion of a ticket to ASN1 (barcode) format

- List of interfaces of the ticketing part,
- Rules for validating a ticket,
- QrCode generation,
- Conversion of a ticket to ASN1 format (barcode),
- Information about the validity of a ticket at a given time

# Validation Rules

When validating a ticket, it will go through several rules (functions that return a Boolean). Only if all rules are correctly passed will the ticket be considered valid.

```
AllowedNetwork(ticket: Ticket): boolean
```

Is the ticket being validated in a network where it can be used?

```
EnabledTicket(ticket: Ticket): boolean
```

Is the ticket enabled?

```
PassengerCount(ticket: Ticket): boolean
```

Does the ticket have the correct number of passengers? A ticket may not have any associated passengers. However, if it does, it must ensure that this does not exceed the allowed limit.

```
UserAge(ticket: Ticket): boolean
```

Does the user who wants to use the ticket have the required age?

```
UserId(ticket: Ticket): boolean
```

Is the user one of the passengers on the ticket?

```
UserProfiles(ticket: Ticket): boolean
```

Does the user have the correct profiles (e.g. "mulhouse:-26ans") to use this ticket?

```
ValidDate(ticket: Ticket): boolean
```

Is the ticket still valid (not expired) and still validatable?

```
ValidationCount(ticket: Ticket): boolean
```

Has the ticket reached its maximum number of possible validations?

```
ValidationDelay(ticket: Ticket): boolean
```

Has the ticket completed its last validation?

---

It is possible to use each rule independently.

```
AllowedTransfer(ticket: Ticket): boolean
```

Is not part of the validation rules but can be called to check if a ticket can be transferred or not.

Example for using the UserId rule independently:

```ts
import { UserId } from '@mybus/ticketing';

new UserId().validate(ticket, { userId: '9a8ab5c8-9f27-441e-bc96-3095fae38e0a' });
```

# Getting ticket status

It is possible to request information about a ticket in use.

```js
getTicketInformation(ticket: TicketDto): Information

/*
 * Returns
 * state: under validation, invalid, expired, ...
 * expireAt: date when the ticket will expire
 * countdown?: time remaining (in seconds) before expiration
 */
```

To only get the state of the ticket,

```js
/*
 * returns: `UNDER_VALIDATION | COMPLETED | UNCOMPOSTED | COMING_SOON`
 */
getTicketState(ticket, validation?): TicketState`
```

# Générer un QrCode pour un ticket

```js
getQrCode(ticket: TicketDto): QrCodeOutput

/*
 * Returns
 * origin: QrCodeOrigin
 * internal?: QrCodeInternal
 * external?: QrCodeExternal
 */
```

# ASN1 conversion

```js
convertTicketToHex(ticket: Ticket): string
```

Returns a hexadecimal string representing the ticket in ASN1 format.

```js
convertHexToTicketId(uicBarCodeHex: string): string | null
```

Provides the inverse transformation.

# Testing rules

All unit tests in this library are done on the same date (the date is mocked with Jest). The arbitrarily chosen date is Friday, September 30, 2022 at 4:33 PM. All tickets used in tests from test.config.ts are generated around this date. In other words, when you are running tests, you should consider yourself at this date and modify the dates of tickets and validations accordingly.
