diff --git a/api/organization_data.bal b/api/organization_data.bal index c381fc9..85ed74d 100644 --- a/api/organization_data.bal +++ b/api/organization_data.bal @@ -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( @@ -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 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; + } } diff --git a/api/person_data.bal b/api/person_data.bal new file mode 100644 index 0000000..24f06e2 --- /dev/null +++ b/api/person_data.bal @@ -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; + } + +} diff --git a/api/types.bal b/api/types.bal index 49d3a76..d9b6afb 100644 --- a/api/types.bal +++ b/api/types.bal @@ -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; +|};