# IWS5.Server
The IWS5.Server project is a worker service responsible for moving work items through workflows in the Process Director system. It listens for new and existing work items to process, as well as timer events, and orchestrates workflow tasks.

The IWS5.Server project was ported to the iWS5 solution from the [PDServer_LPNU](https://github.com/Greenfield-Investments/PDServer_LPNU) project, and exists only in the FEMA branch of the iWS5 solution. The below documentation is for the FEMA branch only. To read the documentation for PDServer_LPNU, see its README in its repository root.

## Contents
The IWS5.Server project contains a lot of code, much of which was never converted to Entity Framework syntax for database operations as the migration from the old standalone project was completed near the end of the active Process Director development cycle.

The important code is as follows:

1. [EventProcessors](EventProcessors/) - Contains classes with logic for processing different workflow tasks, such as queues and decisions. The appropriate event processor is determined based on an event's type and assigned by a factory class.
2. [TriggerProcessors](Helpers/TriggerProcessors/) - Contains classes with logic for processing different trigger operations, such as on-arrival emails and folder field value assignments. The appropriate trigger processor is determined based on a trigger's hook and action and assigned by a factory class.
3. [Worker.cs](Worker.cs) - This is where listener workers are registered and work item processing begins. When debugging, execution begins here and can be followed through the rest of the application code.

## Development & Configuration
The IWS5.Server project has a single `appsettings.json` file which contains configuration variables that must be set/updated for the worker to run:

1. Serilog (logging) - The important variable(s) here are `path`, which control where API log files are written to. The default location is recommended, but can be changed at will.
2. Connection strings (used for database connection):
    1. `GreenfieldPD` - The full connection string to authenticate with a local or production copy of the Process Director database. For example: `Data Source=MY-DATABASE-SERVER;User ID=PDAdmin;Password=MY_SECURE_PASSWORD;Initial Catalog=ProcessDirector;Encrypt=True;Trust Server Certificate=True`. In general, this string is dependent on the SQL Server configuration, but the PDAdmin user (with the `sysadmin` role on the server) should be used.
3. Process agent (worker instance configuration):
    1. `HostName` - The hostname of the server worker. Must be registered in the `ATServer` database table (typically done via iAdmin).
    2. `WaitIntervalSeconds` - The interval to look for new/existing work items and timers on.
    3. `ProcessLimit` - The number of work items to process concurrently. Does nothing, as concurrent processing was never implemented.
    4. `RunExpired` - Whether the server worker should process expired timers. May not be used, unsure.

The [IWS5.Server setup package script](https://github.com/Greenfield-Investments/setup-packages/tree/main/IWS5.Server) modifies the `appsettings.json` file with the values for the database connection provided by the user during installation.

Additionally, a valid Syncfusion Essential Studio license key must be supplied in [Program.cs](Program.cs) for PDF processing functionalities to work properly.

## Additional Notes
The server worker does exactly the bare minimum to move work items through the system. Everything should work, but there are still a number of features that are missing (namely, concurrent work item processing and a scheduler).

The only way to stop the server worker from running is to end the process, and the `Status` column in the `ATServer` database table is not updated whatsoever (differs from the old PDServer application).

Additionally, the vast majority of the application code was never converted to Entity Framework syntax, meaning there is a lot of baked-in SQL which can be very complicated to debug when database schemas change.