---
title: PharmacyEligibilityService
summary: API reference for PharmacyEligibilityService.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-24
library: SAIF.Services
reference_type: package
---

# PharmacyEligibilityService

Implements `IPharmacyEligibility`, the extract job that identifies claims eligible for pharmacy benefits (and claims newly ineligible after their first fill) and writes a fixed-format ODS extract file consumed downstream by the MedImpact pharmacy vendor. It depends on `IUnitOfWork` for persistence and eligibility-report queries, `IUserProfileService`, `IClaimQueryProvider` and `IWorkerQueryProvider` for claim/worker lookups, `ICCClaimApiService` and `IClaimDataApiClient` for building `Claim` aggregates, and a locally constructed `WindowsEnvironmentService`. It writes the extract to a hardcoded local path (`OdsExtractFileName`), archiving the previous file before overwriting it.

## Purpose
- Processes the full pharmacy-eligibility extract run: reads eligible claims, writes their extract records, reads and processes newly-ineligible first-fill claims, appends a trailer record, and returns run-level counts.
- Filters eligibility reports to claims that are not already marked ineligible or major-is-pilot, and separately to first-fill claims whose MedImpact transmit date is set but whose regular transmit date is not.
- Validates that a claim resolved successfully and is not void or denied before it is written to the extract, logging a warning otherwise.
- Formats and appends a fixed-width pharmacy-eligibility record per eligible worker, substituting a SAIF-owned mailing address for workers with a foreign address.
- Flips a first-fill claim's eligibility status to `Ineligible` and stamps update audit fields once its extract record has been written.
- Archives the existing extract file (timestamped copy) before the current run overwrites it.

## Context
- Namespace: `SAIF.Services.Claims.PharmacyElilgbility`
- Assembly/package: `SAIF.Services` (packaged inside the `SAIF.SAIFCode.Core` NuGet package)

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| PharmacyEligibilityService | `public PharmacyEligibilityService(IUnitOfWork uow, IUserProfileService ups, IClaimQueryProvider claimQueryProvider, IWorkerQueryProvider workerQueryProvider, ICCClaimApiService claimApiService, IClaimDataApiClient gwClaimDataApiClient)` | Wires the persistence and claim/worker lookup collaborators and initializes the run's `PharmacyEligibilityResults` counters to zero. |
| ProcessEligibilityExtract | `public PharmacyEligibilityResults ProcessEligibilityExtract(ILogWriter warningReport, ILogWriter addressReport)` | Runs the full extract: processes eligible claims, processes first-fill claims, writes the trailer record, and commits the unit of work; returns the accumulated result counts. |
| GetEligibleClaims | `public bool GetEligibleClaims()` | Loads pharmacy-eligibility reports excluding `Ineligible` and `MajorisPilot` status codes into the internal report collection; returns whether any were found. |
| ProcessEligibilityReports | `public PharmacyEligibilityResults ProcessEligibilityReports(ILogWriter warningReport, ILogWriter addressReport)` | For each loaded eligibility report, verifies the claim exists and is in a writable status, then writes its extract record; increments the reports-read counter for every claim examined. |
| GetFirstFillClaims | `public bool GetFirstFillClaims()` | Loads pharmacy-eligibility reports that are `Ineligible` with a set MedImpact transmit date but no regular transmit date yet, into the internal report collection; returns whether any were found. |
| ProcessFirstFillReports | `public PharmacyEligibilityResults ProcessFirstFillReports(ILogWriter warningReport, ILogWriter addressReport)` | Marks each loaded report as `FirstFill` status, writes its extract record if the claim is valid, and on success flips it back to `Ineligible` via `UpdatePharmElig`; tracks first-fill and reports-read counts. |
| GetClaimData | `public Claim GetClaimData(long claimId)` | Constructs a `Claim` aggregate for the given claim ID using the service's unit-of-work, user-profile, claim/worker query providers, and claim API collaborators. |
| VerifyClaimNotNull | `public bool VerifyClaimNotNull(Claim claimData, long claimId, ILogWriter warningReport)` | Confirms the claim resolved to non-null data and identifier; logs a "retrieval failed" warning and returns `false` otherwise. |
| VerifyClaim | `public bool VerifyClaim(Claim claimData, long claimId, ILogWriter warningReport)` | Confirms the claim's latest status is not `Void` or `Denied`, logging the corresponding warning message and returning `false` when it is. |
| VerifyWorker | `public bool VerifyWorker(Worker worker, Claim claimData, long claimId, ILogWriter addressReport, Address address)` | Confirms the eligibility report, worker, and claim identifier all resolved, and logs an address exception when the worker has no usable address or a foreign address (still returning `true` for a foreign address after logging). |
| UpdatePharmElig | `public bool UpdatePharmElig(long claimId)` | Reverts a first-fill claim's eligibility status to `Ineligible`, recording the previous status/effective date and stamping the transmit date and audit fields; returns `false` if the report can no longer be found. |
| GetWorkerAddress | `public Address GetWorkerAddress(Claim claim, Worker worker)` | Resolves and builds the worker's mailing address from the claim person's addresses via `IClaimQueryProvider.GetClaimPersonAddresses`. |
| WriteExtract | `public bool WriteExtract(long claimId, Claim claimData, Worker worker, ILogWriter addressReport)` | Builds a fixed-format `PharmacyEligibilityExtract` record (status, group ID mapped from eligibility status, demographic and address fields — substituting a SAIF-owned address for foreign addresses) and appends it to the in-memory extract collection; returns `false` if the eligibility report or worker verification fails. |
| WriteExtractTrailer | `public void WriteExtractTrailer()` | Archives the existing extract file, writes a fixed-width trailer record containing the total eligible-report count, and writes the accumulated extract records to the ODS extract file. |
| ArchiveExistingExtract | `public void ArchiveExistingExtract()` | Copies the current extract file to a timestamped archive name, deleting any pre-existing file at that archive path first. |
| WriteWarningMessage | `public void WriteWarningMessage(ILogWriter warningReport, string inMessage)` | Writes a message to the warning log and increments the run's warning count. |
| WriteAddressMessage | `public void WriteAddressMessage(ILogWriter addressReport, string inMessage)` | Writes a message to the address-exception log and increments the run's address-exceptions count. |
| WriteLog | `public void WriteLog(ILogWriter reportLog, string inMessage)` | Writes a single line to the supplied log writer. |

## Behavioral notes
- `ProcessEligibilityExtract` wraps the entire run in `IUnitOfWork.Begin()`/`Commit()`.
- The extract file path is a hardcoded local filesystem path (`c:\downloads\CMN0008_ExtractPharmEligibility\SAIFODSPharmacyEligibility`) marked in source as needing to become configuration.
- Group ID assignment in `WriteExtract` is driven by a fixed switch over eligibility-status codes (`FirstFill`→`001`, `Extended`→`002`, `Limited`→`003`, `Majoris`→`010`, `CMC`→`011`, `CMCLimited`→`012`, `MajorisLimited`→`013`, default→blank).
- Foreign-address workers receive a substituted SAIF department mailing address (constants `Saifaddressline1`/`2`, city, state, zip) instead of their actual address in the extract.

## Sources
- Services/SAIF.Services/Claims/PharmacyElilgbility/PharmacyEligibilityService.cs
