Skip to content

Commit

Permalink
Addign person entity to the system
Browse files Browse the repository at this point in the history
  • Loading branch information
samisa-abeysinghe committed Oct 4, 2022
1 parent 66c3810 commit 16c8f69
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 1 deletion.
22 changes: 21 additions & 1 deletion api/organization_data.bal
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public distinct service class OrganizationData {
string _name = "%" + (name ?: "") + "%";
int id = organization_id ?: 0;

string query_string = "";
Organization org_raw;
if(id > 0) { // organization_id provided, give precedance to that
org_raw = check db_client -> queryRow(
Expand Down Expand Up @@ -100,4 +99,25 @@ public distinct service class OrganizationData {

return parent_orgs;
}

resource function get persons() returns PersonData[]|error? {
// Get list of child organizations
stream<Person, error?> people = db_client->query(
`SELECT *
FROM avinya_db.person
WHERE organization_id = ${self.organization.id}`
);

PersonData[] peopleData = [];

check from Person person in people
do {
PersonData|error personData = new PersonData((), 0, person);
if !(personData is error) {
peopleData.push(personData);
}
};

return peopleData;
}
}
113 changes: 113 additions & 0 deletions api/person_data.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@


public distinct service class PersonData {
private Person person;

isolated function init(string? name = null, int? person_id = 0, Person? person = null) returns error? {
if(person != null) { // if roganization is provided, then use that and do not load from DB
self.person = person.cloneReadOnly();
return;
}

string _name = "%" + (name ?: "") + "%";
int id = person_id ?: 0;

Person person_raw;
if(id > 0) { // organization_id provided, give precedance to that
person_raw = check db_client -> queryRow(
`SELECT *
FROM avinya_db.person
WHERE
id = ${id};`);
} else
{
person_raw = check db_client -> queryRow(
`SELECT *
FROM avinya_db.person
WHERE
name_en LIKE ${_name};`);
}

self.person = person_raw.cloneReadOnly();

}

resource function get preferred_name() returns string?{
return self.person.preferred_name;
}

resource function get full_name() returns string?{
return self.person.full_name;
}

resource function get date_of_birth() returns string?{
return self.person.date_of_birth;
}

resource function get sex() returns string?{
return self.person.sex;
}

resource function get asgardeo_id() returns string?{
return self.person.asgardeo_id;
}

isolated resource function get permanent_address() returns AddressData|error? {
int id = self.person.permanent_address_id ?: 0;
if( id == 0) {
return null; // no point in querying if address id is null
}

return new AddressData(id);
}

isolated resource function get mailing_address() returns AddressData|error? {
int id = self.person.mailing_address_id ?: 0;
if( id == 0) {
return null; // no point in querying if address id is null
}

return new AddressData(id);
}

resource function get phone() returns int? {
return self.person.phone;
}

resource function get organization() returns OrganizationData|error? {
int id = self.person.organization_id ?: 0;
if(id == 0) {
return null; // no point in querying if avinya type is null
}
return new OrganizationData((), id);
}

resource function get avinya_type() returns AvinyaTypeData|error? {
int id = self.person.avinya_type_id ?: 0;
if(id == 0) {
return null; // no point in querying if avinya type is null
}
return new AvinyaTypeData(id);
}

resource function get notes() returns string?{
return self.person.notes;
}

resource function get nic_no() returns string?{
return self.person.nic_no;
}

resource function get passport_no() returns string?{
return self.person.passport_no;
}

resource function get id_no() returns string?{
return self.person.id_no;
}

resource function get email() returns string?{
return self.person.email;
}

}
20 changes: 20 additions & 0 deletions api/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,23 @@ type ParentChildOrganization record {|
int child_org_id;
int parent_org_id;
|};

public type Person record {|
readonly string record_type = "person";
int id?;
string? preferred_name;
string? full_name;
string? date_of_birth;
string? sex;
string? asgardeo_id;
int? permanent_address_id;
int? mailing_address_id;
int? phone;
int? organization_id;
int? avinya_type_id;
string? notes;
string? nic_no;
string? passport_no;
string? id_no;
string? email;
|};

0 comments on commit 16c8f69

Please sign in to comment.