# Jitbit installation

## Database

The SQL scripts in the `sql` subfolder of the Jitbit download need to be modified before use. Do not run `create_db.sql`. From the table scripts, run only the statements that create the application user and tables; do not run database-switching commands or commands that write to `master`.

### Create the database

1. In `rg-servicedesk`, select **SQL Server → Create Database**.
2. Under **Basics**, set:
   - **Database name:** `corpJitbitServiceDeskDB`
   - **Use SQL elastic pool:** Yes
   - **Elastic pool:** `ccmServiceDesk`
   - **Backup storage redundancy:** Zone-redundant
3. Under **Networking**, add your current client IP address. This is for Query Editor access and does not affect web app access rules.
4. Under **Security**, set **Microsoft Defender** and **Ledger** to No.
5. Under **Additional settings**, select **No existing data** and **Default Collation**.
6. Create the database.

### Create the tables and application user

1. Open the database and select **Query Editor**.
2. Sign in using SQL authentication. Retrieve the `ccmjitbitservicedesk-server-admin` credentials from Bitwarden.
3. Run the table-creation statements from `CreateTables.sql`.
4. In a new query, create the application user and assign its roles:

```sql
CREATE USER JitbitHelpDeskUser
WITH PASSWORD = '<replace-with-secret-from-Bitwarden>';

ALTER ROLE db_datawriter ADD MEMBER JitbitHelpDeskUser;
ALTER ROLE db_datareader ADD MEMBER JitbitHelpDeskUser;
```

## App service

### Create the web app

1. Create a **Web App** with these settings:
   - **Name:** `ccm-corp-jitbit-servicedesk`
   - **Publish:** Code
   - **Stack:** .NET 10
   - **Operating system:** Linux
   - **Region:** Australia East
2. Under **Deployment**, disable Continuous deployment and Basic authentication.
3. Under **Networking**, turn Public access on and Virtual network integration off.
4. Under **Monitor**, enable Application Insights and select `ccmJitbitInsights`. Turn Defender off.
5. Create the web app.

### Deploy the app

Extract the initial ZIP, then create a ZIP containing only the files inside the `helpdesk` folder. Upload that ZIP through the Deployment Centre.

### Configure the database connection

In **Settings → Environment Variables**, create `DBConnectionString`. Use the database server, database name, user ID, and secret for your environment. Keep the real password in Bitwarden or the app setting; do not put it in documentation.

```text
Server=tcp:<server>.database.windows.net,1433;Initial Catalog=<database-name>;Persist Security Info=False;User ID=JitbitHelpDeskUser;Password=<replace-with-secret>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=60;
```

The timeout is set to 60 seconds to allow for the database waking from a cost-saving idle state.

In **Settings → Configuration → Stack Settings**, select .NET 10 and set the startup command to `dotnet HelpDesk.dll`.

## Snipe-IT API integration

Asset notes are recorded as activities. Retrieve them with the Snipe-IT activity endpoint:

```powershell
$activityUri = 'https://asset.manage.ccmschools.edu.au/api/v1/reports/activity?item_type=asset&item_id=581&action_type=note%20added'
Invoke-RestMethod -Method Get -Headers $headerSIT -Uri $activityUri
```

The note content is available in the `note` property. Helpdesk URLs can be recorded in this way.

## Jitbit automation

When a ticket's **Level 1** custom field changes to **Devices & Equipment**, configure Jitbit to send a webhook containing the ticket ID to the Azure Runbook.

## Azure Runbook

The runbook should:

1. Retrieve the ticket ID from the webhook.
2. Construct the ticket URL.
3. Connect to Jitbit and retrieve any assets linked to the ticket.
4. Connect to Snipe-IT and retrieve notes on those assets.
5. Add the ticket URL as an asset note if no existing note references it.
