npm package: @kenetto/user-service
Description: User Service
Latest version: 1.1.1

# UserService

Contains the NodeJS service

<br>

## Running the app

Start by running `npm start`

From now on the app is running at `<protocol>`://`<hostname>`:`<port>`/

<br>

## Patient's identification

The patient's health number is used for their identification throughout the service. The service supports health numbers used by different countries (e.g. SNS number for Portugal).

## Information to take into account

The route `<protocol>`://`<hostname>`:`<port>`/users/`<id>` (where `<id>` is the patient's public ID) accepts the following requests methods/verbs: <b>GET</b>, <b>PUT</b> and <b>DELETE</b>. Unless it's a <b>PUT</b> request, for any request made with an ID that doesn't belong to any known user the route responds with the status code 404. If the route receives a request method other than any of these it responds with the status code 405 Method Not Allowed and sets the Allow response header field with the supported methods.

<br>

## Available Operations for /users/`<id>` route

### GET

Depending if the <b>GET</b> request is sent with the <b>full</b> parameter, the response will return correspondently:
- All the patient's information: personal information, documents, contacts, addresses and filiation. The response's body will have the following form:
```
[
    {
        "userID": <patient_id>,
        "title": "<patient_title>",
        "birthDate": "<patient_birth_date>",
        "givenNames": "<patient_given_names>",
        "familyNames": "<patient_family_names>",
        "maritalStatus": "<patient_marital_status>",
        "gender": "<patient_gender>",
        "nationality": "<patient_nationality>",
        "prescriptionCountry": "<patient_prescriptionCountry>",
        "dead": <patient_dead_status>,
        "documents": [
            {
                "type": "<document_type>",
                "value": <document_value>,
                "expirationDate": "<document_expiration_date>"
            },
            ...
        ],
        "links": {
            "contacts": "`<protocol>`://`<hostname>`:`<port>`/users/`<id>`/contacts",
            "addresses": "`<protocol>`://`<hostname>`:`<port>`/users/`<id>`/addresses",
            "filiation": "`<protocol>`://`<hostname>`:`<port>`/users/`<id>`/filiation"
        }
    },
    {
        "<contact_type>": [
            {
                "value": "<contact_value>",
                "contactID": "<contact_ID>"
            },
            ...
        ]
    },
    {
        "addresses": [
            {
                "use": "<address_use>",
                "type": "<address_type>",
                "street": "<address_street>",
                "doorNumber": <address_door_number>,
                "postalCode": "<address_postal_code>",
                "city": "<address_city>",
                "country": "<address_country>"
                "addressID": <address_ID>
            },
            ...
        ]
    },
    {
        "filiation": [
            {
                "relation": "<filiation_relation>",
                "name": "<filiation_name>",
                "filiatedID": <filiatedID>
            },
            ...
        ]
    }
]
```
<br>

- The patient's personal information, including their non-identifying information and documents.
For the patient's contacts, addresses and filiation the response's body will include a URI for each one which can later be used to make a request:
`<protocol>`://`<hostname>`:`<port>`/users/`<id>`/{contacts, addresses, filiation}.
The response body will return the following information:

```
{
    "userID": <patient_id>,
    "title": "<patient_title>",
    "birthDate": "<patient_birth_date>",
    "givenNames": "<patient_given_names>",
    "familyNames": "<patient_family_names>",
    "maritalStatus": "<patient_marital_status>",
    "gender": "<patient_gender>",
    "nationality": "<patient_nationality>",
    "prescriptionCountry": "<patient_prescriptionCountry>",
    "dead": <patient_dead_status>,
    "documents": [
        {
            "type": "<document_type>",
            "value": <document_value>,
            "expirationDate": "<document_expiration_date>"
        },
        ...
    ],
    "links": {
        "contacts": "`<protocol>`://`<hostname>`:`<port>`/users/`<id>`/contacts",
        "addresses": "`<protocol>`://`<hostname>`:`<port>`/users/`<id>`/addresses",
        "filiation": "`<protocol>`://`<hostname>`:`<port>`/users/`<id>`/filiation"
    }
}
```

### PUT

When making a <b>PUT</b> request to the above route the request body type (Content-Type) must be sent as <i>application/json</i>. The request body must use the following syntax:
```
{
    "title": "<patient_title>",
    "birthDate": "<patient_birth_date>",
    "givenNames": "<patient_given_names>",
    "familyNames": "<patient_family_names>",
    "maritalStatus": "<patient_marital_status>",
    "gender": "<patient_gender>",
    "nationality": "<patient_nationality>",
    "prescriptionCountry": "<patient_prescriptionCountry>",
    "dead": <patient_dead_status>,
    "documents": [
        {
            "type": "<document_type>",
            "value": <document_value>,
            "expirationDate": "<document_expiration_date>"
        },
        ...
    ],
    "filiation": [
        {
            "relation": "<filiation_relation>",
            "name": "<filiation_name>",
            "filiatedID": <filiatedID>
        },
        ...
    ],
    "contact": {
        "type": "<contact_type>",
        "value": "<contact_value>"
    },
    "address": {
        "use": "<address_use>",
        "type": "<address_type>",
        "street": "<address_street>",
        "doorNumber": <address_door_number>,
        "postalCode": "<address_postal_code>",
        "city": "<address_city>",
        "country": "<address_country>"
    }
}
```
Note: the service follows the ISO 3166-1 alpha-3 standard for representing countries codes. That means the patient's nationality (`<patient_nationality>`), an address country field (`<address_country>`) and the country where the patient is registered to receive prescriptions (`<prescriptionCountry>`) expect a value following this standard.

If the request type isn't correct the appropriate status code is set.
Depending if an user with the provided ID already exists the received patient's information is either created/persisted in the database or the route returns an error. In both scenarios the appropriate status code is set in the response.

### DELETE

For a <b>DELETE</b> request the route attempts to delete the file holding the received user's ID information (without regarding the separation between personal and medical data).

<br>

## Available Operations for /users/`<id>`/contacts route

### GET

Making a <b>GET</b> request to this route will return all the patient's contacts in the following form:
```
{
    "userID": <id>,
    "<contact_type>": [
        "<contact_value>",
        ... 
    ],
    ...
}
```

### POST

One can make a <b>POST</b> request to this route in order to add a new contact to the patient's contacts list. To do so the request body has to have the following form:
```
[
    {
        "type": "<contact_type>",
        "value": "<contact_value>"
    },
        ...
]
```

### DELETE

(in development)

<br>

## Available Operations for /users/`<id>`/addresses route

### GET

A <b>GET</b> request to this route will return addresses that are known to belong to the patient. The returned data will have the following form:
```
{
    "userID": <id>,
    "addresses": [
        {
            "use": "<address_use>",
            "type": "<address_type>",
            "street": "<address_street>",
            "doorNumber": <address_doorNumber>,
            "city": "<address_city>",
            "postalCode": "<address_postalCode>",
            "country": "<address_country>",
            "addressID": <addressID>
        },
        ...
    ]
}
```

### POST

To add a new address to a patient's addresses list, one has to send a <b>POST</b> request with the following body
```
[
    {
        "use": "<address_use>",
        "type": "<address_type>",
        "street": "<address_street>",
        "doorNumber": <address_doorNumber>,
        "city": "<address_city>",
        "postalCode": "<address_postalCode>",
        "country": "<address_country>"
    },
    ...
]
```

### DELETE

When making a request to the /users/`<id>`/addresses/`<addressID>` route it will delete the addres with the specified `<addressID>` from the patient's addresses list.
The deleted address information is then returned.

<br>

## Available Operations for /users/`<id>`/filiation route

### GET

When receiving a <b>GET</b> request this route will return all patient's filiaiton in the following syntax:
```
{
    "userID": <id>,
    "filiation": [
        {
            "relation": "<filiation_relation>",
            "name": "<filiation_name>",
            "filiatedID": <filiation_filiatedID>
        },
        ...
    ]
}
```

### POST

```
[
    {
        "relation": "<filiation_relation>",
        "name": "<filiation_name>",
        "filiatedID": <filiation_filiatedID>
    },
    ...
]
```

### DELETE

(in development)