• Koppalli Bhuvaneswari
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
apex class
-----------------------
public without sharing class BookMeetingForExistingContactController {

    @AuraEnabled(cacheable=true)
    public static Boolean isThisLinkUsed(String dataStringFromUrl){
        String encryptedData = dataStringFromUrl+'==';
        URL__c urlObj = [SELECT Id, CreatedDate, Name,is_Invitation_Accpted__c,ExpirationDurationInMinutes__c, Encrypted_Id__c, Decrypted_Id__c, Key__c FROM URL__c WHERE Encrypted_Id__c=:encryptedData LIMIT 1];
        
        Long dt1Long = urlObj.CreatedDate.getTime();
        Long dt2Long = DateTime.now().getTime();
        Long minutes = (dt2Long - dt1Long)/60000;

        Integer expirationDuration = Integer.valueOf(minutes);

        // return participantDetailsMap;
        Boolean result;

        if(!urlObj.is_Invitation_Accpted__c && urlObj.ExpirationDurationInMinutes__c >= expirationDuration){
            result = false;
        }
        else{
            result = true;
        }
        return result;
    }
    
    @AuraEnabled(cacheable=true)
    public static User decryptHostUserData(String dataStringFromUrl) {
        try {
            String encriptedDataId = dataStringFromUrl+'==';
            URL__c urlObj = [SELECT Id, Name, Encrypted_Id__c, Decrypted_Id__c, Key__c FROM URL__c WHERE Encrypted_Id__c=:encriptedDataId LIMIT 1];
            System.debug('urlObj'+urlObj);

            Blob key = EncodingUtil.base64Decode(urlObj.Key__c);
            System.debug('key'+key);

            Blob dataToDecrypt = EncodingUtil.base64Decode(urlObj.Encrypted_Id__c);
            System.debug('dataToDecrypt'+dataToDecrypt);

            Blob decryptedData = Crypto.decryptWithManagedIV('AES256', key, dataToDecrypt);
            System.debug('decryptedData'+decryptedData);

            String decryptedDataString = decryptedData.toString();
            System.debug('decryptedDataString'+decryptedDataString);

            String[] decryptedDataSplitToString = decryptedDataString.split(' ');

            Id userId = Id.valueOf(decryptedDataSplitToString[0]);
            System.debug('userId'+userId);
            
            User userObj = [Select Id, Name, Email FROM User Where Id=:userId];
            System.debug( 'userObj '+userObj );

            return userObj;
        } 
       catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
     
    }

    @AuraEnabled(cacheable=true)
    public static Map<String, String> decryptParticipantsUserData(String dataStringFromUrl) {
        try {
            String encriptedDataId = dataStringFromUrl+'==';
            URL__c urlObj = [SELECT Id, Name, Encrypted_Id__c, Decrypted_Id__c, Key__c, ParticipantAsContact__c,ParticipantAsUser__c FROM URL__c WHERE Encrypted_Id__c=:encriptedDataId LIMIT 1];
            System.debug(urlObj);

            Blob key = EncodingUtil.base64Decode(urlObj.Key__c);
            System.debug(key);

            Blob dataToDecrypt = EncodingUtil.base64Decode(urlObj.Encrypted_Id__c);
            System.debug(dataToDecrypt);

            Blob decryptedData = Crypto.decryptWithManagedIV('AES256', key, dataToDecrypt);
            System.debug(decryptedData);

            String decryptedDataString = decryptedData.toString();
            System.debug('decryptedDataString '+decryptedDataString);

            String[] decryptedDataSplitToString = decryptedDataString.split(' ');
            System.debug('decryptedDataSplitToString : '+decryptedDataSplitToString);

            System.debug('decryptedDataSplitToString[1] : '+decryptedDataSplitToString[1]);
            
            String[] participantIdAndType = decryptedDataSplitToString[1].split('-');
            
            Id participantId = Id.valueOf(participantIdAndType[1]);
            System.debug(participantId);

            Map<String, String> participantDetailsMap = new Map<String, String>();
            
            if(participantIdAndType[0]=='c'){
                Contact contactObj = [Select Id, Name, Phone, Email, Account.Name FROM Contact Where Id=:participantId];
                String company = contactObj.Account.name;

                participantDetailsMap.put('Id', String.valueOf(contactObj.Id));
                participantDetailsMap.put('Name', contactObj.Name);
                participantDetailsMap.put('Email', contactObj.Email);
                participantDetailsMap.put('Company', company);
                participantDetailsMap.put('Phone', contactObj.Phone);
                
            }else if(participantIdAndType[0]=='u'){
                User userObj = [Select Id, Name, Email, Phone, CompanyName FROM User Where Id=:participantId];
                System.debug('UserObj : '+userObj);

                participantDetailsMap.put('Id', String.valueOf(userObj.id));
                participantDetailsMap.put('Name', userObj.Name);
                participantDetailsMap.put('Email', userObj.Email);
                participantDetailsMap.put('Company', userObj.CompanyName);
                participantDetailsMap.put('Phone', userObj.Phone);
                
            }

            return participantDetailsMap;
        } 
        catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }

    @AuraEnabled
    public static Boolean createNewMeetingForExistingUser(String strigifyEventData, Date selectedDate){
        Map<String,Object> rawData = (Map<String,Object>) JSON.deserializeUntyped(strigifyEventData);

        // Fetch the participant Details
        String participantId = String.valueOf(rawData.get('contactId'));

        // Fetch the Host Details
        String hostId = String.valueOf(rawData.get('hostId'));

        // Declare Contact(Participant) Id Variable
        Id contctId;
        
        // Fetch the Event Details
        String encriptedDataId = String.valueOf(rawData.get('dataStringFromUrl'))+'==';
        URL__c urlObj = [SELECT Id, Name,isExpired__c,is_Invitation_Accpted__c, Encrypted_Id__c, Decrypted_Id__c,Key__c FROM URL__c WHERE Encrypted_Id__c=:encriptedDataId LIMIT 1];
        
        String eventSub = String.valueOf(rawData.get('meetingSub'));
        String eventDes = String.valueOf(rawData.get('meetingDesc'));
        Date eventStartDate = selectedDate;
        String duration = String.valueOf(rawData.get('meetingDuration'));
        String selectedSlot = String.valueOf(rawData.get('selectedSlot'));
        
        // Get the Hour from SelectedSlot
        String[] startTimeList = selectedSlot.split(':');
        Integer hh = Integer.valueOf(startTimeList[0]);

        // Get the Minute from SelectedSlot
        String[] minuteList = startTimeList[1].split(' ');
        Integer mm = Integer.valueOf(minuteList[0]);

        // Get the Event Duration from eventDuration in Minute
        String[] durationList = duration.split(' ');
        Integer meetingDuration = Integer.valueOf(durationList[0]);
        
        Time eventStartTime;

        if(minuteList[1]=='AM'){
            eventStartTime = Time.newInstance(hh, mm, 0, 0);
        }else if(minuteList[1]=='PM'){
            if(hh==12){
                eventStartTime = Time.newInstance(hh, mm, 0, 0);
            }else{
                eventStartTime = Time.newInstance(hh+12, mm, 0, 0);
            }
        }

        Datetime eventStartDateTime = DateTime.newInstance(eventStartDate, eventStartTime);
        Datetime eventEndDateTime = eventStartDateTime.addMinutes(meetingDuration);

        // Creating New Event
        Event newEventObj = new Event();
        newEventObj.Subject = eventSub;
        newEventObj.StartDateTime = eventStartDateTime;
        newEventObj.EndDateTime = eventEndDateTime;
        newEventObj.IsAllDayEvent = false;
        newEventObj.OwnerId = hostId;
        newEventObj.Description = eventDes;

        insert newEventObj;

        Id newEventObjId = newEventObj.Id;

        // Declare a EventRelation Object List to store EventRelation new object
        List<EventRelation> eventRelationObjList = new List<EventRelation>();
        
        // Create New EventRelation object
        EventRelation eventRelationObj = new EventRelation();
        eventRelationObj.EventId = newEventObjId;
        eventRelationObj.RelationId = participantId;
        eventRelationObj.IsParent = false;
        eventRelationObj.IsInvitee = true;

        System.debug(eventRelationObj);

        // Add new EventRelation Object to eventRelationObjList
        eventRelationObjList.add(eventRelationObj);

        urlObj.is_Invitation_Accpted__c = true;
        try {
            update urlObj;
            insert eventRelationObjList;
            return true;            
        } catch (Exception e) {
            return false;
        }
    }
}



test classs
--------------------------------------
@isTest
private class BookMeetingForExistingContact_CC_Test {

    @isTest
    static void testIsThisLinkUsed() {
        // Create test data
        
        URL__c urlObj = new URL__c(
            Encrypted_Id__c = 'test-encrypted-id==',
            Decrypted_Id__c = 'test-decrypted-id',
            Key__c = 'test-key',
            ExpirationDurationInMinutes__c = 60,
            is_Invitation_Accpted__c = false
        );
        insert urlObj;
                 
        // Call the method being tested
        Boolean result = BookMeetingForExistingContactController.isThisLinkUsed('test-encrypted-id');
        
        // Verify the result
        System.assertEquals(false, result);
    }

    @isTest
    static void testDecryptHostUserData() {
        // Create test data
        /*User host = new User(
            FirstName = 'Test',
            LastName = 'User',
            Email = 'testuser@test.com'
        );
        insert host;*/
          User host = [SELECT FirstName, LastName,Email from User Where Id=:UserInfo.getUserId()];
        URL__c urlObj = new URL__c(
            
            Encrypted_Id__c = 'test-encrypted-id==',
            Decrypted_Id__c = 'test-decrypted-id',
            Key__c = EncodingUtil.base64Encode(Blob.valueOf('test-key')),
            ParticipantAsUser__c = host.Id,ExpirationDurationInMinutes__c=30
        );
        insert urlObj;
        
        // Call the method being tested
        User result = BookMeetingForExistingContactController.decryptHostUserData('test-decrypted-id==');
        
        // Verify the result
        System.assertEquals(host.Id, result.Id);
        System.assertEquals(host.Email, result.Email);
    }

    @isTest
    static void testDecryptParticipantsUserData() {
        // Create test data
        Account account = new Account(
            Name = 'Test Account'
        );
        insert account;

        Contact contact = new Contact(
            FirstName = 'Test',
            LastName = 'Contact',
            Email = 'testcontact@test.com',
            AccountId = account.Id
        );
        insert contact;

        URL__c urlObj = new URL__c(
            
            Encrypted_Id__c = 'test-encrypted-id==',
            Decrypted_Id__c = 'test-decrypted-id',
            Key__c = EncodingUtil.base64Encode(Blob.valueOf('test-key')),
            ParticipantAsContact__c = contact.Id,ExpirationDurationInMinutes__c=30
        );
        insert urlObj;
        
        // Call the method being tested
        Map<String, String> result = BookMeetingForExistingContactController.decryptParticipantsUserData('test-encrypted-id==');
        
        // Verify the result
        System.assertEquals(contact.Id, result.get('Id'));
        System.assertEquals(contact.Email, result.get('Email'));
        System.assertEquals(contact.Account.Name, result.get('Company'));
    }

    @isTest
    static void testCreateNewMeetingForExistingUser() {
        // Create test data
        /*User host = new User(
           FirstName = 'Test',
            LastName = 'User',
             Email = 'testuser@test.com'
        );
         insert host;*/
        User host = [SELECT FirstName, LastName,Email from User Where Id=:UserInfo.getUserId()];

        Account account = new Account(
            Name = 'Test Account'
        );
        insert account;

        Contact contact = new Contact(
            FirstName = 'Test',
            LastName = 'Contact',
            Email = 'testcontact@test.com',
            AccountId = account.Id
        );
        insert contact;
    }
}
 
Hi All,

I am new to Salesforce. I am getting below exception on my test class.
Can you please suggest me here ?

@isTest
    static void test_releaseInitiative()
    {
        init();

        Test.startTest();

        String errorRelease = ViewInitiativesController.releaseInitiative(testInitiative.Id);

        Initiative__c testInitiativeCheck = [SELECT Status__c, Initiative_Released__c FROM Initiative__c WHERE Id = :testInitiative.Id];
        System.assertEquals(true, testInitiativeCheck.Initiative_Released__c, 'Wrong Initiative_Released__c: ' + errorRelease);

        Test.stopTest();
    }

Error is -- System.AuraHandledException: Script-thrown exception

Any help is really appriacted. 
Thanks in advance.