Skip to content

Commit

Permalink
Merge pull request #164 from lahirulakruwan/main
Browse files Browse the repository at this point in the history
Update person api new changes added
  • Loading branch information
YujithIsura authored Oct 1, 2024
2 parents 092ae28 + af74bfa commit 6d867e6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion api/geo_data.bal
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public isolated service class AddressData {
}
}

isolated resource function get street_address() returns string {
isolated resource function get street_address() returns string? {
lock {
return self.address.street_address;
}
Expand Down
37 changes: 27 additions & 10 deletions api/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -5551,7 +5551,8 @@ AND p.organization_id IN (
}
}

remote function update_person(Person person,Address? permanent_address,Address? mailing_address) returns PersonData|error? {
remote function update_person(Person person,Address? permanent_address,City? permanent_address_city,Address? mailing_address,City? mailing_address_city) returns PersonData|error? {


//starting the transaction
boolean first_db_transaction_fail = false;
Expand All @@ -5566,8 +5567,8 @@ AND p.organization_id IN (

transaction {


int permanent_address_id = permanent_address?.id ?: 0;
City permanent_address_city = <City>permanent_address?.city;

Address|error? permanent_address_raw = db_client->queryRow(
`SELECT *
Expand All @@ -5578,11 +5579,14 @@ AND p.organization_id IN (
if (permanent_address_raw is Address) {
io:println("Permanent Address is already exists!");

if(permanent_address != null && permanent_address?.street_address != null &&
permanent_address_city !=null && permanent_address_city?.id !=null) {

permanent_address_res = check db_client->execute(
`UPDATE address SET
street_address = ${permanent_address?.street_address},
phone = ${permanent_address?.phone},
city_id = ${permanent_address_city.id}
city_id = ${permanent_address_city?.id}
WHERE id = ${permanent_address_id};`);

permanent_address_insert_id = permanent_address_id;
Expand All @@ -5591,9 +5595,13 @@ AND p.organization_id IN (
first_db_transaction_fail = true;
io:println("Unable to update permanent address record");
}
}

}else{

if(permanent_address != null && permanent_address?.street_address != null &&
permanent_address_city !=null && permanent_address_city?.id !=null){

permanent_address_res = check db_client->execute(
`INSERT INTO address(
street_address,
Expand All @@ -5602,20 +5610,21 @@ AND p.organization_id IN (
) VALUES(
${permanent_address?.street_address},
${permanent_address?.phone},
${permanent_address_city.id}
${permanent_address_city?.id}
);`
);


permanent_address_insert_id = permanent_address_res.lastInsertId;

if !(permanent_address_insert_id is int) {
first_db_transaction_fail = true;
io:println("Unable to insert permanent address");
}
}
}

int mailing_address_id = mailing_address?.id ?: 0;
City mailing_address_city = <City>mailing_address?.city;

Address|error? mailing_address_raw = db_client->queryRow(
`SELECT *
Expand All @@ -5626,12 +5635,15 @@ AND p.organization_id IN (
if (mailing_address_raw is Address) {

io:println("Mailing Address is already exists!");

if(mailing_address != null && mailing_address?.street_address != null &&
mailing_address_city !=null && mailing_address_city?.id !=null) {

mailing_address_res = check db_client->execute(
`UPDATE address SET
street_address = ${mailing_address?.street_address},
phone = ${mailing_address?.phone},
city_id = ${mailing_address_city.id}
city_id = ${mailing_address_city?.id}
WHERE id = ${mailing_address_id};`);

mailing_address_insert_id = mailing_address_id;
Expand All @@ -5640,9 +5652,13 @@ AND p.organization_id IN (
second_db_transaction_fail = true;
io:println("Unable to update mailing address record");
}
}

}else{

if(mailing_address != null && mailing_address?.street_address != null &&
mailing_address_city !=null && mailing_address_city?.id !=null) {

mailing_address_res = check db_client->execute(
`INSERT INTO address(
street_address,
Expand All @@ -5651,7 +5667,7 @@ AND p.organization_id IN (
) VALUES(
${mailing_address?.street_address},
${mailing_address?.phone},
${mailing_address_city.id}
${mailing_address_city?.id}
);`
);

Expand All @@ -5661,6 +5677,7 @@ AND p.organization_id IN (
second_db_transaction_fail = true;
io:println("Unable to insert mailing address");
}
}

}

Expand Down Expand Up @@ -5706,7 +5723,7 @@ AND p.organization_id IN (
third_db_transaction_fail) {

rollback;
return error("Transaction rollback successfully!");
return error("Transaction rollback");
}else{

// Commit the transaction if three updates are successful
Expand Down Expand Up @@ -5897,7 +5914,7 @@ isolated function updateDutyParticipantsWorkRotation(DutyParticipant[] dutyParti

foreach DutyParticipant activityObject in dutyParticipantsArray {

if (activityObject.role == "member") {
//if (activityObject.role == "member") {

int? currentIndex = dynamicDutyActivitiesArray.indexOf(activityObject.activity_id);
int? nextIndex = (currentIndex + 1) % dynamicDutyActivitiesArray.length();
Expand All @@ -5922,7 +5939,7 @@ isolated function updateDutyParticipantsWorkRotation(DutyParticipant[] dutyParti
}

}
}
//}
}

}
10 changes: 5 additions & 5 deletions api/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
# + name_ta - Name in Tamil, தமிழ்
# + name_si - Name in Sinhala, සිංහල
public type LocalizedName record {
string name_en;
string? name_en;
string? name_ta;
string? name_si;
};

type GeospatialInformation record {|
decimal latitude;
decimal longitude;
decimal? latitude;
decimal? longitude;
|};

public type Province record {|
Expand Down Expand Up @@ -47,9 +47,9 @@ public type Address record {
readonly string? record_type = "address";
int id?;
//*LocalizedName;
string street_address;
string? street_address;
int? phone;
int city_id;
int? city_id;
City? city;
};

Expand Down

0 comments on commit 6d867e6

Please sign in to comment.