diff --git a/PrivacyDashboardiOS/Classes/PrivacyDashboard.swift b/PrivacyDashboardiOS/Classes/PrivacyDashboard.swift index a181ccc..48f65cc 100644 --- a/PrivacyDashboardiOS/Classes/PrivacyDashboard.swift +++ b/PrivacyDashboardiOS/Classes/PrivacyDashboard.swift @@ -51,11 +51,11 @@ public class PrivacyDashboard { readDataAgreementRecordApi(dataAgreementId: dataAgreementId) { success, resultVal in if resultVal["errorCode"] as? Int != 500 && !(resultVal["consentRecord"] is NSNull) { - // If existing record found for the data agreement ID (status code will be !500) + // If existing record found for the data agreement ID // Return the records reponse self.receiveDataBackFromPrivacyDashboard?(resultVal) } else { - // If no record found for the data agreement ID (status code will be 500) + // If no record found for the data agreement ID // Navigate to Data sharing UI screen let navVC = UINavigationController.init(rootViewController: sharingVC) navVC.modalPresentationStyle = .fullScreen @@ -64,6 +64,72 @@ public class PrivacyDashboard { } } + public static func configure(withApiKey: String, withUserId: String, withOrgId: String, withBaseUrl: String, accessToken: String = "") { + let frameworkBundle = Bundle(for: BBConsentOrganisationViewController.self) + let bundleURL = frameworkBundle.resourceURL?.appendingPathComponent("PrivacyDashboardiOS.bundle") + var storyboard = UIStoryboard() + if let resourceBundle = Bundle(url: bundleURL!) { + storyboard = UIStoryboard(name: "PrivacyDashboard", bundle: resourceBundle) + } else { + let myBundle = Bundle(for: BBConsentOrganisationViewController.self) + storyboard = UIStoryboard(name: "PrivacyDashboard", bundle: myBundle) + } + BBConsentPrivacyDashboardiOS.shared.userId = withUserId + BBConsentPrivacyDashboardiOS.shared.accessToken = accessToken + let sharingVC = storyboard.instantiateViewController(withIdentifier: "BBConsentDataSharingVC") as? BBConsentDataSharingVC ?? BBConsentDataSharingVC() + // Passing other required info + let data = withApiKey.data(using: .utf8) ?? Data() + _ = BBConsentKeyChainUtils.save(key: "BBConsentApiKey", data: data) + BBConsentPrivacyDashboardiOS.shared.baseUrl = withBaseUrl + sharingVC.dataAgreementId = withOrgId + } + + public static func updateDataAgreementStatus(dataAgreementId: String, status: Bool) { + readDataAgreementRecordApi(dataAgreementId: dataAgreementId) { success, resultVal in + if resultVal["errorCode"] as? Int != 500 && !(resultVal["consentRecord"] is NSNull) { + // If existing record found for the data agreement ID + let resultDict = resultVal["consentRecord"] as? [String: Any] + guard let recordId = resultDict?["id"] else { return } + updateRecordApiCall(dataAgreementId: dataAgreementId, recordId: recordId as! String, status: status) { resultVal in + debugPrint(resultVal) + self.receiveDataBackFromPrivacyDashboard?(resultVal) + } + } else { + // If no record found for the data agreement ID + createRecordApiCall(dataAgreementId: dataAgreementId) { resultVal in + debugPrint(resultVal) + self.receiveDataBackFromPrivacyDashboard?(resultVal) + } + } + } + } + + private static func createRecordApiCall(dataAgreementId: String, completion: @escaping (_ resultVal: [String: Any]) -> Void) { + BBConsentBaseWebService.shared.makeAPICall(urlString: Constant.URLStrings.fetchDataAgreement + (dataAgreementId), parameters: [:], method: .post) { success, resultVal in + if success { + debugPrint(resultVal) + completion(resultVal) + } else { + debugPrint(resultVal) + completion(resultVal) + } + } + } + + private static func updateRecordApiCall(dataAgreementId: String, recordId: String, status: Bool, completion: @escaping (_ resultVal: [String: Any]) -> Void) { + let parameters = ["optIn" : status] + let url = BBConsentPrivacyDashboardiOS.shared.baseUrl + "/service/individual/record/consent-record/" + (recordId) + "?dataAgreementId=" + dataAgreementId + BBConsentBaseWebService.shared.makeAPICall(urlString: url, parameters: parameters, method: .put) { success, resultVal in + if success { + debugPrint(resultVal) + completion(resultVal) + } else { + debugPrint(resultVal) + completion(resultVal) + } + } + } + // MARK: - Read data agreement api call private static func readDataAgreementRecordApi(dataAgreementId: String, completionBlock:@escaping (_ success: Bool, _ resultVal: [String: Any]) -> Void){ BBConsentBaseWebService.shared.makeAPICall(urlString: Constant.URLStrings.fetchDataAgreement + dataAgreementId, parameters: [:], method: .get) { success, resultVal in diff --git a/PrivacyDashboardiOS/Example/PrivacyDashboardiOS/ViewController.swift b/PrivacyDashboardiOS/Example/PrivacyDashboardiOS/ViewController.swift index 4200b55..c11b231 100644 --- a/PrivacyDashboardiOS/Example/PrivacyDashboardiOS/ViewController.swift +++ b/PrivacyDashboardiOS/Example/PrivacyDashboardiOS/ViewController.swift @@ -24,19 +24,22 @@ class ViewController: UIViewController { // turnOnUserRequest: false, // turnOnAttributeDetail: false) - PrivacyDashboard.showDataSharingUI(apiKey:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTY29wZXMiOlsic2VydmljZSJdLCJPcmdhbmlzYXRpb25JZCI6IjY1MjY1Nzk2OTM4MGYzNWZhMWMzMDI0NSIsIk9yZ2FuaXNhdGlvbkFkbWluSWQiOiI2NTI2NTc5NjkzODBmMzVmYTFjMzAyNDMiLCJleHAiOjE3MDA3MjkxOTF9.2rkHNiLDjQi8WOy4CWn96sMBx8KkvFCUMU0Xe6oXNbY", - userId: "65378403b3f442eb9381b38d", - baseUrlString: "https://staging-consent-bb-api.igrant.io/v2", - dataAgreementId: "6551b99a7adedd223d2e61e4", - organisationName: "My company", - organisationLogoImageUrl: "https://www.kasandbox.org/programming-images/avatars/old-spice-man-blue.png", - termsOfServiceText: "Terms of service.", - termsOfServiceUrl: "http://google.com", - cancelButtonText: "Cancel") +// PrivacyDashboard.showDataSharingUI(apiKey:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTY29wZXMiOlsic2VydmljZSJdLCJPcmdhbmlzYXRpb25JZCI6IjY1MjY1Nzk2OTM4MGYzNWZhMWMzMDI0NSIsIk9yZ2FuaXNhdGlvbkFkbWluSWQiOiI2NTI2NTc5NjkzODBmMzVmYTFjMzAyNDMiLCJleHAiOjE3MDA3MjkxOTF9.2rkHNiLDjQi8WOy4CWn96sMBx8KkvFCUMU0Xe6oXNbY", +// userId: "65378403b3f442eb9381b38d", +// baseUrlString: "https://staging-consent-bb-api.igrant.io/v2", +// dataAgreementId: "6551b99a7adedd223d2e61e4", +// organisationName: "My company", +// organisationLogoImageUrl: "https://www.kasandbox.org/programming-images/avatars/old-spice-man-blue.png", +// termsOfServiceText: "Terms of service.", +// termsOfServiceUrl: "http://google.com", +// cancelButtonText: "Cancel") PrivacyDashboard.receiveDataBackFromPrivacyDashboard = { data in debugPrint("Data receieved here:\(data)") } + + PrivacyDashboard.configure(withApiKey: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJTY29wZXMiOlsic2VydmljZSJdLCJPcmdhbmlzYXRpb25JZCI6IjY1MjY1Nzk2OTM4MGYzNWZhMWMzMDI0NSIsIk9yZ2FuaXNhdGlvbkFkbWluSWQiOiI2NTI2NTc5NjkzODBmMzVmYTFjMzAyNDMiLCJleHAiOjE3MDA3MjkxOTF9.2rkHNiLDjQi8WOy4CWn96sMBx8KkvFCUMU0Xe6oXNbY", withUserId: "65378403b3f442eb9381b38d", withOrgId: "64f09f778e5f3800014a879a", withBaseUrl: "https://staging-consent-bb-api.igrant.io/v2") + PrivacyDashboard.updateDataAgreementStatus(dataAgreementId: "6551c9ba7654351e98a58734", status: true) } override func didReceiveMemoryWarning() {