19626
2023-10-23 1727a4f4d02e429475608e60f142a63bc24127bc
force-app/main/default/classes/OFSUploadImageRest.cls
@@ -10,20 +10,27 @@
        String ccId, acId;
        
        if (String.isNotBlank(repairId)) {
            Repair__c repair = [Select Id, contract_consent_id__c, acceptance_id__c From Repair__c Where Id= :repairId];
            if (repair == null) {
                res.statusCode = 200;
                jsonResponse = '{"status": "Failure", "Message": "no repair found"}';
                res.responseBody = blob.valueOf(jsonResponse);
                return;
            Repair__c repair = null;
            try {
                repair = [Select Id, contract_consent_id__c, acceptance_id__c From Repair__c Where Id= :repairId];
            } catch ( Exception ex ) {
                returnMessage('{"status": "Failure", "Message": "no repair found"}',200);
                // res.statusCode = 200;
                // jsonResponse = '{"status": "Failure", "Message": "no repair found"}';
                // res.responseBody = blob.valueOf(jsonResponse);
                // return;
            }
            if (String.isNotBlank(ccImg)) {
                Repair_Quotation__c repairQ = [Select Id, contract_consent_id__c From Repair_Quotation__c Where Id= :rqId];
                if (repairQ == null) {
                    res.statusCode = 200;
                    jsonResponse = '{"status": "Failure", "Message": "no RepairQuotation found"}';
                    res.responseBody = blob.valueOf(jsonResponse);
                    return;
                Repair_Quotation__c repairQ = null;
                try {
                    repairQ = [Select Id, contract_consent_id__c From Repair_Quotation__c Where Id= :rqId];
                } catch ( Exception ex ) {
                    returnMessage('{"status": "Failure", "Message": "no RepairQuotation found"}',200);
                    // res.statusCode = 200;
                    // jsonResponse = '{"status": "Failure", "Message": "no RepairQuotation found"}';
                    // res.responseBody = blob.valueOf(jsonResponse);
                    // return;
                }
                // Attachment att;
                ContentVersion version;
@@ -60,10 +67,11 @@
                } catch ( Exception ex ) {
                    //TODO:
                    //error message:cannot update exception
                    res.statusCode = 200;
                    jsonResponse = '{"status": "Failure", "Message": "error when try to upsert ContractConsent Attachment. '+ ex +'"}';
                    res.responseBody = blob.valueOf(jsonResponse);
                    return;
                    returnMessage('{"status": "Failure", "Message": "error when try to upsert ContractConsent Attachment. '+ ex +'"}',200);
                    // res.statusCode = 200;
                    // jsonResponse = '{"status": "Failure", "Message": "error when try to upsert ContractConsent Attachment. '+ ex +'"}';
                    // res.responseBody = blob.valueOf(jsonResponse);
                    // return;
                }
                
                try {
@@ -71,10 +79,11 @@
                } catch ( Exception ex ) {
                    //TODO:
                    //error message:cannot update exception
                    res.statusCode = 200;
                    jsonResponse = '{"status": "Failure", "Message": "error when try to update RepairQuotation. '+ ex +'"}';
                    res.responseBody = blob.valueOf(jsonResponse);
                    return;
                    returnMessage('{"status": "Failure", "Message": "error when try to update RepairQuotation. '+ ex +'"}',200);
                    // res.statusCode = 200;
                    // jsonResponse = '{"status": "Failure", "Message": "error when try to update RepairQuotation. '+ ex +'"}';
                    // res.responseBody = blob.valueOf(jsonResponse);
                    // return;
                }
            }
            if (String.isNotBlank(acImg)) {
@@ -112,10 +121,11 @@
                } catch ( Exception ex ) {
                    //TODO:
                    //error message:cannot update exception
                    res.statusCode = 200;
                    jsonResponse = '{"status": "Failure", "Message": "error when try to upsert Acceptance Attachment. '+ ex +'"}';
                    res.responseBody = blob.valueOf(jsonResponse);
                    return;
                    returnMessage('{"status": "Failure", "Message": "error when try to upsert Acceptance Attachment. '+ ex +'"}',200);
                    // res.statusCode = 200;
                    // jsonResponse = '{"status": "Failure", "Message": "error when try to upsert Acceptance Attachment. '+ ex +'"}';
                    // res.responseBody = blob.valueOf(jsonResponse);
                    // return;
                }
            }
            
@@ -123,23 +133,36 @@
            if (String.isNotBlank(acId)) repair.acceptance_id__c = acId;
            try {
                update repair;
                res.statusCode = 200;
                jsonResponse = '{"status": "Success", "Message": "upload Success"}';
                res.responseBody = blob.valueOf(jsonResponse);
                return;
                returnMessage('{"status": "Success", "Message": "upload Success"}',200);
                // res.statusCode = 200;
                // jsonResponse = '{"status": "Success", "Message": "upload Success"}';
                // res.responseBody = blob.valueOf(jsonResponse);
                // return;
            } catch ( Exception ex ) {
                //TODO:
                //error message:cannot update exception
                res.statusCode = 200;
                jsonResponse = '{"status": "Failure", "Message": "error when try to update repair data. '+ ex +'"}';
                res.responseBody = blob.valueOf(jsonResponse);
                return;
                returnMessage('{"status": "Failure", "Message": "error when try to update repair data. '+ ex +'"}',200);
                // res.statusCode = 200;
                // jsonResponse = '{"status": "Failure", "Message": "error when try to update repair data. '+ ex +'"}';
                // res.responseBody = blob.valueOf(jsonResponse);
                // return;
            }
        } else {
            res.statusCode = 200;
            jsonResponse = '{"status": "Failure", "Message": "no repair Id"}';
            returnMessage('{"status": "Failure", "Message": "no repair Id"}',200);
            // res.statusCode = 200;
            // jsonResponse = '{"status": "Failure", "Message": "no repair Id"}';
            // res.responseBody = blob.valueOf(jsonResponse);
            // return;
        }
    }
    public static void returnMessage(String response,Integer code){
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        String jsonResponse;
        res.statusCode = code;
        jsonResponse = response;
            res.responseBody = blob.valueOf(jsonResponse);
            return;
        }
    }
}