diff --git a/api/Dependencies.toml b/api/Dependencies.toml index 588f7a5..6867e2a 100644 --- a/api/Dependencies.toml +++ b/api/Dependencies.toml @@ -15,6 +15,7 @@ dependencies = [ {org = "ballerina", name = "graphql"}, {org = "ballerina", name = "io"}, {org = "ballerina", name = "log"}, + {org = "ballerina", name = "regex"}, {org = "ballerina", name = "sql"}, {org = "ballerina", name = "test"}, {org = "ballerina", name = "time"}, @@ -112,7 +113,7 @@ modules = [ [[package]] org = "ballerina" name = "http" -version = "2.10.16" +version = "2.10.17" dependencies = [ {org = "ballerina", name = "auth"}, {org = "ballerina", name = "cache"}, @@ -313,6 +314,18 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"} ] +[[package]] +org = "ballerina" +name = "regex" +version = "1.4.3" +dependencies = [ + {org = "ballerina", name = "jballerina.java"}, + {org = "ballerina", name = "lang.string"} +] +modules = [ + {org = "ballerina", packageName = "regex", moduleName = "regex"} +] + [[package]] org = "ballerina" name = "sql" diff --git a/api/calendar_meta_data.bal b/api/calendar_meta_data.bal new file mode 100644 index 0000000..0edc892 --- /dev/null +++ b/api/calendar_meta_data.bal @@ -0,0 +1,51 @@ +public isolated service class CalendarMetaData { + + private CalendarMetadata calendar_metadata; + + isolated function init(int? id=0, CalendarMetadata? calendarMetadata = null) returns error? { + + if (calendarMetadata != null) { + self.calendar_metadata = calendarMetadata.cloneReadOnly(); + return; + } + + lock { + + CalendarMetadata calendar_metadata_raw; + + if (id>0) { + + calendar_metadata_raw = check db_client->queryRow( + `SELECT * + FROM calendar_metadata + WHERE id = ${id};`); + + } else { + return error("No id provided"); + } + + self.calendar_metadata = calendar_metadata_raw.cloneReadOnly(); + + } + + } + + isolated resource function get id() returns int?|error { + lock { + return self.calendar_metadata.id; + } + } + + isolated resource function get organization_id() returns int?|error { + lock { + return self.calendar_metadata.organization_id; + } + } + + isolated resource function get monthly_payment_amount() returns decimal?|error { + lock { + return self.calendar_metadata.monthly_payment_amount; + } + } + +} diff --git a/api/main.bal b/api/main.bal index ff6a227..5b812de 100644 --- a/api/main.bal +++ b/api/main.bal @@ -5879,30 +5879,30 @@ AND p.organization_id IN ( isolated resource function get cities(int? district_id) returns CityData[]|error? { stream cities_data; - if(district_id !=null && district_id != 0 && district_id > 0){ + if (district_id != null && district_id != 0 && district_id > 0) { - lock { - cities_data = db_client->query( + lock { + cities_data = db_client->query( `SELECT * from city where district_id=${district_id};`); - } + } - CityData[] cityDatas = []; + CityData[] cityDatas = []; - check from City city_data_record in cities_data - do { - CityData|error cityData = new CityData(null, 0,city_data_record); - if !(cityData is error) { - cityDatas.push(cityData); - } - }; + check from City city_data_record in cities_data + do { + CityData|error cityData = new CityData(null, 0, city_data_record); + if !(cityData is error) { + cityDatas.push(cityData); + } + }; - check cities_data.close(); - return cityDatas; - }else{ - return error("Provide valid value for district_id."); - } + check cities_data.close(); + return cityDatas; + } else { + return error("Provide valid value for district_id."); + } } isolated resource function get all_organizations() returns OrganizationData[]|error? { @@ -5928,6 +5928,135 @@ AND p.organization_id IN ( return organizationDatas; } + remote function add_monthly_leave_dates(MonthlyLeaveDates monthly_leave_dates) returns MonthlyLeaveDatesData|error? { + + string leaveDatesString = ""; + int totalLeaveDates = 0; + decimal dailyAmount = 0.0; + + MonthlyLeaveDates|error? monthlyLeaveDatesRaw = db_client->queryRow( + `SELECT * + FROM monthly_leave_dates + WHERE year = ${monthly_leave_dates.year} and month = ${monthly_leave_dates.month} and + organization_id = ${monthly_leave_dates.organization_id};` + ); + + if ( monthlyLeaveDatesRaw is MonthlyLeaveDates) { + return error("A record for this organization id already exists for the same year and month."); + } + + + int[] leaveDates = monthly_leave_dates.leave_dates_list; + + foreach int date in leaveDates { + leaveDatesString = leaveDatesString + date.toString() + ","; + totalLeaveDates = totalLeaveDates+1; + } + + if(leaveDatesString.length() > 0){ + leaveDatesString = leaveDatesString.substring(0,leaveDatesString.length()-1); + } + + + int? totalSchoolDays = monthly_leave_dates.total_days_in_month - totalLeaveDates; + + + CalendarMetadata|error? monthlyPaymentAmount = check db_client->queryRow( + `SELECT * + FROM calendar_metadata + WHERE organization_id = ${monthly_leave_dates.organization_id};` + ); + + if (monthlyPaymentAmount is CalendarMetadata) { + dailyAmount = monthlyPaymentAmount.monthly_payment_amount/totalSchoolDays ?: 0.0; + } + + + sql:ExecutionResult res = check db_client->execute( + `INSERT INTO monthly_leave_dates ( + year, + month, + organization_id, + leave_dates, + daily_amount + ) VALUES ( + ${monthly_leave_dates.year}, + ${monthly_leave_dates.month}, + ${monthly_leave_dates.organization_id}, + ${leaveDatesString}, + ${dailyAmount} + );` + ); + + int|string? insert_id = res.lastInsertId; + if !(insert_id is int) { + return error("Unable to insert Monthly Leave Dates record"); + } + + return new (insert_id); + } + + remote function update_monthly_leave_dates(MonthlyLeaveDates monthly_leave_dates) returns MonthlyLeaveDatesData|error? { + + int id = monthly_leave_dates.id ?: 0; + if (id == 0) { + return error("Unable to update Monthly Leave Dates record"); + } + + string leaveDatesString = ""; + int totalLeaveDates = 0; + decimal dailyAmount = 0.0; + + int[] leaveDates = monthly_leave_dates.leave_dates_list; + + foreach int date in leaveDates { + leaveDatesString = leaveDatesString + date.toString() + ","; + totalLeaveDates = totalLeaveDates+1; + } + + if(leaveDatesString.length() > 0){ + leaveDatesString = leaveDatesString.substring(0,leaveDatesString.length()-1); + } + + + int? totalSchoolDays = monthly_leave_dates.total_days_in_month - totalLeaveDates; + + + CalendarMetadata|error? monthlyPaymentAmount = check db_client->queryRow( + `SELECT * + FROM calendar_metadata + WHERE organization_id = ${monthly_leave_dates.organization_id};` + ); + + if (monthlyPaymentAmount is CalendarMetadata) { + dailyAmount = monthlyPaymentAmount.monthly_payment_amount/totalSchoolDays ?: 0.0; + } + + sql:ExecutionResult res = check db_client->execute( + `UPDATE monthly_leave_dates SET + year = ${monthly_leave_dates.year}, + month = ${monthly_leave_dates.month}, + organization_id = ${monthly_leave_dates.organization_id}, + leave_dates = ${leaveDatesString}, + daily_amount = ${dailyAmount} + WHERE id = ${id};` + ); + + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update Monthly Leave Dates record"); + } + + return new (id); + } + + isolated resource function get monthly_leave_dates_record_by_id(int? id) returns MonthlyLeaveDatesData|error? { + if (id != null) { + return new (id); + } else { + return error("Provide non-null value for id."); + } + } + } isolated function calculateWeekdays(time:Utc toDate, time:Utc fromDate) returns int { diff --git a/api/monthly_leave_dates.bal b/api/monthly_leave_dates.bal new file mode 100644 index 0000000..f92f72a --- /dev/null +++ b/api/monthly_leave_dates.bal @@ -0,0 +1,100 @@ +import ballerina/io; +import ballerina/regex; + +public isolated service class MonthlyLeaveDatesData { + + private MonthlyLeaveDates monthly_leave_dates; + + isolated function init(int? id = 0, MonthlyLeaveDates? monthlyLeaveDates = null) returns error? { + + if (monthlyLeaveDates != null) { + self.monthly_leave_dates = monthlyLeaveDates.cloneReadOnly(); + return; + } + + lock { + + MonthlyLeaveDates monthly_leave_dates_raw; + + if (id > 0) { + + monthly_leave_dates_raw = check db_client->queryRow( + `SELECT * + FROM monthly_leave_dates + WHERE id = ${id};`); + + } else { + return error("No id provided"); + } + + self.monthly_leave_dates = monthly_leave_dates_raw.cloneReadOnly(); + + } + + } + + isolated resource function get id() returns int?|error { + lock { + return self.monthly_leave_dates.id; + } + } + + isolated resource function get year() returns int?|error { + lock { + return self.monthly_leave_dates.year; + } + } + + isolated resource function get month() returns int?|error { + lock { + return self.monthly_leave_dates.month; + } + } + + isolated resource function get organization_id() returns int?|error { + lock { + return self.monthly_leave_dates.organization_id; + } + } + + isolated resource function get leave_dates_list() returns int[]?|error { + string[] strArray; + + lock{ + string? str = self.monthly_leave_dates.leave_dates; + + // Split the string by commas using string:split + strArray = regex:split(str ?: "",","); + } + // Convert the string array to an integer array + + int[] intArray = from var s in strArray + where s.trim() != "" + select check int:fromString(s); + + // Output the integer array + io:println(intArray); + + return intArray; + + } + + isolated resource function get daily_amount() returns decimal?|error { + lock { + return self.monthly_leave_dates.daily_amount; + } + } + + isolated resource function get created() returns string?|error { + lock { + return self.monthly_leave_dates.created; + } + } + + isolated resource function get updated() returns string?|error { + lock { + return self.monthly_leave_dates.updated; + } + } + +} diff --git a/api/types.bal b/api/types.bal index a942b7c..ead0c67 100644 --- a/api/types.bal +++ b/api/types.bal @@ -689,3 +689,24 @@ public type VehicleFuelConsumption record{| string? created; string? updated; |}; + +public type CalendarMetadata record {| + readonly string? record_type = "calendar_metadata"; + int id?; + int? organization_id; + decimal? monthly_payment_amount; +|}; + +public type MonthlyLeaveDates record {| + readonly string? record_type = "monthly_leave_dates"; + int id?; + int? year; + int? month; + int? total_days_in_month; + int? organization_id; + int[] leave_dates_list; + string? leave_dates; + decimal? daily_amount; + string? created; + string? updated; +|};