From 8003cffc6ba4ade42fe5088cb6f7c4b7f99130d7 Mon Sep 17 00:00:00 2001 From: lahirulakruwan Date: Fri, 3 Jan 2025 08:53:01 +0530 Subject: [PATCH] Student File upload changes added --- api/Config.toml | 4 + api/Dependencies.toml | 38 +++- api/documents_data.bal | 118 ++++++++++++ api/main.bal | 416 ++++++++++++++++++++++++++++++++++++++++- api/person_data.bal | 221 ++++++++++++++++++++++ api/types.bal | 35 ++++ 6 files changed, 819 insertions(+), 13 deletions(-) create mode 100644 api/documents_data.bal diff --git a/api/Config.toml b/api/Config.toml index 5751cbf..e4e3f14 100644 --- a/api/Config.toml +++ b/api/Config.toml @@ -3,3 +3,7 @@ HOST="localhost" PASSWORD="Admin@#123" PORT=3306 USER="root" +GOOGLEDRIVECLIENTID="" +GOOGLEDRIVECLIENTSECRET="" +GOOGLEDRIVEREFRESHTOKEN="" +GOOGLEDRIVEREFRESHURL= "" diff --git a/api/Dependencies.toml b/api/Dependencies.toml index 6867e2a..4e74cab 100644 --- a/api/Dependencies.toml +++ b/api/Dependencies.toml @@ -15,12 +15,14 @@ dependencies = [ {org = "ballerina", name = "graphql"}, {org = "ballerina", name = "io"}, {org = "ballerina", name = "log"}, + {org = "ballerina", name = "mime"}, {org = "ballerina", name = "regex"}, {org = "ballerina", name = "sql"}, {org = "ballerina", name = "test"}, {org = "ballerina", name = "time"}, {org = "ballerinai", name = "observe"}, {org = "ballerinai", name = "transaction"}, + {org = "ballerinax", name = "googleapis.drive"}, {org = "ballerinax", name = "mysql"}, {org = "ballerinax", name = "mysql.driver"} ] @@ -113,7 +115,7 @@ modules = [ [[package]] org = "ballerina" name = "http" -version = "2.10.17" +version = "2.10.19" dependencies = [ {org = "ballerina", name = "auth"}, {org = "ballerina", name = "cache"}, @@ -141,7 +143,7 @@ dependencies = [ [[package]] org = "ballerina" name = "io" -version = "1.6.1" +version = "1.6.3" dependencies = [ {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "lang.value"} @@ -283,6 +285,9 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "lang.int"} ] +modules = [ + {org = "ballerina", packageName = "mime", moduleName = "mime"} +] [[package]] org = "ballerina" @@ -395,7 +400,7 @@ dependencies = [ [[package]] org = "ballerina" name = "websocket" -version = "2.10.2" +version = "2.10.3" dependencies = [ {org = "ballerina", name = "auth"}, {org = "ballerina", name = "constraint"}, @@ -445,6 +450,33 @@ modules = [ {org = "ballerinai", packageName = "transaction", moduleName = "transaction"} ] +[[package]] +org = "ballerinax" +name = "client.config" +version = "1.0.1" +dependencies = [ + {org = "ballerina", name = "http"}, + {org = "ballerina", name = "oauth2"} +] + +[[package]] +org = "ballerinax" +name = "googleapis.drive" +version = "3.3.0" +dependencies = [ + {org = "ballerina", name = "file"}, + {org = "ballerina", name = "http"}, + {org = "ballerina", name = "io"}, + {org = "ballerina", name = "log"}, + {org = "ballerina", name = "os"}, + {org = "ballerina", name = "url"}, + {org = "ballerinai", name = "observe"}, + {org = "ballerinax", name = "client.config"} +] +modules = [ + {org = "ballerinax", packageName = "googleapis.drive", moduleName = "googleapis.drive"} +] + [[package]] org = "ballerinax" name = "mysql" diff --git a/api/documents_data.bal b/api/documents_data.bal new file mode 100644 index 0000000..ebe3d7e --- /dev/null +++ b/api/documents_data.bal @@ -0,0 +1,118 @@ +public isolated service class DocumentsData { + private UserDocument document_list; + + isolated function init(int? id = 0, UserDocument? documentList = null) returns error? { + + if (documentList != null) { + self.document_list = documentList.cloneReadOnly(); + return; + } + lock { + + UserDocument documents_raw; + + if (id > 0) { + + documents_raw = check db_client->queryRow( + `SELECT * + FROM user_documents + WHERE id = ${id};`); + + }else{ + return error("No id provided"); + } + self.document_list = documents_raw.cloneReadOnly(); + } + } + + isolated resource function get id() returns int?|error { + lock { + return self.document_list.id; + } + } + + isolated resource function get folder_id() returns string?|error { + lock{ + return self.document_list.folder_id; + } + } + + isolated resource function get nic_front_id() returns string?|error { + lock { + return self.document_list.nic_front_id; + } + } + + isolated resource function get nic_back_id() returns string?|error { + lock { + return self.document_list.nic_back_id; + } + } + + isolated resource function get birth_certificate_front_id() returns string?|error { + lock { + return self.document_list.birth_certificate_front_id; + } + } + + isolated resource function get birth_certificate_back_id() returns string?|error { + lock { + return self.document_list.birth_certificate_back_id; + } + } + + isolated resource function get ol_certificate_id() returns string?|error { + lock { + return self.document_list.ol_certificate_id; + } + } + + isolated resource function get al_certificate_id() returns string?|error { + lock { + return self.document_list.al_certificate_id; + } + } + + isolated resource function get additional_certificate_01_id() returns string?|error { + lock { + return self.document_list.additional_certificate_01_id; + } + } + + isolated resource function get additional_certificate_02_id() returns string?|error { + lock { + return self.document_list.additional_certificate_02_id; + } + } + + isolated resource function get additional_certificate_03_id() returns string?|error { + lock { + return self.document_list.additional_certificate_03_id; + } + } + + isolated resource function get additional_certificate_04_id() returns string?|error { + lock { + return self.document_list.additional_certificate_04_id; + } + } + + isolated resource function get additional_certificate_05_id() returns string?|error { + lock { + return self.document_list.additional_certificate_05_id; + } + } + + isolated resource function get document_type() returns string?|error { + lock { + return self.document_list.document_type; + } + } + + isolated resource function get document() returns string?|error { + lock { + return self.document_list.document; + } + } +} + diff --git a/api/main.bal b/api/main.bal index 9c1825c..33aaa01 100644 --- a/api/main.bal +++ b/api/main.bal @@ -3,11 +3,22 @@ import ballerina/io; import ballerina/log; import ballerina/sql; import ballerina/time; +import ballerinax/googleapis.drive as drive; +import ballerina/mime; + // @display { // label: "Global Data API", // id: "global-data" // } +configurable string GOOGLEDRIVECLIENTID = ?; +configurable string GOOGLEDRIVECLIENTSECRET = ?; +configurable string GOOGLEDRIVEREFRESHTOKEN = ?; +configurable string GOOGLEDRIVEREFRESHURL = ?; + + + + service /graphql on new graphql:Listener(4000) { resource function get geo() returns GeoData { return new (); @@ -5746,24 +5757,69 @@ AND p.organization_id IN ( } - remote function insert_person(Person person, Address? mailing_address, City? mailing_address_city) returns PersonData|error? { + + remote function insert_person(Person person,Address? mailing_address, City? mailing_address_city) returns PersonData|error?{ //starting the transaction boolean first_db_transaction_fail = false; boolean second_db_transaction_fail = false; boolean third_db_transaction_fail = false; + boolean fourth_db_transaction_fail = false; int|string? mailing_address_insert_id = null; string message = ""; + string orgFolderId = ""; + + drive:Client driveClient = check getDriveClient(); + + OrganizationFolderMapping|error orgFolderRaw = db_client->queryRow( + `SELECT * + FROM organization_folder_mapping orgFolMap + WHERE orgFolMap.organization_id = ${person.organization_id};` + ); + + if (orgFolderRaw is OrganizationFolderMapping) { + orgFolderId = orgFolderRaw.organization_folder_id ?: ""; + }else{ + return error(orgFolderRaw.message()); + } + + drive:File|error create_folder_response = driveClient->createFolder(person.nic_no.toString(),orgFolderId); + + string created_folder_id= ""; + + if (create_folder_response is drive:File) { + + created_folder_id = create_folder_response?.id.toString(); + + }else{ + return error(create_folder_response.message()); + } + + transaction { + + sql:ExecutionResult insert_user_document_folder_id = check db_client->execute( + `INSERT INTO user_documents( + folder_id + ) VALUES( + ${created_folder_id} + );` + ); + int|string? user_document_folder_id = insert_user_document_folder_id.lastInsertId; + + if !(user_document_folder_id is int) { + fourth_db_transaction_fail = true; + message = "Unable to insert folder id"; + } - transaction { Person|error? personRaw = db_client->queryRow( `SELECT * - FROM person - WHERE - nic_no = ${person.nic_no};` + FROM person p + left join organization o on o.id = p.organization_id + WHERE o.id = ${person.organization_id} and + p.nic_no = ${person.nic_no};` ); if (personRaw is Person) { @@ -5817,7 +5873,8 @@ AND p.organization_id IN ( bank_account_number, bank_account_name, bank_branch, - created_by + created_by, + documents_id ) VALUES ( ${person.preferred_name}, ${person.full_name}, @@ -5839,7 +5896,8 @@ AND p.organization_id IN ( ${person.bank_account_number}, ${person.bank_account_name}, ${person.bank_branch}, - ${person.created_by} + ${person.created_by}, + ${user_document_folder_id} );`); int|string? insert_person_id = insert_person_res.lastInsertId; @@ -5850,12 +5908,16 @@ AND p.organization_id IN ( message = "Unable to insert person"; } + if (first_db_transaction_fail || second_db_transaction_fail || - third_db_transaction_fail) { - + third_db_transaction_fail || + fourth_db_transaction_fail + ) { + rollback; - return error(message); + boolean|error deleted_document_folder_response = driveClient->deleteFile(created_folder_id); + return error(message); } else { // Commit the transaction if three updates are successful @@ -5867,6 +5929,317 @@ AND p.organization_id IN ( } + remote function upload_document(UserDocument user_document) returns DocumentsData|error?{ + + string uploadContent = user_document.document.toString(); + byte[] base64DecodedDocument = (check mime:base64Decode(uploadContent.toBytes())); + string userFolderId = ""; + string? nic_front_id = ""; + string? nic_back_id = ""; + string? birth_certificate_front_id = ""; + string? birth_certificate_back_id = ""; + string? ol_certificate_id = ""; + string? al_certificate_id = ""; + string? additional_certificate_01_id = ""; + string? additional_certificate_02_id = ""; + string? additional_certificate_03_id = ""; + string? additional_certificate_04_id = ""; + string? additional_certificate_05_id = ""; + + drive:Client driveClient = check getDriveClient(); + + UserDocument|error userDocumentRaw = db_client->queryRow( + `SELECT * + FROM user_documents userDoc + WHERE userDoc.id = ${user_document.id};` + ); + + if (userDocumentRaw is UserDocument) { + userFolderId = userDocumentRaw.folder_id ?: ""; + nic_front_id = userDocumentRaw.nic_front_id; + nic_back_id = userDocumentRaw.nic_back_id; + birth_certificate_front_id = userDocumentRaw.birth_certificate_front_id; + birth_certificate_back_id = userDocumentRaw.birth_certificate_back_id; + ol_certificate_id = userDocumentRaw.ol_certificate_id; + al_certificate_id = userDocumentRaw.al_certificate_id; + additional_certificate_01_id = userDocumentRaw.additional_certificate_01_id; + additional_certificate_02_id = userDocumentRaw.additional_certificate_02_id; + additional_certificate_03_id = userDocumentRaw.additional_certificate_03_id; + additional_certificate_04_id = userDocumentRaw.additional_certificate_04_id; + additional_certificate_05_id = userDocumentRaw.additional_certificate_05_id; + + }else{ + return error(userDocumentRaw.message()); + } + + drive:File|error create_file_response = + driveClient->uploadFileUsingByteArray(base64DecodedDocument,user_document.document_type.toString(),userFolderId); + + + if (create_file_response is drive:File) { + + string created_file_id = create_file_response?.id.toString(); + + if(user_document.document_type == "nicFront"){ + + + if(nic_front_id != null){ + + boolean|error deleted_nic_front_document_response = driveClient->deleteFile(nic_front_id); + + if (deleted_nic_front_document_response is boolean) { + log:printInfo("nic front document deleted."); + } else { + log:printError("Unable to delete nic front document."); + } + + } + sql:ExecutionResult res = check db_client->execute( + `UPDATE user_documents SET + nic_front_id = ${created_file_id} + WHERE id = ${user_document.id};` + ); + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update the nic front id for the user document record"); + } + } + if(user_document.document_type == "nicBack"){ + + if(nic_back_id != null){ + + boolean|error deleted_nic_back_document_response = driveClient->deleteFile(nic_back_id); + + if (deleted_nic_back_document_response is boolean) { + log:printInfo("nic back document deleted."); + } else { + log:printError("Unable to delete nic back document."); + } + + } + + sql:ExecutionResult res = check db_client->execute( + `UPDATE user_documents SET + nic_back_id = ${created_file_id} + WHERE id = ${user_document.id};` + ); + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update the nic back id for the user document record"); + } + } + if(user_document.document_type == "birthCertificateFront"){ + + if(birth_certificate_front_id != null){ + + boolean|error deleted_birth_certificate_front_document_response = driveClient->deleteFile(birth_certificate_front_id); + + if (deleted_birth_certificate_front_document_response is boolean) { + log:printInfo("birth certificate front document deleted."); + } else { + log:printError("Unable to delete birth certificate front document."); + } + + } + + sql:ExecutionResult res = check db_client->execute( + `UPDATE user_documents SET + birth_certificate_front_id = ${created_file_id} + WHERE id = ${user_document.id};` + ); + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update the birth certificate front id for the user document record"); + } + } + if(user_document.document_type == "birthCertificateBack"){ + + if(birth_certificate_back_id != null){ + + boolean|error deleted_birth_certificate_back_document_response = driveClient->deleteFile(birth_certificate_back_id); + + if (deleted_birth_certificate_back_document_response is boolean) { + log:printInfo("birth certificate back document deleted."); + } else { + log:printError("Unable to delete birth certificate back document."); + } + + } + + sql:ExecutionResult res = check db_client->execute( + `UPDATE user_documents SET + birth_certificate_back_id = ${created_file_id} + WHERE id = ${user_document.id};` + ); + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update the birth certificate back id for the user document record"); + } + } + if(user_document.document_type == "olDocument"){ + + if(ol_certificate_id != null){ + + boolean|error deleted_ol_certificate_document_response = driveClient->deleteFile(ol_certificate_id); + + if (deleted_ol_certificate_document_response is boolean) { + log:printInfo("ol certificate document deleted."); + } else { + log:printError("Unable to delete ol certificate document."); + } + + } + + sql:ExecutionResult res = check db_client->execute( + `UPDATE user_documents SET + ol_certificate_id = ${created_file_id} + WHERE id = ${user_document.id};` + ); + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update the ol id for the user document record"); + } + } + if(user_document.document_type == "alDocument"){ + + if(al_certificate_id != null){ + + boolean|error deleted_al_certificate_document_response = driveClient->deleteFile(al_certificate_id); + + if (deleted_al_certificate_document_response is boolean) { + log:printInfo("al certificate document deleted."); + } else { + log:printError("Unable to delete al certificate document."); + } + + } + + sql:ExecutionResult res = check db_client->execute( + `UPDATE user_documents SET + al_certificate_id = ${created_file_id} + WHERE id = ${user_document.id};` + ); + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update the al id for the user document record"); + } + } + if(user_document.document_type == "additionalCertificate01"){ + + if(additional_certificate_01_id != null){ + + boolean|error deleted_additional_certificate_01_document_response = driveClient->deleteFile(additional_certificate_01_id); + + if (deleted_additional_certificate_01_document_response is boolean) { + log:printInfo("additional certificate 01 document deleted."); + } else { + log:printError("Unable to delete additional certificate 01 document."); + } + + } + + sql:ExecutionResult res = check db_client->execute( + `UPDATE user_documents SET + additional_certificate_01_id = ${created_file_id} + WHERE id = ${user_document.id};` + ); + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update the additional certificate 01 id for the user document record"); + } + } + if(user_document.document_type == "additionalCertificate02"){ + + if(additional_certificate_02_id != null){ + + boolean|error deleted_additional_certificate_02_document_response = driveClient->deleteFile(additional_certificate_02_id); + + if (deleted_additional_certificate_02_document_response is boolean) { + log:printInfo("additional certificate 02 document deleted."); + } else { + log:printError("Unable to delete additional certificate 02 document."); + } + + } + + sql:ExecutionResult res = check db_client->execute( + `UPDATE user_documents SET + additional_certificate_02_id = ${created_file_id} + WHERE id = ${user_document.id};` + ); + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update the additional certificate 02 id for the user document record"); + } + } + if(user_document.document_type == "additionalCertificate03"){ + + if(additional_certificate_03_id != null){ + + boolean|error deleted_additional_certificate_03_document_response = driveClient->deleteFile(additional_certificate_03_id); + + if (deleted_additional_certificate_03_document_response is boolean) { + log:printInfo("additional certificate 03 document deleted."); + } else { + log:printError("Unable to delete additional certificate 03 document."); + } + + } + + sql:ExecutionResult res = check db_client->execute( + `UPDATE user_documents SET + additional_certificate_03_id = ${created_file_id} + WHERE id = ${user_document.id};` + ); + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update the additional certificate 03 id for the user document record"); + } + } + if(user_document.document_type == "additionalCertificate04"){ + + if(additional_certificate_04_id != null){ + + boolean|error deleted_additional_certificate_04_document_response = driveClient->deleteFile(additional_certificate_04_id); + + if (deleted_additional_certificate_04_document_response is boolean) { + log:printInfo("additional certificate 04 document deleted."); + } else { + log:printError("Unable to delete additional certificate 04 document."); + } + + } + + sql:ExecutionResult res = check db_client->execute( + `UPDATE user_documents SET + additional_certificate_04_id = ${created_file_id} + WHERE id = ${user_document.id};` + ); + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update the additional certificate 04 id for the user document record"); + } + } + if(user_document.document_type == "additionalCertificate05"){ + + if(additional_certificate_05_id != null){ + + boolean|error deleted_additional_certificate_05_document_response = driveClient->deleteFile(additional_certificate_05_id); + + if (deleted_additional_certificate_05_document_response is boolean) { + log:printInfo("additional certificate 05 document deleted."); + } else { + log:printError("Unable to delete additional certificate 05 document."); + } + + } + + sql:ExecutionResult res = check db_client->execute( + `UPDATE user_documents SET + additional_certificate_05_id = ${created_file_id} + WHERE id = ${user_document.id};` + ); + if (res.affectedRowCount == sql:EXECUTION_FAILED) { + return error("Unable to update the additional certificate 05 id for the user document record"); + } + } + }else{ + return error(create_file_response.message()); + } + + return new (user_document.id); + } + isolated resource function get districts() returns DistrictData[]|error? { stream districts_data; @@ -6110,8 +6483,31 @@ AND p.organization_id IN ( } } + // remote function onError(error err) returns http:Response { + // http:Response response = new; + // response.statusCode = 400; + // response.setPayload({message: err.message()}); + // return response; + // } + } +isolated function getDriveClient() returns drive:Client|error{ + + drive:ConnectionConfig config = { + auth: { + clientId: GOOGLEDRIVECLIENTID, + clientSecret: GOOGLEDRIVECLIENTSECRET, + refreshUrl:GOOGLEDRIVEREFRESHURL, + refreshToken: GOOGLEDRIVEREFRESHTOKEN + } + }; + drive:Client driveClient = check new (config); + return driveClient; +} + + + isolated function calculateWeekdays(time:Utc toDate, time:Utc fromDate) returns int { int weekdays = 0; time:Utc currentDate = fromDate; diff --git a/api/person_data.bal b/api/person_data.bal index b0b7d0e..eed6229 100644 --- a/api/person_data.bal +++ b/api/person_data.bal @@ -1,3 +1,5 @@ +import ballerinax/googleapis.drive; +import ballerina/mime; public isolated service class PersonData { @@ -308,4 +310,223 @@ public isolated service class PersonData { } } + isolated resource function get documents_id() returns int? { + lock { + return self.person.documents_id; + } + } + + isolated resource function get document_list() returns DocumentsData[]|error? { + int id = 0; + drive:Client driveClient = check getDriveClient(); + DocumentsData[] documents=[]; + UserDocument[] documentList=[]; + UserDocument user_document_list_raw; + + lock { + + id = self.person.documents_id ?: 0; + if (id == 0) { + return null; // no point in querying if document id is null + } + + user_document_list_raw = check db_client->queryRow( + `SELECT * + FROM user_documents + WHERE + id = ${id};`); + } + + string? nic_front_id = user_document_list_raw.nic_front_id; + string? nic_back_id = user_document_list_raw.nic_back_id; + string? birth_certificate_front_id = user_document_list_raw.birth_certificate_front_id; + string? birth_certificate_back_id = user_document_list_raw.birth_certificate_back_id; + string? ol_certificate_id = user_document_list_raw.ol_certificate_id; + string? al_certificate_id = user_document_list_raw.al_certificate_id; + string? additional_certificate_01_id = user_document_list_raw.additional_certificate_01_id; + string? additional_certificate_02_id = user_document_list_raw.additional_certificate_02_id; + string? additional_certificate_03_id = user_document_list_raw.additional_certificate_03_id; + string? additional_certificate_04_id = user_document_list_raw.additional_certificate_04_id; + string? additional_certificate_05_id = user_document_list_raw.additional_certificate_05_id; + + + if nic_front_id is string { + UserDocument|error nic_front_document = getDocument(driveClient,nic_front_id,"nicFront"); + if(nic_front_document is UserDocument){ + documentList.push(nic_front_document); + }else{ + return error(nic_front_document.message()); + } + } + // else { + // return error("Nic front document doesn't exist.Skipping file retrieval."); + // } + + if nic_back_id is string { + UserDocument|error nic_back_document = getDocument(driveClient,nic_back_id,"nicBack"); + if(nic_back_document is UserDocument){ + documentList.push(nic_back_document); + }else{ + return error(nic_back_document.message()); + } + } + // else { + // return error("Nic back document doesn't exist.Skipping file retrieval."); + // } + + if birth_certificate_front_id is string { + UserDocument|error birth_certificate_front_document = getDocument(driveClient,birth_certificate_front_id,"birthCertificateFront"); + if(birth_certificate_front_document is UserDocument){ + documentList.push(birth_certificate_front_document); + }else{ + return error(birth_certificate_front_document.message()); + } + } + // else { + // return error("Birth Certificate front document doesn't exist.Skipping file retrieval."); + // } + + if birth_certificate_back_id is string { + UserDocument|error birth_certificate_back_document = getDocument(driveClient,birth_certificate_back_id,"birthCertificateBack"); + if(birth_certificate_back_document is UserDocument){ + documentList.push(birth_certificate_back_document); + }else{ + return error(birth_certificate_back_document.message()); + } + } + // else { + // return error("Birth Certificate back document doesn't exist.Skipping file retrieval."); + // } + + if ol_certificate_id is string { + UserDocument|error ol_document = getDocument(driveClient,ol_certificate_id,"olDocument"); + if(ol_document is UserDocument){ + documentList.push(ol_document); + }else{ + return error(ol_document.message()); + } + } + // else { + // return error("OL document doesn't exist.Skipping file retrieval."); + // } + + if al_certificate_id is string { + UserDocument|error al_document = getDocument(driveClient,al_certificate_id,"alDocument"); + if(al_document is UserDocument){ + documentList.push(al_document); + }else{ + return error(al_document.message()); + } + } + // else { + // return error("AL document doesn't exist.Skipping file retrieval."); + // } + + if additional_certificate_01_id is string { + UserDocument|error additional_certificate_01_document = getDocument(driveClient,additional_certificate_01_id,"additionalCertificate01"); + if(additional_certificate_01_document is UserDocument){ + documentList.push(additional_certificate_01_document); + }else{ + return error(additional_certificate_01_document.message()); + } + } + // else { + // return error("Additional Certificate 01 doesn't exist.Skipping file retrieval."); + // } + + if additional_certificate_02_id is string { + UserDocument|error additional_certificate_02_document = getDocument(driveClient,additional_certificate_02_id,"additionalCertificate02"); + if(additional_certificate_02_document is UserDocument){ + documentList.push(additional_certificate_02_document); + }else{ + return error(additional_certificate_02_document.message()); + } + } + // else { + // return error("Additional Certificate 02 doesn't exist.Skipping file retrieval."); + // } + + if additional_certificate_03_id is string { + UserDocument|error additional_certificate_03_document = getDocument(driveClient,additional_certificate_03_id,"additionalCertificate03"); + if(additional_certificate_03_document is UserDocument){ + documentList.push(additional_certificate_03_document); + }else{ + return error(additional_certificate_03_document.message()); + } + } + // else { + // return error("Additional Certificate 03 doesn't exist.Skipping file retrieval."); + // } + + if additional_certificate_04_id is string { + UserDocument|error additional_certificate_04_document = getDocument(driveClient,additional_certificate_04_id,"additionalCertificate04"); + if(additional_certificate_04_document is UserDocument){ + documentList.push(additional_certificate_04_document); + }else{ + return error(additional_certificate_04_document.message()); + } + } + // else { + // return error("Additional Certificate 04 doesn't exist.Skipping file retrieval."); + // } + + if additional_certificate_05_id is string { + UserDocument|error additional_certificate_05_document = getDocument(driveClient,additional_certificate_05_id,"additionalCertificate05"); + if(additional_certificate_05_document is UserDocument){ + documentList.push(additional_certificate_05_document); + }else{ + return error(additional_certificate_05_document.message()); + } + } + // else { + // return error("Additional Certificate 05 doesn't exist.Skipping file retrieval."); + // } + + + from UserDocument user_document_record in documentList + do { + DocumentsData|error documentData = new DocumentsData(0,user_document_record); + if !(documentData is error) { + documents.push(documentData); + } + }; + + return documents; + } + } + +isolated function getDocument(drive:Client driveClient,string id,string document_name) returns UserDocument|error{ + + UserDocument user_document={ + id:(), + additional_certificate_01_id: (), + additional_certificate_02_id: (), + additional_certificate_03_id: (), + additional_certificate_04_id: (), + additional_certificate_05_id: (), + birth_certificate_back_id: (), + birth_certificate_front_id: (), + document_type: (), + nic_back_id: (), + nic_front_id: (), + al_certificate_id:(), + ol_certificate_id:(), + document: (), + folder_id: () + }; + + drive:FileContent|error document_file_stream = check driveClient->getFileContent(id); + + if(document_file_stream is drive:FileContent){ + byte[] base64EncodedDocument = (check mime:base64Encode(document_file_stream.content)); + string base64EncodedStringDocument = check string:fromBytes(base64EncodedDocument); + user_document.document_type = document_name; + user_document.document = base64EncodedStringDocument; + }else{ + user_document.document_type = (); + user_document.document = (); + } + return user_document; +} + diff --git a/api/types.bal b/api/types.bal index 32734de..66ed834 100644 --- a/api/types.bal +++ b/api/types.bal @@ -1,3 +1,5 @@ + + # Localized names in English, Sinhala, and Tamil. # Names are stored with a `name_` prefix, followed # by the respective ISO 639-1 language code. @@ -132,6 +134,8 @@ public type Person record {| string? academy_org_name; string? branch_code; string? current_job; + int? documents_id; + UserDocument[]? documentList; int? created_by; int? updated_by; |}; @@ -711,3 +715,34 @@ public type MonthlyLeaveDates record {| string? created; string? updated; |}; + +public type UserDocument record {| + readonly string? record_type = "user_document"; + int id?; + string? folder_id; + string? nic_front_id; + string? nic_back_id; + string? birth_certificate_front_id; + string? birth_certificate_back_id; + string? ol_certificate_id; + string? al_certificate_id; + string? additional_certificate_01_id; + string? additional_certificate_02_id; + string? additional_certificate_03_id; + string? additional_certificate_04_id; + string? additional_certificate_05_id; + string? document_type; + string? document; +|}; + +public type OrganizationFolderMapping record {| + readonly string? record_type = "organization_folder_mapping"; + int id?; + int? organization_id; + string? organization_folder_id; +|}; + +public type ErrorDetail record {| + string message; + int errorCode; +|};