Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Create Person Resource #574

@wintermeyer

Description

@wintermeyer

Background

In our medical information system, a Person represents an individual entity such as a patient, doctor, or caregiver. Each Person is associated with an Entity and contains attributes that define their identity, status, and role within the system.

Proposal

Implement a Person resource in Ash Framework to manage individual identities. This resource will:

  • Be linked to the Entity resource.
  • Store key personal information such as name, gender, and date of birth.
  • Support indexing for efficient querying.
  • Automatically delete associated records when a Person is removed.

Overview

To understand how Person fits within the system, here’s the ER diagram:

erDiagram
    Entity {
        INTEGER id
        ENUM type
        VARCHAR name
    }

    Person {
        INTEGER id
        INTEGER entity_id
        VARCHAR first_name
        VARCHAR last_name
        ENUM gender
        DATE date_of_birth
        DATE date_of_death
        VARCHAR nationality
        VARCHAR occupation
        ENUM marital_status
        VARCHAR social_security_number
        VARCHAR status
        VARCHAR veka_number
        DATE veka_expiration_date
        VARCHAR invoice_type
        BOOLEAN client_pays_share
        BOOLEAN client_exempt_share
        VARCHAR klv7_services
        BOOLEAN klv7_direct_billing
        BOOLEAN klv7_client_billing
        DATE first_contact_date
    }

    Entity ||--o{ Person : "has"
Loading

Visit #566 for the big picture.

Example Data

Person Table (linked to Entity)

id entity_id first_name last_name gender date_of_birth
1 1 Anna Meier female 1975-06-12
2 3 Thomas Schmidt male 1988-03-05
3 4 Peter Müller male 1995-11-23

Example Implementation

Warning

This is untested code. Stuff is missing. Please treat it as pseudo code and validate it before using it in production.

defmodule Omedis.Person do
  use Ash.Resource, data_layer: AshPostgres.DataLayer

  postgres do
    table "persons"
    repo Omedis.Repo

    indexes do
      index [:last_name, :first_name], unique: false
    end
  end

  attributes do
    attribute :date_of_death, :date
    attribute :nationality, :string
    attribute :occupation, :string
    attribute :marital_status, :atom, constraints: [one_of: [:single, :married, :divorced, :widowed, :other]]
    attribute :social_security_number, :string
    attribute :status, :string
    attribute :veka_number, :string
    attribute :veka_expiration_date, :date
    attribute :invoice_type, :string
    attribute :client_pays_share, :boolean
    attribute :client_exempt_share, :boolean
    attribute :klv7_services, :string
    attribute :klv7_direct_billing, :boolean
    attribute :klv7_client_billing, :boolean
    attribute :first_contact_date, :date
    attribute :first_name, :string, allow_nil?: false, constraints: [max_length: 255]
    attribute :last_name, :string, allow_nil?: false, constraints: [max_length: 255]
    attribute :gender, :atom, allow_nil?: false, constraints: [one_of: [:male, :female, :non_binary, :other]]
    attribute :date_of_birth, :date, allow_nil?: false
  end

  relationships do
    unique :entity_id # Ensures that an entity can only be linked to a single person, preventing duplicate associations
    belongs_to :entity, Omedis.Entity,
      source_attribute: :id,
      destination_attribute: :entity_id,
      on_destroy: :destroy!
  end

  validations do
    validate_length(:first_name, min: 1, max: 255)
    validate_length(:last_name, min: 1, max: 255)
  end
end

Thoughts

I am not 100% sure if it makes sense to auto delete the entity when the person is deleted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions