@RestResource(urlMapping='/OFSUploadRepairImage/*') global with sharing class OFSUploadRepairImageRest { @HttpPost global static void doPost(String repairId, String ccImg, String acImg, String rqId) { system.debug('OFSUploadImageRest.start'); RestResponse res = RestContext.response; res.addHeader('Content-Type', 'application/json'); String jsonResponse; String ccId; // 同意書 String acId; // 検収書 if (String.isNotBlank(repairId)) { Repair__c repair = [Select Id, Agreed_Date__c, Facility_Return_Receipt_Collection_Date__c, acceptance_id__c From Repair__c Where Id= :repairId]; if (repair == null) { res.statusCode = 200; jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "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 = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "no RepairQuotation found"}'; res.responseBody = blob.valueOf(jsonResponse); return; } List attList = [Select Id, Name, Body, ParentId From Attachment Where Id= :repairQ.contract_consent_id__c]; Attachment att = null; if (attList.size() > 0) { att = attList[0]; } else { att = new Attachment(); att.Name = 'ContractConsent.jpg'; att.ParentId = rqId; } att.Body = EncodingUtil.base64Decode(ccImg); try { if (attList.size() > 0) { update att; } else { insert att; } ccId = att.Id; repairQ.contract_consent_id__c = ccId; } catch ( Exception ex ) { //TODO: //error message:cannot update exception res.statusCode = 200; jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to upsert ContractConsent Attachment. '+ ex +'"}'; res.responseBody = blob.valueOf(jsonResponse); return; } try { update repairQ; } catch ( Exception ex ) { //TODO: //error message:cannot update exception res.statusCode = 200; jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to update RepairQuotation. '+ ex +'"}'; res.responseBody = blob.valueOf(jsonResponse); return; } } if (String.isNotBlank(acImg)) { List attList = [Select Id, Name, Body, ParentId From Attachment Where Id= :repair.acceptance_id__c]; Attachment att = null; if (attList.size() > 0) { att = attList[0]; } else { att = new Attachment(); att.Name = 'Acceptance.jpg'; att.ParentId = repairId; } att.Body = EncodingUtil.base64Decode(acImg); try { if (attList.size() > 0) { update att; } else { insert att; } acId = att.Id; } catch ( Exception ex ) { //TODO: //error message:cannot update exception res.statusCode = 200; jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to upsert Acceptance Attachment. '+ ex +'"}'; res.responseBody = blob.valueOf(jsonResponse); return; } } if (String.isNotBlank(ccId)) { repair.Repair_Quotation_Id__c = rqId; repair.Agreed_Date__c = System.today(); // TODO GOTO 更新していい? } if (String.isNotBlank(acId)) { repair.acceptance_id__c = acId; repair.Facility_Return_Receipt_Collection_Date__c = System.today(); } try { update repair; res.statusCode = 200; Map rs = new Map(); if (String.isNotBlank(ccId)) { rs.put('ccImgId', ccId); } rs.put('Agreed_Date__c', repair.Agreed_Date__c != null ? String.valueOf(repair.Agreed_Date__c).replace('-', '/') : ''); rs.put('Facility_Return_Receipt_Collection_Date__c', repair.Facility_Return_Receipt_Collection_Date__c != null ? String.valueOf(repair.Facility_Return_Receipt_Collection_Date__c).replace('-', '/') : ''); jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorSuccess +'", "'+ System.Label.OFSErrorMessage +'": ' + JSON.serialize(rs) + '}'; res.responseBody = blob.valueOf(jsonResponse); return; } catch ( Exception ex ) { //TODO: //error message:cannot update exception res.statusCode = 200; jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to update repair data. '+ ex +'"}'; res.responseBody = blob.valueOf(jsonResponse); return; } } else { res.statusCode = 200; jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "no repair Id"}'; res.responseBody = blob.valueOf(jsonResponse); return; } } }