Skip to content

Commit

Permalink
Data model supports multiple providers and default provider (#8271)
Browse files Browse the repository at this point in the history
* model supports multiple providers and default provider

* grant metabase access to facility providers table

* Rework liquibase changes to be backwards compatible

* minimize diff

* accidental delete

* remove test change

* switch to just using getorderingprovider method

* nvm

* don't fail tests using invalid null provider facilities
  • Loading branch information
mehansen authored Dec 16, 2024
1 parent a121ea2 commit cf673f9
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.validation.constraints.NotEmpty;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -34,10 +36,12 @@ public class Facility extends OrganizationScopedEternalEntity implements Located

@ManyToOne(optional = false)
@JoinColumn(name = "ordering_provider_id", nullable = false)
@Getter
@Setter
private Provider orderingProvider;

@ManyToOne
@JoinColumn(name = "default_ordering_provider_id")
private Provider defaultOrderingProvider;

@ManyToOne(optional = true, fetch = FetchType.EAGER)
@JoinColumn(name = "default_device_type_id")
@Getter
Expand All @@ -48,6 +52,14 @@ public class Facility extends OrganizationScopedEternalEntity implements Located
@Getter
private SpecimenType defaultSpecimenType;

@OneToMany
@JoinTable(
name = "facility_providers",
joinColumns = @JoinColumn(name = "facility_id"),
inverseJoinColumns = @JoinColumn(name = "provider_id"))
@NotEmpty(message = "Minimum 1 ordering provider is required")
private Set<Provider> orderingProviders = new HashSet<>();

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "facility_device_type",
Expand All @@ -66,11 +78,20 @@ public Facility(FacilityBuilder facilityBuilder) {
this.telephone = facilityBuilder.phone;
this.email = facilityBuilder.email;
this.orderingProvider = facilityBuilder.orderingProvider;
this.defaultOrderingProvider = facilityBuilder.orderingProvider;
this.orderingProviders.add(facilityBuilder.orderingProvider);
this.configuredDeviceTypes.addAll(facilityBuilder.configuredDevices);
this.setDefaultDeviceTypeSpecimenType(
facilityBuilder.defaultDeviceType, facilityBuilder.defaultSpecimenType);
}

public Provider getOrderingProvider() {
if (defaultOrderingProvider != null) {
return defaultOrderingProvider;
}
return orderingProviders.iterator().next();
}

public void setDefaultDeviceTypeSpecimenType(DeviceType deviceType, SpecimenType specimenType) {
if (deviceType != null) {
configuredDeviceTypes.add(deviceType);
Expand Down
97 changes: 91 additions & 6 deletions backend/src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5626,7 +5626,6 @@ databaseChangeLog:
references: role
- sql: |
GRANT SELECT ON ${database.defaultSchemaName}.api_user_role TO ${noPhiUsername};
- changeSet:
id: add-timer-started-at-to-test_order-table
author: [email protected]
Expand All @@ -5643,7 +5642,6 @@ databaseChangeLog:
- dropColumn:
tableName: test_order
columnName: timer_started_at

- changeSet:
id: add-hepatitis-c-to-supported_disease-table
author: [email protected]
Expand All @@ -5662,7 +5660,6 @@ databaseChangeLog:
rollback:
- sql:
sql: DELETE FROM ${database.defaultSchemaName}.supported_disease WHERE name = 'Hepatitis-C';

- changeSet:
id: update-hepatitis-c-naming-in-supported-disease-table
author: [email protected]
Expand All @@ -5681,7 +5678,6 @@ databaseChangeLog:
UPDATE ${database.defaultSchemaName}.supported_disease
SET name = 'Hepatitis-C'
WHERE name = 'Hepatitis C' AND loinc = 'LP14400-3'
- changeSet:
id: add-gonorrhea-to-supported_disease-table
author: [email protected]
Expand All @@ -5700,7 +5696,6 @@ databaseChangeLog:
rollback:
- sql:
sql: DELETE FROM ${database.defaultSchemaName}.supported_disease WHERE name = 'Gonorrhea';

- changeSet:
id: add-chlamydia-to-supported_disease-table
author: [email protected]
Expand All @@ -5718,4 +5713,94 @@ databaseChangeLog:
(gen_random_uuid(), 'Chlamydia', 'LP14298-1')
rollback:
- sql:
sql: DELETE FROM ${database.defaultSchemaName}.supported_disease WHERE name = 'Chlamydia';
sql: DELETE FROM ${database.defaultSchemaName}.supported_disease WHERE name = 'Chlamydia';
- changeSet:
id: create-facility_providers-table
author: [email protected]
changes:
- tagDatabase:
tag: add-facility-providers-table
- createTable:
tableName: facility_providers
remarks: Join table for mapping multiple providers to a facility.
columns:
- column:
name: facility_id
type: uuid
constraints:
nullable: false
foreignKeyName: fk__facility_providers__facility
references: facility
- column:
name: provider_id
type: uuid
constraints:
nullable: false
foreignKeyName: fk__facility_providers__provider
references: provider
- changeSet:
id: grant-facility_providers-access-to-metabase-user
author: [email protected]
changes:
- sql:
sql: |
GRANT SELECT ON ${database.defaultSchemaName}.facility_providers TO ${noPhiUsername};
rollback:
- sql:
sql: |
REVOKE SELECT ON TABLE ${database.defaultSchemaName}.facility_providers FROM ${noPhiUsername};
- changeSet:
id: add-default-provider
author: [email protected]
changes:
- tagDatabase:
tag: add-default-provider-col
- addColumn:
tableName: facility
columns:
- column:
name: default_ordering_provider_id
remarks: The default ordering provider for tests at this facility.
type: uuid
constraints:
foreignKeyName: fk__facility__default_ordering_provider
references: provider(internal_id)
- changeSet:
id: populate-facility-providers-table
author: [email protected]
changes:
- tagDatabase:
tag: populate-facility-providers-table
- sql:
sql: |
INSERT INTO ${database.defaultSchemaName}.facility_providers (
facility_id,
provider_id
)
SELECT
internal_id,
ordering_provider_id
FROM ${database.defaultSchemaName}.facility;
rollback:
- sql:
sql: |
TRUNCATE TABLE ${database.defaultSchemaName}.facility_providers;
- changeSet:
id: populate-default-provider-column
author: [email protected]
changes:
- tagDatabase:
tag: populate-default-provider-column
- sql:
sql: |
UPDATE ${database.defaultSchemaName}.facility
SET
default_ordering_provider_id = facility.ordering_provider_id
WHERE facility.default_ordering_provider_id is NULL;
rollback:
- sql:
sql: |
UPDATE ${database.defaultSchemaName}.facility
SET
default_ordering_provider_id = NULL
WHERE facility.default_ordering_provider_id is NOT NULL;

0 comments on commit cf673f9

Please sign in to comment.