Refactor getFormByEncounter into service class (#4721)