Skip to content

Commit

Permalink
Implemented Professionals Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TanookiVerde committed Jun 24, 2024
1 parent e044cd9 commit 187fe16
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 43 deletions.
26 changes: 16 additions & 10 deletions api/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,29 +283,35 @@ class Meta:
table = "meta__tableinitialization"


class HealthCareRoleFamily(Model):
class OccupationFamily(Model):
code = fields.CharField(pk=True, max_length=4)
name = fields.CharField(max_length=512)


class ProfessionalRegistry(Model):
code = fields.CharField(pk=True, max_length=16)
type = fields.CharField(max_length=12)
professional = fields.ForeignKeyField("app.HealthCareProfessional", related_name="crm_professional")
type = fields.CharField(max_length=12, null=True)
professional = fields.ForeignKeyField("app.HealthCareProfessional", related_name="professional_registry")


class HealthCareRole(Model):
class Occupation(Model):
cbo = fields.CharField(max_length=6, pk=True)
family = fields.ForeignKeyField("app.HealthCareRoleFamily", related_name="role_family")
family = fields.ForeignKeyField("app.OccupationFamily", related_name="role_family")
description = fields.CharField(max_length=512)
professionals = fields.ReverseRelation["HealthCareProfessional"]


class HealthCareProfessional(Model):
id_sus = fields.CharField(pk=True, max_length=16)
name = fields.CharField(max_length=512)
cns = fields.CharField(max_length=16, index=True)
cpf = fields.CharField(max_length=11, index=True, validators=[CPFValidator()])
role = fields.ForeignKeyField("app.HealthCareRole", related_name="professional_role")
cpf = fields.CharField(max_length=11, index=True, null=True, validators=[CPFValidator()])
roles = fields.ManyToManyField("app.Occupation", related_name="professional_roles")
created_at = fields.DatetimeField(auto_now_add=True)
updated_at = fields.DatetimeField(auto_now=True)
updated_at = fields.DatetimeField(auto_now=True)

class HealthCareProfessionalOccupation(Model):
professional = fields.ForeignKeyField("app.HealthCareProfessional", related_name="professional_occcupation")
role = fields.ForeignKeyField("app.Occupation", related_name="professional_role")

class Meta:
table = "healthcareprofessional_occupation"
unique_together = ("professional", "role")
56 changes: 35 additions & 21 deletions api/app/routers/entities_mrg.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
MergedPatientTelecom,
MergedPatientCns,
HealthCareProfessional,
HealthCareRole,
ProfessionalRegistry
ProfessionalRegistry,
HealthCareProfessionalOccupation
)
from app.utils import get_instance

Expand Down Expand Up @@ -225,46 +225,60 @@ async def create_or_update_professionals(

# Index by ID_SUS
professionals_indexed = {
p.id_profissional_sus: p
for p in professionals.dict(exclude_none=True)
p.id_profissional_sus: p.dict(exclude_none=True)
for p in professionals
}

# Insert Professionals
professionals_inserts = []
for id_sus, professional in professionals_indexed.keys():
for id_sus, professional in professionals_indexed.items():
professional['id_sus'] = professional.pop('id_profissional_sus')
professional['name'] = professional.pop('nome')
professionals_inserts.append(HealthCareProfessional(**professional))
await HealthCareProfessional.bulk_create(professionals_inserts, batch_size=500)
await HealthCareProfessional.bulk_create(
professionals_inserts,
batch_size=500,
on_conflict=["id_sus"],
update_fields=["name", "cpf","cns"]
)

# Retrieve Inserted Professionals
professionals = await HealthCareProfessional.filter(
id_sus__not_in=list(professionals_indexed.keys())
id_sus__in=list(professionals_indexed.keys())
)

# Insert Roles
role_inserts = []
# Insert Health Care Professionals Occupations
occupation_inserts = []
for id_sus, professional in zip(list(professionals_indexed.keys()), professionals):
for cbo in professionals_indexed[id_sus]['id_cbo_lista']:
role_inserts.append(
HealthCareRole(
professional_id=professional.id,
cbo=cbo,
family_id=cbo[:2]
occupation_inserts.append(
HealthCareProfessionalOccupation(
professional_id=professional.id_sus,
role_id=cbo
)
)
await HealthCareRole.bulk_create(role_inserts, batch_size=500)
await HealthCareProfessionalOccupation.bulk_create(
occupation_inserts,
batch_size=500,
ignore_conflicts=True
)

# Insert Professional Registry
# Insert Health Care Professional Registry
registry_inserts = []
for id_sus, professional in zip(list(professionals_indexed.keys()), professionals):
for registry in professionals_indexed[id_sus]['id_registro_conselho_lista']:
registry_list = professionals_indexed[id_sus].get('id_registro_conselho_lista', [])
for registry in registry_list:
registry_inserts.append(
ProfessionalRegistry(
professional_id=professional.id,
code=registry
professional_id=professional.id_sus,
code=registry,
type=None
)
)
await ProfessionalRegistry.bulk_create(registry_inserts, batch_size=500)
await ProfessionalRegistry.bulk_create(
registry_inserts,
batch_size=500,
ignore_conflicts=True
)

return 0
return len(professionals)
8 changes: 4 additions & 4 deletions api/data/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@
"version": 1
},
{
"entity_model_name": "HealthCareRoleFamily",
"table_name": "healthcarerolefamily",
"entity_model_name": "OccupationFamily",
"table_name": "occupationfamily",
"source_csv_name": "./data/ocupacao_familias.csv",
"conflict_columns": ["code"],
"version": 1
},
{
"entity_model_name": "HealthCareRole",
"table_name": "healthcarerole",
"entity_model_name": "Occupation",
"table_name": "occupation",
"source_csv_name": "./data/ocupacao_cbo.csv",
"conflict_columns": ["cbo"],
"version": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@

async def upgrade(db: BaseDBAsyncClient) -> str:
return """
CREATE TABLE IF NOT EXISTS "healthcarerolefamily" (
CREATE TABLE IF NOT EXISTS "occupationfamily" (
"code" VARCHAR(4) NOT NULL PRIMARY KEY,
"name" VARCHAR(512) NOT NULL
);
CREATE TABLE IF NOT EXISTS "healthcarerole" (
CREATE TABLE IF NOT EXISTS "occupation" (
"cbo" VARCHAR(6) NOT NULL PRIMARY KEY,
"description" VARCHAR(512) NOT NULL,
"family_id" VARCHAR(4) NOT NULL REFERENCES "healthcarerolefamily" ("code") ON DELETE CASCADE
"family_id" VARCHAR(4) NOT NULL REFERENCES "occupationfamily" ("code") ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS "healthcareprofessional" (
"id_sus" VARCHAR(16) NOT NULL PRIMARY KEY,
"name" VARCHAR(512) NOT NULL,
"cns" VARCHAR(16) NOT NULL,
"cpf" VARCHAR(11) NOT NULL,
"cpf" VARCHAR(11),
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
"role_id" VARCHAR(6) NOT NULL REFERENCES "healthcarerole" ("cbo") ON DELETE CASCADE
"updated_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS "idx_healthcarep_cns_b9cb7c" ON "healthcareprofessional" ("cns");
CREATE INDEX IF NOT EXISTS "idx_healthcarep_cpf_3e22a9" ON "healthcareprofessional" ("cpf");
CREATE TABLE IF NOT EXISTS "healthcareprofessional_occupation" (
"id" SERIAL NOT NULL PRIMARY KEY,
"professional_id" VARCHAR(16) NOT NULL REFERENCES "healthcareprofessional" ("id_sus") ON DELETE CASCADE,
"role_id" VARCHAR(6) NOT NULL REFERENCES "occupation" ("cbo") ON DELETE CASCADE,
CONSTRAINT "uid_healthcarep_profess_18ff4d" UNIQUE ("professional_id", "role_id")
);
CREATE TABLE IF NOT EXISTS "professionalregistry" (
"code" VARCHAR(16) NOT NULL PRIMARY KEY,
"type" VARCHAR(12) NOT NULL,
Expand All @@ -33,6 +38,7 @@ async def upgrade(db: BaseDBAsyncClient) -> str:
async def downgrade(db: BaseDBAsyncClient) -> str:
return """
DROP TABLE IF EXISTS "healthcareprofessional";
DROP TABLE IF EXISTS "healthcarerole";
DROP TABLE IF EXISTS "healthcarerolefamily";
DROP TABLE IF EXISTS "healthcareprofessional_occupation";
DROP TABLE IF EXISTS "occupation";
DROP TABLE IF EXISTS "occupationfamily";
DROP TABLE IF EXISTS "professionalregistry";"""
11 changes: 11 additions & 0 deletions api/migrations/app/18_20240624191424_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
return """
ALTER TABLE "professionalregistry" ALTER COLUMN "type" DROP NOT NULL;"""


async def downgrade(db: BaseDBAsyncClient) -> str:
return """
ALTER TABLE "professionalregistry" ALTER COLUMN "type" SET NOT NULL;"""

0 comments on commit 187fe16

Please sign in to comment.