I am new junior developer and learning apex and could someone review my code and comment on it ?
This is my first api response class. Any tips and tricks ? what is wrong and what is good ?
public with sharing class PostiApiResponseHandler {
private final static PostiRegisterService REGISTER_SERVICE = new PostiRegisterService();
private final static PostiProcessingLogSelector LOG_SELECTOR = new PostiProcessingLogSelector();
public static void parseResponse(String responseBody) {
Map<String, Object> response = deserializeResponse(responseBody);
logOperationStatus(response);
Map<String, Object> message = getMessage(response);
logMessageDetails(message);
if (isValidMessage(message)) {
ProcessingLogData logData = extractProcessingLogData(message);
processResponseMessage(logData, message);
}
}
private static Map<String, Object> deserializeResponse(String responseBody) {
return (Map<String, Object>) JSON.deserializeUntyped(responseBody);
}
private static void logOperationStatus(Map<String, Object> response) {
System.debug('Operation Status: ' + response.get('operationStatus'));
}
private static Map<String, Object> getMessage(Map<String, Object> response) {
return (Map<String, Object>) response.get('message');
}
private static void logMessageDetails(Map<String, Object> message) {
System.debug('Register ID: ' + message.get('id'));
System.debug('Status: ' + message.get('status'));
System.debug('Is Approved: ' + message.get('isApproved'));
System.debug('Last Processing ID: ' + message.get('lastProcessingId'));
System.debug('Last Processing Date: ' + message.get('lastProcessingDate'));
}
private static boolean isValidMessage(Map<String, Object> message) {
return message != null && message.containsKey('id') && message.containsKey('lastProcessingDate') && message.containsKey('lastProcessingId');
}
private static ProcessingLogData extractProcessingLogData(Map<String, Object> message) {
Datetime lastProcessingDate = Datetime.valueOf((String) message.get('lastProcessingDate'));
String lastProcessingId = (String) message.get('lastProcessingId');
String registerId = (String) message.get('id');
return new ProcessingLogData(registerId, lastProcessingDate, lastProcessingId);
}
private static void processResponseMessage(ProcessingLogData logData, Map<String, Object> message) {
Posti_Processing_Log__c storedLog = LOG_SELECTOR.getLogByRegisterId(logData.registerId);
if (isNewProcessingData(logData.lastProcessingDate, logData.lastProcessingId, storedLog)) {
List<Map<String, Object>> updates = (List<Map<String, Object>>) message.get('updates');
processUpdates(updates, logData.registerId);
upsertProcessingLog(storedLog, logData);
}
}
private static boolean isNewProcessingData(Datetime lastProcessingDate, String lastProcessingId, Posti_Processing_Log__c storedLog) {
return storedLog == null || lastProcessingDate > storedLog.Last_Processing_Date__c || !lastProcessingId.equals(storedLog.Last_Processing_Id__c);
}
private static void processUpdates(List<Map<String, Object>> updates, String registerId) {
for (Map<String, Object> updateItem : updates) {
Datetime processingDate = Datetime.valueOf((String) updateItem.get('processingDate'));
List<Map<String, Object>> deliverables = (List<Map<String, Object>>) updateItem.get('deliverables');
UpdateData updateData = new UpdateData(processingDate, deliverables);
processDeliverables(updateData, registerId);
}
}
private static void processDeliverables(UpdateData updateData, String registerId) {
for (Map<String, Object> deliverable : updateData.deliverables) {
if ('RESULT_JSON_FILE_CHANGES'.equals(deliverable.get('deliverableType'))) {
String deliverableId = (String) deliverable.get('id');
Integer numberOfChunks = Integer.valueOf((String) deliverable.get('numberOfChunks'));
PostiRegisterService.downloadDeliverableFiles(registerId, deliverableId, numberOfChunks);
}
}
}
private static void upsertProcessingLog(Posti_Processing_Log__c storedLog, ProcessingLogData logData) {
if (storedLog != null) {
if (!Schema.sObjectType.Posti_Processing_Log__c.fields.Last_Processing_Date__c.isUpdateable() ||
!Schema.sObjectType.Posti_Processing_Log__c.fields.Last_Processing_Id__c.isUpdateable()) {
throw new System.DmlException('Insufficient access rights to update Posti_Processing_Log__c');
}
storedLog.Last_Processing_Date__c = logData.lastProcessingDate;
storedLog.Last_Processing_Id__c = logData.lastProcessingId;
update storedLog;
} else {
if (!Schema.sObjectType.Posti_Processing_Log__c.isCreateable()) {
throw new System.DmlException('Insufficient access rights to create Posti_Processing_Log__c');
}
storedLog = new Posti_Processing_Log__c();
storedLog.External_Id__c = logData.registerId;
storedLog.Last_Processing_Date__c = logData.lastProcessingDate;
storedLog.Last_Processing_Id__c = logData.lastProcessingId;
insert storedLog;
}
}
public static void processDownloadResponse(String responseBody) {
Map<String, Object> response = (Map<String, Object>) JSON.deserializeUntyped(responseBody);
List<Map<String, Object>> records = (List<Map<String, Object>>) response.get('records');
for (Map<String, Object> record : records) {
String customerId = (String) record.get('customerId');
Map<String, Object> details = (Map<String, Object>) record.get('details');
Map<String, Object> personStatus = (Map<String, Object>) details.get('personStatus');
Map<String, Object> changes = (Map<String, Object>) record.get('changes');
if ('2'.equals(personStatus.get('sPersonStatusCode'))) {
PostiAccountUpdater.updateDeceasedStatus(customerId);
} else {
if (changes != null) {
PostiAccountUpdater.updateAccount(customerId, changes);
}
}
}
}
}
Im just looking for some experienced person validation for my code
New contributor
lailakatar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.